diff app/src/main/java/com/bartsent/simpleresizer/EditImage.kt @ 36:70f1d11d53ad

Attempt to make it work on Android 9 and earlier.
author David Barts <n5jrn@me.com>
date Fri, 19 Mar 2021 12:49:13 -0700
parents bead5d7e8c69
children 0dbd924cb5e8
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Thu Mar 11 22:41:48 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Fri Mar 19 12:49:13 2021 -0700
@@ -1,7 +1,9 @@
 package com.bartsent.simpleresizer
 
+import android.Manifest
 import android.content.ContentValues
 import android.content.Intent
+import android.content.pm.PackageManager
 import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.graphics.Canvas
@@ -98,6 +100,16 @@
         }.show()
     }
 
+    fun showError(message: String): Unit {
+        AlertDialog.Builder(this).also {
+            it.setMessage(message)
+            it.setNeutralButton(R.string.ok_text) { dialog, _ ->
+                dialog.dismiss()
+            }
+            it.create()
+        }.show()
+    }
+
     override fun onResume() {
         super.onResume()
 
@@ -299,7 +311,31 @@
         finish()
     }
 
-    fun doneClicked(view: View): Unit {
+    private val REQUEST_WRITE_EXTERNAL = 42
+
+    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray): Unit {
+        if (requestCode != REQUEST_WRITE_EXTERNAL) {
+            Log.e("EditImage", "unexpected request code in onRequestPermissionsResult!")
+            return
+        }
+        if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
+            doneClicked(null)
+        } else {
+            showError(getString(R.string.error_unable_no_permissions))
+        }
+    }
+
+    fun doneClicked(view: View?): Unit {
+        // If we need a permission, request it and bail. We will be called again
+        // (with the permission) if it is granted.
+        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) {
+            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
+                requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_WRITE_EXTERNAL)
+                return
+            }
+        }
+
+        // If we get here, we have permission to save (if we need it).
         val image = viewModel.bitmap!!
         binding.progressBar.visibility = ProgressBar.VISIBLE
         ThreadPools.WORKERS.execute {
@@ -341,14 +377,20 @@
                 }
             } catch (ioe: IOException) {
                 errorMessage = ioe.message ?: getString(R.string.error_io)
+            } catch (se: SecurityException) {
+                errorMessage = se.message ?: getString(R.string.error_security)
+            } catch (e: Exception) {
+                Log.e("EditImage", "unexpected exception when saving!", e)
+                errorMessage = e.message ?: getString(R.string.error_unexpected, e::class.qualifiedName)
             }
             runOnUiThread {
                 binding.progressBar.visibility = ProgressBar.INVISIBLE
                 if (errorMessage == null) {
                     unsetImage()
                     finish()
-                } else
-                    Toast.makeText(applicationContext, errorMessage, Toast.LENGTH_LONG).show()
+                } else {
+                    showError(errorMessage)
+                }
             }
         }
     }