Add wake lock when linking devices.
This commit is contained in:
parent
23f90e070e
commit
84e22789c8
4 changed files with 87 additions and 1 deletions
|
@ -28,6 +28,7 @@ import org.signal.core.ui.SignalPreview
|
|||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.compose.ComposeFragment
|
||||
import org.thoughtcrime.securesms.permissions.Permissions
|
||||
import org.thoughtcrime.securesms.util.VibrateUtil
|
||||
import org.thoughtcrime.securesms.util.navigation.safeNavigate
|
||||
|
||||
/**
|
||||
|
@ -35,6 +36,10 @@ import org.thoughtcrime.securesms.util.navigation.safeNavigate
|
|||
*/
|
||||
class AddLinkDeviceFragment : ComposeFragment() {
|
||||
|
||||
companion object {
|
||||
private const val VIBRATE_DURATION_MS = 50
|
||||
}
|
||||
|
||||
private val viewModel: LinkDeviceViewModel by activityViewModels()
|
||||
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
|
@ -59,7 +64,12 @@ class AddLinkDeviceFragment : ComposeFragment() {
|
|||
hasPermissions = cameraPermissionState.status.isGranted,
|
||||
onRequestPermissions = { askPermissions() },
|
||||
onShowFrontCamera = { viewModel.showFrontCamera() },
|
||||
onQrCodeScanned = { data -> viewModel.onQrCodeScanned(data) },
|
||||
onQrCodeScanned = { data ->
|
||||
if (VibrateUtil.isHapticFeedbackEnabled(requireContext())) {
|
||||
VibrateUtil.vibrate(requireContext(), VIBRATE_DURATION_MS)
|
||||
}
|
||||
viewModel.onQrCodeScanned(data)
|
||||
},
|
||||
onQrCodeApproved = {
|
||||
navController.popBackStack()
|
||||
viewModel.addDevice(shouldSync = false)
|
||||
|
|
|
@ -88,6 +88,7 @@ class LinkDeviceFragment : ComposeFragment() {
|
|||
private val viewModel: LinkDeviceViewModel by activityViewModels()
|
||||
private lateinit var biometricAuth: BiometricDeviceAuthentication
|
||||
private lateinit var biometricDeviceLockLauncher: ActivityResultLauncher<String>
|
||||
private lateinit var linkDeviceWakeLock: LinkDeviceWakeLock
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
@ -110,6 +111,8 @@ class LinkDeviceFragment : ComposeFragment() {
|
|||
BiometricPrompt(requireActivity(), BiometricAuthenticationListener()),
|
||||
promptInfo
|
||||
)
|
||||
|
||||
linkDeviceWakeLock = LinkDeviceWakeLock(requireActivity())
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
|
@ -123,6 +126,20 @@ class LinkDeviceFragment : ComposeFragment() {
|
|||
val navController: NavController by remember { mutableStateOf(findNavController()) }
|
||||
val context = LocalContext.current
|
||||
|
||||
LaunchedEffect(state.dialogState) {
|
||||
when (state.dialogState) {
|
||||
DialogState.None, is DialogState.SyncingFailed, DialogState.SyncingTimedOut -> {
|
||||
Log.i(TAG, "Releasing wake lock for linked device")
|
||||
linkDeviceWakeLock.release()
|
||||
}
|
||||
DialogState.SyncingMessages, DialogState.Linking -> {
|
||||
Log.i(TAG, "Acquiring wake lock for linked device")
|
||||
linkDeviceWakeLock.acquire()
|
||||
}
|
||||
DialogState.Unlinking -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(state.oneTimeEvent) {
|
||||
when (val event = state.oneTimeEvent) {
|
||||
LinkDeviceSettingsState.OneTimeEvent.None -> {
|
||||
|
|
|
@ -222,6 +222,11 @@ class LinkDeviceViewModel : ViewModel() {
|
|||
|
||||
if (result !is LinkDeviceResult.Success) {
|
||||
Log.w(TAG, "[addDeviceWithSync] Unable to link device $result")
|
||||
_state.update {
|
||||
it.copy(
|
||||
dialogState = DialogState.None
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -287,6 +292,11 @@ class LinkDeviceViewModel : ViewModel() {
|
|||
|
||||
if (result !is LinkDeviceResult.Success) {
|
||||
Log.w(TAG, "Unable to link device $result")
|
||||
_state.update {
|
||||
it.copy(
|
||||
dialogState = DialogState.None
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package org.thoughtcrime.securesms.linkdevice
|
||||
|
||||
import android.os.PowerManager
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.lifecycle.DefaultLifecycleObserver
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import org.thoughtcrime.securesms.util.WakeLockUtil
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
/**
|
||||
* Holds on to and manages a wake-lock when linking a device.
|
||||
*/
|
||||
class LinkDeviceWakeLock(
|
||||
private val activity: ComponentActivity
|
||||
) : DefaultLifecycleObserver {
|
||||
|
||||
companion object {
|
||||
private val TIMEOUT = 10.minutes.inWholeMilliseconds
|
||||
}
|
||||
|
||||
private var wakeLock: PowerManager.WakeLock? = null
|
||||
|
||||
init {
|
||||
activity.lifecycle.addObserver(this)
|
||||
}
|
||||
|
||||
fun acquire() {
|
||||
synchronized(this) {
|
||||
if (wakeLock?.isHeld == true) {
|
||||
return
|
||||
}
|
||||
|
||||
wakeLock = WakeLockUtil.acquire(activity, PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, TIMEOUT, "linkDevice")
|
||||
}
|
||||
}
|
||||
|
||||
fun release() {
|
||||
synchronized(this) {
|
||||
if (wakeLock?.isHeld == true) {
|
||||
wakeLock?.release()
|
||||
wakeLock = null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause(owner: LifecycleOwner) {
|
||||
release()
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue