From 31721af0c3a00e4a9a2434e50f34e5dca0c57637 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 25 Aug 2017 18:56:25 +0900 Subject: [PATCH] Dont crash if request ignore battery optimizations is not supported --- .../syncthingandroid/activities/MainActivity.java | 11 ++++++++++- src/main/res/values/strings.xml | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java index 83f61f69..e5f7f9bc 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/MainActivity.java @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; +import android.content.ActivityNotFoundException; import android.content.ComponentName; import android.content.Context; import android.content.DialogInterface; @@ -148,7 +149,15 @@ public class MainActivity extends SyncthingActivity .setPositiveButton(R.string.dialog_disable_battery_optimization_turn_off, (d, i) -> { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + getPackageName())); - startActivity(intent); + try { + startActivity(intent); + } catch (ActivityNotFoundException e) { + // Some devices dont seem to support this request (according to Google Play + // crash reports). + Log.w(TAG, "Request ignore battery optimizations not supported", e); + Toast.makeText(this, R.string.dialog_disable_battery_optimizations_not_supported, Toast.LENGTH_LONG).show(); + sp.edit().putBoolean("battery_optimization_dont_show_again", true).apply(); + } }) .setNeutralButton(R.string.dialog_disable_battery_optimization_later, (d, i) -> mBatteryOptimizationDialogDismissed = true) .setNegativeButton(R.string.dialog_disable_battery_optimization_dont_show_again, (d, i) -> { diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index d5e1d622..c88534b9 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -60,6 +60,7 @@ Please report any problems you encounter via Github. Later Don\'t show again Turn off for Syncthing + Your device does not support disabling battery optimizations