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 75ebca195..f90538467 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
@@ -8,6 +8,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
@@ -35,6 +36,7 @@ import eu.kanade.tachiyomi.util.preference.preference
import eu.kanade.tachiyomi.util.preference.preferenceCategory
import eu.kanade.tachiyomi.util.preference.summaryRes
import eu.kanade.tachiyomi.util.preference.titleRes
+import eu.kanade.tachiyomi.util.system.MiuiUtil
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
@@ -76,6 +78,11 @@ class SettingsBackupController : SettingsController() {
summaryRes = R.string.pref_restore_backup_summ
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).apply {
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 000000000..fde9bd772
--- /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 af063bfc4..c8d10137c 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -418,6 +418,7 @@
What do you want to backup?
Creating backup
Backup failed
+ MIUI Optimization must be enabled for restore to work correctly.
Restore is already in progress
Restoring backup
Restoring backup failed