From 9933b5adbe5a81e017b8b53b2b31424e4ca0a294 Mon Sep 17 00:00:00 2001 From: Luna712 <142361265+Luna712@users.noreply.github.com> Date: Sun, 8 Oct 2023 17:00:44 -0600 Subject: [PATCH] Fix issue where DownloadedPlayerActivity interferes with MainActivity --- .../lagradost/cloudstream3/CommonActivity.kt | 34 +++++++++++++++++-- .../lagradost/cloudstream3/MainActivity.kt | 5 +-- .../ui/player/DownloadedPlayerActivity.kt | 8 ++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt index a7d899b6..8a650ac1 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/CommonActivity.kt @@ -64,6 +64,35 @@ object CommonActivity { private set(value) { _activity = WeakReference(value) } + private val activeActivities = mutableListOf>() // Keep track of active activities + + @MainThread + fun registerActivity(activity: Activity) { + activeActivities.add(WeakReference(activity)) + } + + @MainThread + fun unregisterActivity(activity: Activity) { + val iterator = activeActivities.iterator() + while (iterator.hasNext()) { + val weakReference = iterator.next() + val storedActivity = weakReference.get() + if (storedActivity == null || storedActivity == activity) { + iterator.remove() + } + } + } + + @MainThread + fun getCurrentActivity(): Activity? { + for (weakReference in activeActivities) { + val activity = weakReference.get() + if (activity != null && !activity.isFinishing) { + return activity + } + } + return null + } @MainThread fun Activity?.getCastSession(): CastSession? { @@ -203,8 +232,9 @@ object CommonActivity { setLocale(this, localeCode) } - fun init(act: ComponentActivity?) { - if (act == null) return + fun init() { + val act = getCurrentActivity() as? ComponentActivity ?: return + activity = act //https://stackoverflow.com/questions/52594181/how-to-know-if-user-has-disabled-picture-in-picture-feature-permission //https://developer.android.com/guide/topics/ui/picture-in-picture diff --git a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt index 17823f7c..60468ff6 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt @@ -681,6 +681,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { broadcastIntent.setClass(this, VideoDownloadRestartReceiver::class.java) this.sendBroadcast(broadcastIntent) afterPluginsLoadedEvent -= ::onAllPluginsLoaded + CommonActivity.unregisterActivity(this) super.onDestroy() } @@ -1369,8 +1370,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener { // val navView: BottomNavigationView = findViewById(R.id.nav_view) setUpBackup() - - CommonActivity.init(this) + CommonActivity.registerActivity(this) + CommonActivity.init() val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController 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 d181e175..39338179 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 @@ -74,7 +74,8 @@ class DownloadedPlayerActivity : AppCompatActivity() { CommonActivity.loadThemes(this) super.onCreate(savedInstanceState) - CommonActivity.init(this) + CommonActivity.registerActivity(this) + CommonActivity.init() setContentView(R.layout.empty_layout) @@ -110,4 +111,9 @@ class DownloadedPlayerActivity : AppCompatActivity() { return } } + + override fun onDestroy() { + CommonActivity.unregisterActivity(this) + super.onDestroy() + } } \ No newline at end of file