Przeglądaj źródła

可选择关闭预见式返回动画 #183

HapeLee 1 rok temu
rodzic
commit
addc128f7d

+ 14 - 0
app/src/main/java/io/legado/app/base/BaseActivity.kt

@@ -12,6 +12,7 @@ import android.view.Menu
 import android.view.MenuItem
 import android.view.MotionEvent
 import android.view.View
+import android.window.OnBackInvokedDispatcher
 import androidx.activity.enableEdgeToEdge
 import androidx.appcompat.app.AppCompatActivity
 import androidx.core.graphics.drawable.toDrawable
@@ -81,6 +82,19 @@ abstract class BaseActivity<VB : ViewBinding>(
         initTheme()
         window.decorView.disableAutoFill()
         AppContextWrapper.applyLocaleAndFont(this)
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+            val enable = !AppConfig.isPredictiveBackEnabled
+            if (enable) {
+                onBackInvokedDispatcher.registerOnBackInvokedCallback(
+                    OnBackInvokedDispatcher.PRIORITY_DEFAULT
+                ) {
+                    onBackPressedDispatcher.onBackPressed()
+                }
+            } else {
+                //不注册才是启用
+            }
+        }
+
         super.onCreate(savedInstanceState)
         WindowCompat.setDecorFitsSystemWindows(window, false)
         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q)

+ 1 - 0
app/src/main/java/io/legado/app/constant/PreferKey.kt

@@ -209,4 +209,5 @@ object PreferKey {
     const val colorImage = "colorImage"
     const val bookshelfRefreshingLimit = "bookshelfRefreshingLimit"
     const val systemMediaControlCompatibilityChange = "systemMediaControlCompatibilityChange"
+    const val isPredictiveBackEnabled = "isPredictiveBackEnabled"
 }

+ 6 - 0
app/src/main/java/io/legado/app/help/config/AppConfig.kt

@@ -846,5 +846,11 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
         set(value) {
             appCtx.putPrefBoolean(PreferKey.systemMediaControlCompatibilityChange, value)
         }
+
+    var isPredictiveBackEnabled: Boolean
+        get() = appCtx.getPrefBoolean(PreferKey.isPredictiveBackEnabled, true)
+        set(value) {
+            appCtx.putPrefBoolean(PreferKey.isPredictiveBackEnabled, value)
+        }
 }
 

+ 4 - 0
app/src/main/java/io/legado/app/ui/config/ThemeConfigFragment.kt

@@ -185,6 +185,10 @@ class ThemeConfigFragment : PreferenceFragmentCompat(),
                 }, 100)
             }
 
+            PreferKey.isPredictiveBackEnabled -> {
+                toastOnUi("重启以应用")
+            }
+
             PreferKey.cPrimary -> {
                 val color = getPrefInt(key, ThemeStore.primaryColor(requireContext()))
                 ThemeStore.editTheme(requireContext())

+ 7 - 0
app/src/main/res/xml/pref_config_theme.xml

@@ -138,6 +138,13 @@
             app:entries="@array/default_home_page"
             app:entryValues="@array/default_home_page_value" />
 
+
+        <io.legado.app.lib.prefs.SwitchPreference
+            android:defaultValue="true"
+            android:key="isPredictiveBackEnabled"
+            android:summary="仅部分设备可用"
+            android:title="预见性返回手势" />
+
     </io.legado.app.lib.prefs.PreferenceCategory>
 
     <io.legado.app.lib.prefs.PreferenceCategory