From b92a93a8f33b5199deb113b9790dc1d84183906a Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 10 Jul 2021 11:04:41 -0400 Subject: [PATCH] Add warning for MIUI users when trying to restore backups with MIUI Optimization disabled --- .../ui/setting/SettingsBackupController.kt | 7 ++++ .../kanade/tachiyomi/util/system/MiuiUtil.kt | 38 +++++++++++++++++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 46 insertions(+) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/util/system/MiuiUtil.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 6e1a90c6b0..929bb20d66 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -7,6 +7,7 @@ import android.content.Intent import android.net.Uri import android.os.Bundle import android.view.View +import android.widget.Toast import androidx.core.net.toUri import androidx.core.os.bundleOf import androidx.documentfile.provider.DocumentFile @@ -25,6 +26,7 @@ import eu.kanade.tachiyomi.data.backup.legacy.LegacyBackupRestoreValidator import eu.kanade.tachiyomi.data.preference.asImmediateFlow import eu.kanade.tachiyomi.ui.base.controller.DialogController import eu.kanade.tachiyomi.util.system.getFilePicker +import eu.kanade.tachiyomi.util.system.MiuiUtil import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.requestFilePermissionsSafe import kotlinx.coroutines.flow.launchIn @@ -67,6 +69,11 @@ class SettingsBackupController : SettingsController() { summaryRes = R.string.restore_from_backup_file onClick { + if (MiuiUtil.isMiui() && MiuiUtil.isMiuiOptimizationDisabled()) { + context.toast(R.string.restore_miui_warning, Toast.LENGTH_LONG) + return@onClick + } + if (!BackupRestoreService.isRunning(context)) { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.addCategory(Intent.CATEGORY_OPENABLE) diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/MiuiUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/MiuiUtil.kt new file mode 100644 index 0000000000..fde9bd7725 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/MiuiUtil.kt @@ -0,0 +1,38 @@ +package eu.kanade.tachiyomi.util.system + +import android.annotation.SuppressLint +import timber.log.Timber + +object MiuiUtil { + + fun isMiui(): Boolean { + return getSystemProperty("ro.miui.ui.version.name")?.isNotEmpty() ?: false + } + + @SuppressLint("PrivateApi") + fun isMiuiOptimizationDisabled(): Boolean { + if ("0" == getSystemProperty("persist.sys.miui_optimization")) { + return true + } + + return try { + Class.forName("android.miui.AppOpsUtils") + .getDeclaredMethod("isXOptMode") + .invoke(null) as Boolean + } catch (e: Exception) { + false + } + } + + @SuppressLint("PrivateApi") + private fun getSystemProperty(key: String?): String? { + return try { + Class.forName("android.os.SystemProperties") + .getDeclaredMethod("get", String::class.java) + .invoke(null, key) as String + } catch (e: Exception) { + Timber.w(e, "Unable to use SystemProperties.get") + null + } + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1d2b097b77..dfe4071c8e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -680,6 +680,7 @@ What do you want to backup? Restoring backup %02d min, %02d sec + MIUI Optimization must be enabled for restore to work correctly. Progress Complete