From f005fcd1d2b0d197eab706a2d5a36520f7f2ff82 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 10 Nov 2016 22:57:18 +0900 Subject: [PATCH] Request user to disable battery optimization for Syncthing --- lint.xml | 3 ++ src/main/AndroidManifest.xml | 1 + .../activities/MainActivity.java | 40 ++++++++++++++++++- src/main/res/values/strings.xml | 6 +++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lint.xml b/lint.xml index 2be1eca3..ab1df9f9 100644 --- a/lint.xml +++ b/lint.xml @@ -15,6 +15,9 @@ + + + diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 55cf7f8e..d8d80f31 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ + getFirstStartTime() + USAGE_REPORTING_DIALOG_DELAY && @@ -103,6 +116,31 @@ public class MainActivity extends SyncthingActivity } } + @TargetApi(23) + private void showBatteryOptimizationDialogIfNecessary() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + boolean dontShowAgain = sp.getBoolean("battery_optimization_dont_show_again", false); + if (dontShowAgain || Build.VERSION.SDK_INT < Build.VERSION_CODES.M || + pm.isIgnoringBatteryOptimizations(getPackageName())) { + return; + } + + new AlertDialog.Builder(this) + .setTitle(R.string.dialog_disable_battery_optimization_title) + .setMessage(R.string.dialog_disable_battery_optimization_message) + .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); + }) + .setNeutralButton(R.string.dialog_disable_battery_optimization_later, null) + .setNegativeButton(R.string.dialog_disable_battery_optimization_dont_show_again, (d, i) -> { + sp.edit().putBoolean("battery_optimization_dont_show_again", true).apply(); + }) + .show(); + } + private void dismissDisabledDialog() { if (mDisabledDialog != null) { mDisabledDialog.dismiss(); diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 0e6d0fb7..8af68027 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -57,6 +57,12 @@ Please report any problems you encounter via Github. Device %1$s wants to share folder %2$s + Battery Optimization + Android may stop synchronization after some time. To prevent this, turn off battery optimization + Later + Don\'t show again + Turn off for Syncthing +