Skip to content

Commit

Permalink
direct boot aware
Browse files Browse the repository at this point in the history
  • Loading branch information
lucky committed Jul 4, 2022
1 parent 38dfa2d commit 8ea32fc
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "me.lucky.duress"
minSdk 23
targetSdk 32
versionCode 4
versionName "1.0.3"
versionCode 5
versionName "1.0.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down Expand Up @@ -42,10 +42,10 @@ dependencies {
implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'androidx.security:security-crypto:1.0.0'
implementation 'androidx.preference:preference-ktx:1.2.0'
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<service
android:name=".AccessibilityService"
android:exported="true"
android:directBootAware="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/me/lucky/duress/AccessibilityService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AccessibilityService : AccessibilityService() {
}

private fun init() {
prefs = Preferences(this)
prefs = Preferences.new(this)
keyguardManager = getSystemService(KeyguardManager::class.java)
registerReceiver(lockReceiver, IntentFilter().apply {
addAction(Intent.ACTION_USER_PRESENT)
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/me/lucky/duress/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package me.lucky.duress

import android.accessibilityservice.AccessibilityServiceInfo
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.provider.Settings
import android.view.View
Expand All @@ -17,9 +18,14 @@ import me.lucky.duress.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var prefs: Preferences
private lateinit var prefsdb: Preferences
private val admin by lazy { DeviceAdminManager(this) }
private var accessibilityManager: AccessibilityManager? = null

private val prefsListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
prefs.copyTo(prefsdb, key)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
Expand All @@ -31,11 +37,19 @@ class MainActivity : AppCompatActivity() {

override fun onStart() {
super.onStart()
prefs.registerListener(prefsListener)
update()
}

override fun onStop() {
super.onStop()
prefs.unregisterListener(prefsListener)
}

private fun init() {
prefs = Preferences(this)
prefsdb = Preferences(this, encrypted = false)
prefs.copyTo(prefsdb)
accessibilityManager = getSystemService(AccessibilityManager::class.java)
binding.apply {
tabs.selectTab(tabs.getTabAt(prefs.mode))
Expand Down
53 changes: 44 additions & 9 deletions app/src/main/java/me/lucky/duress/Preferences.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package me.lucky.duress

import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.os.UserManager
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys

class Preferences(ctx: Context) {
class Preferences(ctx: Context, encrypted: Boolean = true) {
companion object {
private const val ENABLED = "enabled"
private const val MODE = "mode"
Expand All @@ -19,16 +23,28 @@ class Preferences(ctx: Context) {
private const val FILE_NAME = "sec_shared_prefs"
// migration
private const val SERVICE_ENABLED = "service_enabled"

fun new(ctx: Context) = Preferences(
ctx,
encrypted = Build.VERSION.SDK_INT < Build.VERSION_CODES.N ||
ctx.getSystemService(UserManager::class.java).isUserUnlocked,
)
}

private val mk = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
private val prefs = EncryptedSharedPreferences.create(
FILE_NAME,
mk,
ctx,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
private val prefs: SharedPreferences = if (encrypted) {
val mk = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
EncryptedSharedPreferences.create(
FILE_NAME,
mk,
ctx,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
} else {
val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
ctx.createDeviceProtectedStorageContext() else ctx
PreferenceManager.getDefaultSharedPreferences(context)
}

var isEnabled: Boolean
get() = prefs.getBoolean(ENABLED, prefs.getBoolean(SERVICE_ENABLED, false))
Expand Down Expand Up @@ -61,6 +77,25 @@ class Preferences(ctx: Context) {
var isShowProminentDisclosure: Boolean
get() = prefs.getBoolean(SHOW_PROMINENT_DISCLOSURE, true)
set(value) = prefs.edit { putBoolean(SHOW_PROMINENT_DISCLOSURE, value) }

fun registerListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) =
prefs.registerOnSharedPreferenceChangeListener(listener)

fun unregisterListener(listener: SharedPreferences.OnSharedPreferenceChangeListener) =
prefs.unregisterOnSharedPreferenceChangeListener(listener)

fun copyTo(dst: Preferences, key: String? = null) = dst.prefs.edit {
for (entry in prefs.all.entries) {
val k = entry.key
if (key != null && k != key) continue
val v = entry.value ?: continue
when (v) {
is Boolean -> putBoolean(k, v)
is Int -> putInt(k, v)
is String -> putString(k, v)
}
}
}
}

enum class Mode(val value: Int) {
Expand Down
1 change: 1 addition & 0 deletions fastlane/metadata/android/en-US/changelogs/5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
direct boot aware

0 comments on commit 8ea32fc

Please sign in to comment.