asking permissions to notifications
This commit is contained in:
parent
5d27a28dc9
commit
53bdb0bf09
8 changed files with 104 additions and 78 deletions
|
@ -75,18 +75,41 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
|
||||||
menuButton = (ImageButton) findViewById(R.id.overview_menuButton);
|
menuButton = (ImageButton) findViewById(R.id.overview_menuButton);
|
||||||
menuButton.setOnClickListener(this);
|
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)));
|
onStatusEvent(new EventSetWakeLock(SP.getBoolean("lockscreen", false)));
|
||||||
|
|
||||||
|
doMigrations();
|
||||||
|
|
||||||
registerBus();
|
registerBus();
|
||||||
setUpTabs(false);
|
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
|
@Subscribe
|
||||||
public void onStatusEvent(final EventSetWakeLock ev) {
|
public void onStatusEvent(final EventSetWakeLock ev) {
|
||||||
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
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
|
@Override
|
||||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||||
|
|
|
@ -981,16 +981,19 @@ public class OverviewFragment extends Fragment implements View.OnClickListener,
|
||||||
if (timeView != null) { //must not exists
|
if (timeView != null) { //must not exists
|
||||||
timeView.setText(DateUtil.timeString(new Date()));
|
timeView.setText(DateUtil.timeString(new Date()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateNotifications();
|
||||||
|
|
||||||
|
pumpStatusLayout.setVisibility(View.GONE);
|
||||||
|
loopStatusLayout.setVisibility(View.GONE);
|
||||||
|
|
||||||
if (!MainApp.getConfigBuilder().isProfileValid("Overview")) {
|
if (!MainApp.getConfigBuilder().isProfileValid("Overview")) {
|
||||||
pumpStatusView.setText(R.string.noprofileset);
|
pumpStatusView.setText(R.string.noprofileset);
|
||||||
pumpStatusLayout.setVisibility(View.VISIBLE);
|
pumpStatusLayout.setVisibility(View.VISIBLE);
|
||||||
loopStatusLayout.setVisibility(View.GONE);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pumpStatusLayout.setVisibility(View.GONE);
|
|
||||||
loopStatusLayout.setVisibility(View.VISIBLE);
|
loopStatusLayout.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
updateNotifications();
|
|
||||||
CareportalFragment.updateAge(getActivity(), sage, iage, cage, pbage);
|
CareportalFragment.updateAge(getActivity(), sage, iage, cage, pbage);
|
||||||
BgReading actualBG = DatabaseHelper.actualBg();
|
BgReading actualBG = DatabaseHelper.actualBg();
|
||||||
BgReading lastBG = DatabaseHelper.lastBg();
|
BgReading lastBG = DatabaseHelper.lastBg();
|
||||||
|
|
|
@ -60,6 +60,10 @@ public class Notification {
|
||||||
public static final int PROFILE_SWITCH_MISSING = 32;
|
public static final int PROFILE_SWITCH_MISSING = 32;
|
||||||
public static final int NOT_ENG_MODE_OR_RELEASE = 33;
|
public static final int NOT_ENG_MODE_OR_RELEASE = 33;
|
||||||
public static final int WRONG_PUMP_PASSWORD = 34;
|
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;
|
public int id;
|
||||||
|
|
|
@ -42,9 +42,12 @@ public class NotificationRecyclerViewAdapter extends RecyclerView.Adapter<Notifi
|
||||||
public void onBindViewHolder(NotificationsViewHolder holder, int position) {
|
public void onBindViewHolder(NotificationsViewHolder holder, int position) {
|
||||||
Notification notification = notificationsList.get(position);
|
Notification notification = notificationsList.get(position);
|
||||||
holder.dismiss.setTag(notification);
|
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.dismiss.setText("snooze");
|
||||||
holder.text.setText(notification.text+'\n');
|
|
||||||
|
holder.text.setText(notification.text + '\n');
|
||||||
holder.time.setText(DateUtil.timeString(notification.date));
|
holder.time.setText(DateUtil.timeString(notification.date));
|
||||||
if (notification.level == Notification.URGENT)
|
if (notification.level == Notification.URGENT)
|
||||||
holder.cv.setBackgroundColor(ContextCompat.getColor(MainApp.instance(), R.color.notificationUrgent));
|
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());
|
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));
|
nstore.snoozeTo(System.currentTimeMillis() + (SP.getInt("nsalarm_staledatavalue", 15) * 60 * 1000L));
|
||||||
}
|
}
|
||||||
|
if (notification instanceof NotificationWithAction) {
|
||||||
|
((NotificationWithAction) notification).action.run();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ public class NotificationStore {
|
||||||
}
|
}
|
||||||
store.add(n);
|
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);
|
raiseSystemNotification(n);
|
||||||
if (usesChannels && n.soundId != null) {
|
if (usesChannels && n.soundId != null) {
|
||||||
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
|
Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,13 +8,15 @@ import android.os.Build;
|
||||||
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 info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
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 {
|
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_STORAGE = 0x1;
|
||||||
public static final int CASE_SMS = 0x2;
|
public static final int CASE_SMS = 0x2;
|
||||||
public static final int CASE_LOCATION = 0x3;
|
public static final int CASE_LOCATION = 0x3;
|
||||||
|
@ -41,45 +43,52 @@ public class AndroidPermission {
|
||||||
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static synchronized void askForSMSPermissions(Activity activity) {
|
public static synchronized void notifyForSMSPermissions(Activity activity) {
|
||||||
if (askForSMS) { //only when settings were changed an MainActivity resumes.
|
|
||||||
askForSMS = false;
|
|
||||||
if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) {
|
if (SP.getBoolean(R.string.smscommunicator_remotecommandsallowed, false)) {
|
||||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
|
||||||
AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.RECEIVE_SMS,
|
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.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) {
|
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) {
|
public static synchronized void notifyForStoragePermission(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 askForLocationPermissions(Activity activity) {
|
|
||||||
if (askForLocation) { //only when settings were changed an MainActivity resumes.
|
|
||||||
askForLocation = false;
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
AndroidPermission.askForPermission(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
|
if (!checkForPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
|
||||||
Manifest.permission.ACCESS_FINE_LOCATION,
|
NotificationWithAction notification = new NotificationWithAction(Notification.PERMISSION_STORAGE, MainApp.gs(R.string.needstoragepermission), Notification.URGENT);
|
||||||
Manifest.permission.ACCESS_FINE_LOCATION}, AndroidPermission.CASE_LOCATION);
|
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 setAskForSMS() {
|
public static synchronized void notifyForLocationPermissions(Activity activity) {
|
||||||
askForSMS = true;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1084,4 +1084,5 @@
|
||||||
<string name="askforpermission">Ask for permission</string>
|
<string name="askforpermission">Ask for permission</string>
|
||||||
<string name="needlocationpermission">Application needs location permission for BT scan</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="needstoragepermission">Application needs storage permission to be able store log files</string>
|
||||||
|
<string name="request">Request</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Loading…
Reference in a new issue