mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Add ability to scan linked device qr code from gallery.
This commit is contained in:
@@ -34,6 +34,7 @@ import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.BiometricDeviceAuthentication
|
||||
import org.thoughtcrime.securesms.BiometricDeviceLockContract
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.components.settings.app.usernamelinks.main.QrImageSelectionActivity
|
||||
import org.thoughtcrime.securesms.compose.ComposeFragment
|
||||
import org.thoughtcrime.securesms.permissions.Permissions
|
||||
import org.thoughtcrime.securesms.util.navigation.safeNavigate
|
||||
@@ -50,6 +51,7 @@ class AddLinkDeviceFragment : ComposeFragment() {
|
||||
private val viewModel: LinkDeviceViewModel by activityViewModels()
|
||||
private lateinit var biometricAuth: BiometricDeviceAuthentication
|
||||
private lateinit var biometricDeviceLockLauncher: ActivityResultLauncher<String>
|
||||
private lateinit var galleryLauncher: ActivityResultLauncher<Unit>
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
@@ -72,6 +74,12 @@ class AddLinkDeviceFragment : ComposeFragment() {
|
||||
BiometricPrompt(requireActivity(), BiometricAuthenticationListener()),
|
||||
promptInfo
|
||||
)
|
||||
|
||||
galleryLauncher = registerForActivityResult(QrImageSelectionActivity.Contract()) { uri ->
|
||||
if (uri != null) {
|
||||
viewModel.scanImage(requireContext(), uri)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@@ -113,7 +121,8 @@ class AddLinkDeviceFragment : ComposeFragment() {
|
||||
viewModel.onLinkDeviceResult(true)
|
||||
navController.popBackStack()
|
||||
},
|
||||
onLinkDeviceFailure = { viewModel.onLinkDeviceResult(false) }
|
||||
onLinkDeviceFailure = { viewModel.onLinkDeviceResult(false) },
|
||||
onGalleryOpened = { galleryLauncher.launch(Unit) }
|
||||
)
|
||||
}
|
||||
|
||||
@@ -161,7 +170,8 @@ private fun MainScreen(
|
||||
onQrCodeDismissed: () -> Unit = {},
|
||||
onQrCodeRetry: () -> Unit = {},
|
||||
onLinkDeviceSuccess: () -> Unit = {},
|
||||
onLinkDeviceFailure: () -> Unit = {}
|
||||
onLinkDeviceFailure: () -> Unit = {},
|
||||
onGalleryOpened: () -> Unit = {}
|
||||
) {
|
||||
Scaffolds.Settings(
|
||||
title = "",
|
||||
@@ -188,6 +198,7 @@ private fun MainScreen(
|
||||
linkDeviceResult = state.linkDeviceResult,
|
||||
onLinkDeviceSuccess = onLinkDeviceSuccess,
|
||||
onLinkDeviceFailure = onLinkDeviceFailure,
|
||||
onGalleryOpened = onGalleryOpened,
|
||||
modifier = Modifier.padding(contentPadding)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ fun LinkDeviceQrScanScreen(
|
||||
linkDeviceResult: LinkDeviceRepository.LinkDeviceResult,
|
||||
onLinkDeviceSuccess: () -> Unit,
|
||||
onLinkDeviceFailure: () -> Unit,
|
||||
onGalleryOpened: () -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val lifecycleOwner = LocalLifecycleOwner.current
|
||||
@@ -106,7 +107,8 @@ fun LinkDeviceQrScanScreen(
|
||||
},
|
||||
hasPermission = hasPermission,
|
||||
onRequestPermissions = onRequestPermissions,
|
||||
qrString = stringResource(R.string.AddLinkDeviceFragment__scan_the_qr_code)
|
||||
qrString = stringResource(R.string.AddLinkDeviceFragment__scan_the_qr_code),
|
||||
onGalleryOpened = onGalleryOpened
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,11 +4,15 @@ import android.content.Context
|
||||
import android.net.Uri
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.load.DecodeFormat
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import org.signal.core.util.toOptional
|
||||
import org.signal.qr.QrProcessor
|
||||
import org.thoughtcrime.securesms.R
|
||||
import org.thoughtcrime.securesms.jobs.LinkedDeviceInactiveCheckJob
|
||||
|
||||
@@ -168,4 +172,24 @@ class LinkDeviceViewModel : ViewModel() {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun scanImage(context: Context, uri: Uri) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val loadBitmap = Glide.with(context)
|
||||
.asBitmap()
|
||||
.format(DecodeFormat.PREFER_ARGB_8888)
|
||||
.load(uri)
|
||||
.submit()
|
||||
|
||||
val result = QrProcessor().getScannedData(loadBitmap.get()).toOptional()
|
||||
if (result.isPresent) {
|
||||
onQrCodeScanned(result.get())
|
||||
} else {
|
||||
_state.value = _state.value.copy(
|
||||
qrCodeInvalid = true,
|
||||
showFrontCamera = null
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user