diff options
author | blunden <blunden2@gmail.com> | 2018-03-18 14:02:30 +0100 |
---|---|---|
committer | Michael Bestas <mkbestas@lineageos.org> | 2020-12-12 01:23:35 +0200 |
commit | 399647dde74e8fbf152c98b36657944fd5369eea (patch) | |
tree | fa8b303475f1e0259e48a6282e47ea50d8c01379 /java/com/android/incallui | |
parent | 25237ec70323d72c299dd984117031de432b8e42 (diff) |
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
Diffstat (limited to 'java/com/android/incallui')
-rw-r--r-- | java/com/android/incallui/AndroidManifest.xml | 3 | ||||
-rw-r--r-- | java/com/android/incallui/InCallDndHandler.java | 81 | ||||
-rw-r--r-- | java/com/android/incallui/InCallPresenter.java | 9 |
3 files changed, 93 insertions, 0 deletions
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 @@ <!-- Set audio selector window type TYPE_APPLICATION_OVERLAY --> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> + <!-- Required when the "Enable Do Not Disturb during call" setting is enabled --> + <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" /> + <!-- Set android:taskAffinity="com.android.incallui" for all activities to ensure proper navigation. Otherwise system could bring up MainActivity instead, e.g. when user unmerge a call. diff --git a/java/com/android/incallui/InCallDndHandler.java b/java/com/android/incallui/InCallDndHandler.java new file mode 100644 index 000000000..4ab3e840a --- /dev/null +++ b/java/com/android/incallui/InCallDndHandler.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2018 The LineageOS Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.incallui; + +import android.app.NotificationManager; +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import com.android.incallui.call.CallList; +import com.android.incallui.call.DialerCall; +import com.android.incallui.call.state.DialerCallState; +import com.android.incallui.InCallPresenter.InCallState; + +public class InCallDndHandler implements InCallPresenter.InCallStateListener { + + private static final String KEY_ENABLE_DND = "incall_enable_dnd"; + + private SharedPreferences prefs; + private DialerCall activeCall; + private NotificationManager notificationManager; + private int userSelectedDndMode; + + public InCallDndHandler(Context context) { + prefs = PreferenceManager.getDefaultSharedPreferences(context); + notificationManager = context.getSystemService(NotificationManager.class); + + // Save the user's Do Not Disturb mode so that it can be restored when the call ends + userSelectedDndMode = notificationManager.getCurrentInterruptionFilter(); + } + + @Override + public void onStateChange(InCallState oldState, InCallState newState, CallList callList) { + DialerCall activeCall = callList.getActiveCall(); + + if (activeCall != null && this.activeCall == null) { + Log.d(this, "Transition to active call " + activeCall); + handleDndState(activeCall); + this.activeCall = activeCall; + } else if (activeCall == null && this.activeCall != null) { + Log.d(this, "Transition from active call " + this.activeCall); + handleDndState(this.activeCall); + this.activeCall = null; + } + } + + private void handleDndState(DialerCall call) { + if (!prefs.getBoolean(KEY_ENABLE_DND, false)) { + return; + } + if (DialerCallState.isConnectingOrConnected(call.getState())) { + Log.d(this, "Enabling Do Not Disturb mode"); + setDoNotDisturbMode(NotificationManager.INTERRUPTION_FILTER_NONE); + } else { + Log.d(this, "Restoring previous Do Not Disturb mode"); + setDoNotDisturbMode(userSelectedDndMode); + } + } + + private void setDoNotDisturbMode(int newMode) { + if (notificationManager.isNotificationPolicyAccessGranted()) { + notificationManager.setInterruptionFilter(newMode); + } else { + Log.e(this, "Failed to set Do Not Disturb mode " + newMode + " due to lack of permissions"); + } + } +} diff --git a/java/com/android/incallui/InCallPresenter.java b/java/com/android/incallui/InCallPresenter.java index 1bb606ea0..8881029cc 100644 --- a/java/com/android/incallui/InCallPresenter.java +++ b/java/com/android/incallui/InCallPresenter.java @@ -128,6 +128,7 @@ public class InCallPresenter implements CallList.Listener, AudioModeProvider.Aud private StatusBarNotifier statusBarNotifier; private ExternalCallNotifier externalCallNotifier; private InCallVibrationHandler vibrationHandler; + private InCallDndHandler dndHandler; private ContactInfoCache contactInfoCache; private Context context; private final OnCheckBlockedListener onCheckBlockedListener = @@ -368,6 +369,9 @@ public class InCallPresenter implements CallList.Listener, AudioModeProvider.Aud vibrationHandler = new InCallVibrationHandler(context); addListener(vibrationHandler); + dndHandler = new InCallDndHandler(context); + addListener(dndHandler); + this.proximitySensor = proximitySensor; addListener(this.proximitySensor); @@ -1662,6 +1666,11 @@ public class InCallPresenter implements CallList.Listener, AudioModeProvider.Aud } vibrationHandler = null; + if (dndHandler != null) { + removeListener(dndHandler); + } + dndHandler = null; + if (callList != null) { callList.removeListener(this); callList.removeListener(spamCallListListener); |