Skip to content

Commit

Permalink
Merge pull request #66 from hegocre/feature/app_lock_impr
Browse files Browse the repository at this point in the history
App lock improvements
  • Loading branch information
hegocre authored Dec 19, 2023
2 parents 3e5ad57 + 7b447d4 commit 5884f16
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 56 deletions.
11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,21 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.12.0'

implementation 'com.goterl:lazysodium-android:5.1.0@aar'
implementation 'net.java.dev.jna:jna:5.13.0@aar'
implementation 'net.java.dev.jna:jna:5.14.0@aar'

implementation 'org.commonmark:commonmark:0.21.0'
implementation 'io.coil-kt:coil-compose:2.5.0'

//Compose dependencies
implementation platform('androidx.compose:compose-bom:2023.10.01')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.material3:material3:1.2.0-alpha12'
implementation 'androidx.compose.material3:material3:1.2.0-beta01'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material:material-icons-extended'
implementation 'androidx.compose.runtime:runtime-livedata'
implementation 'androidx.navigation:navigation-compose:2.7.5'
implementation 'androidx.activity:activity-compose:1.8.1'
implementation 'androidx.compose.foundation:foundation:1.6.0-beta03'
implementation 'androidx.navigation:navigation-compose:2.7.6'
implementation 'androidx.activity:activity-compose:1.8.2'
implementation 'androidx.biometric:biometric:1.1.0'
implementation "androidx.autofill:autofill:1.1.0"

