Last active
October 18, 2023 13:43
-
-
Save chiragthummar/008ea43e53932bb67d2d1d910f82e2f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.Manifest | |
| import android.content.Intent | |
| import android.net.Uri | |
| import android.os.Build | |
| import android.provider.Settings | |
| import android.widget.Toast | |
| import androidx.annotation.RequiresApi | |
| import androidx.compose.foundation.layout.Box | |
| import androidx.compose.foundation.layout.Column | |
| import androidx.compose.foundation.layout.fillMaxSize | |
| import androidx.compose.foundation.layout.padding | |
| import androidx.compose.material3.AlertDialog | |
| import androidx.compose.material3.Button | |
| import androidx.compose.material3.CenterAlignedTopAppBar | |
| import androidx.compose.material3.ExperimentalMaterial3Api | |
| import androidx.compose.material3.MaterialTheme | |
| import androidx.compose.material3.Scaffold | |
| import androidx.compose.material3.Text | |
| import androidx.compose.material3.TextButton | |
| import androidx.compose.material3.TopAppBar | |
| import androidx.compose.material3.TopAppBarDefaults | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.mutableStateOf | |
| import androidx.compose.runtime.remember | |
| import androidx.compose.ui.Alignment | |
| import androidx.compose.ui.Modifier | |
| import androidx.compose.ui.graphics.Color | |
| import androidx.compose.ui.platform.LocalContext | |
| import androidx.compose.ui.text.TextStyle | |
| import androidx.compose.ui.text.font.FontWeight | |
| import androidx.compose.ui.unit.dp | |
| import androidx.compose.ui.unit.sp | |
| import androidx.core.content.ContextCompat.startActivity | |
| import com.google.accompanist.permissions.ExperimentalPermissionsApi | |
| import com.google.accompanist.permissions.rememberMultiplePermissionsState | |
| @RequiresApi(Build.VERSION_CODES.TIRAMISU) | |
| @OptIn(ExperimentalPermissionsApi::class, ExperimentalMaterial3Api::class) | |
| @Composable | |
| fun MultiplePermissionDemo() { | |
| val multiplePermission = rememberMultiplePermissionsState( | |
| permissions = listOf( | |
| Manifest.permission.CAMERA, | |
| Manifest.permission.RECORD_AUDIO | |
| ) | |
| ) | |
| val context = LocalContext.current | |
| val showRationalDialog = remember { mutableStateOf(false) } | |
| if (showRationalDialog.value) { | |
| AlertDialog( | |
| onDismissRequest = { | |
| showRationalDialog.value = false | |
| }, | |
| title = { | |
| Text( | |
| text = "Permission", | |
| fontWeight = FontWeight.Bold, | |
| fontSize = 16.sp | |
| ) | |
| }, | |
| text = { | |
| Text( | |
| if (multiplePermission.revokedPermissions.size == 2) { | |
| "We need camera and audio permission to shoot video" | |
| } else if (multiplePermission.revokedPermissions.first().permission == Manifest.permission.CAMERA) { | |
| "We need camera permission. Please grant the permission." | |
| } else { | |
| "We need audio permission. Please grant the permission." | |
| }, | |
| fontSize = 16.sp | |
| ) | |
| }, | |
| confirmButton = { | |
| TextButton( | |
| onClick = { | |
| showRationalDialog.value = false | |
| val intent = Intent( | |
| Settings.ACTION_APPLICATION_DETAILS_SETTINGS, | |
| Uri.fromParts("package", context.packageName, null) | |
| ) | |
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
| startActivity(context, intent, null) | |
| }) { | |
| Text("OK", style = TextStyle(color = Color.Black)) | |
| } | |
| }, | |
| dismissButton = { | |
| TextButton( | |
| onClick = { | |
| showRationalDialog.value = false | |
| }) { | |
| Text("Cancel", style = TextStyle(color = Color.Black)) | |
| } | |
| }, | |
| ) | |
| } | |
| Scaffold(topBar = { | |
| CenterAlignedTopAppBar( | |
| title = { Text(text = "Request Multiple Permission", color = Color.White) }, | |
| colors = TopAppBarDefaults.centerAlignedTopAppBarColors( | |
| containerColor = MaterialTheme.colorScheme.primary | |
| ) | |
| ) | |
| }) { | |
| Box( | |
| modifier = Modifier | |
| .fillMaxSize() | |
| .padding(it), | |
| contentAlignment = Alignment.Center | |
| ) { | |
| Column(horizontalAlignment = Alignment.CenterHorizontally) { | |
| Button(onClick = { | |
| if (!multiplePermission.allPermissionsGranted) { | |
| if (multiplePermission.shouldShowRationale) { | |
| // Show a rationale if needed (optional) | |
| showRationalDialog.value = true | |
| } else { | |
| // Request the permission | |
| multiplePermission.launchMultiplePermissionRequest() | |
| } | |
| } else { | |
| Toast.makeText( | |
| context, | |
| "We have camera and audio permission", | |
| Toast.LENGTH_SHORT | |
| ).show() | |
| } | |
| }) { | |
| Text(text = "Ask for permission") | |
| } | |
| Text( | |
| modifier = Modifier.padding(horizontal = 20.dp, vertical = 5.dp), | |
| text = if (multiplePermission.allPermissionsGranted) { | |
| "All Permission Granted" | |
| } else if (multiplePermission.shouldShowRationale) { | |
| // If the user has denied the permission but the rationale can be shown, | |
| // then gently explain why the app requires this permission | |
| if (multiplePermission.revokedPermissions.size == 2) { | |
| "We need camera and audio permission to shoot video" | |
| } else if (multiplePermission.revokedPermissions.first().permission == Manifest.permission.CAMERA) { | |
| "We need camera permission. Please grant the permission." | |
| } else { | |
| "We need audio permission. Please grant the permission." | |
| } | |
| } else { | |
| // If it's the first time the user lands on this feature, or the user | |
| // doesn't want to be asked again for this permission, explain that the | |
| // permission is required | |
| "We need camera and audio permission to shoot video" | |
| }, | |
| fontWeight = FontWeight.Bold, | |
| fontSize = 16.sp | |
| ) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment