Skip to content

Commit

Permalink
fix: permission for android 34 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
rizrmdhn authored Jun 19, 2024
2 parents d787057 + 10625d3 commit a5d2983
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.singa.asl.ui.screen.history_camera

import android.Manifest
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.READ_MEDIA_IMAGES
import android.Manifest.permission.READ_MEDIA_VIDEO
import android.Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
Expand Down Expand Up @@ -53,6 +59,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.LifecycleOwner
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.google.accompanist.permissions.rememberPermissionState
import com.singa.asl.R
import com.singa.asl.ui.screen.history.HistoryScreenViewModel
Expand Down Expand Up @@ -116,7 +123,6 @@ fun HistoryCameraContent(
}

val recordPermissionState = rememberPermissionState(Manifest.permission.RECORD_AUDIO)
val galleryPermissionState = rememberPermissionState(Manifest.permission.READ_EXTERNAL_STORAGE)

var recording by remember {
mutableStateOf<Recording?>(null)
Expand Down Expand Up @@ -236,11 +242,16 @@ fun HistoryCameraContent(
}
}


val galleryPermissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
val testGalleryLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) {
if (it) {
if (it[READ_EXTERNAL_STORAGE] == true) {
Toast.makeText(context, "Permission granted", Toast.LENGTH_SHORT).show()
galleryLauncher.launch("video/*")
} else if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
it[READ_MEDIA_VIDEO] == true && it[READ_MEDIA_VISUAL_USER_SELECTED] == true
) {
Toast.makeText(context, "Permission granted", Toast.LENGTH_SHORT).show()
galleryLauncher.launch("video/*")
} else {
Expand All @@ -250,7 +261,6 @@ fun HistoryCameraContent(
}



LaunchedEffect(isFrontCamera) {
if (isFrontCamera) {
cameraController.bindToLifecycle(lifecycleOwner)
Expand Down Expand Up @@ -386,23 +396,55 @@ fun HistoryCameraContent(
modifier = Modifier
.padding(10.dp),
onClick = {
when {
galleryPermissionState.hasPermission -> {
Toast.makeText(context, "Permission granted", Toast.LENGTH_SHORT)
.show()
galleryLauncher.launch("video/*")
}
galleryPermissionState.shouldShowRationale -> {
Toast.makeText(
context,
"Permission denied",
Toast.LENGTH_SHORT
).show()
}
else -> {
galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
testGalleryLauncher.launch(
arrayOf(
READ_MEDIA_VIDEO,
READ_MEDIA_VISUAL_USER_SELECTED
)
)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
testGalleryLauncher.launch(
arrayOf(
READ_MEDIA_IMAGES,
READ_MEDIA_VIDEO
)
)
} else {
testGalleryLauncher.launch(arrayOf(READ_EXTERNAL_STORAGE))
}

// when {
// galleryPermissionState.hasPermission -> {
// Toast.makeText(
// context,
// "Permission granted",
// Toast.LENGTH_SHORT
// )
// .show()
// galleryLauncher.launch("video/*")
// }
//
// galleryPermissionState.shouldShowRationale -> {
// Toast.makeText(
// context,
// "Permission denied",
// Toast.LENGTH_SHORT
// ).show()
// }
//
// else -> {
// Log.d(
// "HistoryCameraContent",
// "HistoryCameraContent: request permission"
// )
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
// galleryPermissionLauncher.launch(Manifest.permission.READ_MEDIA_VISUAL_USER_SELECTED)
// } else {
// galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
// }
// }
// }
},
colors = IconButtonDefaults.iconButtonColors(
contentColor = Color.White
Expand All @@ -425,13 +467,15 @@ fun HistoryCameraContent(
isRecording = true
recordVideo()
}

recordPermissionState.shouldShowRationale -> {
Toast.makeText(
context,
"Permission denied",
Toast.LENGTH_SHORT
).show()
}

else -> {
recordAudioPermissionLauncher.launch(Manifest.permission.RECORD_AUDIO)
}
Expand Down

0 comments on commit a5d2983

Please sign in to comment.