From 4fb83d04c9692982700692fe3b57fa6ae0bf23e5 Mon Sep 17 00:00:00 2001 From: Graham Jones Date: Sat, 5 Feb 2022 20:40:32 +0000 Subject: [PATCH] Added check of system battery usage optimisation with dialog to prompt user to disable it. --- .../openseizuredetector/StartupActivity.java | 65 ++++++++++++++----- app/src/main/res/values/strings.xml | 2 + 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java index 77fbb16..accf8a1 100644 --- a/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/StartupActivity.java @@ -33,14 +33,11 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.graphics.Color; -import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; import android.preference.PreferenceManager; -import android.provider.Settings; import android.text.SpannableString; -import android.text.SpannedString; import android.text.util.Linkify; import android.util.Log; import android.view.View; @@ -77,6 +74,8 @@ public class StartupActivity extends Activity { private Handler mHandler = new Handler(); // used to update ui from mUiTimer private boolean mUsingPebbleDataSource = true; private String mPebbleAppPackageName = null; + private boolean mBatteryOptDialogDisplayed = false; + private AlertDialog mBatteryOptDialog; @Override protected void onCreate(Bundle savedInstanceState) { @@ -162,19 +161,6 @@ public class StartupActivity extends Activity { mUtil.requestPermissions(this); } - // FIXME = this does not seem to work - says there is a problem if optimisation is switched off... - PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); - if (powerManager.isIgnoringBatteryOptimizations(getPackageName())) { - Log.i(TAG,"Power Management OK - we are ignoring Battery Optimizations"); - } else { - Log.e(TAG,"Power Management Problem - not ignoring Battery Optimisations"); - Intent i = new Intent(); - i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); - i.setData(Uri.parse("package:" + getPackageName())); - startActivity(i); - } - - String versionName = mUtil.getAppVersionName(); tv = (TextView) findViewById(R.id.appNameTv); tv.setText("OpenSeizureDetector V" + versionName); @@ -202,6 +188,18 @@ public class StartupActivity extends Activity { mUtil.writeToSysLogFile("StartupActivity.onStart() - binding to server"); mUtil.bindToServer(this, mConnection); + // Check power management settings + PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); + if (powerManager.isIgnoringBatteryOptimizations(getPackageName())) { + Log.i(TAG,"Power Management OK - we are ignoring Battery Optimizations"); + mBatteryOptDialogDisplayed = false; + } else { + Log.e(TAG,"Power Management Problem - not ignoring Battery Optimisations"); + //mUtil.showToast("WARNING - Phone is Optimising OpenSeizureDetector Battery Usage - this is likely to prevent it working correctly when running on battery!"); + if (!mBatteryOptDialogDisplayed) showBatteryOptimisationWarningDialog(); + } + + // Check to see if this is the first time the app has been run, and display welcome dialog if it is. checkFirstRun(); @@ -248,6 +246,16 @@ public class StartupActivity extends Activity { smsAlarmsActive = SP.getBoolean("SMSAlarm", false); phoneAlarmsActive = SP.getBoolean("PhoneCallAlarm", false); + // Check power management settings + PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE); + if (powerManager.isIgnoringBatteryOptimizations(getPackageName())) { + Log.i(TAG, "Power Management OK - we are ignoring Battery Optimizations"); + if (mBatteryOptDialogDisplayed) { + mBatteryOptDialog.cancel(); + mBatteryOptDialogDisplayed = false; + } + } + // Settings ok tv = (TextView) findViewById(R.id.textItem1); pb = (ProgressBar) findViewById(R.id.progressBar1); @@ -361,7 +369,7 @@ public class StartupActivity extends Activity { // If all the parameters are ok, close this activity and open the main // user interface activity instead. if (allOk) { - if (!mDialogDisplayed) { + if (!mDialogDisplayed && !mBatteryOptDialogDisplayed) { if (!mStartedMainActivity) { Log.i(TAG, "serverStatusRunnable() - starting main activity..."); mUtil.writeToSysLogFile("StartupActivity.serverStatusRunnable - all checks ok - starting main activity."); @@ -478,4 +486,27 @@ public class StartupActivity extends Activity { prefs.edit().putString("AppVersionName", versionName).commit(); } + private void showBatteryOptimisationWarningDialog() { + AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( + this); + final SpannableString s = new SpannableString( + getString(R.string.battery_usage_optimisation_dialog_text) + ); + // This makes the links display as links, but they do not respond to clicks for some reason... + Linkify.addLinks(s, Linkify.ALL); + alertDialogBuilder + .setTitle(R.string.battery_usage_optimisation_dialog_title) + .setMessage(s) + .setCancelable(false) + .setPositiveButton(getString(R.string.okBtnTxt), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + dialog.cancel(); + mBatteryOptDialogDisplayed = false; + } + }); + mBatteryOptDialog = alertDialogBuilder.create(); + Log.i(TAG, "Displaying Update Dialog"); + mBatteryOptDialog.show(); + mBatteryOptDialogDisplayed = true; + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 14c3ab5..a015996 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -341,4 +341,6 @@ Not Updating using Mobile Internet No Network Connection Error Connecting to Server + WARNING: Battery Usage Optimisation + WARNING: The Android System Battery Optimisation Settings are configured to \'Optimise\' OpenSeizureDetector. This means it is likely to be shutdown when operating on battery power, so will not work correctly\n\nPlease go into the Phone Settings and search for \'Optimise Battery Usage\' and ensure that OpenSeizureDetector is NOT optimised