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 android.content.ContentResolver;
20 import android.net.Uri;
21 import android.provider.Settings;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import com.android.incallui.call.CallList;
25 import com.android.incallui.call.state.DialerCallState;
26 import java.util.Objects;
27 
28 /**
29  * Class that determines when ringtones should be played and can play the call waiting tone when
30  * necessary.
31  */
32 public class DialerRingtoneManager {
33 
34   /*
35    * Flag used to determine if the Dialer is responsible for playing ringtones for incoming calls.
36    * Once we're ready to enable Dialer Ringing, these flags should be removed.
37    */
38   private static final boolean IS_DIALER_RINGING_ENABLED = false;
39   private final InCallTonePlayer inCallTonePlayer;
40   private final CallList callList;
41   private Boolean isDialerRingingEnabledForTesting;
42 
43   /**
44    * Creates the DialerRingtoneManager with the given {@link InCallTonePlayer}.
45    *
46    * @param inCallTonePlayer the tone player used to play in-call tones.
47    * @param callList the CallList used to check for {@link DialerCallState#CALL_WAITING}
48    * @throws NullPointerException if inCallTonePlayer or callList are null
49    */
DialerRingtoneManager( @onNull InCallTonePlayer inCallTonePlayer, @NonNull CallList callList)50   public DialerRingtoneManager(
51       @NonNull InCallTonePlayer inCallTonePlayer, @NonNull CallList callList) {
52     this.inCallTonePlayer = Objects.requireNonNull(inCallTonePlayer);
53     this.callList = Objects.requireNonNull(callList);
54   }
55 
56   /**
57    * Determines if a ringtone should be played for the given call state (see {@link
58    * DialerCallState}) and {@link Uri}.
59    *
60    * @param callState the call state for the call being checked.
61    * @param ringtoneUri the ringtone to potentially play.
62    * @return {@code true} if the ringtone should be played, {@code false} otherwise.
63    */
shouldPlayRingtone(int callState, @Nullable Uri ringtoneUri)64   public boolean shouldPlayRingtone(int callState, @Nullable Uri ringtoneUri) {
65     return isDialerRingingEnabled()
66         && translateCallStateForCallWaiting(callState) == DialerCallState.INCOMING
67         && ringtoneUri != null;
68   }
69 
70   /**
71    * Determines if an incoming call should vibrate as well as ring.
72    *
73    * @param resolver {@link ContentResolver} used to look up the {@link
74    *     Settings.System#VIBRATE_WHEN_RINGING} setting.
75    * @return {@code true} if the call should vibrate, {@code false} otherwise.
76    */
shouldVibrate(ContentResolver resolver)77   public boolean shouldVibrate(ContentResolver resolver) {
78     return Settings.System.getInt(resolver, Settings.System.VIBRATE_WHEN_RINGING, 0) != 0;
79   }
80 
81   /**
82    * The incoming callState is never set as {@link DialerCallState#CALL_WAITING} because {@link
83    * DialerCall#translateState(int)} doesn't account for that case, check for it here
84    */
translateCallStateForCallWaiting(int callState)85   private int translateCallStateForCallWaiting(int callState) {
86     if (callState != DialerCallState.INCOMING) {
87       return callState;
88     }
89     return callList.getActiveCall() == null
90         ? DialerCallState.INCOMING
91         : DialerCallState.CALL_WAITING;
92   }
93 
isDialerRingingEnabled()94   private boolean isDialerRingingEnabled() {
95     return isDialerRingingEnabledForTesting != null
96         ? isDialerRingingEnabledForTesting
97         : IS_DIALER_RINGING_ENABLED;
98   }
99 
100   /**
101    * Determines if a call waiting tone should be played for the the given call state (see {@link
102    * DialerCallState}).
103    *
104    * @param callState the call state for the call being checked.
105    * @return {@code true} if the call waiting tone should be played, {@code false} otherwise.
106    */
shouldPlayCallWaitingTone(int callState)107   public boolean shouldPlayCallWaitingTone(int callState) {
108     return isDialerRingingEnabled()
109         && translateCallStateForCallWaiting(callState) == DialerCallState.CALL_WAITING
110         && !inCallTonePlayer.isPlayingTone();
111   }
112 
113   /** Plays the call waiting tone. */
playCallWaitingTone()114   public void playCallWaitingTone() {
115     if (!isDialerRingingEnabled()) {
116       return;
117     }
118     inCallTonePlayer.play(InCallTonePlayer.TONE_CALL_WAITING);
119   }
120 
121   /** Stops playing the call waiting tone. */
stopCallWaitingTone()122   public void stopCallWaitingTone() {
123     if (!isDialerRingingEnabled()) {
124       return;
125     }
126     inCallTonePlayer.stop();
127   }
128 
setDialerRingingEnabledForTesting(boolean status)129   void setDialerRingingEnabledForTesting(boolean status) {
130     isDialerRingingEnabledForTesting = status;
131   }
132 }
133