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
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsController
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
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
/ * *
* hide the status and navigation bar
* /
fun Activity . hideBars ( ) {
window . apply {
if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . R ) {
setDecorFitsSystemWindows ( false )
insetsController ?. apply {
hide ( WindowInsets . Type . statusBars ( ) or WindowInsets . Type . navigationBars ( ) )
2022-03-29 23:23:10 +02:00
systemBarsBehavior = if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . S ) {
WindowInsetsController . BEHAVIOR _DEFAULT
} else {
@Suppress ( " deprecation " )
WindowInsetsController . BEHAVIOR _SHOW _BARS _BY _SWIPE
}
2021-01-08 10:58:24 +01:00
}
} else {
@Suppress ( " deprecation " )
decorView . systemUiVisibility = ( View . SYSTEM _UI _FLAG _IMMERSIVE
or View . SYSTEM _UI _FLAG _LAYOUT _STABLE
or View . SYSTEM _UI _FLAG _LAYOUT _HIDE _NAVIGATION
or View . SYSTEM _UI _FLAG _LAYOUT _FULLSCREEN
or View . SYSTEM _UI _FLAG _HIDE _NAVIGATION
or View . SYSTEM _UI _FLAG _FULLSCREEN )
}
}
}
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 )
}