force check states

This commit is contained in:
lucky 2022-02-14 05:30:59 +03:00
parent cde21d14f0
commit 8809b143d8
12 changed files with 70 additions and 57 deletions

View File

@ -9,14 +9,12 @@ class CodeReceiver : BroadcastReceiver() {
const val KEY = "code"
const val ACTION = "me.lucky.wasted.action.TRIGGER"
fun panic(context: Context?, intent: Intent?) {
if (context == null || intent == null) return
fun panic(context: Context, intent: Intent?) {
if (intent?.action != ACTION) return
val prefs = Preferences(context)
if (!prefs.isServiceEnabled) return
val code = prefs.code
if (!prefs.isServiceEnabled ||
code == "" ||
intent.action != ACTION ||
intent.getStringExtra(KEY) != code) return
if (code == "" || intent.getStringExtra(KEY) != code) return
val admin = DeviceAdminManager(context)
try {
admin.lockNow()
@ -26,6 +24,8 @@ class CodeReceiver : BroadcastReceiver() {
}
override fun onReceive(context: Context?, intent: Intent?) {
if (context == null ||
Preferences(context).triggers.and(Trigger.BROADCAST.value) == 0) return
panic(context, intent)
}
}

View File

@ -92,7 +92,7 @@ open class MainActivity : AppCompatActivity() {
private fun setup() {
binding.apply {
code.setOnClickListener {
showLaunchersSettings()
showTriggersSettings()
}
code.setOnLongClickListener {
prefs.code = makeCode()
@ -150,28 +150,28 @@ open class MainActivity : AppCompatActivity() {
.show()
}
private fun showLaunchersSettings() {
var launchers = prefs.launchers
val values = Launcher.values().toMutableList()
val strings = resources.getStringArray(R.array.launchers).toMutableList()
private fun showTriggersSettings() {
var triggers = prefs.triggers
val values = Trigger.values().toMutableList()
val strings = resources.getStringArray(R.array.triggers).toMutableList()
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
strings.removeAt(values.indexOf(Launcher.TILE))
values.remove(Launcher.TILE)
strings.removeAt(values.indexOf(Trigger.TILE))
values.remove(Trigger.TILE)
}
MaterialAlertDialogBuilder(this)
.setMultiChoiceItems(
strings.toTypedArray(),
values.map { launchers.and(it.value) != 0 }.toBooleanArray(),
values.map { triggers.and(it.value) != 0 }.toBooleanArray(),
) { _, index, isChecked ->
val flag = values[index]
launchers = when (isChecked) {
true -> launchers.or(flag.value)
false -> launchers.and(flag.value.inv())
triggers = when (isChecked) {
true -> triggers.or(flag.value)
false -> triggers.and(flag.value.inv())
}
}
.setPositiveButton(android.R.string.ok) { _, _ ->
prefs.launchers = launchers
setLaunchersState(prefs.isServiceEnabled)
prefs.triggers = triggers
setTriggersState(prefs.isServiceEnabled)
}
.show()
}
@ -197,7 +197,7 @@ open class MainActivity : AppCompatActivity() {
private fun updateCodeColorState() {
binding.code.setBackgroundColor(getColor(
if (prefs.launchers != 0) R.color.code_receiver_on else R.color.code_receiver_off
if (prefs.triggers != 0) R.color.code_on else R.color.code_off
))
}
@ -208,17 +208,17 @@ open class MainActivity : AppCompatActivity() {
return
}
prefs.isServiceEnabled = true
setLaunchersState(true)
setTriggersState(true)
}
private fun setLaunchersState(value: Boolean) {
private fun setTriggersState(value: Boolean) {
if (value) {
val launchers = prefs.launchers
setPanicKitState(launchers.and(Launcher.PANIC_KIT.value) != 0)
setQSTileState(launchers.and(Launcher.TILE.value) != 0)
shortcut.setState(launchers.and(Launcher.SHORTCUT.value) != 0)
setCodeReceiverState(launchers.and(Launcher.BROADCAST.value) != 0)
setNotificationListenerState(launchers.and(Launcher.NOTIFICATION.value) != 0)
val triggers = prefs.triggers
setPanicKitState(triggers.and(Trigger.PANIC_KIT.value) != 0)
setQSTileState(triggers.and(Trigger.TILE.value) != 0)
shortcut.setState(triggers.and(Trigger.SHORTCUT.value) != 0)
setCodeReceiverState(triggers.and(Trigger.BROADCAST.value) != 0)
setNotificationListenerState(triggers.and(Trigger.NOTIFICATION.value) != 0)
} else {
setPanicKitState(false)
setQSTileState(false)
@ -240,7 +240,7 @@ open class MainActivity : AppCompatActivity() {
private fun setOff() {
prefs.isServiceEnabled = false
setWipeOnInactivityComponentsState(false)
setLaunchersState(false)
setTriggersState(false)
admin.remove()
}

View File

@ -21,7 +21,9 @@ class NotificationListenerService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn)
if (sbn == null || !prefs.isServiceEnabled) return
if (sbn == null ||
!prefs.isServiceEnabled ||
prefs.triggers.and(Trigger.NOTIFICATION.value) == 0) return
val code = prefs.code
if (code == "" ||
sbn.notification.extras[Notification.EXTRA_TEXT]?.toString() != code) return

View File

@ -12,7 +12,10 @@ class PanicResponderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (!Panic.isTriggerIntent(intent) || !prefs.isServiceEnabled) {
if (!Panic.isTriggerIntent(intent) ||
!prefs.isServiceEnabled ||
prefs.triggers.and(Trigger.PANIC_KIT.value) == 0)
{
finishAndRemoveTask()
return
}

View File

@ -16,7 +16,7 @@ class Preferences(ctx: Context) {
private const val MAX_FAILED_PASSWORD_ATTEMPTS = "max_failed_password_attempts"
private const val WIPE_ON_INACTIVITY = "wipe_on_inactivity"
private const val LAUNCHERS = "launchers"
private const val TRIGGERS = "triggers"
private const val WIPE_ON_INACTIVITY_DAYS = "wipe_on_inactivity_days"
private const val FILE_NAME = "sec_shared_prefs"
@ -25,6 +25,7 @@ class Preferences(ctx: Context) {
private const val CODE_ENABLED = "code_enabled"
private const val WIPE_ON_INACTIVE = "wipe_on_inactive"
private const val WIPE_ON_INACTIVE_DAYS = "wipe_on_inactive_days"
private const val LAUNCHERS = "launchers"
}
private val mk = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
@ -40,12 +41,15 @@ class Preferences(ctx: Context) {
get() = prefs.getBoolean(SERVICE_ENABLED, false)
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
var launchers: Int
var triggers: Int
get() = prefs.getInt(
LAUNCHERS,
if (prefs.getBoolean(CODE_ENABLED, false)) Launcher.BROADCAST.value else 0,
TRIGGERS,
prefs.getInt(
LAUNCHERS,
if (prefs.getBoolean(CODE_ENABLED, false)) Trigger.BROADCAST.value else 0
),
)
set(value) = prefs.edit { putInt(LAUNCHERS, value) }
set(value) = prefs.edit { putInt(TRIGGERS, value) }
var code: String
get() = prefs.getString(CODE, "") ?: ""
@ -78,7 +82,7 @@ class Preferences(ctx: Context) {
set(value) = prefs.edit { putInt(WIPE_ON_INACTIVITY_DAYS, value) }
}
enum class Launcher(val value: Int) {
enum class Trigger(val value: Int) {
PANIC_KIT(1),
TILE(1 shl 1),
SHORTCUT(1 shl 2),

View File

@ -6,6 +6,10 @@ import androidx.appcompat.app.AppCompatActivity
class ShortcutActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (Preferences(this).triggers.and(Trigger.SHORTCUT.value) == 0) {
finishAndRemoveTask()
return
}
CodeReceiver.panic(this, intent)
finishAndRemoveTask()
}

View File

@ -39,7 +39,7 @@ class TileService : TileService() {
override fun onClick() {
super.onClick()
if (!prefs.isServiceEnabled) return
if (!prefs.isServiceEnabled || prefs.triggers.and(Trigger.TILE.value) == 0) return
if (!prefs.isWipeData) {
try {
admin.lockNow()

View File

@ -35,7 +35,7 @@
android:id="@+id/code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/code_receiver_off"
android:background="@color/code_off"
android:padding="16dp"
android:textAlignment="center"
android:textColor="@color/black"

View File

@ -26,11 +26,11 @@
<string name="dialog_confirm_panic_title">Активировать тревогу\?</string>
<string name="dialog_confirm_panic_message">Это заблокирует устройство и опционально сотрёт его данные.</string>
<string name="yes">Да</string>
<string name="launchers_array_panic_kit">Тревожная кнопка</string>
<string name="launchers_array_tile">Плитка</string>
<string name="launchers_array_shortcut">Ярлык</string>
<string name="launchers_array_broadcast">Широковещательное сообщение</string>
<string name="launchers_array_notification">Уведомление</string>
<string name="triggers_array_panic_kit">Тревожная кнопка</string>
<string name="triggers_array_tile">Плитка</string>
<string name="triggers_array_shortcut">Ярлык</string>
<string name="triggers_array_broadcast">Широковещательное сообщение</string>
<string name="triggers_array_notification">Уведомление</string>
<string name="notification_listener_service_label">Вайпер</string>
<string name="notification_listener_service_description">Сканирует уведомления на наличие кода аутентификации</string>
</resources>

View File

@ -6,6 +6,6 @@
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="code_receiver_on">#FFD600</color>
<color name="code_receiver_off">#E13741</color>
<color name="code_on">#FFD600</color>
<color name="code_off">#E13741</color>
</resources>

View File

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="launchers">
<item>@string/launchers_array_panic_kit</item>
<item>@string/launchers_array_tile</item>
<item>@string/launchers_array_shortcut</item>
<item>@string/launchers_array_broadcast</item>
<item>@string/launchers_array_notification</item>
<string-array name="triggers">
<item>@string/triggers_array_panic_kit</item>
<item>@string/triggers_array_tile</item>
<item>@string/triggers_array_shortcut</item>
<item>@string/triggers_array_broadcast</item>
<item>@string/triggers_array_notification</item>
</string-array>
</resources>

View File

@ -26,11 +26,11 @@
<string name="dialog_confirm_panic_title">Activate panic\?</string>
<string name="dialog_confirm_panic_message">This will lock a device and optionally wipe its data.</string>
<string name="yes">Yes</string>
<string name="launchers_array_panic_kit">PanicKit</string>
<string name="launchers_array_tile">Tile</string>
<string name="launchers_array_shortcut">Shortcut</string>
<string name="launchers_array_broadcast">Broadcast</string>
<string name="launchers_array_notification">Notification</string>
<string name="triggers_array_panic_kit">PanicKit</string>
<string name="triggers_array_tile">Tile</string>
<string name="triggers_array_shortcut">Shortcut</string>
<string name="triggers_array_broadcast">Broadcast</string>
<string name="triggers_array_notification">Notification</string>
<string name="notification_listener_service_label">Viper</string>
<string name="notification_listener_service_description">Scan notifications for authentication code</string>
</resources>