Fix permission request - single permission on Android 12 and below
This commit is contained in:
parent
00c5db0253
commit
1558ead809
1 changed files with 13 additions and 30 deletions
|
|
@ -22,7 +22,7 @@ import com.duckai.app.R
|
|||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var webView: WebView
|
||||
private var filePathCallback: ValueCallback<Array<Uri>>? = null
|
||||
private var pendingFileCallback: ValueCallback<Array<Uri>>? = null
|
||||
|
||||
companion object {
|
||||
private const val BASE_URL = "https://duck.ai"
|
||||
|
|
@ -31,23 +31,18 @@ class MainActivity : AppCompatActivity() {
|
|||
private val filePickerLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.OpenMultipleDocuments()
|
||||
) { uris ->
|
||||
if (uris.isNotEmpty()) {
|
||||
filePathCallback?.onReceiveValue(uris.toTypedArray())
|
||||
Toast.makeText(this, getString(R.string.file_attached), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
filePathCallback?.onReceiveValue(null)
|
||||
}
|
||||
filePathCallback = null
|
||||
pendingFileCallback?.onReceiveValue(uris.toTypedArray())
|
||||
pendingFileCallback = null
|
||||
}
|
||||
|
||||
private val permissionLauncher = registerForActivityResult(
|
||||
ActivityResultContracts.RequestMultiplePermissions()
|
||||
) { permissions ->
|
||||
val allGranted = permissions.values.all { it }
|
||||
if (allGranted) {
|
||||
ActivityResultContracts.RequestPermission()
|
||||
) { granted ->
|
||||
if (granted) {
|
||||
openFilePicker()
|
||||
} else {
|
||||
Toast.makeText(this, getString(R.string.permission_required), Toast.LENGTH_SHORT).show()
|
||||
pendingFileCallback?.onReceiveValue(null)
|
||||
pendingFileCallback = null
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -83,8 +78,8 @@ class MainActivity : AppCompatActivity() {
|
|||
filePathCallback: ValueCallback<Array<Uri>>?,
|
||||
fileChooserParams: FileChooserParams?
|
||||
): Boolean {
|
||||
this@MainActivity.filePathCallback = filePathCallback
|
||||
checkPermissionAndPickFile()
|
||||
pendingFileCallback = filePathCallback
|
||||
checkPermissionAndOpenPicker()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
@ -103,27 +98,15 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun checkPermissionAndPickFile() {
|
||||
private fun checkPermissionAndOpenPicker() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
val permissions = arrayOf(
|
||||
Manifest.permission.READ_MEDIA_IMAGES,
|
||||
Manifest.permission.READ_MEDIA_VIDEO,
|
||||
Manifest.permission.READ_MEDIA_AUDIO
|
||||
)
|
||||
val notGranted = permissions.filter {
|
||||
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
|
||||
}
|
||||
if (notGranted.isEmpty()) {
|
||||
openFilePicker()
|
||||
} else {
|
||||
permissionLauncher.launch(notGranted.toTypedArray())
|
||||
}
|
||||
} else {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
openFilePicker()
|
||||
} else {
|
||||
permissionLauncher.launch(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE))
|
||||
permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue