Skip to content

Commit

Permalink
Implement configurable auto lock delay duration
Browse files Browse the repository at this point in the history
  • Loading branch information
nain-F49FF806 committed Dec 7, 2024
1 parent e63fa88 commit 0def953
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
defaultConfig {
minSdk = rootProject.extra["minSdkVersion"] as Int
targetSdk = rootProject.extra["targetSdkVersion"] as Int
versionCode = 1733550000
versionCode = 1733568000
versionName = "2024.12.07"
applicationId = "alt.nainapps.aer"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) 2022 2bllw8
* Copyright (c) 2024 nain
* SPDX-License-Identifier: GPL-3.0-only
*/
package alt.nainapps.aer.config

import alt.nainapps.aer.R
import alt.nainapps.aer.config.autolock.buildValidatedAutoLockDelayListener
import alt.nainapps.aer.config.password.ChangePasswordDialog
import alt.nainapps.aer.config.password.SetPasswordDialog
import alt.nainapps.aer.lock.LockStore
Expand All @@ -13,8 +15,10 @@ import alt.nainapps.aer.shell.AnemoShell
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.view.View
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.Switch
import android.widget.TextView
import java.util.function.Consumer
Expand Down Expand Up @@ -68,6 +72,13 @@ class ConfigurationActivity : Activity() {
lockStore.isAutoLockEnabled = isChecked
}

val autoLockDelayMinutesEditable = findViewById<EditText>(R.id.config_auto_lock_delay_minutes)
autoLockDelayMinutesEditable.text = Editable.Factory.getInstance().newEditable(
lockStore.autoLockDelayMinutes.toString()
)
val autoLockDelayListener = buildValidatedAutoLockDelayListener(this.baseContext, lockStore, autoLockDelayMinutesEditable)
autoLockDelayMinutesEditable.addTextChangedListener(autoLockDelayListener)

biometricSwitch = findViewById(R.id.configuration_biometric_unlock)
biometricSwitch!!.visibility = if (lockStore.canAuthenticateBiometric()) View.VISIBLE else View.GONE
biometricSwitch!!.isChecked = lockStore.isBiometricUnlockEnabled
Expand Down Expand Up @@ -120,4 +131,4 @@ class ConfigurationActivity : Activity() {
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 nain
* SPDX-License-Identifier: GPL-3.0-only
*/

package alt.nainapps.aer.config.autolock

import alt.nainapps.aer.R
import alt.nainapps.aer.lock.LockStore
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText

fun interface AutoLockDelayMinutesTextListener : TextWatcher {

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable) {
afterTextChanged(s.toString())
}
// this will be provided as lambda
fun afterTextChanged(text: String)
}

fun buildValidatedAutoLockDelayListener(context: Context, lockstore: LockStore, input: EditText): AutoLockDelayMinutesTextListener {
return AutoLockDelayMinutesTextListener { text ->
// validate text isNumber
try {
text.toLong()
} catch (error: NumberFormatException) {
input.setError("NaN: Not a Number",
context.getDrawable(R.drawable.ic_error))
return@AutoLockDelayMinutesTextListener
}
val minutes: Long = text.toLong()
// validate not zero
if (minutes == 0L) {
input.setError("Auto lock delay can't be zero",
context.getDrawable(R.drawable.ic_error))
}
lockstore.autoLockDelayMinutes = minutes
}
}
29 changes: 25 additions & 4 deletions app/src/main/java/alt/nainapps/aer/lock/LockStore.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2021 2bllw8
* Copyright (c) 2024 nain
* SPDX-License-Identifier: GPL-3.0-only
*/
package alt.nainapps.aer.lock
Expand Down Expand Up @@ -114,6 +115,22 @@ class LockStore private constructor(context: Context) : OnSharedPreferenceChange
}
}

@get:Synchronized
@set:Synchronized
var autoLockDelayMinutes: Long
get() = preferences.getLong(KEY_AUTO_LOCK_DELAY_MINUTES, DEFAULT_AUTO_LOCK_DELAY_MINUTES)
set(delayMinutes) {
preferences.edit().putLong(KEY_AUTO_LOCK_DELAY_MINUTES, delayMinutes).apply()

if (!isLocked) {
if (isAutoLockEnabled) {
// If auto-lock is enabled while the storage is unlocked, schedule new job
cancelAutoLock()
scheduleAutoLock()
}
}
}

fun canAuthenticateBiometric(): Boolean {
return Build.VERSION.SDK_INT >= 29 && biometricManager != null && biometricManager.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS
}
Expand Down Expand Up @@ -146,7 +163,7 @@ class LockStore private constructor(context: Context) : OnSharedPreferenceChange
private fun scheduleAutoLock() {
jobScheduler.schedule(
JobInfo.Builder(AUTO_LOCK_JOB_ID, autoLockComponent)
.setMinimumLatency(AUTO_LOCK_DELAY)
.setMinimumLatency(millisFromMinutes(autoLockDelayMinutes))
.build()
)
}
Expand All @@ -166,24 +183,28 @@ class LockStore private constructor(context: Context) : OnSharedPreferenceChange
}
}

