From 399647dde74e8fbf152c98b36657944fd5369eea Mon Sep 17 00:00:00 2001 From: blunden Date: Sun, 18 Mar 2018 14:02:30 +0100 Subject: Add setting to enable Do Not Disturb during calls Android N and earlier enabled Do Not Disturb mode while in a call. Reimplement this behavior to prevent incoming notifications from vibrating or playing sounds. Change-Id: Ic38ac775c6e353898190e1571b6d6e521c0e5aa1 --- .../android/dialer/app/res/values/cm_strings.xml | 4 ++ .../android/dialer/app/res/xml/sound_settings.xml | 5 ++ .../dialer/app/settings/SoundSettingsFragment.java | 35 ++++++++++ java/com/android/incallui/AndroidManifest.xml | 3 + java/com/android/incallui/InCallDndHandler.java | 81 ++++++++++++++++++++++ java/com/android/incallui/InCallPresenter.java | 9 +++ 6 files changed, 137 insertions(+) create mode 100644 java/com/android/incallui/InCallDndHandler.java diff --git a/java/com/android/dialer/app/res/values/cm_strings.xml b/java/com/android/dialer/app/res/values/cm_strings.xml index ba15b469d..0ba0d500a 100644 --- a/java/com/android/dialer/app/res/values/cm_strings.xml +++ b/java/com/android/dialer/app/res/values/cm_strings.xml @@ -18,6 +18,7 @@ dialer_general_incall_category_key In-call + Enable Do Not Disturb during calls incall_vibrate_outgoing Vibrate on answer incall_vibrate_call_waiting @@ -27,4 +28,7 @@ incall_vibrate_45secs Vibrate every minute Vibrates at the 45 second mark of every minute during outgoing calls + In order to enable Do Not Disturb, the Phone app needs to be granted the permission to control the Do Not Disturb status.\nPlease allow it. + Allow + Deny diff --git a/java/com/android/dialer/app/res/xml/sound_settings.xml b/java/com/android/dialer/app/res/xml/sound_settings.xml index e26e51aad..4da5c1514 100644 --- a/java/com/android/dialer/app/res/xml/sound_settings.xml +++ b/java/com/android/dialer/app/res/xml/sound_settings.xml @@ -47,6 +47,11 @@ android:key="@string/incall_category_key" android:title="@string/incall_category_title"> + + diff --git a/java/com/android/dialer/app/settings/SoundSettingsFragment.java b/java/com/android/dialer/app/settings/SoundSettingsFragment.java index 3c254cc4b..d9f24ab7e 100644 --- a/java/com/android/dialer/app/settings/SoundSettingsFragment.java +++ b/java/com/android/dialer/app/settings/SoundSettingsFragment.java @@ -16,7 +16,11 @@ package com.android.dialer.app.settings; +import android.app.AlertDialog; +import android.app.NotificationManager; import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; import android.media.RingtoneManager; import android.os.Build; import android.os.Bundle; @@ -70,6 +74,9 @@ public class SoundSettingsFragment extends PreferenceFragment private SwitchPreference vibrateWhenRinging; private SwitchPreference playDtmfTone; private ListPreference dtmfToneLength; + private SwitchPreference enableDndInCall; + + private NotificationManager notificationManager; @Override public Context getContext() { @@ -92,6 +99,7 @@ public class SoundSettingsFragment extends PreferenceFragment dtmfToneLength = (ListPreference) findPreference(context.getString(R.string.dtmf_tone_length_preference_key)); + enableDndInCall = (SwitchPreference) findPreference("incall_enable_dnd"); if (hasVibrator()) { vibrateWhenRinging.setOnPreferenceChangeListener(this); @@ -116,6 +124,8 @@ public class SoundSettingsFragment extends PreferenceFragment playDtmfTone.setOnPreferenceChangeListener(this); playDtmfTone.setChecked(shouldPlayDtmfTone()); + enableDndInCall.setOnPreferenceChangeListener(this); + TelephonyManager telephonyManager = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager.canChangeDtmfToneLength() @@ -130,6 +140,7 @@ public class SoundSettingsFragment extends PreferenceFragment getPreferenceScreen().removePreference(dtmfToneLength); dtmfToneLength = null; } + notificationManager = context.getSystemService(NotificationManager.class); } @Override @@ -178,6 +189,30 @@ public class SoundSettingsFragment extends PreferenceFragment int index = dtmfToneLength.findIndexOfValue((String) objValue); Settings.System.putInt( getActivity().getContentResolver(), Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index); + } else if (preference == enableDndInCall) { + boolean newValue = (Boolean) objValue; + if (newValue && !notificationManager.isNotificationPolicyAccessGranted()) { + new AlertDialog.Builder(getContext()) + .setMessage(R.string.incall_dnd_dialog_message) + .setPositiveButton(R.string.allow, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS); + startActivity(intent); + } + }) + .setNegativeButton(R.string.deny, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }) + .show(); + + // At this time, it is unknown whether the user granted the permission + return false; + } } return true; } diff --git a/java/com/android/incallui/AndroidManifest.xml b/java/com/android/incallui/AndroidManifest.xml index 4cb600190..8e69c0dd9 100644 --- a/java/com/android/incallui/AndroidManifest.xml +++ b/java/com/android/incallui/AndroidManifest.xml @@ -46,6 +46,9 @@ + + +