1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.incallui.ringtone; 18 19 import com.google.common.base.Preconditions; 20 21 import android.content.ContentResolver; 22 import android.net.Uri; 23 import android.provider.Settings; 24 import android.support.annotation.Nullable; 25 26 import com.android.contacts.common.compat.CompatUtils; 27 import com.android.contacts.common.testing.NeededForTesting; 28 import com.android.incallui.Call; 29 import com.android.incallui.Call.State; 30 import com.android.incallui.CallList; 31 32 /** 33 * Class that determines when ringtones should be played and can play the call waiting tone when 34 * necessary. 35 */ 36 public class DialerRingtoneManager { 37 38 /* 39 * Flag used to determine if the Dialer is responsible for playing ringtones for incoming calls. 40 * Once we're ready to enable Dialer Ringing, these flags should be removed. 41 */ 42 private static final boolean IS_DIALER_RINGING_ENABLED = false; 43 private Boolean mIsDialerRingingEnabledForTesting; 44 45 private final InCallTonePlayer mInCallTonePlayer; 46 private final CallList mCallList; 47 48 /** 49 * Creates the DialerRingtoneManager with the given {@link InCallTonePlayer}. 50 * 51 * @param inCallTonePlayer the tone player used to play in-call tones. 52 * @param callList the CallList used to check for {@link State#CALL_WAITING} 53 * @throws NullPointerException if inCallTonePlayer or callList are null 54 */ DialerRingtoneManager(InCallTonePlayer inCallTonePlayer, CallList callList)55 public DialerRingtoneManager(InCallTonePlayer inCallTonePlayer, CallList callList) { 56 mInCallTonePlayer = Preconditions.checkNotNull(inCallTonePlayer); 57 mCallList = Preconditions.checkNotNull(callList); 58 } 59 60 /** 61 * Determines if a ringtone should be played for the given call state (see {@link State}) and 62 * {@link Uri}. 63 * 64 * @param callState the call state for the call being checked. 65 * @param ringtoneUri the ringtone to potentially play. 66 * @return {@code true} if the ringtone should be played, {@code false} otherwise. 67 */ shouldPlayRingtone(int callState, @Nullable Uri ringtoneUri)68 public boolean shouldPlayRingtone(int callState, @Nullable Uri ringtoneUri) { 69 return isDialerRingingEnabled() 70 && translateCallStateForCallWaiting(callState) == State.INCOMING 71 && ringtoneUri != null; 72 } 73 74 /** 75 * Determines if an incoming call should vibrate as well as ring. 76 * 77 * @param resolver {@link ContentResolver} used to look up the 78 * {@link Settings.System#VIBRATE_WHEN_RINGING} setting. 79 * @return {@code true} if the call should vibrate, {@code false} otherwise. 80 */ shouldVibrate(ContentResolver resolver)81 public boolean shouldVibrate(ContentResolver resolver) { 82 return Settings.System.getInt(resolver, Settings.System.VIBRATE_WHEN_RINGING, 0) != 0; 83 } 84 85 /** 86 * The incoming callState is never set as {@link State#CALL_WAITING} because 87 * {@link Call#translateState(int)} doesn't account for that case, check for it here 88 */ translateCallStateForCallWaiting(int callState)89 private int translateCallStateForCallWaiting(int callState) { 90 if (callState != State.INCOMING) { 91 return callState; 92 } 93 return mCallList.getActiveCall() == null ? State.INCOMING : State.CALL_WAITING; 94 } 95 isDialerRingingEnabled()96 private boolean isDialerRingingEnabled() { 97 if (mIsDialerRingingEnabledForTesting != null) { 98 return mIsDialerRingingEnabledForTesting; 99 } 100 return CompatUtils.isNCompatible() && IS_DIALER_RINGING_ENABLED; 101 } 102 103 /** 104 * Determines if a call waiting tone should be played for the the given call state 105 * (see {@link State}). 106 * 107 * @param callState the call state for the call being checked. 108 * @return {@code true} if the call waiting tone should be played, {@code false} otherwise. 109 */ shouldPlayCallWaitingTone(int callState)110 public boolean shouldPlayCallWaitingTone(int callState) { 111 return isDialerRingingEnabled() 112 && translateCallStateForCallWaiting(callState) == State.CALL_WAITING 113 && !mInCallTonePlayer.isPlayingTone(); 114 } 115 116 /** 117 * Plays the call waiting tone. 118 */ playCallWaitingTone()119 public void playCallWaitingTone() { 120 if (!isDialerRingingEnabled()) { 121 return; 122 } 123 mInCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING); 124 } 125 126 /** 127 * Stops playing the call waiting tone. 128 */ stopCallWaitingTone()129 public void stopCallWaitingTone() { 130 if (!isDialerRingingEnabled()) { 131 return; 132 } 133 mInCallTonePlayer.stop(); 134 } 135 136 @NeededForTesting setDialerRingingEnabledForTesting(boolean status)137 void setDialerRingingEnabledForTesting(boolean status) { 138 mIsDialerRingingEnabledForTesting = status; 139 } 140 } 141