Cleaning up permissions

Accompanist doesn't handle "ask every time" properly so we need to use our old xml methods
This commit is contained in:
ThomasSession 2024-09-09 16:17:37 +10:00
parent a6efb43f3e
commit fe7d3ef7fe
3 changed files with 34 additions and 21 deletions

View File

@ -25,6 +25,7 @@ import org.session.libsignal.utilities.PublicKeyValidation
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2
import org.thoughtcrime.securesms.database.threadDatabase import org.thoughtcrime.securesms.database.threadDatabase
import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.ui.theme.LocalDimensions import org.thoughtcrime.securesms.ui.theme.LocalDimensions
import org.thoughtcrime.securesms.ui.theme.LocalColors import org.thoughtcrime.securesms.ui.theme.LocalColors
import org.thoughtcrime.securesms.ui.components.QRScannerScreen import org.thoughtcrime.securesms.ui.components.QRScannerScreen
@ -68,6 +69,11 @@ class QRCodeActivity : PassphraseRequiredActionBarActivity() {
finish() finish()
} }
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Permissions.onRequestPermissionsResult(this, requestCode, permissions, grantResults)
}
} }
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.ui
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.content.ContextWrapper
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -48,3 +49,12 @@ fun ComposeView.setThemedContent(content: @Composable () -> Unit) = setContent {
fun PermissionState.isPermanentlyDenied(): Boolean { fun PermissionState.isPermanentlyDenied(): Boolean {
return !status.shouldShowRationale && !status.isGranted return !status.shouldShowRationale && !status.isGranted
} }
fun Context.findActivity(): Activity {
var context = this
while (context is ContextWrapper) {
if (context is Activity) return context
context = context.baseContext
}
throw IllegalStateException("Permissions should be called in the context of an Activity")
}

View File

@ -2,7 +2,9 @@ package org.thoughtcrime.securesms.ui.components
import android.Manifest import android.Manifest
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.provider.Settings import android.provider.Settings
import androidx.camera.core.CameraSelector import androidx.camera.core.CameraSelector
@ -20,7 +22,6 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold import androidx.compose.material3.Scaffold
import androidx.compose.material3.Snackbar import androidx.compose.material3.Snackbar
@ -46,10 +47,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.viewinterop.AndroidView
import com.google.accompanist.permissions.ExperimentalPermissionsApi import androidx.core.app.ActivityCompat
import com.google.accompanist.permissions.isGranted import androidx.core.content.ContextCompat
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale
import com.google.zxing.BinaryBitmap import com.google.zxing.BinaryBitmap
import com.google.zxing.ChecksumException import com.google.zxing.ChecksumException
import com.google.zxing.FormatException import com.google.zxing.FormatException
@ -59,24 +58,24 @@ import com.google.zxing.Result
import com.google.zxing.common.HybridBinarizer import com.google.zxing.common.HybridBinarizer
import com.google.zxing.qrcode.QRCodeReader import com.google.zxing.qrcode.QRCodeReader
import com.squareup.phrase.Phrase import com.squareup.phrase.Phrase
import java.util.concurrent.Executors
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import network.loki.messenger.R import network.loki.messenger.R
import org.session.libsession.utilities.StringSubstitutionConstants.APP_NAME_KEY import org.session.libsession.utilities.StringSubstitutionConstants.APP_NAME_KEY
import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.mediasend.MediaSendActivity
import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.ui.AlertDialog import org.thoughtcrime.securesms.ui.AlertDialog
import org.thoughtcrime.securesms.ui.DialogButtonModel import org.thoughtcrime.securesms.ui.DialogButtonModel
import org.thoughtcrime.securesms.ui.GetString import org.thoughtcrime.securesms.ui.GetString
import org.thoughtcrime.securesms.ui.findActivity
import org.thoughtcrime.securesms.ui.getSubbedString import org.thoughtcrime.securesms.ui.getSubbedString
import org.thoughtcrime.securesms.ui.isPermanentlyDenied
import org.thoughtcrime.securesms.ui.theme.LocalColors
import org.thoughtcrime.securesms.ui.theme.LocalDimensions import org.thoughtcrime.securesms.ui.theme.LocalDimensions
import org.thoughtcrime.securesms.ui.theme.LocalType import org.thoughtcrime.securesms.ui.theme.LocalType
import java.util.concurrent.Executors
private const val TAG = "NewMessageFragment" private const val TAG = "NewMessageFragment"
@OptIn(ExperimentalPermissionsApi::class)
@Composable @Composable
fun QRScannerScreen( fun QRScannerScreen(
errors: Flow<String>, errors: Flow<String>,
@ -93,11 +92,13 @@ fun QRScannerScreen(
) { ) {
LocalSoftwareKeyboardController.current?.hide() LocalSoftwareKeyboardController.current?.hide()
val cameraPermissionState = rememberPermissionState(Manifest.permission.CAMERA) val context = LocalContext.current
val permission = Manifest.permission.CAMERA
var showCameraPermissionDialog by remember { mutableStateOf(false) } var showCameraPermissionDialog by remember { mutableStateOf(false) }
if (cameraPermissionState.status.isGranted) { if (ContextCompat.checkSelfPermission(context, permission)
== PackageManager.PERMISSION_GRANTED) {
ScanQrCode(errors, onScan) ScanQrCode(errors, onScan)
} else { } else {
Column( Column(
@ -120,14 +121,12 @@ fun QRScannerScreen(
stringResource(R.string.cameraGrantAccess), stringResource(R.string.cameraGrantAccess),
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
onClick = { onClick = {
// if the permission has been denied permanently, ask the user to go to the settings Permissions.with(context.findActivity())
if (cameraPermissionState.isPermanentlyDenied()){ .request(permission)
showCameraPermissionDialog = true .withPermanentDenialDialog(
} context.getSubbedString(R.string.permissionsCameraDenied,
// otherwise ask for permission APP_NAME_KEY to context.getString(R.string.app_name))
else { ).execute()
cameraPermissionState.run { launchPermissionRequest() }
}
} }
) )
Spacer(modifier = Modifier.weight(1f)) Spacer(modifier = Modifier.weight(1f))
@ -136,8 +135,6 @@ fun QRScannerScreen(
// camera permission denied permanently dialog // camera permission denied permanently dialog
if(showCameraPermissionDialog){ if(showCameraPermissionDialog){
val context = LocalContext.current
AlertDialog( AlertDialog(
onDismissRequest = { showCameraPermissionDialog = false }, onDismissRequest = { showCameraPermissionDialog = false },
title = stringResource(R.string.permissionsRequired), title = stringResource(R.string.permissionsRequired),