diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 453c1fae..a71c5ecb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,7 @@ - + @@ -17,7 +17,11 @@ - + + + + tools:targetApi="tiramisu"> - + android:exported="false"> + diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index a41028bd..3a10aa10 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -19,6 +19,7 @@ import android.view.View import android.view.ViewGroup import android.view.WindowManager import android.widget.Toast +import androidx.activity.OnBackPressedCallback import androidx.activity.result.ActivityResultLauncher import androidx.annotation.IdRes import androidx.annotation.MainThread @@ -132,7 +133,6 @@ import com.lagradost.cloudstream3.utils.DataStore.setKey import com.lagradost.cloudstream3.utils.DataStoreHelper import com.lagradost.cloudstream3.utils.DataStoreHelper.migrateResumeWatching import com.lagradost.cloudstream3.utils.Event -import com.lagradost.cloudstream3.utils.IOnBackPressed import com.lagradost.cloudstream3.utils.InAppUpdater.Companion.runAutoUpdate import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showBottomDialog import com.lagradost.cloudstream3.utils.UIHelper.changeStatusBarState @@ -650,34 +650,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { builder.show().setDefaultFocus() } - private fun backPressed() { - this.window?.navigationBarColor = - this.colorFromAttribute(R.attr.primaryGrayBackground) - this.updateLocale() - this.updateLocale() - - val navHostFragment = - supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as? NavHostFragment - val navController = navHostFragment?.navController - val isAtHome = - navController?.currentDestination?.matchDestination(R.id.navigation_home) == true - - if (isAtHome && isTvSettings()) { - showConfirmExitDialog() - } else { - super.onBackPressed() - } - } - - override fun onBackPressed() { - ((supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as? NavHostFragment?)?.childFragmentManager?.primaryNavigationFragment as? IOnBackPressed)?.onBackPressed() - ?.let { runNormal -> - if (runNormal) backPressed() - } ?: run { - backPressed() - } - } - override fun onDestroy() { val broadcastIntent = Intent() broadcastIntent.action = "restart_service" @@ -1087,6 +1059,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { } catch (_: Throwable) { } } + override fun onCreate(savedInstanceState: Bundle?) { app.initClient(this) val settingsManager = PreferenceManager.getDefaultSharedPreferences(this) @@ -1384,6 +1357,12 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { this.putString(SearchFragment.SEARCH_QUERY, nextSearchQuery) } } + + if (isTvSettings()) { + if (navDestination.matchDestination(R.id.navigation_home)) { + attachBackPressedCallback() + } else detachBackPressedCallback() + } } //val navController = findNavController(R.id.nav_host_fragment) @@ -1598,6 +1577,44 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { // } // } + onBackPressedDispatcher.addCallback( + this, + object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + window?.navigationBarColor = colorFromAttribute(R.attr.primaryGrayBackground) + updateLocale() + + // If we don't disable we end up in a loop with default behavior calling + // this callback as well, so we disable it, run default behavior, + // then re-enable this callback so it can be used for next back press. + isEnabled = false + onBackPressedDispatcher.onBackPressed() + isEnabled = true + } + } + ) + } + + private var backPressedCallback: OnBackPressedCallback? = null + + private fun attachBackPressedCallback() { + if (backPressedCallback == null) { + backPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + showConfirmExitDialog() + window?.navigationBarColor = + colorFromAttribute(R.attr.primaryGrayBackground) + updateLocale() + } + } + } + + backPressedCallback?.isEnabled = true + onBackPressedDispatcher.addCallback(this, backPressedCallback ?: return) + } + + private fun detachBackPressedCallback() { + backPressedCallback?.isEnabled = false } suspend fun checkGithubConnectivity(): Boolean { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt index f62482ed..c3ec2bbd 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/download/DownloadChildFragment.kt @@ -60,7 +60,7 @@ class DownloadChildFragment : Fragment() { } }.sortedBy { it.data.episode + (it.data.season ?: 0) * 100000 } if (eps.isEmpty()) { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() return@main } @@ -78,7 +78,7 @@ class DownloadChildFragment : Fragment() { val folder = arguments?.getString("folder") val name = arguments?.getString("name") if (folder == null) { - activity?.onBackPressed() // TODO FIX + activity?.onBackPressedDispatcher?.onBackPressed() // TODO FIX return } fixPaddingStatusbar(binding?.downloadChildRoot) @@ -87,7 +87,7 @@ class DownloadChildFragment : Fragment() { title = name setNavigationIcon(R.drawable.ic_baseline_arrow_back_24) setNavigationOnClickListener { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() } } diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/player/DownloadedPlayerActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/player/DownloadedPlayerActivity.kt index 4c3376bb..1e2ea540 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/player/DownloadedPlayerActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/player/DownloadedPlayerActivity.kt @@ -6,6 +6,7 @@ import android.net.Uri import android.os.Bundle import android.util.Log import android.view.KeyEvent +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import com.lagradost.cloudstream3.CommonActivity import com.lagradost.cloudstream3.R @@ -34,10 +35,6 @@ class DownloadedPlayerActivity : AppCompatActivity() { CommonActivity.onUserLeaveHint(this) } - override fun onBackPressed() { - finish() - } - private fun playLink(url: String) { this.navigate( R.id.global_to_navigation_player, GeneratorPlayer.newInstance( @@ -109,6 +106,15 @@ class DownloadedPlayerActivity : AppCompatActivity() { finish() return } + + onBackPressedDispatcher.addCallback( + this, + object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + finish() + } + } + ) } override fun onResume() { diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt index c30e70e5..ef3db0b4 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/result/ResultTrailerPlayer.kt @@ -7,6 +7,7 @@ import android.os.Bundle import android.view.View import android.view.ViewGroup import android.widget.FrameLayout +import androidx.activity.OnBackPressedCallback import androidx.core.view.isGone import androidx.core.view.isVisible import com.lagradost.cloudstream3.CommonActivity.screenHeight @@ -15,10 +16,8 @@ import com.lagradost.cloudstream3.R import com.lagradost.cloudstream3.ui.player.CSPlayerEvent import com.lagradost.cloudstream3.ui.player.PlayerEventSource import com.lagradost.cloudstream3.ui.player.SubtitleData -import com.lagradost.cloudstream3.utils.IOnBackPressed - -open class ResultTrailerPlayer : ResultFragmentPhone(), IOnBackPressed { +open class ResultTrailerPlayer : ResultFragmentPhone() { override var lockRotation = false override var isFullScreenPlayer = false @@ -28,7 +27,7 @@ open class ResultTrailerPlayer : ResultFragmentPhone(), IOnBackPressed { const val TAG = "RESULT_TRAILER" } - var playerWidthHeight: Pair? = null + private var playerWidthHeight: Pair? = null override fun nextEpisode() {} @@ -154,6 +153,10 @@ open class ResultTrailerPlayer : ResultFragmentPhone(), IOnBackPressed { } fixPlayerSize() uiReset() + + if (isFullScreenPlayer) { + attachBackPressedCallback() + } else detachBackPressedCallback() } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -172,12 +175,26 @@ open class ResultTrailerPlayer : ResultFragmentPhone(), IOnBackPressed { } } - override fun onBackPressed(): Boolean { - return if (isFullScreenPlayer) { - updateFullscreen(false) - false - } else { - true + private var backPressedCallback: OnBackPressedCallback? = null + + private fun attachBackPressedCallback() { + if (backPressedCallback == null) { + backPressedCallback = object : OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + updateFullscreen(false) + } + } } + + backPressedCallback?.isEnabled = true + + activity?.onBackPressedDispatcher?.addCallback( + activity ?: return, + backPressedCallback ?: return + ) + } + + private fun detachBackPressedCallback() { + backPressedCallback?.isEnabled = false } } \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt index 6ea6363e..37c71134 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsFragment.kt @@ -82,7 +82,7 @@ class SettingsFragment : Fragment() { setTitle(title) setNavigationIcon(R.drawable.ic_baseline_arrow_back_24) setNavigationOnClickListener { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() } } fixPaddingStatusbar(settingsToolbar) @@ -97,7 +97,7 @@ class SettingsFragment : Fragment() { setNavigationIcon(R.drawable.ic_baseline_arrow_back_24) children.firstOrNull { it is ImageView }?.tag = getString(R.string.tv_no_focus_tag) setNavigationOnClickListener { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() } } fixPaddingStatusbar(settingsToolbar) diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt index b490386f..c5256ffa 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/extensions/PluginsFragment.kt @@ -70,7 +70,7 @@ class PluginsFragment : Fragment() { val isLocal = arguments?.getBoolean(PLUGINS_BUNDLE_LOCAL) == true if (url == null || name == null) { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() return } @@ -119,7 +119,7 @@ class PluginsFragment : Fragment() { if (searchView?.isIconified == false) { searchView.isIconified = true } else { - activity?.onBackPressed() + activity?.onBackPressedDispatcher?.onBackPressed() } } searchView?.setOnQueryTextFocusChangeListener { _, hasFocus -> diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/IOnBackPressed.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/IOnBackPressed.kt deleted file mode 100644 index b4922945..00000000 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/IOnBackPressed.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.lagradost.cloudstream3.utils - -interface IOnBackPressed { - fun onBackPressed(): Boolean -} \ No newline at end of file diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt index 231a634d..134d7127 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/UIHelper.kt @@ -418,7 +418,7 @@ object UIHelper { } fun FragmentActivity.popCurrentPage() { - this.onBackPressed() + this.onBackPressedDispatcher.onBackPressed() /*val currentFragment = supportFragmentManager.fragments.lastOrNull { it.isVisible } ?: return @@ -438,7 +438,7 @@ object UIHelper { val currentFragment = supportFragmentManager.fragments.lastOrNull { it.isVisible } - ?: //this.onBackPressed() + ?: //this.onBackPressedDispatcher.onBackPressed() return /* diff --git a/app/src/main/res/xml/data_extraction_rules.xml b/app/src/main/res/xml/data_extraction_rules.xml new file mode 100644 index 00000000..ae9ece33 --- /dev/null +++ b/app/src/main/res/xml/data_extraction_rules.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file