Fix and simplify

This commit is contained in:
Luna712 2023-10-09 14:01:04 -06:00
parent 9933b5adbe
commit b672f2f427
4 changed files with 24 additions and 45 deletions

View file

@ -61,7 +61,9 @@
android:exported="true" android:exported="true"
android:resizeableActivity="true" android:resizeableActivity="true"
android:screenOrientation="userLandscape" android:screenOrientation="userLandscape"
android:supportsPictureInPicture="true"> android:supportsPictureInPicture="true"
android:taskAffinity="com.lagradost.cloudstream3.downloadedplayer"
android:launchMode="singleTask">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />

View file

@ -64,34 +64,10 @@ object CommonActivity {
private set(value) { private set(value) {
_activity = WeakReference(value) _activity = WeakReference(value)
} }
private val activeActivities = mutableListOf<WeakReference<Activity>>() // Keep track of active activities
@MainThread @MainThread
fun registerActivity(activity: Activity) { fun setActivityInstance(newActivity: Activity?) {
activeActivities.add(WeakReference(activity)) activity = newActivity
}
@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 @MainThread
@ -232,24 +208,25 @@ object CommonActivity {
setLocale(this, localeCode) setLocale(this, localeCode)
} }
fun init() { fun init(act: Activity) {
val act = getCurrentActivity() as? ComponentActivity ?: return setActivityInstance(act)
val componentActivity = activity as? ComponentActivity ?: return
activity = act
//https://stackoverflow.com/questions/52594181/how-to-know-if-user-has-disabled-picture-in-picture-feature-permission //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 //https://developer.android.com/guide/topics/ui/picture-in-picture
canShowPipMode = canShowPipMode =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && // OS SUPPORT Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && // OS SUPPORT
act.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && // HAS FEATURE, MIGHT BE BLOCKED DUE TO POWER DRAIN componentActivity.packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && // HAS FEATURE, MIGHT BE BLOCKED DUE TO POWER DRAIN
act.hasPIPPermission() // CHECK IF FEATURE IS ENABLED IN SETTINGS componentActivity.hasPIPPermission() // CHECK IF FEATURE IS ENABLED IN SETTINGS
act.updateLocale() componentActivity.updateLocale()
act.updateTv() componentActivity.updateTv()
NewPipe.init(DownloaderTestImpl.getInstance()) NewPipe.init(DownloaderTestImpl.getInstance())
for (resumeApp in resumeApps) { for (resumeApp in resumeApps) {
resumeApp.launcher = resumeApp.launcher =
act.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> componentActivity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
val resultCode = result.resultCode val resultCode = result.resultCode
val data = result.data val data = result.data
if (resultCode == AppCompatActivity.RESULT_OK && data != null && resumeApp.position != null && resumeApp.duration != null) { if (resultCode == AppCompatActivity.RESULT_OK && data != null && resumeApp.position != null && resumeApp.duration != null) {
@ -266,11 +243,11 @@ object CommonActivity {
// Ask for notification permissions on Android 13 // Ask for notification permissions on Android 13
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
ContextCompat.checkSelfPermission( ContextCompat.checkSelfPermission(
act, componentActivity,
Manifest.permission.POST_NOTIFICATIONS Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED ) != PackageManager.PERMISSION_GRANTED
) { ) {
val requestPermissionLauncher = act.registerForActivityResult( val requestPermissionLauncher = componentActivity.registerForActivityResult(
ActivityResultContracts.RequestPermission() ActivityResultContracts.RequestPermission()
) { isGranted: Boolean -> ) { isGranted: Boolean ->
Log.d(TAG, "Notification permission: $isGranted") Log.d(TAG, "Notification permission: $isGranted")

View file

@ -67,6 +67,7 @@ import com.lagradost.cloudstream3.CommonActivity.onColorSelectedEvent
import com.lagradost.cloudstream3.CommonActivity.onDialogDismissedEvent import com.lagradost.cloudstream3.CommonActivity.onDialogDismissedEvent
import com.lagradost.cloudstream3.CommonActivity.onUserLeaveHint import com.lagradost.cloudstream3.CommonActivity.onUserLeaveHint
import com.lagradost.cloudstream3.CommonActivity.screenHeight import com.lagradost.cloudstream3.CommonActivity.screenHeight
import com.lagradost.cloudstream3.CommonActivity.setActivityInstance
import com.lagradost.cloudstream3.CommonActivity.showToast import com.lagradost.cloudstream3.CommonActivity.showToast
import com.lagradost.cloudstream3.CommonActivity.updateLocale import com.lagradost.cloudstream3.CommonActivity.updateLocale
import com.lagradost.cloudstream3.databinding.ActivityMainBinding import com.lagradost.cloudstream3.databinding.ActivityMainBinding
@ -590,6 +591,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
afterPluginsLoadedEvent += ::onAllPluginsLoaded afterPluginsLoadedEvent += ::onAllPluginsLoaded
setActivityInstance(this)
try { try {
if (isCastApiAvailable()) { if (isCastApiAvailable()) {
//mCastSession = mSessionManager.currentCastSession //mCastSession = mSessionManager.currentCastSession
@ -681,7 +683,6 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
broadcastIntent.setClass(this, VideoDownloadRestartReceiver::class.java) broadcastIntent.setClass(this, VideoDownloadRestartReceiver::class.java)
this.sendBroadcast(broadcastIntent) this.sendBroadcast(broadcastIntent)
afterPluginsLoadedEvent -= ::onAllPluginsLoaded afterPluginsLoadedEvent -= ::onAllPluginsLoaded
CommonActivity.unregisterActivity(this)
super.onDestroy() super.onDestroy()
} }
@ -1370,8 +1371,8 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
// val navView: BottomNavigationView = findViewById(R.id.nav_view) // val navView: BottomNavigationView = findViewById(R.id.nav_view)
setUpBackup() setUpBackup()
CommonActivity.registerActivity(this)
CommonActivity.init() CommonActivity.init(this)
val navHostFragment = val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController val navController = navHostFragment.navController

View file

@ -74,8 +74,7 @@ class DownloadedPlayerActivity : AppCompatActivity() {
CommonActivity.loadThemes(this) CommonActivity.loadThemes(this)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
CommonActivity.registerActivity(this) CommonActivity.init(this)
CommonActivity.init()
setContentView(R.layout.empty_layout) setContentView(R.layout.empty_layout)
@ -112,8 +111,8 @@ class DownloadedPlayerActivity : AppCompatActivity() {
} }
} }
override fun onDestroy() { override fun onResume() {
CommonActivity.unregisterActivity(this) super.onResume()
super.onDestroy() CommonActivity.setActivityInstance(this)
} }
} }