The saved kiosk setting is cleared on startup unless AppConfig.KIOSK_PERSISTED is set, so a device left locked is released on the next launch. Scanning the kiosk barcode still toggles lockdown for the current session. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.8 KiB
Kotlin
105 lines
3.8 KiB
Kotlin
package mx.joykoo.scanner
|
|
|
|
import android.app.Activity
|
|
import android.app.ActivityManager
|
|
import android.app.admin.DevicePolicyManager
|
|
import android.content.ComponentName
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.IntentFilter
|
|
import android.content.pm.PackageManager
|
|
import android.os.Build
|
|
import android.util.Log
|
|
|
|
/**
|
|
* Kiosk lockdown via Android Lock Task mode, toggled by scanning AppConfig.KIOSK_BARCODE.
|
|
* The on/off setting is re-applied on every resume, and kept across restarts only when
|
|
* AppConfig.KIOSK_PERSISTED is set; otherwise each launch starts with kiosk off.
|
|
*
|
|
* - Device owner: true lockdown. Home/Recents and the notification shade are blocked,
|
|
* only the power menu stays available, and the app becomes the persistent home
|
|
* screen so it comes back after a reboot.
|
|
* - Not device owner: falls back to screen pinning, which shows a system prompt and
|
|
* can be undone by the user (hold Back + Recents).
|
|
*/
|
|
class KioskController(private val activity: Activity) {
|
|
|
|
private val dpm = activity.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
|
|
private val am = activity.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
|
|
private val admin = ComponentName(activity, KioskAdminReceiver::class.java)
|
|
private val homeAlias = ComponentName(activity, "${activity.packageName}.KioskHome")
|
|
private val prefs = activity.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
|
|
val isDeviceOwner: Boolean
|
|
get() = dpm.isDeviceOwnerApp(activity.packageName)
|
|
|
|
var isEnabled: Boolean
|
|
get() = prefs.getBoolean(KEY_ENABLED, false)
|
|
private set(value) = prefs.edit().putBoolean(KEY_ENABLED, value).apply()
|
|
|
|
init {
|
|
if (!AppConfig.KIOSK_PERSISTED) isEnabled = false
|
|
}
|
|
|
|
private val isInLockTask: Boolean
|
|
get() = am.lockTaskModeState != ActivityManager.LOCK_TASK_MODE_NONE
|
|
|
|
fun toggle(): Boolean {
|
|
isEnabled = !isEnabled
|
|
apply()
|
|
return isEnabled
|
|
}
|
|
|
|
/** Brings the device in line with the saved setting. Call from onResume. */
|
|
fun apply() {
|
|
try {
|
|
if (isEnabled) enter() else exit()
|
|
} catch (e: Exception) {
|
|
Log.e(TAG, "Kiosk ${if (isEnabled) "enter" else "exit"} failed", e)
|
|
}
|
|
}
|
|
|
|
private fun enter() {
|
|
if (isDeviceOwner) {
|
|
dpm.setLockTaskPackages(admin, arrayOf(activity.packageName))
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
|
dpm.setLockTaskFeatures(admin, DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS)
|
|
}
|
|
setHomeScreen(true)
|
|
}
|
|
if (!isInLockTask) activity.startLockTask()
|
|
}
|
|
|
|
private fun exit() {
|
|
if (isInLockTask) activity.stopLockTask()
|
|
if (isDeviceOwner) {
|
|
setHomeScreen(false)
|
|
dpm.setLockTaskPackages(admin, emptyArray())
|
|
}
|
|
}
|
|
|
|
private fun setHomeScreen(enabled: Boolean) {
|
|
activity.packageManager.setComponentEnabledSetting(
|
|
homeAlias,
|
|
if (enabled) PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
|
else PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
PackageManager.DONT_KILL_APP
|
|
)
|
|
if (enabled) {
|
|
val filter = IntentFilter(Intent.ACTION_MAIN).apply {
|
|
addCategory(Intent.CATEGORY_HOME)
|
|
addCategory(Intent.CATEGORY_DEFAULT)
|
|
}
|
|
dpm.addPersistentPreferredActivity(admin, filter, homeAlias)
|
|
} else {
|
|
dpm.clearPackagePersistentPreferredActivities(admin, activity.packageName)
|
|
}
|
|
}
|
|
|
|
companion object {
|
|
private const val TAG = "JoykooScanner"
|
|
private const val PREFS = "kiosk"
|
|
private const val KEY_ENABLED = "enabled"
|
|
}
|
|
}
|