// convert minutes to milliseconds
private fun millisFromMinutes(minutes: Long): Long {
return minutes * 60L * 1000L
}

companion object {
private const val TAG = "LockStore"

private const val LOCK_PREFERENCES = "lock_store"
private const val KEY_LOCK = "is_locked"
private const val KEY_PASSWORD = "password_hash"
private const val KEY_AUTO_LOCK = "auto_lock"
private const val KEY_AUTO_LOCK_DELAY_MINUTES = "auto_lock_delay_minutes"
private const val KEY_BIOMETRIC_UNLOCK = "biometric_unlock"
private const val DEFAULT_LOCK_VALUE = false
private const val DEFAULT_AUTO_LOCK_VALUE = false
private const val DEFAULT_AUTO_LOCK_DELAY_MINUTES = 15L

private const val HASH_ALGORITHM = "SHA-256"

private const val AUTO_LOCK_JOB_ID = 64

// 15 minutes in milliseconds
private const val AUTO_LOCK_DELAY = 1000L * 60L * 15L

@Volatile
private var instance: LockStore? = null

Expand Down
54 changes: 20 additions & 34 deletions app/src/main/res/layout/configuration.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!--
~ Copyright (c) 2022 2bllw8
~ Copyright (c) 2024 nain
~ SPDX-License-Identifier: GPL-3.0-only
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
Expand Down Expand Up @@ -55,11 +56,15 @@
android:textSize="16sp"
tools:text="@string/configuration_password_set" />

<View
android:id="@+id/divider"
<Switch
android:id="@+id/configuration_biometric_unlock"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:minHeight="56dp"
android:paddingHorizontal="16dp"
android:text="@string/configuration_storage_unlock_biometric"
android:textSize="16sp" />

<Switch
android:id="@+id/configuration_auto_lock"
Expand All @@ -77,46 +82,27 @@
android:orientation="horizontal">

<TextView
android:id="@+id/configuration_auto_lock_delay"
android:layout_width="match_parent"
android:id="@+id/configuration_auto_lock_delay_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:labelFor="@+id/config_auto_lock_delay_minutes"
android:paddingHorizontal="16dp"
android:text="@string/configuration_auto_lock_delay_minutes_label"
android:textColor="?android:textColorPrimary"
android:textSize="16sp"
android:labelFor="@+id/config_auto_lock_delay_minutes_input"
android:text="@string/configuration_auto_lock_delay_minutes_label"
tools:text="@string/configuration_auto_lock_delay_minutes_label"/>
tools:text="@string/configuration_auto_lock_delay_minutes_label" />

<EditText
android:id="@+id/config_auto_lock_delay_minutes_input"
android:id="@+id/config_auto_lock_delay_minutes"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:autofillHints="autoLockDelayMinutes"
android:ems="5"
android:inputType="numberDecimal"
android:text="@string/configuration_auto_lock_delay_minutes_default"
/>

android:minHeight="56dp"
android:minEms="3"
android:maxEms="5"
android:inputType="number" />
</LinearLayout>

<View
android:id="@+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />

<Switch
android:id="@+id/configuration_biometric_unlock"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:minHeight="56dp"
android:paddingHorizontal="16dp"
android:text="@string/configuration_storage_unlock_biometric"
android:textSize="16sp" />


</LinearLayout>
</ScrollView>
5 changes: 2 additions & 3 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@
<string name="configuration_storage_lock">Lock storage access</string>
<string name="configuration_storage_unlock">Unlock storage access</string>
<string name="configuration_storage_unlock_biometric">Allow storage unlock with biometric access</string>
<string name="configuration_storage_lock_auto">Automatically lock access after _ minutes</string>
<string name="configuration_storage_lock_auto">Automatically lock access after delay</string>
<string name="title_activity_storage_pref">StoragePrefActivity</string>
<string name="shortcut_storageconfig_long">Storage Configuration</string>
<string name="shortcut_storageconfig_short">Storage Config</string>
<string name="shortcut_storageconfig_disabled">Storage Config (Disabled)</string>
<string name="storage_config_select_help">Tap to select / Drag to reorder:</string>
<string name="storage_config_screen_title">Aer Storage Backend</string>
<string name="configuration_auto_lock_delay_minutes_label">Autolock delay (in minutes)</string>
<string name="configuration_auto_lock_delay_minutes_default">15</string>
<string name="configuration_auto_lock_delay_minutes_label">Auto lock delay (in minutes)</string>

</resources>

0 comments on commit 0def953

Please sign in to comment.