AndroidAPS/app/src/main/java/info/nightscout/androidaps/utils/AndroidPermission.java

134 lines
8 KiB
Java
Raw Normal View History

2019-02-26 20:38:27 +01:00
package info.nightscout.androidaps.utils;
2018-05-22 17:34:09 +02:00
import android.Manifest;
import android.annotation.SuppressLint;
2018-05-22 17:34:09 +02:00
import android.app.Activity;
import android.content.ActivityNotFoundException;
2018-05-22 17:34:09 +02:00
import android.content.Context;
import android.content.Intent;
2018-05-22 17:34:09 +02:00
import android.content.pm.PackageManager;
import android.net.Uri;
2018-05-22 17:34:09 +02:00
import android.os.Build;
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;
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;
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;
@SuppressLint("BatteryLife")
private static void askForPermission(Activity activity, String[] permission, Integer requestCode) {
2018-05-22 17:34:09 +02:00
boolean test = false;
boolean testBattery = false;
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);
String packageName = activity.getPackageName();
testBattery = testBattery || !powerManager.isIgnoringBatteryOptimizations(packageName);
}
2018-05-22 17:34:09 +02:00
}
if (test) {
ActivityCompat.requestPermissions(activity, permission, requestCode);
}
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);
}
}
2018-05-22 17:34:09 +02:00
}
public static void askForPermission(Activity activity, String permission, Integer requestCode) {
String[] permissions = {permission};
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) {
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) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
String packageName = context.getPackageName();
selfCheck = selfCheck && powerManager.isIgnoringBatteryOptimizations(packageName);
}
}
return !selfCheck;
2018-05-22 17:34:09 +02: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)) {
NotificationWithAction notification = new NotificationWithAction(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)) {
2018-11-12 19:00:06 +01:00
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_PHONESTATE, MainApp.gs(R.string.smscommunicator_missingphonestatepermission), Notification.URGENT);
2019-08-28 17:57:48 +02:00
notification.action(R.string.request, () ->
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) {
if (permissionNotGranted(activity, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)) {
NotificationWithAction notification = new NotificationWithAction(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));
} 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) {
if (permissionNotGranted(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
NotificationWithAction notification = new NotificationWithAction(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,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE));
2019-12-30 00:53:44 +01:00
RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification));
} 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) {
if (permissionNotGranted(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
NotificationWithAction notification = new NotificationWithAction(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));
} 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
}
}