Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nkming2 committed Jan 17, 2017
1 parent dcceec7 commit f3f8c7d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/main/kotlin/com/nkming/powermenu/ScreenshotHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -67,10 +73,27 @@ class ScreenshotHandler(context: Context)
object: AsyncTask<Unit, Unit, Unit>()
{
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,
Expand All @@ -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)
Expand All @@ -103,11 +121,26 @@ class ScreenshotHandler(context: Context)
object: AsyncTask<Unit, Unit, Unit>()
{
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())
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="screenshot_fail">Failed taking screenshot, try disabling "mount namespace separation" in SuperSU</string>
<string name="start_activity_fail">Failed starting activity, please report</string>
<string name="su_failed">Failed running su command, not rooted?</string>
<string name="screenshot_succeed_fallback">Screenshot captured</string>

<string name="restart_mode_normal">Reboot</string>
<string name="restart_mode_normal_or_soft">Reboot (hold to soft reboot)</string>
Expand Down

0 comments on commit f3f8c7d

Please sign in to comment.