Skip to content
Snippets Groups Projects
Commit d290a047 authored by Lennart Goedhart's avatar Lennart Goedhart Committed by GitHub
Browse files

Merge pull request #87 from volkerrichert/master

Add REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
parents a0dcbfce 982a57d7
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,9 @@ ...@@ -26,6 +26,9 @@
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> 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 --> <!-- I have set screenOrientation to "portrait" to avoid the restart of AsyncTasks when you rotate the phone -->
<activity <activity
android:name=".medtronic.MainActivity" android:name=".medtronic.MainActivity"
......
...@@ -3,6 +3,7 @@ package info.nightscout.android.medtronic; ...@@ -3,6 +3,7 @@ package info.nightscout.android.medtronic;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
...@@ -12,10 +13,14 @@ import android.content.SharedPreferences; ...@@ -12,10 +13,14 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.hardware.usb.UsbDevice; import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager; import android.hardware.usb.UsbManager;
import android.net.Uri;
import android.os.BatteryManager; import android.os.BatteryManager;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PowerManager;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.v4.app.TaskStackBuilder; import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
...@@ -106,6 +111,28 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc ...@@ -106,6 +111,28 @@ public class MainActivity extends AppCompatActivity implements OnSharedPreferenc
stopCgmService(); 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)) {
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( LocalBroadcastManager.getInstance(this).registerReceiver(
statusMessageReceiver, statusMessageReceiver,
new IntentFilter(MedtronicCnlIntentService.Constants.ACTION_STATUS_MESSAGE)); new IntentFilter(MedtronicCnlIntentService.Constants.ACTION_STATUS_MESSAGE));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment