From 859a2d893fa974ce56f774f84e9da1f40e40436e Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 15 Feb 2020 15:37:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/PreferKey.kt | 1 + .../java/io/legado/app/help/storage/Backup.kt | 24 +++++++++++++++---- .../io/legado/app/ui/main/MainActivity.kt | 14 +---------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index f654786a9..9d42ab86e 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -34,4 +34,5 @@ object PreferKey { const val chineseConverterType = "chineseConverterType" const val launcherIcon = "launcherIcon" const val selectText = "selectText" + const val lastBackup = "lastBackup" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index d44d617b6..668d32877 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -4,15 +4,15 @@ import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile import io.legado.app.App +import io.legado.app.constant.PreferKey import io.legado.app.help.ReadBookConfig -import io.legado.app.utils.DocumentUtils -import io.legado.app.utils.FileUtils -import io.legado.app.utils.GSON -import io.legado.app.utils.isContentPath +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.withContext import org.jetbrains.anko.defaultSharedPreferences import java.io.File +import java.util.concurrent.TimeUnit object Backup { @@ -34,7 +34,23 @@ object Backup { ) } + fun autoBack(context: Context) { + val lastBackup = context.getPrefLong(PreferKey.lastBackup) + if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) { + return + } + Coroutine.async { + val backupPath = context.getPrefString(PreferKey.backupPath) + if (backupPath.isNullOrEmpty()) { + backup(context) + } else { + backup(context, backupPath) + } + } + } + suspend fun backup(context: Context, path: String = legadoPath) { + context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis()) withContext(IO) { writeListToJson(App.db.bookDao().all, "bookshelf.json", backupPath) writeListToJson(App.db.bookGroupDao().all, "bookGroup.json", backupPath) diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index be2466ade..93e040e66 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -15,7 +15,6 @@ import io.legado.app.base.VMBaseActivity import io.legado.app.constant.EventBus import io.legado.app.constant.PreferKey import io.legado.app.help.AppConfig -import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.storage.Backup import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService @@ -131,22 +130,11 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun finish() { if (!BuildConfig.DEBUG) { - backup() + Backup.autoBack(this) } super.finish() } - private fun backup() { - Coroutine.async { - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath.isNullOrEmpty()) { - Backup.backup(this@MainActivity) - } else { - Backup.backup(this@MainActivity, backupPath) - } - } - } - override fun onDestroy() { super.onDestroy() ReadAloud.stop(this)