From 2cc0b174c32c0ad28fa273312eb80accfd9aba6d Mon Sep 17 00:00:00 2001 From: Volker Richert <v.richert@addmore.de> Date: Tue, 29 Nov 2016 12:39:34 +0100 Subject: [PATCH] Add "REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" for Android 6.0+ --- app/app.iml | 17 ++-- app/src/main/AndroidManifest.xml | 3 + .../android/medtronic/MainActivity.java | 28 ++++++ app/src/main/res/xml-v23/preferences.xml | 97 +++++++++++++++++++ 4 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/xml-v23/preferences.xml diff --git a/app/app.iml b/app/app.iml index 64d6745..5fefeca 100644 --- a/app/app.iml +++ b/app/app.iml @@ -67,14 +67,6 @@ <sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src/main/shaders" isTestSource="false" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" /> - <sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> <sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" /> @@ -83,6 +75,14 @@ <sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/test/shaders" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" /> + <sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/assets" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/builds" /> @@ -118,6 +118,7 @@ <excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> + <excludeFolder url="file://$MODULE_DIR$/build/intermediates/pre-dexed" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/restart-dex" /> diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 998174b..7b0921f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -26,6 +26,9 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> + <!-- allow to disable battery optimization --> + <uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" /> + <!-- I have set screenOrientation to "portrait" to avoid the restart of AsyncTasks when you rotate the phone --> <activity android:name=".medtronic.MainActivity" diff --git a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java index 8a381cf..fa3d8d1 100644 --- a/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java +++ b/app/src/main/java/info/nightscout/android/medtronic/MainActivity.java @@ -3,6 +3,7 @@ package info.nightscout.android.medtronic; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; +import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; @@ -12,10 +13,14 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbManager; +import android.net.Uri; import android.os.BatteryManager; +import android.os.Build; import android.os.Bundle; import android.os.Handler; +import android.os.PowerManager; import android.preference.PreferenceManager; +import android.provider.Settings; import android.support.v4.app.TaskStackBuilder; import android.support.v4.content.LocalBroadcastManager; import android.support.v7.app.AlertDialog; @@ -106,6 +111,29 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc stopCgmService(); } + // Disable battery optimization to avoid missing values on 6.0+ + // taken from https://github.com/NightscoutFoundation/xDrip/blob/master/app/src/main/java/com/eveningoutpost/dexdrip/Home.java#L277L298 + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + final String packageName = getPackageName(); + //Log.d(TAG, "Maybe ignoring battery optimization"); + final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); + if (!pm.isIgnoringBatteryOptimizations(packageName) && + !prefs.getBoolean("requested_ignore_battery_optimizations", false)) { + Log.d(TAG, "Requesting ignore battery optimization"); + try { + // ignoring battery optimizations required for constant connection + // to peripheral device - eg CGM transmitter. + final Intent intent = new Intent(); + intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); + intent.setData(Uri.parse("package:" + packageName)); + startActivity(intent); + } catch (ActivityNotFoundException e) { + Log.d(TAG, "Device does not appear to support battery optimization whitelisting!"); + } + } + } + LocalBroadcastManager.getInstance(this).registerReceiver( statusMessageReceiver, new IntentFilter(MedtronicCnlIntentService.Constants.ACTION_STATUS_MESSAGE)); diff --git a/app/src/main/res/xml-v23/preferences.xml b/app/src/main/res/xml-v23/preferences.xml new file mode 100644 index 0000000..5dd0972 --- /dev/null +++ b/app/src/main/res/xml-v23/preferences.xml @@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="utf-8"?> +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> + <PreferenceCategory android:title="Monitor"> + <info.nightscout.android.utils.CustomSwitchPreference + android:disableDependentsState="false" + android:key="mmolxl" + android:summaryOff="Values are shown and set in mg/dL" + android:summaryOn="Values are shown and set in mmol/L" + android:switchTextOff="mg/dL" + android:switchTextOn="mmol/L" + android:title="BG Unit"/> + <info.nightscout.android.utils.CustomSwitchPreference + android:defaultValue="false" + android:dependency="mmolxl" + android:key="mmolDecimals" + android:summaryOff="1 decimal value will be shown" + android:summaryOn="2 decimal values will be shown" + android:switchTextOff="1" + android:switchTextOn="2" + android:title="Decimals"/> + </PreferenceCategory> + <PreferenceCategory android:title="Device setting"> + <info.nightscout.android.utils.CustomSwitchPreference + android:disableDependentsState="false" + android:key="requested_ignore_battery_optimizations" + android:summaryOff="Request to ignore battery optimization" + android:summaryOn="Don't request to ignore battery optimization" + android:switchTextOff="off" + android:switchTextOn="on" + android:title="Disable to ignore battery optimization"/> + </PreferenceCategory> + <PreferenceCategory android:title="Sharing"> + <CheckBoxPreference + android:disableDependentsState="false" + android:key="@string/preference_enable_rest_upload" + android:summary="Enable upload of data to Nightscout" + android:title="REST API Upload"/> + <EditTextPreference + android:defaultValue="https://YOUR.NIGHTSCOUT.SITE" + android:dependency="@string/preference_enable_rest_upload" + android:dialogMessage="The hostname of your Nightscout site" + android:dialogTitle="Enter Nightscout URL" + android:key="@string/preference_nightscout_url" + android:title="Nightscout URL"/> + <info.nightscout.android.utils.ValidatingEditTextPreference + android:defaultValue="YOURAPISECRET" + android:dependency="@string/preference_enable_rest_upload" + android:dialogMessage="Your Nightscout API secret" + android:dialogTitle="Enter your Nightscout API secret" + android:key="@string/preference_api_secret" + android:title="API Secret"/> + <CheckBoxPreference + android:key="@string/preference_enable_xdrip_plus" + android:summary="Enable local broadcast of data to xDrip+" + android:title="Send to xDrip+"/> + </PreferenceCategory> + <PreferenceCategory android:title="Disclaimer"> + <SwitchPreference + android:disableDependentsState="false" + android:key="@string/preference_eula_accepted" + android:summaryOff="Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers" + android:summaryOn="Nightscout should not be used to make medical decisions. There is no support or any warranty of any kind. The quality and performance of the project is with you. This is a project that was created and is supported completely by volunteers" + android:switchTextOff="NO" + android:switchTextOn="YES" + android:title="I UNDERSTAND"/> + </PreferenceCategory> + <PreferenceCategory android:title="App Version"> + <Preference + android:key="version" + android:title="@string/versionName" /> + </PreferenceCategory> + <PreferenceCategory android:title="Debug"> + <SwitchPreference + android:defaultValue="true" + android:key="@string/preferences_enable_crashlytics" + android:summary="Send crash errors to developer" + android:title="Crash Reporting" /> + <SwitchPreference + android:defaultValue="true" + android:key="@string/preferences_enable_answers" + android:summary="Sends usage data to the developer to help develop a better app." + android:title="Share usage data" /> + <SwitchPreference + android:defaultValue="false" + android:key="@string/preferences_enable_remote_logcat" + android:summary="Allow the developer to debug your app. Only enable if asked to by the support team." + android:title="Remote Debugging" /> + <ListPreference + android:defaultValue="1" + android:disableDependentsState="false" + android:entries="@array/levelList" + android:entryValues="@array/calib_types_values" + android:key="logLevel" + android:summary="Select item from array" + android:title="Logging Level" /> + </PreferenceCategory> +</PreferenceScreen> \ No newline at end of file -- GitLab