diff --git a/src/main/kotlin/com/nkming/powermenu/ScreenshotHandler.kt b/src/main/kotlin/com/nkming/powermenu/ScreenshotHandler.kt index 206ed02..35e63ea 100644 --- a/src/main/kotlin/com/nkming/powermenu/ScreenshotHandler.kt +++ b/src/main/kotlin/com/nkming/powermenu/ScreenshotHandler.kt @@ -11,6 +11,7 @@ import android.os.Build import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationManagerCompat import android.view.Surface +import android.widget.Toast import com.nkming.utils.graphic.BitmapLoader import com.nkming.utils.graphic.BitmapUtils import com.nkming.utils.graphic.DrawableUtils @@ -21,6 +22,11 @@ import java.io.File class ScreenshotHandler(context: Context) { + companion object + { + private val LOG_TAG = ScreenshotHandler::class.java.canonicalName + } + fun onScreenshotSuccess(filepath: String, rotation: Int) { val l = @@ -67,10 +73,27 @@ class ScreenshotHandler(context: Context) object: AsyncTask() { override fun doInBackground(vararg params: Unit) + { + try + { + _doInBackground() + } + catch (e: Exception) + { + Log.e("$LOG_TAG._fixScreenshotOrientation", "Failed", e) + _onFailureFallback() + } + } + + override fun onPostExecute(result: Unit) + { + l() + } + + private fun _doInBackground() { val uri = Uri.fromFile(File(filepath)) - val bmp = BitmapLoader(_context) - .loadUri(uri) + val bmp = BitmapLoader(_context).loadUri(uri) val mat = Matrix() mat.postRotate(_getRotationValue(rotation)) val rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.width, @@ -79,11 +102,6 @@ class ScreenshotHandler(context: Context) Bitmap.CompressFormat.PNG, 100) } - override fun onPostExecute(result: Unit) - { - l() - } - private fun _getRotationValue(rotation: Int): Float { return when (rotation) @@ -103,11 +121,26 @@ class ScreenshotHandler(context: Context) object: AsyncTask() { override fun doInBackground(vararg params: Unit) + { + try + { + _doInBackground() + } + catch (e: Exception) + { + Log.e("$LOG_TAG._notifyScreenshot", "Failed", e) + _onFailureFallback() + } + } + + private fun _doInBackground() { val uri = Uri.fromFile(File(filepath)) val thumbnail = _getThumbnail(uri) val dp512 = DimensionUtils.dpToPx(_context, 512f) + // Somehow the bitmap couldn't be load on some device, not sure + // why... val bmp = BitmapLoader(_context) .setTargetSize(Size(dp512.toInt(), (dp512 / 2f).toInt())) .setSizeCalc(FillSizeCalc()) @@ -244,5 +277,11 @@ class ScreenshotHandler(context: Context) }.execute() } + private fun _onFailureFallback() + { + Toast.makeText(_context, R.string.screenshot_succeed_fallback, + Toast.LENGTH_LONG).show() + } + private val _context = context } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index e2df587..1a94fdf 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -9,6 +9,7 @@ Failed taking screenshot, try disabling "mount namespace separation" in SuperSU Failed starting activity, please report Failed running su command, not rooted? + Screenshot captured Reboot Reboot (hold to soft reboot)