From 53bdb0bf09579276d8032f04879ce7e106666a14 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 22 May 2018 23:06:17 +0200 Subject: [PATCH] asking permissions to notifications --- .../nightscout/androidaps/MainActivity.java | 71 ++++++++----------- .../plugins/Overview/OverviewFragment.java | 9 ++- .../Overview/notifications/Notification.java | 4 ++ .../NotificationRecyclerViewAdapter.java | 10 ++- .../notifications/NotificationStore.java | 2 +- .../notifications/NotificationWithAction.java | 16 +++++ .../nightscout/utils/AndroidPermission.java | 69 ++++++++++-------- app/src/main/res/values/strings.xml | 1 + 8 files changed, 104 insertions(+), 78 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/NotificationWithAction.java diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 555eef8267..9d869cdde6 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -75,18 +75,41 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe menuButton = (ImageButton) findViewById(R.id.overview_menuButton); menuButton.setOnClickListener(this); - if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { - Intent intent = new Intent(this, SetupWizardActivity.class); - startActivity(intent); - finish(); - } - onStatusEvent(new EventSetWakeLock(SP.getBoolean("lockscreen", false))); + doMigrations(); + registerBus(); setUpTabs(false); } + @Override + protected void onResume() { + super.onResume(); + + if (!SP.getBoolean(R.string.key_setupwizard_processed, false)) { + Intent intent = new Intent(this, SetupWizardActivity.class); + startActivity(intent); + } else { + checkEula(); + } + + AndroidPermission.notifyForStoragePermission(this); + AndroidPermission.notifyForBatteryOptimizationPermission(this); + AndroidPermission.notifyForLocationPermissions(this); + AndroidPermission.notifyForSMSPermissions(this); + + MainApp.bus().post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN)); + } + + @Override + public void onDestroy() { + if (mWakeLock != null) + if (mWakeLock.isHeld()) + mWakeLock.release(); + super.onDestroy(); + } + @Subscribe public void onStatusEvent(final EventSetWakeLock ev) { final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); @@ -194,42 +217,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe } } - //check for sms permission if enable in prefernces - @Subscribe - public void onStatusEvent(final EventPreferenceChange ev) { - if (ev.isChanged(R.string.key_smscommunicator_remotecommandsallowed)) { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { - synchronized (this) { - if (SP.getBoolean(R.string.key_smscommunicator_remotecommandsallowed, false)) { - AndroidPermission.setAskForSMS(); - } - } - } - } - } - - @Override - protected void onResume() { - super.onResume(); - - checkEula(); - AndroidPermission.askForStoragePermission(this); - AndroidPermission.askForBatteryOptimizationPermission(this); - doMigrations(); - - AndroidPermission.askForSMSPermissions(this); - AndroidPermission.askForLocationPermissions(this); - MainApp.bus().post(new EventFeatureRunning(EventFeatureRunning.Feature.MAIN)); - } - - @Override - public void onDestroy() { - if (mWakeLock != null) - if (mWakeLock.isHeld()) - mWakeLock.release(); - super.onDestroy(); - } - @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 7d7c0e7efb..31aa098579 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -981,16 +981,19 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, if (timeView != null) { //must not exists timeView.setText(DateUtil.timeString(new Date())); } + + updateNotifications(); + + pumpStatusLayout.setVisibility(View.GONE); + loopStatusLayout.setVisibility(View.GONE); + if (!MainApp.getConfigBuilder().isProfileValid("Overview")) { pumpStatusView.setText(R.string.noprofileset); pumpStatusLayout.setVisibility(View.VISIBLE); - loopStatusLayout.setVisibility(View.GONE); return; } - pumpStatusLayout.setVisibility(View.GONE); loopStatusLayout.setVisibility(View.VISIBLE); - updateNotifications(); CareportalFragment.updateAge(getActivity(), sage, iage, cage, pbage); BgReading actualBG = DatabaseHelper.actualBg(); BgReading lastBG = DatabaseHelper.lastBg(); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/Notification.java index d4fc034d10..9a374bf3f3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/Notification.java @@ -60,6 +60,10 @@ public class Notification { public static final int PROFILE_SWITCH_MISSING = 32; public static final int NOT_ENG_MODE_OR_RELEASE = 33; public static final int WRONG_PUMP_PASSWORD = 34; + public static final int PERMISSION_STORAGE = 35; + public static final int PERMISSION_LOCATION = 36; + public static final int PERMISSION_BATTERY = 37; + public static final int PERMISSION_SMS = 38; public int id; diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/NotificationRecyclerViewAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/NotificationRecyclerViewAdapter.java index 5024c2699a..895b99a43e 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/NotificationRecyclerViewAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/notifications/NotificationRecyclerViewAdapter.java @@ -42,9 +42,12 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter Build.VERSION_CODES.LOLLIPOP_MR1) { - AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS, + public static synchronized void notifyForSMSPermissions(Activity activity) { + if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) { + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { + if (!checkForPermission(activity, Manifest.permission.RECEIVE_SMS)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_SMS, MainApp.gs(R.string.smscommunicator_missingsmspermission), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS, Manifest.permission.SEND_SMS, - Manifest.permission.RECEIVE_MMS}, AndroidPermission.CASE_SMS); - } + Manifest.permission.RECEIVE_MMS}, AndroidPermission.CASE_SMS)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_SMS)); } } } - public static synchronized void askForBatteryOptimizationPermission(Activity activity) { + public static synchronized void notifyForBatteryOptimizationPermission(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY); + if (!checkForPermission(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); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS}, AndroidPermission.CASE_BATTERY)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_BATTERY)); } } - public static synchronized void askForStoragePermission(Activity activity) { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) { - AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, - Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE); + public static synchronized void notifyForStoragePermission(Activity activity) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (!checkForPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE}, AndroidPermission.CASE_STORAGE)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_STORAGE)); } } - public static synchronized void askForLocationPermissions(Activity activity) { - if (askForLocation) { //only when settings were changed an MainActivity resumes. - askForLocation = false; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION); - } + public static synchronized void notifyForLocationPermissions(Activity activity) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (!checkForPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)) { + NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_LOCATION, MainApp.gs(R.string.needlocationpermission), Notification.URGENT); + notification.action(MainApp.gs(R.string.request), () -> AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION)); + MainApp.bus().post(new EventNewNotification(notification)); + } else + MainApp.bus().post(new EventDismissNotification(Notification.PERMISSION_LOCATION)); } } - - public static synchronized void setAskForSMS() { - askForSMS = true; - } - } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27695b1d6a..ebcbfe5b61 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1084,4 +1084,5 @@ Ask for permission Application needs location permission for BT scan Application needs storage permission to be able store log files + Request