move PanicKit and code to launchers

This commit is contained in:
lucky 2022-01-14 15:39:34 +03:00
parent 5f31be5eba
commit 88da4bcc37
6 changed files with 37 additions and 27 deletions

View File

@ -51,6 +51,7 @@
<activity <activity
android:name=".PanicResponderActivity" android:name=".PanicResponderActivity"
android:noHistory="true" android:noHistory="true"
android:enabled="false"
android:exported="true" android:exported="true"
android:theme="@android:style/Theme.NoDisplay"> android:theme="@android:style/Theme.NoDisplay">
<intent-filter> <intent-filter>
@ -62,6 +63,7 @@
<activity <activity
android:name=".PanicConnectionActivity" android:name=".PanicConnectionActivity"
android:noHistory="true" android:noHistory="true"
android:enabled="false"
android:exported="true"> android:exported="true">
<intent-filter> <intent-filter>
<action android:name="info.guardianproject.panic.action.CONNECT" /> <action android:name="info.guardianproject.panic.action.CONNECT" />

View File

@ -88,14 +88,8 @@ open class MainActivity : AppCompatActivity() {
private fun setup() { private fun setup() {
binding.apply { binding.apply {
description.setOnLongClickListener {
showLaunchersSettings()
true
}
code.setOnClickListener { code.setOnClickListener {
prefs.isCodeEnabled = !prefs.isCodeEnabled showLaunchersSettings()
updateCodeColorState()
setCodeReceiverState(prefs.isServiceEnabled && prefs.isCodeEnabled)
} }
code.setOnLongClickListener { code.setOnLongClickListener {
prefs.code = makeCode() prefs.code = makeCode()
@ -172,7 +166,7 @@ open class MainActivity : AppCompatActivity() {
} }
.setPositiveButton(R.string.ok) { _, _ -> .setPositiveButton(R.string.ok) { _, _ ->
prefs.launchers = launchers prefs.launchers = launchers
setLaunchers(prefs.isServiceEnabled) setLaunchersState(prefs.isServiceEnabled)
} }
.show() .show()
} }
@ -196,7 +190,7 @@ open class MainActivity : AppCompatActivity() {
private fun updateCodeColorState() { private fun updateCodeColorState() {
binding.code.setBackgroundColor(getColor( binding.code.setBackgroundColor(getColor(
if (prefs.isCodeEnabled) R.color.code_receiver_on else R.color.code_receiver_off if (prefs.launchers != 0) R.color.code_receiver_on else R.color.code_receiver_off
)) ))
} }
@ -207,19 +201,23 @@ open class MainActivity : AppCompatActivity() {
return return
} }
prefs.isServiceEnabled = true prefs.isServiceEnabled = true
setCodeReceiverState(prefs.isCodeEnabled) setLaunchersState(true)
setLaunchers(true)
} }
private fun setLaunchers(value: Boolean) { private fun setLaunchersState(value: Boolean) {
if (value) { if (value) {
val launchers = prefs.launchers val launchers = prefs.launchers
setPanicKitState(launchers.and(Launcher.PANIC_KIT.flag) != 0)
setTileState(launchers.and(Launcher.TILE.flag) != 0) setTileState(launchers.and(Launcher.TILE.flag) != 0)
if (launchers.and(Launcher.SHORTCUT.flag) != 0) shortcut.push() else shortcut.remove() shortcut.setState(launchers.and(Launcher.SHORTCUT.flag) != 0)
setCodeReceiverState(launchers.and(Launcher.CODE.flag) != 0)
} else { } else {
setPanicKitState(false)
setTileState(false) setTileState(false)
shortcut.remove() shortcut.setState(false)
setCodeReceiverState(false)
} }
updateCodeColorState()
} }
private fun showWipeJobServiceStartFailedPopup() { private fun showWipeJobServiceStartFailedPopup() {
@ -232,9 +230,8 @@ open class MainActivity : AppCompatActivity() {
private fun setOff() { private fun setOff() {
prefs.isServiceEnabled = false prefs.isServiceEnabled = false
setCodeReceiverState(false)
setWipeOnInactivityComponentsState(false) setWipeOnInactivityComponentsState(false)
setLaunchers(false) setLaunchersState(false)
admin.remove() admin.remove()
} }
@ -247,6 +244,11 @@ open class MainActivity : AppCompatActivity() {
private fun setTileState(value: Boolean) = private fun setTileState(value: Boolean) =
setComponentState(QSTileService::class.java, value) setComponentState(QSTileService::class.java, value)
private fun setPanicKitState(value: Boolean) {
setComponentState(PanicConnectionActivity::class.java, value)
setComponentState(PanicResponderActivity::class.java, value)
}
private fun setComponentState(cls: Class<*>, value: Boolean) { private fun setComponentState(cls: Class<*>, value: Boolean) {
packageManager.setComponentEnabledSetting( packageManager.setComponentEnabledSetting(
ComponentName(this, cls), ComponentName(this, cls),

View File

@ -17,12 +17,12 @@ class Preferences(ctx: Context) {
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 LAUNCHERS = "launchers"
private const val CODE_ENABLED = "code_enabled"
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"
// migration // migration
private const val DO_WIPE = "do_wipe" private const val DO_WIPE = "do_wipe"
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"
} }
@ -41,17 +41,16 @@ class Preferences(ctx: Context) {
set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) } set(value) = prefs.edit { putBoolean(SERVICE_ENABLED, value) }
var launchers: Int var launchers: Int
get() = prefs.getInt(LAUNCHERS, 0) get() = prefs.getInt(
LAUNCHERS,
if (prefs.getBoolean(CODE_ENABLED, false)) Launcher.CODE.flag else 0,
)
set(value) = prefs.edit { putInt(LAUNCHERS, value) } set(value) = prefs.edit { putInt(LAUNCHERS, value) }
var code: String var code: String
get() = prefs.getString(CODE, "") ?: "" get() = prefs.getString(CODE, "") ?: ""
set(value) = prefs.edit { putString(CODE, value) } set(value) = prefs.edit { putString(CODE, value) }
var isCodeEnabled: Boolean
get() = prefs.getBoolean(CODE_ENABLED, false)
set(value) = prefs.edit { putBoolean(CODE_ENABLED, value) }
var isWipeData: Boolean var isWipeData: Boolean
get() = prefs.getBoolean(WIPE_DATA, prefs.getBoolean(DO_WIPE, false)) get() = prefs.getBoolean(WIPE_DATA, prefs.getBoolean(DO_WIPE, false))
set(value) = prefs.edit { putBoolean(WIPE_DATA, value) } set(value) = prefs.edit { putBoolean(WIPE_DATA, value) }
@ -80,6 +79,8 @@ class Preferences(ctx: Context) {
} }
enum class Launcher(val flag: Int) { enum class Launcher(val flag: Int) {
TILE(1), PANIC_KIT(1),
SHORTCUT(1 shl 1), TILE(1 shl 1),
SHORTCUT(1 shl 2),
CODE(1 shl 3)
} }

View File

@ -13,7 +13,7 @@ class ShortcutManager(private val ctx: Context) {
private val prefs by lazy { Preferences(ctx) } private val prefs by lazy { Preferences(ctx) }
fun push() { private fun push() {
ShortcutManagerCompat.pushDynamicShortcut( ShortcutManagerCompat.pushDynamicShortcut(
ctx, ctx,
ShortcutInfoCompat.Builder(ctx, SHORTCUT_ID) ShortcutInfoCompat.Builder(ctx, SHORTCUT_ID)
@ -28,5 +28,6 @@ class ShortcutManager(private val ctx: Context) {
) )
} }
fun remove() = ShortcutManagerCompat.removeDynamicShortcuts(ctx, arrayListOf(SHORTCUT_ID)) private fun remove() = ShortcutManagerCompat.removeDynamicShortcuts(ctx, arrayListOf(SHORTCUT_ID))
} fun setState(value: Boolean) = if (value) push() else remove()
}

View File

@ -28,7 +28,9 @@
<string name="dialog_confirm_panic_message">Активировать тревогу\? Это заблокирует устройство и опционально сотрёт его данные.</string> <string name="dialog_confirm_panic_message">Активировать тревогу\? Это заблокирует устройство и опционально сотрёт его данные.</string>
<string name="yes">Да</string> <string name="yes">Да</string>
<string-array name="launchers"> <string-array name="launchers">
<item>PanicKit</item>
<item>Плитка</item> <item>Плитка</item>
<item>Ярлык</item> <item>Ярлык</item>
<item>Код</item>
</string-array> </string-array>
</resources> </resources>

View File

@ -28,7 +28,9 @@
<string name="dialog_confirm_panic_message">Activate panic\? This will lock a device and optionally wipe its data.</string> <string name="dialog_confirm_panic_message">Activate panic\? This will lock a device and optionally wipe its data.</string>
<string name="yes">Yes</string> <string name="yes">Yes</string>
<string-array name="launchers"> <string-array name="launchers">
<item>PanicKit</item>
<item>Tile</item> <item>Tile</item>
<item>Shortcut</item> <item>Shortcut</item>
<item>Code</item>
</string-array> </string-array>
</resources> </resources>