request battery optimalization whitelisting

This commit is contained in:
Milos Kozak 2017-03-31 09:21:40 +02:00
parent 0cb4ace052
commit d6f31820d9
4 changed files with 86 additions and 0 deletions

View file

@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" /> <uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<!-- To receive data from xdrip. --> <!-- To receive data from xdrip. -->
<uses-permission android:name="com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE" /> <uses-permission android:name="com.eveningoutpost.dexdrip.permissions.RECEIVE_BG_ESTIMATE" />

View file

@ -1,13 +1,17 @@
package info.nightscout.androidaps; package info.nightscout.androidaps;
import android.Manifest; import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.graphics.Rect; import android.graphics.Rect;
import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
@ -37,8 +41,10 @@ import info.nightscout.androidaps.tabs.TabPageAdapter;
import info.nightscout.utils.ImportExportPrefs; import info.nightscout.utils.ImportExportPrefs;
import info.nightscout.utils.LocaleHelper; import info.nightscout.utils.LocaleHelper;
import info.nightscout.utils.LogDialog; import info.nightscout.utils.LogDialog;
import info.nightscout.utils.OKDialog;
import info.nightscout.utils.PasswordProtection; import info.nightscout.utils.PasswordProtection;
import info.nightscout.utils.SP; import info.nightscout.utils.SP;
import info.nightscout.utils.ToastUtils;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static Logger log = LoggerFactory.getLogger(MainActivity.class); private static Logger log = LoggerFactory.getLogger(MainActivity.class);
@ -59,6 +65,7 @@ public class MainActivity extends AppCompatActivity {
askForPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, askForPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, CASE_STORAGE); Manifest.permission.WRITE_EXTERNAL_STORAGE}, CASE_STORAGE);
} }
askForBatteryOptimizationPermission();
if (Config.logFunctionCalls) if (Config.logFunctionCalls)
log.debug("onCreate"); log.debug("onCreate");
@ -228,6 +235,37 @@ public class MainActivity extends AppCompatActivity {
askForSMSPermissions(); askForSMSPermissions();
} }
private void askForBatteryOptimizationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final String packageName = getPackageName();
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
log.debug("Requesting ignore battery optimization");
OKDialog.show(this, getString(R.string.pleaseallowpermission), String.format(getString(R.string.needwhitelisting), getString(R.string.app_name)), new Runnable() {
@Override
public void run() {
try {
final Intent intent = new Intent();
// ignoring battery optimizations required for constant connection
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
} catch (ActivityNotFoundException e) {
final String msg = getString(R.string.batteryoptimalizationerror);
ToastUtils.showToastInUiThread(getApplicationContext(), msg);
log.error(msg);
}
}
});
}
}
}
private synchronized void askForSMSPermissions() { private synchronized void askForSMSPermissions() {
if (askForSMS) { //only when settings were changed an MainActivity resumes. if (askForSMS) { //only when settings were changed an MainActivity resumes.
askForSMS = false; askForSMS = false;

View file

@ -0,0 +1,44 @@
package info.nightscout.utils;
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
/**
* Created by mike on 31.03.2017.
*/
public class OKDialog {
private static Logger log = LoggerFactory.getLogger(OKDialog.class);
public static void show(final Activity activity, String title, String message, final Runnable runnable) {
try {
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.AppTheme));
builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton(MainApp.sResources.getString(R.string.ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (runnable != null) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
activity.runOnUiThread(runnable);
}
}
});
builder.create().show();
} catch (Exception e) {
log.debug("show_dialog exception: " + e);
}
}
}

View file

@ -554,4 +554,7 @@
<string name="patientage_summary">Please select patient age to setup safety limits</string> <string name="patientage_summary">Please select patient age to setup safety limits</string>
<string name="key_i_understand" translatable="false">I_understand</string> <string name="key_i_understand" translatable="false">I_understand</string>
<string name="Glimp">Glimp</string> <string name="Glimp">Glimp</string>
<string name="batteryoptimalizationerror">Device does not appear to support battery optimization whitelisting!</string>
<string name="pleaseallowpermission">Please Allow Permission</string>
<string name="needwhitelisting">%s needs battery optimalization whitelisting for proper performance</string>
</resources> </resources>