package com.lagradost.cloudstream3 import android.app.PictureInPictureParams import android.content.pm.PackageManager import android.os.Build import android.os.Bundle import com.google.android.material.bottomnavigation.BottomNavigationView import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContentProviderCompat.requireContext import androidx.lifecycle.ViewModelStore import androidx.lifecycle.ViewModelStoreOwner import androidx.navigation.findNavController import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupWithNavController import com.lagradost.cloudstream3.UIHelper.checkWrite import com.lagradost.cloudstream3.UIHelper.hasPIPPermission import com.lagradost.cloudstream3.UIHelper.requestRW import com.lagradost.cloudstream3.UIHelper.shouldShowPIPMode class MainActivity : AppCompatActivity() { /*, ViewModelStoreOwner { private val appViewModelStore: ViewModelStore by lazy { ViewModelStore() } override fun getViewModelStore(): ViewModelStore { return appViewModelStore }*/ companion object { var isInPlayer: Boolean = false var canShowPipMode: Boolean = false var isInPIPMode: Boolean = false } private fun enterPIPMode() { if (!shouldShowPIPMode(isInPlayer) || !canShowPipMode) return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { try { enterPictureInPictureMode(PictureInPictureParams.Builder().build()) } catch (e: Exception) { enterPictureInPictureMode() } } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { enterPictureInPictureMode() } } } override fun onUserLeaveHint() { super.onUserLeaveHint() if (isInPlayer && canShowPipMode) { enterPIPMode() } } private fun AppCompatActivity.backPressed(): Boolean { val currentFragment = supportFragmentManager.fragments.last { it.isVisible } if (currentFragment != null && supportFragmentManager.fragments.size > 2) { //MainActivity.showNavbar() supportFragmentManager.beginTransaction() .setCustomAnimations(R.anim.enter_anim, R.anim.exit_anim, R.anim.pop_enter, R.anim.pop_exit) .remove(currentFragment) .commitAllowingStateLoss() return true } return false } override fun onBackPressed() { if (backPressed()) return super.onBackPressed() } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val navView: BottomNavigationView = findViewById(R.id.nav_view) //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 canShowPipMode = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && // OS SUPPORT packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && // HAS FEATURE, MIGHT BE BLOCKED DUE TO POWER DRAIN hasPIPPermission() // CHECK IF FEATURE IS ENABLED IN SETTINGS val navController = findNavController(R.id.nav_host_fragment) // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. val appBarConfiguration = AppBarConfiguration(setOf( R.id.navigation_home, R.id.navigation_search, R.id.navigation_notifications)) //setupActionBarWithNavController(navController, appBarConfiguration) navView.setupWithNavController(navController) if (!checkWrite()) { requestRW() if (checkWrite()) return } } }