2021-01-08 10:58:24 +01:00
package org.mosad.teapod.util
import android.app.Activity
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Build
2021-01-20 16:10:41 +01:00
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.commit
import org.mosad.teapod.R
2022-09-20 19:47:42 +02:00
import org.mosad.teapod.ui.activity.player.PlayerActivity
2021-01-21 20:31:15 +01:00
import kotlin.system.exitProcess
2021-01-20 16:10:41 +01:00
/ * *
* Show a fragment on top of the current fragment .
* The current fragment is replaced and the new one is added
* to the back stack .
* /
fun FragmentActivity . showFragment ( fragment : Fragment ) {
supportFragmentManager . commit {
replace ( R . id . nav _host _fragment , fragment , fragment . javaClass . simpleName )
addToBackStack ( fragment . javaClass . name )
show ( fragment )
}
}
2021-01-08 10:58:24 +01:00
2022-09-20 19:47:42 +02:00
/ * *
* Start the player as new activity .
*
* @param seasonId The ID of the season the episode to be played is in
* @param episodeId The ID of the episode to play
* /
fun Activity . startPlayer ( seasonId : String , episodeId : String ) {
val intent = Intent ( this , PlayerActivity :: class . java ) . apply {
putExtra ( getString ( R . string . intent _season _id ) , seasonId )
putExtra ( getString ( R . string . intent _episode _id ) , episodeId )
}
startActivity ( intent )
}
2021-01-08 10:58:24 +01:00
/ * *
* hide the status and navigation bar
* /
fun Activity . hideBars ( ) {
2022-04-15 13:32:16 +02:00
hideBars ( window , window . decorView . rootView )
2021-01-08 10:58:24 +01:00
}
fun Activity . isInPiPMode ( ) : Boolean {
return if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . N ) {
isInPictureInPictureMode
} else {
false // pip mode not supported
}
}
/ * *
* Bring up launcher task to front
* /
fun Activity . navToLauncherTask ( ) {
val activityManager = ( getSystemService ( Context . ACTIVITY _SERVICE ) as ActivityManager )
activityManager . appTasks . forEach { task ->
val baseIntent = task . taskInfo . baseIntent
val categories = baseIntent . categories
if ( categories != null && categories . contains ( Intent . CATEGORY _LAUNCHER ) ) {
task . moveToFront ( )
return
}
}
}
2021-01-21 20:31:15 +01:00
/ * *
* exit and remove the app from tasks
* /
fun Activity . exitAndRemoveTask ( ) {
finishAndRemoveTask ( )
exitProcess ( 0 )
}