cloudstream/app/src/main/java/com/lagradost/cloudstream3/MainActivity.kt

219 lines
9 KiB
Kotlin
Raw Normal View History

2021-04-30 17:20:15 +00:00
package com.lagradost.cloudstream3
2021-06-10 23:00:22 +00:00
import android.app.PictureInPictureParams
2021-07-04 00:59:51 +00:00
import android.content.Intent
2021-06-10 23:00:22 +00:00
import android.content.pm.PackageManager
import android.os.Build
2021-04-30 17:20:15 +00:00
import android.os.Bundle
2021-07-17 15:56:26 +00:00
import android.widget.Toast
2021-04-30 17:20:15 +00:00
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
2021-07-04 00:59:51 +00:00
import com.anggrayudi.storage.SimpleStorage
2021-07-04 17:00:04 +00:00
import com.anggrayudi.storage.file.StorageId
2021-06-14 00:00:29 +00:00
import com.google.android.gms.cast.framework.CastButtonFactory
2021-06-14 16:58:43 +00:00
import com.google.android.material.bottomnavigation.BottomNavigationView
2021-05-20 21:25:41 +00:00
import com.lagradost.cloudstream3.UIHelper.checkWrite
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.UIHelper.hasPIPPermission
2021-05-20 21:25:41 +00:00
import com.lagradost.cloudstream3.UIHelper.requestRW
2021-06-10 23:00:22 +00:00
import com.lagradost.cloudstream3.UIHelper.shouldShowPIPMode
2021-07-08 19:39:49 +00:00
import com.lagradost.cloudstream3.receivers.VideoDownloadRestartReceiver
2021-07-08 17:46:47 +00:00
import com.lagradost.cloudstream3.services.RESTART_ALL_DOWNLOADS_AND_QUEUE
import com.lagradost.cloudstream3.services.START_VALUE_KEY
import com.lagradost.cloudstream3.services.VideoDownloadKeepAliveService
2021-07-17 15:56:26 +00:00
import com.lagradost.cloudstream3.utils.DataStore.getKey
import com.lagradost.cloudstream3.utils.DataStore.getKeys
import com.lagradost.cloudstream3.utils.DataStore.removeKeys
2021-07-08 17:46:47 +00:00
import com.lagradost.cloudstream3.utils.VideoDownloadManager
2021-06-14 00:00:29 +00:00
import kotlinx.android.synthetic.main.fragment_result.*
2021-04-30 17:20:15 +00:00
2021-06-14 16:58:43 +00:00
2021-06-10 23:00:22 +00:00
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
2021-07-04 00:59:51 +00:00
lateinit var mainContext: MainActivity
//https://github.com/anggrayudi/SimpleStorage/blob/4eb6306efb6cdfae4e34f170c8b9d4e135b04d51/sample/src/main/java/com/anggrayudi/storage/sample/activity/MainActivity.kt#L624
const val REQUEST_CODE_STORAGE_ACCESS = 1
const val REQUEST_CODE_PICK_FOLDER = 2
const val REQUEST_CODE_PICK_FILE = 3
const val REQUEST_CODE_ASK_PERMISSIONS = 4
2021-06-10 23:00:22 +00:00
}
2021-07-04 00:59:51 +00:00
private lateinit var storage: SimpleStorage
2021-06-10 23:00:22 +00:00
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()
}
}
2021-05-22 22:25:56 +00:00
}
2021-06-10 23:00:22 +00:00
override fun onUserLeaveHint() {
super.onUserLeaveHint()
if (isInPlayer && canShowPipMode) {
enterPIPMode()
}
}
2021-05-22 22:25:56 +00:00
2021-05-18 13:43:32 +00:00
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()
}
2021-04-30 17:20:15 +00:00
2021-07-04 00:59:51 +00:00
private fun setupSimpleStorage() {
storage = SimpleStorage(this)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Mandatory for Activity, but not for Fragment
storage.onActivityResult(requestCode, resultCode, data)
}
override fun onSaveInstanceState(outState: Bundle) {
storage.onSaveInstanceState(outState)
super.onSaveInstanceState(outState)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
storage.onRestoreInstanceState(savedInstanceState)
}
2021-07-08 17:46:47 +00:00
override fun onDestroy() {
val broadcastIntent = Intent()
broadcastIntent.action = "restart_service"
broadcastIntent.setClass(this, VideoDownloadRestartReceiver::class.java)
this.sendBroadcast(broadcastIntent)
super.onDestroy()
}
2021-04-30 17:20:15 +00:00
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-06-29 23:14:48 +00:00
mainContext = this
2021-07-04 00:59:51 +00:00
setupSimpleStorage()
2021-07-04 17:00:04 +00:00
2021-07-17 15:56:26 +00:00
if (!storage.isStorageAccessGranted(StorageId.PRIMARY)) {
2021-07-04 17:00:04 +00:00
storage.requestStorageAccess(REQUEST_CODE_STORAGE_ACCESS)
}
2021-06-29 23:14:48 +00:00
2021-04-30 17:20:15 +00:00
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
2021-06-10 23:00:22 +00:00
//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
2021-04-30 17:20:15 +00:00
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.
2021-07-04 00:59:51 +00:00
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_search, R.id.navigation_notifications
)
)
2021-04-30 17:20:15 +00:00
//setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
2021-05-20 21:25:41 +00:00
if (!checkWrite()) {
requestRW()
if (checkWrite()) return
}
2021-06-14 00:00:29 +00:00
CastButtonFactory.setUpMediaRouteButton(this, media_route_button)
2021-06-14 16:58:43 +00:00
2021-07-15 16:45:25 +00:00
// THIS IS CURRENTLY REMOVED BECAUSE HIGHER VERS OF ANDROID NEEDS A NOTIFICATION
//if (!VideoDownloadManager.isMyServiceRunning(this, VideoDownloadKeepAliveService::class.java)) {
// val mYourService = VideoDownloadKeepAliveService()
// val mServiceIntent = Intent(this, mYourService::class.java).putExtra(START_VALUE_KEY, RESTART_ALL_DOWNLOADS_AND_QUEUE)
// this.startService(mServiceIntent)
//}
2021-07-17 15:56:26 +00:00
//settingsManager.getBoolean("disable_automatic_data_downloads", true) &&
if ( isUsingMobileData()) {
Toast.makeText(this, "Downloads not resumed on mobile data", Toast.LENGTH_LONG).show()
} else {
val keys = getKeys(VideoDownloadManager.KEY_RESUME_PACKAGES)
val resumePkg = keys.mapNotNull { k -> getKey<VideoDownloadManager.DownloadResumePackage>(k) }
// To remove a bug where this is permanent
removeKeys(VideoDownloadManager.KEY_RESUME_PACKAGES)
for (pkg in resumePkg) { // ADD ALL CURRENT DOWNLOADS
VideoDownloadManager.downloadFromResume(this, pkg, false)
}
2021-07-08 17:46:47 +00:00
2021-07-17 15:56:26 +00:00
// ADD QUEUE
// array needed because List gets cast exception to linkedList for some unknown reason
val resumeQueue =
getKey<Array<VideoDownloadManager.DownloadQueueResumePackage>>(VideoDownloadManager.KEY_RESUME_QUEUE_PACKAGES)
resumeQueue?.sortedBy { it.index }?.forEach {
VideoDownloadManager.downloadFromResume(this, it.pkg)
}
}
2021-06-14 00:00:29 +00:00
/*
val castContext = CastContext.getSharedInstance(applicationContext)
fun buildMediaQueueItem(video: String): MediaQueueItem {
// val movieMetadata = MediaMetadata(MediaMetadata.MEDIA_TYPE_PHOTO)
//movieMetadata.putString(MediaMetadata.KEY_TITLE, "CloudStream")
val mediaInfo = MediaInfo.Builder(Uri.parse(video).toString())
.setStreamType(MediaInfo.STREAM_TYPE_NONE)
.setContentType(MimeTypes.IMAGE_JPEG)
// .setMetadata(movieMetadata).build()
.build()
return MediaQueueItem.Builder(mediaInfo).build()
}*/
/*
castContext.addCastStateListener { state ->
if (state == CastState.CONNECTED) {
println("TESTING")
val isCasting = castContext?.sessionManager?.currentCastSession?.remoteMediaClient?.currentItem != null
if(!isCasting) {
val castPlayer = CastPlayer(castContext)
println("LOAD ITEM")
castPlayer.loadItem(buildMediaQueueItem("https://cdn.discordapp.com/attachments/551382684560261121/730169809408622702/ChromecastLogo6.png"),0)
}
}
}*/
2021-04-30 17:20:15 +00:00
}
}