# HG changeset patch # User David Barts # Date 1616183353 25200 # Node ID 70f1d11d53ad2fd9ecb0d76c450804a54e72a6d0 # Parent 6607f675a5f71a54ca86813415f43b824540ac66 Attempt to make it work on Android 9 and earlier. diff -r 6607f675a5f7 -r 70f1d11d53ad app/build.gradle --- 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' diff -r 6607f675a5f7 -r 70f1d11d53ad app/src/main/AndroidManifest.xml --- 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 @@ + + - + diff -r 6607f675a5f7 -r 70f1d11d53ad app/src/main/java/com/bartsent/simpleresizer/EditImage.kt --- 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, 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) + } } } } diff -r 6607f675a5f7 -r 70f1d11d53ad app/src/main/res/values/strings.xml --- 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 @@ I/O error. No URI supplied, and no remembered image! Failed to save bitmap. + Security error. + Unable to save image without the necessary permissions. + Unexpected %s. %d ✕ %d OK 180˚ diff -r 6607f675a5f7 -r 70f1d11d53ad build.gradle --- 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