asking permissions to notifications

This commit is contained in:
Milos Kozak 2018-05-22 23:06:17 +02:00
parent 5d27a28dc9
commit 53bdb0bf09
8 changed files with 104 additions and 78 deletions

View file

@ -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);

View file

@ -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();

View file

@ -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;

View file

@ -42,9 +42,12 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter<Notifi
public void onBindViewHolder(NotificationsViewHolder holder, int position) {
Notification notification = notificationsList.get(position);
holder.dismiss.setTag(notification);
if (Objects.equals(notification.text, MainApp.gs(R.string.nsalarm_staledata)))
if (notification instanceof NotificationWithAction)
holder.dismiss.setText(((NotificationWithAction) notification).buttonText);
else if (Objects.equals(notification.text, MainApp.gs(R.string.nsalarm_staledata)))
holder.dismiss.setText("snooze");
holder.text.setText(notification.text+'\n');
holder.text.setText(notification.text + '\n');
holder.time.setText(DateUtil.timeString(notification.date));
if (notification.level == Notification.URGENT)
holder.cv.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.notificationUrgent));
@ -100,6 +103,9 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter<Notifi
log.debug("snooze nsalarm_staledatavalue in minutes is " + SP.getInt("nsalarm_staledatavalue", 15) + "\n in ms is: " + msToSnooze + " currentTimeMillis is: " + System.currentTimeMillis());
nstore.snoozeTo(System.currentTimeMillis() + (SP.getInt("nsalarm_staledatavalue", 15) * 60 * 1000L));
}
if (notification instanceof NotificationWithAction) {
((NotificationWithAction) notification).action.run();
}
break;
}
}

View file

@ -61,7 +61,7 @@ public class NotificationStore {
}
store.add(n);
if (SP.getBoolean(MainApp.gs(R.string.key_raise_notifications_as_android_notifications), false)) {
if (SP.getBoolean(MainApp.gs(R.string.key_raise_notifications_as_android_notifications), false) && !(n instanceof NotificationWithAction)) {
raiseSystemNotification(n);
if (usesChannels && n.soundId != null) {
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);

View file

@ -0,0 +1,16 @@
package info.nightscout.androidaps.plugins.Overview.notifications;
public class NotificationWithAction extends Notification {
Runnable action;
String buttonText;
public NotificationWithAction(int id, String text, int level) {
super(id, text, level);
}
public void action(String buttonText, Runnable action) {
this.buttonText = buttonText;
this.action = action;
}
}

View file

@ -8,13 +8,15 @@ import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.androidaps.plugins.Overview.notifications.NotificationWithAction;
public class AndroidPermission {
private static boolean askForSMS = false;
private static boolean askForLocation = true;
public static final int CASE_STORAGE = 0x1;
public static final int CASE_SMS = 0x2;
public static final int CASE_LOCATION = 0x3;
@ -41,45 +43,52 @@ public class AndroidPermission {
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
public static synchronized void askForSMSPermissions(Activity activity) {
if (askForSMS) { //only when settings were changed an MainActivity resumes.
askForSMS = false;
if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) {
if (Build.VERSION.SDK_INT > 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;
}
}

View file

@ -1084,4 +1084,5 @@
<string name="askforpermission">Ask for permission</string>
<string name="needlocationpermission">Application needs location permission for BT scan</string>
<string name="needstoragepermission">Application needs storage permission to be able store log files</string>
<string name="request">Request</string>
</resources>