comparison app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 37:0dbd924cb5e8

Explain why permission needed, per guidelines.
author David Barts <n5jrn@me.com>
date Tue, 23 Mar 2021 22:30:34 -0700
parents 70f1d11d53ad
children cfb19d4ccf78
comparison
equal deleted inserted replaced
36:70f1d11d53ad 37:0dbd924cb5e8
323 } else { 323 } else {
324 showError(getString(R.string.error_unable_no_permissions)) 324 showError(getString(R.string.error_unable_no_permissions))
325 } 325 }
326 } 326 }
327 327
328 fun requestWritePermission(): Unit {
329 requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL)
330 }
331
328 fun doneClicked(view: View?): Unit { 332 fun doneClicked(view: View?): Unit {
329 // If we need a permission, request it and bail. We will be called again 333 // If we need WRITE_EXTERNAL_STORAGE, request it and bail. We will be called again
330 // (with the permission) if it is granted. 334 // (with the permission) if it is granted.
331 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) { 335 if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
332 if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { 336 if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
333 requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL) 337 if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE))
338 AlertDialog.Builder(this).also {
339 it.setMessage(getString(R.string.permission_needed, getString(R.string.app_name)))
340 it.setNeutralButton(R.string.ok_text) { dialog, _ ->
341 dialog.dismiss()
342 requestWritePermission()
343 }
344 it.create()
345 }.show()
346 else
347 requestWritePermission()
334 return 348 return
335 } 349 }
336 } 350 }
337 351
338 // If we get here, we have permission to save (if we need it). 352 // If we get here, we have permission to save (if we need it).