Expand All @@ -109,7 +110,7 @@ dependencies {

implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2'

implementation 'com.materialkolor:material-kolor:1.2.9'
implementation 'com.materialkolor:material-kolor:1.3.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.arch.core:core-testing:2.2.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ class MainActivity : FragmentActivity() {
onCorrectPasscode = passwordsViewModel::disableLock
)
} else {
passwordsViewModel.disableLock()
if (hasAppLock == false) {
// Avoid asking for passcode just after setting it
passwordsViewModel.disableLock()
}
NextcloudPasswordsApp(
passwordsViewModel = passwordsViewModel,
onLogOut = { logOut() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ fun InputPasscodeDialog(
.focusRequester(requester),
value = passcode,
onValueChange = { newPasscode ->
if (newPasscode.length <= 4 &&
if (newPasscode.length <= 16 &&
(newPasscode.toIntOrNull() != null || newPasscode.isEmpty())
) {
setPasscode(newPasscode)
Expand All @@ -536,7 +536,6 @@ fun InputPasscodeDialog(
maxLines = 1,
label = { Text(text = stringResource(id = R.string.passcode)) },
isError = showEmptyError && passcode.isBlank(),
supportingText = { Text(text = "${passcode.length}/4") },
keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Number),
trailingIcon = {
IconButton(onClick = { showPasscode = !showPasscode }) {
Expand All @@ -554,7 +553,7 @@ fun InputPasscodeDialog(

TextButton(
onClick = {
if (passcode.length != 4 || passcode.toIntOrNull() == null) {
if (passcode.length < 4 || passcode.toIntOrNull() == null) {
showEmptyError = true
} else {
onInputPasscode(passcode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package com.hegocre.nextcloudpasswords.ui.components

import androidx.biometric.BiometricManager
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -12,6 +15,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Backspace
Expand All @@ -33,8 +37,11 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -48,7 +55,9 @@ import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun NextcloudPasswordsAppLock(
onCheckPasscode: (String) -> Deferred<Boolean>,
Expand All @@ -73,13 +82,8 @@ fun NextcloudPasswordsAppLock(
}

LaunchedEffect(key1 = inputPassword) {
if (inputPassword.length == 4) {
if (onCheckPasscode(inputPassword).await()) {
onCorrectPasscode()
} else {
setInputPassword("")
isError = true
}
if (onCheckPasscode(inputPassword).await()) {
onCorrectPasscode()
}
}

Expand Down Expand Up @@ -119,26 +123,17 @@ fun NextcloudPasswordsAppLock(

Spacer(modifier = Modifier.height(168.dp))

Row {
KeyboardDigitIndicator(
enabled = inputPassword.isNotEmpty(),
isError = isError
)

KeyboardDigitIndicator(
enabled = inputPassword.length > 1,
isError = isError
)

KeyboardDigitIndicator(
enabled = inputPassword.length > 2,
isError = isError
)

KeyboardDigitIndicator(
enabled = inputPassword.length > 3,
isError = isError
)
if (inputPassword.isEmpty()) {
Spacer(modifier = Modifier.height(10.dp))
} else {
LazyRow(modifier = Modifier.padding(horizontal = 16.dp)) {
items(count = inputPassword.length, key = { it }) {
KeyboardDigitIndicator(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.animateItemPlacement()
)
}
}
}

Spacer(modifier = Modifier.height(48.dp))
Expand Down Expand Up @@ -251,10 +246,34 @@ fun NextcloudPasswordsAppLock(
)

if (isPreview || inputPassword.isNotBlank()) {
val interactionSource = remember { MutableInteractionSource() }

val viewConfiguration = LocalViewConfiguration.current

LaunchedEffect(interactionSource, inputPassword) {
var isLongClick = false

interactionSource.interactions.collectLatest { interaction ->
when (interaction) {
is PressInteraction.Press -> {
isLongClick = false
delay(viewConfiguration.longPressTimeoutMillis)
isLongClick = true
setInputPassword("")
}

is PressInteraction.Release -> {
if (isLongClick.not()) {
setInputPassword(inputPassword.dropLast(1))
}
}
}
}
}

FilledTonalIconButton(
onClick = {
setInputPassword(inputPassword.dropLast(1))
},
onClick = {},
interactionSource = interactionSource,
modifier = Modifier
.padding(AppLockDefaults.DIGIT_PADDING)
.height(AppLockDefaults.DIGIT_SIZE)
Expand Down Expand Up @@ -301,27 +320,25 @@ fun KeyboardNumber(

@Composable
fun KeyboardDigitIndicator(
enabled: Boolean,
isError: Boolean
color: Color,
modifier: Modifier = Modifier
) {
var visible by remember { mutableStateOf(false) }

LaunchedEffect(key1 = Unit) {
visible = true
}

val scale by animateFloatAsState(targetValue = if (visible) 1f else 0f, label = "opacity")

Spacer(
modifier = Modifier
modifier = modifier
.padding(horizontal = 8.dp)
.height(10.dp)
.width(10.dp)
.scale(scale)
.clip(CircleShape)
.background(
if (!isError) {
MaterialTheme.colorScheme.primary.copy(
alpha = animateFloatAsState(
targetValue = if (enabled) 1f else 0.4f,
label = "alpha"
).value
)
} else {
MaterialTheme.colorScheme.error.copy(alpha = 0.7f)
}
)
.background(color)
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.hegocre.nextcloudpasswords.ui.viewmodels

import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.ColorStateList
import android.os.Build
import android.os.PowerManager
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
Expand Down Expand Up @@ -110,6 +116,33 @@ class PasswordsViewModel(application: Application) : AndroidViewModel(applicatio
private set

init {
val screenLockFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
}
val screenOffReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (context != null && intent != null) {
val action = intent.action
val powerManage =
context.getSystemService(Context.POWER_SERVICE) as PowerManager
if (screenLockFilter.matchAction(action) && !powerManage.isInteractive) {
viewModelScope.launch {
_isLocked.emit(true)
}
}
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
application.registerReceiver(
screenOffReceiver,
screenLockFilter,
Context.RECEIVER_EXPORTED
)
} else {
application.registerReceiver(screenOffReceiver, screenLockFilter)
}

if (!sessionOpen.value)
openSession(masterPassword.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ class PreferencesManager private constructor(context: Context) {
private var instance: PreferencesManager? = null

fun getInstance(context: Context): PreferencesManager {
if (instance == null) instance = PreferencesManager(context)
return instance as PreferencesManager
synchronized(this) {
if (instance == null) instance = PreferencesManager(context)
return instance as PreferencesManager
}
}

private object PreferenceKeys {
Expand Down

0 comments on commit 5884f16

Please sign in to comment.