changeset 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 6607f675a5f7
children 0dbd924cb5e8
files app/build.gradle app/src/main/AndroidManifest.xml app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/res/values/strings.xml build.gradle
diffstat 5 files changed, 56 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/app/build.gradle	Thu Mar 11 22:41:48 2021 -0800
+++ b/app/build.gradle	Fri Mar 19 12:49:13 2021 -0700
@@ -11,9 +11,8 @@
         applicationId "com.bartsent.simpleresizer"
         minSdkVersion 23
         targetSdkVersion 30
-        versionCode 2
-        versionName "1.01"
-
+        versionCode 4
+        versionName "1.03"
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }
 
@@ -43,7 +42,7 @@
     implementation 'androidx.appcompat:appcompat:1.2.0'
     implementation 'com.google.android.material:material:1.3.0'
     implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
-    implementation 'androidx.preference:preference:1.1.1'
+    implementation 'androidx.preference:preference-ktx:1.1.1'
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
--- a/app/src/main/AndroidManifest.xml	Thu Mar 11 22:41:48 2021 -0800
+++ b/app/src/main/AndroidManifest.xml	Fri Mar 19 12:49:13 2021 -0700
@@ -2,6 +2,9 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.bartsent.simpleresizer">
 
+    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
+        android:maxSdkVersion="28" />
+
     <application
         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
@@ -11,7 +14,7 @@
         android:theme="@style/Theme.SimpleResizer">
 
         <activity android:name=".SettingsActivity" android:label="@string/title_activity_settings">
-            <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".EditImage"/>
+            <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".EditImage" />
             <intent-filter>
                 <action android:name="android.intent.action.APPLICATION_PREFERENCES" />
                 <category android:name="android.intent.category.PREFERENCE" />
--- 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)
+                }
             }
         }
     }
--- a/app/src/main/res/values/strings.xml	Thu Mar 11 22:41:48 2021 -0800
+++ b/app/src/main/res/values/strings.xml	Fri Mar 19 12:49:13 2021 -0700
@@ -11,6 +11,9 @@
     <string name="error_io">I/O error.</string>
     <string name="error_no_uri">No URI supplied, and no remembered image!</string>
     <string name="error_save_bitmap">Failed to save bitmap.</string>
+    <string name="error_security">Security error.</string>
+    <string name="error_unable_no_permissions">Unable to save image without the necessary permissions.</string>
+    <string name="error_unexpected">Unexpected %s.</string>
     <string name="image_size_text">%d ✕ %d</string>
     <string name="ok_text">OK</string>
     <string name="r_180">180˚</string>
--- a/build.gradle	Thu Mar 11 22:41:48 2021 -0800
+++ b/build.gradle	Fri Mar 19 12:49:13 2021 -0700
@@ -6,7 +6,7 @@
         jcenter()
     }
     dependencies {
-        classpath "com.android.tools.build:gradle:4.1.2"
+        classpath 'com.android.tools.build:gradle:4.1.3'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
         // NOTE: Do not place your application dependencies here; they belong