change tile logic

This commit is contained in:
lucky 2021-12-24 23:28:18 +03:00
parent 854eda8250
commit afce3f24df
9 changed files with 55 additions and 26 deletions

View File

@ -10,8 +10,8 @@ android {
applicationId "me.lucky.wasted"
minSdk 23
targetSdk 31
versionCode 8
versionName "1.1.1"
versionCode 9
versionName "1.1.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

View File

@ -20,7 +20,7 @@ class CodeReceiver : BroadcastReceiver() {
val dpm = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
try {
dpm.lockNow()
if (prefs.doWipe) dpm.wipeData(0)
if (prefs.doWipe) dpm.wipeData(Utils.getWipeDataFlags())
} catch (exc: SecurityException) {}
}
}

View File

@ -0,0 +1,15 @@
package me.lucky.wasted
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
class DeviceAdmin(ctx: Context) {
val dpm by lazy {
ctx.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
val deviceAdmin by lazy { ComponentName(ctx, DeviceAdminReceiver::class.java) }
fun remove() = dpm.removeActiveAdmin(deviceAdmin)
fun isActive(): Boolean = dpm.isAdminActive(deviceAdmin)
}

View File

@ -17,10 +17,7 @@ open class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val prefs by lazy { Preferences(this) }
private val dpm by lazy {
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
private val deviceAdmin by lazy { ComponentName(this, DeviceAdminReceiver::class.java) }
private val admin by lazy { DeviceAdmin(this) }
private val requestAdminPolicy =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
@ -44,7 +41,7 @@ open class MainActivity : AppCompatActivity() {
}
private fun update() {
if (!isAdminActive() && prefs.isServiceEnabled)
if (!admin.isActive() && prefs.isServiceEnabled)
Toast.makeText(
this,
getString(R.string.service_unavailable_toast),
@ -73,7 +70,7 @@ open class MainActivity : AppCompatActivity() {
}
toggle.setOnCheckedChangeListener { _, isChecked ->
when (isChecked) {
true -> if (!isAdminActive()) requestAdmin() else setOn()
true -> if (!admin.isActive()) requestAdmin() else setOn()
false -> setOff()
}
}
@ -86,14 +83,14 @@ open class MainActivity : AppCompatActivity() {
}
private fun setOff() {
dpm.removeActiveAdmin(deviceAdmin)
admin.remove()
setControlReceiverState(this, false)
prefs.isServiceEnabled = false
}
private fun requestAdmin() {
val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN).apply {
putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin)
putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin.deviceAdmin)
putExtra(
DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.device_admin_description),
@ -103,7 +100,6 @@ open class MainActivity : AppCompatActivity() {
}
private fun makeCode(): String = UUID.randomUUID().toString()
private fun isAdminActive(): Boolean = dpm.isAdminActive(deviceAdmin)
private fun setControlReceiverState(ctx: Context, value: Boolean) {
ctx.packageManager.setComponentEnabledSetting(

View File

@ -23,7 +23,7 @@ class PanicResponderActivity : AppCompatActivity() {
try {
dpm.lockNow()
if (PanicResponder.receivedTriggerFromConnectedApp(this) &&
prefs.doWipe) dpm.wipeData(0)
prefs.doWipe) dpm.wipeData(Utils.getWipeDataFlags())
} catch (exc: SecurityException) {}
finish()
}

View File

@ -1,7 +1,5 @@
package me.lucky.wasted
import android.app.admin.DevicePolicyManager
import android.content.Context
import android.os.Build
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
@ -13,22 +11,25 @@ import kotlin.concurrent.timerTask
@RequiresApi(Build.VERSION_CODES.N)
class QSTileService : TileService() {
private val prefs by lazy { Preferences(this) }
private val dpm by lazy {
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
private val admin by lazy { DeviceAdmin(this) }
private val counter = AtomicInteger(0)
private var timer: Timer? = null
override fun onStartListening() {
super.onStartListening()
update(if (prefs.isServiceEnabled) Tile.STATE_INACTIVE else Tile.STATE_UNAVAILABLE)
update(
if (prefs.isServiceEnabled && admin.isActive()) Tile.STATE_INACTIVE
else Tile.STATE_UNAVAILABLE
)
}
override fun onClick() {
super.onClick()
if (!prefs.isServiceEnabled) return
if (!prefs.doWipe) {
dpm.lockNow()
try {
admin.dpm.lockNow()
} catch (exc: SecurityException) {}
return
}
when (counter.getAndIncrement()) {
@ -37,13 +38,16 @@ class QSTileService : TileService() {
timer?.cancel()
timer = Timer()
timer?.schedule(timerTask {
update(Tile.STATE_INACTIVE)
counter.set(0)
try {
admin.dpm.lockNow()
admin.dpm.wipeData(Utils.getWipeDataFlags())
} catch (exc: SecurityException) {}
}, 2000)
}
1 -> {
dpm.lockNow()
dpm.wipeData(0)
else -> {
timer?.cancel()
update(Tile.STATE_INACTIVE)
counter.set(0)
}
}
}

View File

@ -0,0 +1,13 @@
package me.lucky.wasted
import android.app.admin.DevicePolicyManager
import android.os.Build
class Utils {
companion object {
fun getWipeDataFlags(): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
DevicePolicyManager.WIPE_SILENTLY else 0
}
}
}

View File

@ -2,7 +2,7 @@
<string name="app_name">Wasted</string>
<string name="description">Turn on Wasted to lock device on panic trigger. You can use PanicKit, Tile or send broadcast message with this authentication code.</string>
<string name="device_admin_label">Wasted</string>
<string name="device_admin_description">Allow Wasted to lock device and wipe data on panic trigger</string>
<string name="device_admin_description">Allow Wasted to lock device and (optionally) wipe data on panic trigger</string>
<string name="service_unavailable_toast">Admin service unavailable</string>
<string name="wipe_data_check_box">Wipe data</string>
<string name="panic_app_dialog_title">Confirm Panic App</string>

View File

@ -0,0 +1 @@
change Tile logic