Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cc25f901 authored by blunden's avatar blunden Committed by Joey Rizzoli
Browse files

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
parent 5f210a08
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <string name="incall_category_key" translatable="false">dialer_general_incall_category_key</string>
    <string name="incall_category_title">In-call</string>
    <string name="incall_enable_dnd_title">Enable Do Not Disturb during calls</string>
    <string name="incall_vibrate_outgoing_key" translatable="false">incall_vibrate_outgoing</string>
    <string name="incall_vibrate_outgoing_title">Vibrate on answer</string>
    <string name="incall_vibrate_call_waiting_key" translatable="false">incall_vibrate_call_waiting</string>
@@ -27,4 +28,7 @@
    <string name="incall_vibrate_45_key" translatable="false">incall_vibrate_45secs</string>
    <string name="incall_vibrate_45_title">Vibrate every minute</string>
    <string name="incall_vibrate_45_summary">Vibrates at the 45 second mark of every minute during outgoing calls</string>
    <string name="incall_dnd_dialog_message">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.</string>
    <string name="allow">Allow</string>
    <string name="deny">Deny</string>
</resources>
+5 −0
Original line number Diff line number Diff line
@@ -47,6 +47,11 @@
    android:key="@string/incall_category_key"
    android:title="@string/incall_category_title">

    <SwitchPreference
        android:defaultValue="false"
        android:key="incall_enable_dnd"
        android:title="@string/incall_enable_dnd_title"/>

    <SwitchPreference
      android:key="@string/incall_vibrate_outgoing_key"
      android:title="@string/incall_vibrate_outgoing_title" />
+42 −0
Original line number Diff line number Diff line
@@ -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;
@@ -71,6 +75,9 @@ public class SoundSettingsFragment extends PreferenceFragment
  private SwitchPreference mVibrateWhenRinging;
  private SwitchPreference mPlayDtmfTone;
  private ListPreference mDtmfToneLength;
  private SwitchPreference mEnableDndInCall;

  private NotificationManager mNotificationManager;

  @Override
  public Context getContext() {
@@ -93,6 +100,7 @@ public class SoundSettingsFragment extends PreferenceFragment
    mDtmfToneLength =
        (ListPreference)
            findPreference(context.getString(R.string.dtmf_tone_length_preference_key));
    mEnableDndInCall = (SwitchPreference) findPreference("incall_enable_dnd");

    if (hasVibrator()) {
      mVibrateWhenRinging.setOnPreferenceChangeListener(this);
@@ -117,6 +125,8 @@ public class SoundSettingsFragment extends PreferenceFragment
    mPlayDtmfTone.setOnPreferenceChangeListener(this);
    mPlayDtmfTone.setChecked(shouldPlayDtmfTone());

    mEnableDndInCall.setOnPreferenceChangeListener(this);

    TelephonyManager telephonyManager =
        (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
    if (SdkVersionOverride.getSdkVersion(Build.VERSION_CODES.M) >= Build.VERSION_CODES.M
@@ -132,6 +142,7 @@ public class SoundSettingsFragment extends PreferenceFragment
      getPreferenceScreen().removePreference(mDtmfToneLength);
      mDtmfToneLength = null;
    }
    mNotificationManager = context.getSystemService(NotificationManager.class);
  }

  @Override
@@ -181,6 +192,37 @@ public class SoundSettingsFragment extends PreferenceFragment
      Settings.System.putInt(
          getActivity().getContentResolver(), Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, index);
    }
    if (preference == mEnableDndInCall) {
      boolean newValue = (Boolean) objValue;
      if (newValue) {
        if (mNotificationManager.isNotificationPolicyAccessGranted()) {
          return true;
        } else {
          AlertDialog dialog = 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();
                }
              })
              .create();
          dialog.show();

          // At this time, it is unknown whether the user granted the permission
          return false;
        }
      }
      return true;
    }
    return true;
  }

+3 −0
Original line number Diff line number Diff line
@@ -37,6 +37,9 @@
  <!-- Testing location -->
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

  <!-- 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 DialtactsActivity instead, e.g. when user unmerge a
  call.
+83 −0
Original line number Diff line number Diff line
/*
 * 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.InCallPresenter.InCallState;

public class InCallDndHandler implements
        InCallPresenter.InCallStateListener {

    private static final String KEY_ENABLE_DND = "incall_enable_dnd";

    private SharedPreferences mPrefs;
    private DialerCall mActiveCall;
    private NotificationManager mNotificationManager;
    private int mUserSelectedDndMode;

    public InCallDndHandler(Context context) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        mNotificationManager = context.getSystemService(NotificationManager.class);

        // Save the user's Do Not Disturb mode so that it can be restored when the call ends
        mUserSelectedDndMode = mNotificationManager.getCurrentInterruptionFilter();
    }

    @Override
    public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
        DialerCall activeCall = callList.getActiveCall();

        if (activeCall != null && mActiveCall == null) {
            Log.d(this, "Transition to active call " + activeCall);
            handleDndState(activeCall);
            mActiveCall = activeCall;
        } else if (activeCall == null && mActiveCall != null) {
            Log.d(this, "Transition from active call " + mActiveCall);
            handleDndState(mActiveCall);
            mActiveCall = null;
        }
    }

    private void handleDndState(DialerCall call) {
        if (DialerCall.State.isConnectingOrConnected(call.getState())) {
            if (mPrefs.getBoolean(KEY_ENABLE_DND, false)) {
                Log.d(this, "Enabling Do Not Disturb mode");
                setDoNotDisturbMode(NotificationManager.INTERRUPTION_FILTER_NONE);
            }
        } else {
            if (mPrefs.getBoolean(KEY_ENABLE_DND, false)) {
                Log.d(this, "Restoring previous Do Not Disturb mode");
                setDoNotDisturbMode(mUserSelectedDndMode);
            }
        }
    }

    private void setDoNotDisturbMode(int newMode) {
        if (mNotificationManager.isNotificationPolicyAccessGranted()) {
            mNotificationManager.setInterruptionFilter(newMode);
        } else {
            Log.e(this, "Failed to set Do Not Disturb mode " + newMode
                + " due to lack of permissions");
        }
    }
}
Loading