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

View File

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

View File

@ -21,7 +21,9 @@ class NotificationListenerService : NotificationListenerService() {
override fun onNotificationPosted(sbn: StatusBarNotification?) { override fun onNotificationPosted(sbn: StatusBarNotification?) {
super.onNotificationPosted(sbn) 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 val code = prefs.code
if (code == "" || if (code == "" ||
sbn.notification.extras[Notification.EXTRA_TEXT]?.toString() != code) return sbn.notification.extras[Notification.EXTRA_TEXT]?.toString() != code) return

View File

@ -12,7 +12,10 @@ class PanicResponderActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (!Panic.isTriggerIntent(intent) || !prefs.isServiceEnabled) { if (!Panic.isTriggerIntent(intent) ||
!prefs.isServiceEnabled ||
prefs.triggers.and(Trigger.PANIC_KIT.value) == 0)
{
finishAndRemoveTask() finishAndRemoveTask()
return 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 MAX_FAILED_PASSWORD_ATTEMPTS = "max_failed_password_attempts"
private const val WIPE_ON_INACTIVITY = "wipe_on_inactivity" 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 WIPE_ON_INACTIVITY_DAYS = "wipe_on_inactivity_days"
private const val FILE_NAME = "sec_shared_prefs" 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 CODE_ENABLED = "code_enabled"
private const val WIPE_ON_INACTIVE = "wipe_on_inactive" private const val WIPE_ON_INACTIVE = "wipe_on_inactive"
private const val WIPE_ON_INACTIVE_DAYS = "wipe_on_inactive_days" 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) private val mk = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
@ -40,12 +41,15 @@ class Preferences(ctx: Context) {
get() = prefs.getBoolean(SERVICE_ENABLED, false) get() = prefs.getBoolean(SERVICE_ENABLED, false)
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) } set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
var launchers: Int var triggers: Int
get() = prefs.getInt( get() = prefs.getInt(
LAUNCHERS, TRIGGERS,
if (prefs.getBoolean(CODE_ENABLED, false)) Launcher.BROADCAST.value else 0, 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 var code: String
get() = prefs.getString(CODE, "") ?: "" get() = prefs.getString(CODE, "") ?: ""
@ -78,7 +82,7 @@ class Preferences(ctx: Context) {
set(value) = prefs.edit { putInt(WIPE_ON_INACTIVITY_DAYS, value) } 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), PANIC_KIT(1),
TILE(1 shl 1), TILE(1 shl 1),
SHORTCUT(1 shl 2), SHORTCUT(1 shl 2),

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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