From e3598cc4755a00972bae5bf4043864f1ee35d889 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 26 Sep 2021 19:54:17 +0200 Subject: [PATCH] DND --- .../java/com/jens/automation2/Actions.java | 14 ++++- .../automation2/ActivityManageProfile.java | 53 ++++++++++++++----- .../com/jens/automation2/Miscellaneous.java | 4 +- .../java/com/jens/automation2/Profile.java | 26 +++++++++ .../jens/automation2/XmlFileInterface.java | 13 ++++- 5 files changed, 92 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index c3b83b7..762bb06 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -621,7 +621,19 @@ public class Actions } } - public void useDownloadedWebpage(String result) + public static void setDND(Context context, int desiredDndMode) + { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + { + Miscellaneous.logEvent("i", context.getResources().getString(R.string.soundSettings), "Changing DND to " + String.valueOf(desiredDndMode), 4); + NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager.setInterruptionFilter(desiredDndMode); + } + else + Miscellaneous.logEvent("w", context.getResources().getString(R.string.soundSettings), "Cannot change DND to " + String.valueOf(desiredDndMode) + ". This Android version is too and doesn\'t have that feature, yet.", 4); + } + + public void useDownloadedWebpage(String result) { // Toast.makeText(context, "Result: " + result, Toast.LENGTH_LONG).show(); } diff --git a/app/src/main/java/com/jens/automation2/ActivityManageProfile.java b/app/src/main/java/com/jens/automation2/ActivityManageProfile.java index cb1489e..f81ac10 100644 --- a/app/src/main/java/com/jens/automation2/ActivityManageProfile.java +++ b/app/src/main/java/com/jens/automation2/ActivityManageProfile.java @@ -127,6 +127,13 @@ public class ActivityManageProfile extends Activity spinnerSoundMode.setSelection(0); spinnerDndMode.setSelection(0); + + if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + { + // Disable DND controls + checkBoxChangeDnd.setEnabled(false); + spinnerDndMode.setEnabled(false); + } // Scale SeekBars to the system's maximum volume values AudioManager am = (AudioManager) Miscellaneous.getAnyContext().getSystemService(Context.AUDIO_SERVICE); @@ -134,17 +141,29 @@ public class ActivityManageProfile extends Activity seekBarVolumeNotifications.setMax(am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION)); seekBarVolumeAlarms.setMax(am.getStreamMaxVolume(AudioManager.STREAM_ALARM)); - soundModeAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, new String[] { getResources().getString(R.string.soundModeSilent), getResources().getString(R.string.soundModeVibrate), getResources().getString(R.string.soundModeNormal) }); + soundModeAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, new String[] + { + getResources().getString(R.string.soundModeSilent), + getResources().getString(R.string.soundModeVibrate), + getResources().getString(R.string.soundModeNormal) + }); spinnerSoundMode.setAdapter(soundModeAdapter); - dndModeAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, new String[] { getResources().getString(R.string.dndOff), getResources().getString(R.string.dndPriority), getResources().getString(R.string.dndAlarms), getResources().getString(R.string.dndNothing) }); + dndModeAdapter = new ArrayAdapter(this, R.layout.text_view_for_poi_listview_mediumtextsize, new String[] + { + getResources().getString(R.string.dndOff), + getResources().getString(R.string.dndPriority), + getResources().getString(R.string.dndNothing), + getResources().getString(R.string.dndAlarms) + }); spinnerDndMode.setAdapter(dndModeAdapter); /* + Order in spinner: 1, 2, 4, 3 NotificationManager.INTERRUPTION_FILTER_UNKNOWN -> Returned when the value is unavailable for any reason. - NotificationManager.INTERRUPTION_FILTER_ALL -> Normal interruption filter - no notifications are suppressed. -> essentially turn off DND - NotificationManager.INTERRUPTION_FILTER_PRIORITY -> Priority interruption filter - all notifications are suppressed except those that match the priority criteria. - NotificationManager.INTERRUPTION_FILTER_ALARMS -> Alarms only interruption filter - all notifications except those of category - NotificationManager.INTERRUPTION_FILTER_NONE -> No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted. + NotificationManager.INTERRUPTION_FILTER_ALL -> 1 -> Normal interruption filter - no notifications are suppressed. -> essentially turn off DND + NotificationManager.INTERRUPTION_FILTER_PRIORITY -> 2 -> Priority interruption filter - all notifications are suppressed except those that match the priority criteria. + NotificationManager.INTERRUPTION_FILTER_ALARMS -> 4 -> Alarms only interruption filter - all notifications except those of category + NotificationManager.INTERRUPTION_FILTER_NONE -> 3 -> No interruptions filter - all notifications are suppressed and all audio streams (except those used for phone calls) and vibrations are muted. */ checkBoxChangeSoundMode.setOnCheckedChangeListener(new OnCheckedChangeListener() @@ -351,6 +370,7 @@ public class ActivityManageProfile extends Activity { etName.setText(ActivityMainProfiles.profileToEdit.getName()); checkBoxChangeSoundMode.setChecked(ActivityMainProfiles.profileToEdit.getChangeSoundMode()); + checkBoxChangeDnd.setChecked(ActivityMainProfiles.profileToEdit.getChangeDndMode()); checkBoxChangeVolumeMusicVideoGameMedia.setChecked(ActivityMainProfiles.profileToEdit.getChangeVolumeMusicVideoGameMedia()); checkBoxChangeVolumeNotifications.setChecked(ActivityMainProfiles.profileToEdit.getChangeVolumeNotifications()); checkBoxChangeVolumeAlarms.setChecked(ActivityMainProfiles.profileToEdit.getChangeVolumeAlarms()); @@ -362,6 +382,7 @@ public class ActivityManageProfile extends Activity checkBoxChangeVibrateWhenRinging.setChecked(ActivityMainProfiles.profileToEdit.getChangeVibrateWhenRinging()); spinnerSoundMode.setSelection(ActivityMainProfiles.profileToEdit.getSoundMode()); + spinnerDndMode.setSelection(ActivityMainProfiles.profileToEdit.getDndMode()-1); seekBarVolumeMusic.setProgress(ActivityMainProfiles.profileToEdit.getVolumeMusic()); seekBarVolumeNotifications.setProgress(ActivityMainProfiles.profileToEdit.getVolumeNotifications()); seekBarVolumeAlarms.setProgress(ActivityMainProfiles.profileToEdit.getVolumeAlarms()); @@ -383,6 +404,7 @@ public class ActivityManageProfile extends Activity ActivityMainProfiles.profileToEdit.setName(etName.getText().toString()); ActivityMainProfiles.profileToEdit.setChangeSoundMode(checkBoxChangeSoundMode.isChecked()); + ActivityMainProfiles.profileToEdit.setChangeDndMode(checkBoxChangeDnd.isChecked()); ActivityMainProfiles.profileToEdit.setChangeVolumeMusicVideoGameMedia(checkBoxChangeVolumeMusicVideoGameMedia.isChecked()); ActivityMainProfiles.profileToEdit.setChangeVolumeNotifications(checkBoxChangeVolumeNotifications.isChecked()); ActivityMainProfiles.profileToEdit.setChangeVolumeAlarms(checkBoxChangeVolumeAlarms.isChecked()); @@ -398,6 +420,7 @@ public class ActivityManageProfile extends Activity ActivityMainProfiles.profileToEdit.setHapticFeedback(checkBoxHapticFeedback.isChecked()); ActivityMainProfiles.profileToEdit.setVibrateWhenRinging(checkBoxVibrateWhenRinging.isChecked()); ActivityMainProfiles.profileToEdit.setSoundMode(spinnerSoundMode.getSelectedItemPosition()); + ActivityMainProfiles.profileToEdit.setDndMode(spinnerDndMode.getSelectedItemPosition()+1); ActivityMainProfiles.profileToEdit.setVolumeMusic(seekBarVolumeMusic.getProgress()); ActivityMainProfiles.profileToEdit.setVolumeNotifications(seekBarVolumeNotifications.getProgress()); ActivityMainProfiles.profileToEdit.setVolumeAlarms(seekBarVolumeAlarms.getProgress()); @@ -425,21 +448,23 @@ public class ActivityManageProfile extends Activity } if(!checkBoxChangeSoundMode.isChecked() - & + && + !checkBoxChangeDnd.isChecked() + && !checkBoxChangeVolumeMusicVideoGameMedia.isChecked() - & + && !checkBoxChangeVolumeNotifications.isChecked() - & + && !checkBoxChangeVolumeAlarms.isChecked() - & + && !checkBoxChangeIncomingCallsRingtone.isChecked() - & + && !checkBoxChangeNotificationRingtone.isChecked() - & + && !checkBoxChangeAudibleSelection.isChecked() - & + && !checkBoxChangeScreenLockUnlockSound.isChecked() - & + && !checkBoxChangeHapticFeedback.isChecked() ) { diff --git a/app/src/main/java/com/jens/automation2/Miscellaneous.java b/app/src/main/java/com/jens/automation2/Miscellaneous.java index 7c1519b..e077ab5 100644 --- a/app/src/main/java/com/jens/automation2/Miscellaneous.java +++ b/app/src/main/java/com/jens/automation2/Miscellaneous.java @@ -698,8 +698,8 @@ public class Miscellaneous extends Service */ public static boolean isPhoneRooted() { - if(true) - return true; +// if(true) +// return true; // get from build info String buildTags = Build.TAGS; diff --git a/app/src/main/java/com/jens/automation2/Profile.java b/app/src/main/java/com/jens/automation2/Profile.java index 821c390..d39b8ef 100644 --- a/app/src/main/java/com/jens/automation2/Profile.java +++ b/app/src/main/java/com/jens/automation2/Profile.java @@ -24,6 +24,9 @@ public class Profile implements Comparable protected boolean changeSoundMode; protected int soundMode; + protected boolean changeDndMode; + protected int dndMode; + boolean changeVolumeMusicVideoGameMedia; protected int volumeMusic; @@ -81,6 +84,26 @@ public class Profile implements Comparable return soundMode; } + public boolean getChangeDndMode() + { + return changeDndMode; + } + + public void setChangeDndMode(boolean changeDndMode) + { + this.changeDndMode = changeDndMode; + } + + public int getDndMode() + { + return dndMode; + } + + public void setDndMode(int dndMode) + { + this.dndMode = dndMode; + } + public void setChangeVolumeMusicVideoGameMedia(boolean changeVolumeMusicVideoGameMedia) { this.changeVolumeMusicVideoGameMedia = changeVolumeMusicVideoGameMedia; @@ -449,6 +472,9 @@ public class Profile implements Comparable if(changeSoundMode) Actions.setSound(context, soundMode); + + if(changeDndMode) + Actions.setDND(context, dndMode); if(changeVolumeMusicVideoGameMedia) am.setStreamVolume(AudioManager.STREAM_MUSIC, volumeMusic, AudioManager.FLAG_PLAY_SOUND); diff --git a/app/src/main/java/com/jens/automation2/XmlFileInterface.java b/app/src/main/java/com/jens/automation2/XmlFileInterface.java index d503293..7a1ef1a 100644 --- a/app/src/main/java/com/jens/automation2/XmlFileInterface.java +++ b/app/src/main/java/com/jens/automation2/XmlFileInterface.java @@ -204,7 +204,14 @@ public class XmlFileInterface serializer.endTag(null, "changeHapticFeedback");// serializer.startTag(null, "hapticFeedback"); serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getHapticFeedback())); - serializer.endTag(null, "hapticFeedback"); + serializer.endTag(null, "hapticFeedback"); + + serializer.startTag(null, "changeDndMode"); + serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getChangeDndMode())); + serializer.endTag(null, "changeDndMode");// + serializer.startTag(null, "dndMode"); + serializer.text(String.valueOf(Profile.getProfileCollection().get(i).getDndMode())); + serializer.endTag(null, "dndMode"); serializer.endTag(null, "Profile"); } @@ -607,6 +614,10 @@ public class XmlFileInterface newProfile.setChangeSoundMode(Boolean.parseBoolean(readTag(parser, "changeSoundMode"))); else if (name.equals("soundMode")) newProfile.setSoundMode(Integer.parseInt(readTag(parser, "soundMode"))); + else if (name.equals("changeDndMode")) + newProfile.setChangeDndMode(Boolean.parseBoolean(readTag(parser, "changeDndMode"))); + else if (name.equals("dndMode")) + newProfile.setDndMode(Integer.parseInt(readTag(parser, "dndMode"))); else if (name.equals("changeVolumeMusicVideoGameMedia")) newProfile.setChangeVolumeMusicVideoGameMedia(Boolean.parseBoolean(readTag(parser, "changeVolumeMusicVideoGameMedia"))); else if (name.equals("volumeMusic"))