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