From 8809b143d816ebc673f3f475424c64586ec289df Mon Sep 17 00:00:00 2001
From: lucky <>
Date: Mon, 14 Feb 2022 05:30:59 +0300
Subject: [PATCH] force check states
---
.../main/java/me/lucky/wasted/CodeReceiver.kt | 12 ++---
.../main/java/me/lucky/wasted/MainActivity.kt | 46 +++++++++----------
.../wasted/NotificationListenerService.kt | 4 +-
.../me/lucky/wasted/PanicResponderActivity.kt | 5 +-
.../main/java/me/lucky/wasted/Preferences.kt | 16 ++++---
.../java/me/lucky/wasted/ShortcutActivity.kt | 4 ++
.../main/java/me/lucky/wasted/TileService.kt | 2 +-
app/src/main/res/layout/activity_main.xml | 2 +-
app/src/main/res/values-ru/strings.xml | 10 ++--
app/src/main/res/values/colors.xml | 4 +-
app/src/main/res/values/string-arrays.xml | 12 ++---
app/src/main/res/values/strings.xml | 10 ++--
12 files changed, 70 insertions(+), 57 deletions(-)
diff --git a/app/src/main/java/me/lucky/wasted/CodeReceiver.kt b/app/src/main/java/me/lucky/wasted/CodeReceiver.kt
index df5d267..3347d7e 100644
--- a/app/src/main/java/me/lucky/wasted/CodeReceiver.kt
+++ b/app/src/main/java/me/lucky/wasted/CodeReceiver.kt
@@ -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)
}
}
diff --git a/app/src/main/java/me/lucky/wasted/MainActivity.kt b/app/src/main/java/me/lucky/wasted/MainActivity.kt
index 40c4024..a2b071f 100644
--- a/app/src/main/java/me/lucky/wasted/MainActivity.kt
+++ b/app/src/main/java/me/lucky/wasted/MainActivity.kt
@@ -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()
}
diff --git a/app/src/main/java/me/lucky/wasted/NotificationListenerService.kt b/app/src/main/java/me/lucky/wasted/NotificationListenerService.kt
index d026f7a..4c0c82f 100644
--- a/app/src/main/java/me/lucky/wasted/NotificationListenerService.kt
+++ b/app/src/main/java/me/lucky/wasted/NotificationListenerService.kt
@@ -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
diff --git a/app/src/main/java/me/lucky/wasted/PanicResponderActivity.kt b/app/src/main/java/me/lucky/wasted/PanicResponderActivity.kt
index 830ee62..b10b2d2 100644
--- a/app/src/main/java/me/lucky/wasted/PanicResponderActivity.kt
+++ b/app/src/main/java/me/lucky/wasted/PanicResponderActivity.kt
@@ -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
}
diff --git a/app/src/main/java/me/lucky/wasted/Preferences.kt b/app/src/main/java/me/lucky/wasted/Preferences.kt
index 8779ff3..fcd12e9 100644
--- a/app/src/main/java/me/lucky/wasted/Preferences.kt
+++ b/app/src/main/java/me/lucky/wasted/Preferences.kt
@@ -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),
diff --git a/app/src/main/java/me/lucky/wasted/ShortcutActivity.kt b/app/src/main/java/me/lucky/wasted/ShortcutActivity.kt
index 1f663e8..b179eec 100644
--- a/app/src/main/java/me/lucky/wasted/ShortcutActivity.kt
+++ b/app/src/main/java/me/lucky/wasted/ShortcutActivity.kt
@@ -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()
}
diff --git a/app/src/main/java/me/lucky/wasted/TileService.kt b/app/src/main/java/me/lucky/wasted/TileService.kt
index 65cc9a7..9254838 100644
--- a/app/src/main/java/me/lucky/wasted/TileService.kt
+++ b/app/src/main/java/me/lucky/wasted/TileService.kt
@@ -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()
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index d624f18..6f0c2f1 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -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"
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index 794df32..d032468 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -26,11 +26,11 @@
Активировать тревогу\?
Это заблокирует устройство и опционально сотрёт его данные.
Да
- Тревожная кнопка
- Плитка
- Ярлык
- Широковещательное сообщение
- Уведомление
+ Тревожная кнопка
+ Плитка
+ Ярлык
+ Широковещательное сообщение
+ Уведомление
Вайпер
Сканирует уведомления на наличие кода аутентификации
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index 8705aad..137eb52 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -6,6 +6,6 @@
#FF000000
#FFFFFFFF
- #FFD600
- #E13741
+ #FFD600
+ #E13741
\ No newline at end of file
diff --git a/app/src/main/res/values/string-arrays.xml b/app/src/main/res/values/string-arrays.xml
index fd44d2f..922cdbd 100644
--- a/app/src/main/res/values/string-arrays.xml
+++ b/app/src/main/res/values/string-arrays.xml
@@ -1,10 +1,10 @@
-
- - @string/launchers_array_panic_kit
- - @string/launchers_array_tile
- - @string/launchers_array_shortcut
- - @string/launchers_array_broadcast
- - @string/launchers_array_notification
+
+ - @string/triggers_array_panic_kit
+ - @string/triggers_array_tile
+ - @string/triggers_array_shortcut
+ - @string/triggers_array_broadcast
+ - @string/triggers_array_notification
\ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index b70daec..7c96596 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -26,11 +26,11 @@
Activate panic\?
This will lock a device and optionally wipe its data.
Yes
- PanicKit
- Tile
- Shortcut
- Broadcast
- Notification
+ PanicKit
+ Tile
+ Shortcut
+ Broadcast
+ Notification
Viper
Scan notifications for authentication code