2019-02-26 20:38:27 +01:00
|
|
|
package info.nightscout.androidaps.utils;
|
2018-05-22 17:34:09 +02:00
|
|
|
|
|
|
|
import android.Manifest;
|
2019-08-03 20:01:55 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2018-05-22 17:34:09 +02:00
|
|
|
import android.app.Activity;
|
2019-08-21 18:38:01 +02:00
|
|
|
import android.content.ActivityNotFoundException;
|
2018-05-22 17:34:09 +02:00
|
|
|
import android.content.Context;
|
2019-08-02 15:13:57 +02:00
|
|
|
import android.content.Intent;
|
2018-05-22 17:34:09 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2019-08-02 15:13:57 +02:00
|
|
|
import android.net.Uri;
|
2018-05-22 17:34:09 +02:00
|
|
|
import android.os.Build;
|
2019-08-02 15:13:57 +02:00
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.provider.Settings;
|
|
|
|
|
2019-05-16 13:57:37 +02:00
|
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
2018-05-22 17:34:09 +02:00
|
|
|
|
2018-05-22 23:06:17 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2018-05-22 17:34:09 +02:00
|
|
|
import info.nightscout.androidaps.R;
|
2019-12-13 13:12:36 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginType;
|
2019-10-01 00:00:22 +02:00
|
|
|
import info.nightscout.androidaps.plugins.bus.RxBus;
|
2019-02-28 23:16:50 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
|
|
|
|
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
|
|
|
|
import info.nightscout.androidaps.plugins.general.overview.notifications.Notification;
|
|
|
|
import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationWithAction;
|
2019-12-13 13:12:36 +01:00
|
|
|
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
|
2018-05-22 17:34:09 +02:00
|
|
|
|
|
|
|
public class AndroidPermission {
|
|
|
|
|
|
|
|
public static final int CASE_STORAGE = 0x1;
|
|
|
|
public static final int CASE_SMS = 0x2;
|
|
|
|
public static final int CASE_LOCATION = 0x3;
|
|
|
|
public static final int CASE_BATTERY = 0x4;
|
2019-08-03 20:01:55 +02:00
|
|
|
public static final int CASE_PHONE_STATE = 0x5;
|
2018-05-22 17:34:09 +02:00
|
|
|
|
2019-08-24 18:58:17 +02:00
|
|
|
private static boolean permission_battery_optimization_failed = false;
|
|
|
|
|
2019-08-03 20:01:55 +02:00
|
|
|
@SuppressLint("BatteryLife")
|
|
|
|
private static void askForPermission(Activity activity, String[] permission, Integer requestCode) {
|
2018-05-22 17:34:09 +02:00
|
|
|
boolean test = false;
|
2019-08-02 15:13:57 +02:00
|
|
|
boolean testBattery = false;
|
2019-08-03 20:01:55 +02:00
|
|
|
for (String s : permission) {
|
|
|
|
test = test || (ContextCompat.checkSelfPermission(activity, s) != PackageManager.PERMISSION_GRANTED);
|
|
|
|
if (s.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
|
|
|
|
PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
|
2019-08-02 15:13:57 +02:00
|
|
|
String packageName = activity.getPackageName();
|
|
|
|
testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName);
|
|
|
|
}
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
if (test) {
|
|
|
|
ActivityCompat.requestPermissions(activity, permission, requestCode);
|
|
|
|
}
|
2019-08-02 15:13:57 +02:00
|
|
|
if (testBattery) {
|
2019-08-24 18:58:17 +02:00
|
|
|
try {
|
|
|
|
Intent i = new Intent();
|
|
|
|
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
|
|
|
i.setData(Uri.parse("package:" + activity.getPackageName()));
|
|
|
|
activity.startActivityForResult(i, CASE_BATTERY);
|
|
|
|
} catch (ActivityNotFoundException e) {
|
|
|
|
permission_battery_optimization_failed = true;
|
|
|
|
OKDialog.show(activity, MainApp.gs(R.string.permission), MainApp.gs(R.string.alert_dialog_permission_battery_optimization_failed), activity::recreate);
|
2019-10-01 00:00:22 +02:00
|
|
|
}
|
2019-08-02 15:13:57 +02:00
|
|
|
}
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void askForPermission(Activity activity, String permission, Integer requestCode) {
|
|
|
|
String[] permissions = {permission};
|
2019-08-02 15:13:57 +02:00
|
|
|
askForPermission(activity, permissions, requestCode);
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 19:11:16 +02:00
|
|
|
public static boolean permissionNotGranted(Context context, String permission) {
|
2019-08-03 20:01:55 +02:00
|
|
|
boolean selfCheck = ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
|
|
|
if (permission.equals(Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
|
2019-08-24 18:58:17 +02:00
|
|
|
if (!permission_battery_optimization_failed) {
|
2019-08-21 18:38:01 +02:00
|
|
|
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
String packageName = context.getPackageName();
|
|
|
|
selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName);
|
|
|
|
}
|
2019-08-02 15:13:57 +02:00
|
|
|
}
|
2019-08-03 20:01:55 +02:00
|
|
|
return !selfCheck;
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
|
2019-12-27 04:17:49 +01:00
|
|
|
public static synchronized void notifyForSMSPermissions(Activity activity, SmsCommunicatorPlugin smsCommunicatorPlugin) {
|
|
|
|
if (smsCommunicatorPlugin.isEnabled(PluginType.GENERAL)) {
|
2019-08-03 19:11:16 +02:00
|
|
|
if (permissionNotGranted(activity, Manifest.permission.RECEIVE_SMS)) {
|
2020-01-01 23:23:16 +01:00
|
|
|
NotificationWithAction notification = new NotificationWithAction(MainApp.instance(), Notification.PERMISSION_SMS, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.URGENT);
|
2019-08-28 17:57:48 +02:00
|
|
|
notification.action(R.string.request, () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS,
|
2019-08-03 19:11:16 +02:00
|
|
|
Manifest.permission.SEND_SMS,
|
|
|
|
Manifest.permission.RECEIVE_MMS}, AndroidPermission.CASE_SMS));
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
|
2019-08-03 19:11:16 +02:00
|
|
|
} else
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(Notification.PERMISSION_SMS));
|
2018-11-12 19:00:06 +01:00
|
|
|
// Following is a bug in Android 8
|
|
|
|
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
|
2019-08-03 19:11:16 +02:00
|
|
|
if (permissionNotGranted(activity, Manifest.permission.READ_PHONE_STATE)) {
|
2020-01-01 23:23:16 +01:00
|
|
|
NotificationWithAction notification = new NotificationWithAction(MainApp.instance(), Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT);
|
2019-08-28 17:57:48 +02:00
|
|
|
notification.action(R.string.request, () ->
|
2019-08-03 20:01:55 +02:00
|
|
|
AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_PHONE_STATE}, AndroidPermission.CASE_PHONE_STATE));
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
|
2018-11-12 19:00:06 +01:00
|
|
|
} else
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(Notification.PERMISSION_PHONESTATE));
|
2018-11-12 19:00:06 +01:00
|
|
|
}
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 23:06:17 +02:00
|
|
|
public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) {
|
2019-08-03 20:01:55 +02:00
|
|
|
if (permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
|
2020-01-01 23:23:16 +01:00
|
|
|
NotificationWithAction notification = new NotificationWithAction(MainApp.instance(), Notification.PERMISSION_BATTERY, String.format(MainApp.gs(R.string.needwhitelisting), MainApp.gs(R.string.app_name)), Notification.URGENT);
|
2019-08-28 17:57:48 +02:00
|
|
|
notification.action(R.string.request, () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY));
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
|
2019-08-03 20:01:55 +02:00
|
|
|
} else
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(Notification.PERMISSION_BATTERY));
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 23:06:17 +02:00
|
|
|
public static synchronized void notifyForStoragePermission(Activity activity) {
|
2019-08-03 20:01:55 +02:00
|
|
|
if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
2020-01-01 23:23:16 +01:00
|
|
|
NotificationWithAction notification = new NotificationWithAction(MainApp.instance(), Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT);
|
2019-08-28 17:57:48 +02:00
|
|
|
notification.action(R.string.request, () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
|
2019-08-03 20:01:55 +02:00
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE));
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
|
2019-08-03 20:01:55 +02:00
|
|
|
} else
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(Notification.PERMISSION_STORAGE));
|
2018-05-22 18:59:34 +02:00
|
|
|
}
|
|
|
|
|
2018-05-22 23:06:17 +02:00
|
|
|
public static synchronized void notifyForLocationPermissions(Activity activity) {
|
2019-08-03 20:01:55 +02:00
|
|
|
if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
|
2020-01-01 23:23:16 +01:00
|
|
|
NotificationWithAction notification = new NotificationWithAction(MainApp.instance(), Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT);
|
2019-08-28 17:57:48 +02:00
|
|
|
notification.action(R.string.request, () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION));
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
|
2019-08-03 20:01:55 +02:00
|
|
|
} else
|
2019-12-30 00:53:44 +01:00
|
|
|
RxBus.Companion.getINSTANCE().send(new EventDismissNotification(Notification.PERMISSION_LOCATION));
|
2018-05-22 17:34:09 +02:00
|
|
|
}
|
|
|
|
}
|