add TileService

This commit is contained in:
lucky 2021-12-24 13:37:34 +03:00
parent 169acb84dd
commit 1ee550ee50
11 changed files with 82 additions and 12 deletions

View File

@ -8,7 +8,7 @@ Lock device and wipe data on panic trigger.
<img src="https://user-images.githubusercontent.com/53379023/146694310-41316fdb-b7c7-44e7-b18d-a1f4d0a7bec5.png" width="30%" height="30%">
You can use [PanicKit](https://guardianproject.info/code/panickit/) or send broadcast message
You can use [PanicKit](https://guardianproject.info/code/panickit/), Tile or send broadcast message
with authentication code. On trigger, using
[Device Administration API](https://developer.android.com/guide/topics/admin/device-admin), it
locks device and runs wipe.

View File

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

View File

@ -65,6 +65,20 @@
</intent-filter>
</activity>
<service
android:name=".QSTileService"
android:icon="@android:drawable/ic_lock_lock"
android:label="@string/tile_label"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:exported="true">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
<meta-data
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
android:value="true" />
</service>
</application>
</manifest>

View File

@ -7,7 +7,6 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import java.util.*
@ -19,7 +18,8 @@ open class MainActivity : AppCompatActivity() {
private val prefs by lazy { Preferences(this) }
private val dpm by lazy {
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager }
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
private val deviceAdmin by lazy { ComponentName(this, DeviceAdminReceiver::class.java) }
private val requestAdminPolicy =

View File

@ -2,13 +2,11 @@ package me.lucky.wasted
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import info.guardianproject.panic.PanicResponder
class PanicConnectionActivity : MainActivity() {
private val pm by lazy { packageManager }
override fun onCreate(savedInstanceState: Bundle?) {

View File

@ -3,7 +3,6 @@ package me.lucky.wasted
import android.app.admin.DevicePolicyManager
import android.content.Context
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import info.guardianproject.panic.Panic
@ -12,7 +11,8 @@ import info.guardianproject.panic.PanicResponder
class PanicResponderActivity : AppCompatActivity() {
private val prefs by lazy { Preferences(this) }
private val dpm by lazy {
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager }
getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

View File

@ -1,7 +1,6 @@
package me.lucky.wasted
import android.content.Context
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys

View File

@ -0,0 +1,57 @@
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
import androidx.annotation.RequiresApi
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
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 counter = AtomicInteger(0)
private var timer: Timer? = null
override fun onStartListening() {
super.onStartListening()
update(if (prefs.isServiceEnabled) Tile.STATE_INACTIVE else Tile.STATE_UNAVAILABLE)
}
override fun onClick() {
super.onClick()
if (!prefs.isServiceEnabled) return
if (!prefs.doWipe) {
dpm.lockNow()
return
}
when (counter.getAndIncrement()) {
0 -> {
update(Tile.STATE_ACTIVE)
timer?.cancel()
timer = Timer()
timer?.schedule(timerTask {
update(Tile.STATE_INACTIVE)
counter.set(0)
}, 2000)
}
1 -> {
dpm.lockNow()
dpm.wipeData(0)
}
}
}
private fun update(tileState: Int) {
qsTile.apply {
state = tileState
updateTile()
}
}
}

View File

@ -1,6 +1,6 @@
<resources>
<string name="app_name">Wasted</string>
<string name="description">Turn on Wasted to lock device on panic trigger. You can use PanicKit or send broadcast message with this authentication code.</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="service_unavailable_toast">Admin service unavailable</string>
@ -10,4 +10,5 @@
<string name="panic_app_unknown_app">an unknown app</string>
<string name="allow">Allow</string>
<string name="cancel">Cancel</string>
<string name="tile_label">Unlock device</string>
</resources>

View File

@ -0,0 +1 @@
add TileService

View File

@ -1,4 +1,4 @@
Lock device and wipe data on panic trigger.
You can use PanicKit or send broadcast message with authentication code.
You can use PanicKit, Tile or send broadcast message with authentication code.
On trigger, using Device Administration API, it locks device and runs wipe.