Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.telecomm; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.media.AudioManager; |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 21 | import android.os.SystemVibrator; |
| 22 | import android.os.Vibrator; |
| 23 | import android.provider.Settings; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 24 | import android.telecomm.CallState; |
| 25 | |
| 26 | import com.google.common.collect.Lists; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Controls the ringtone player. |
| 32 | */ |
| 33 | final class Ringer extends CallsManagerListenerBase { |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 34 | private static final long[] VIBRATION_PATTERN = new long[] { |
| 35 | 0, // No delay before starting |
| 36 | 1000, // How long to vibrate |
| 37 | 1000, // How long to wait before vibrating again |
| 38 | }; |
| 39 | |
| 40 | /** Indicate that we want the pattern to repeat at the step which turns on vibration. */ |
| 41 | private static final int VIBRATION_PATTERN_REPEAT = 1; |
| 42 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 43 | private final AsyncRingtonePlayer mRingtonePlayer = new AsyncRingtonePlayer(); |
| 44 | |
| 45 | /** |
| 46 | * Used to keep ordering of unanswered incoming calls. There can easily exist multiple incoming |
| 47 | * calls and explicit ordering is useful for maintaining the proper state of the ringer. |
| 48 | */ |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 49 | private final List<Call> mRingingCalls = Lists.newLinkedList(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 50 | |
| 51 | private final CallAudioManager mCallAudioManager; |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 52 | private final CallsManager mCallsManager; |
| 53 | private final InCallTonePlayer.Factory mPlayerFactory; |
| 54 | private final Context mContext; |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 55 | private final Vibrator mVibrator; |
| 56 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 57 | private InCallTonePlayer mCallWaitingPlayer; |
| 58 | |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Used to track the status of {@link #mVibrator} in the case of simultaneous incoming calls. |
| 61 | */ |
| 62 | private boolean mIsVibrating = false; |
| 63 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 64 | /** Initializes the Ringer. */ |
| 65 | Ringer( |
| 66 | CallAudioManager callAudioManager, |
| 67 | CallsManager callsManager, |
| 68 | InCallTonePlayer.Factory playerFactory, |
| 69 | Context context) { |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 70 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 71 | mCallAudioManager = callAudioManager; |
| 72 | mCallsManager = callsManager; |
| 73 | mPlayerFactory = playerFactory; |
| 74 | mContext = context; |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 75 | // We don't rely on getSystemService(Context.VIBRATOR_SERVICE) to make sure this |
| 76 | // vibrator object will be isolated from others. |
| 77 | mVibrator = new SystemVibrator(TelecommApp.getInstance()); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onCallAdded(Call call) { |
| 82 | if (call.isIncoming() && call.getState() == CallState.RINGING) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 83 | if (mRingingCalls.contains(call)) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 84 | Log.wtf(this, "New ringing call is already in list of unanswered calls"); |
| 85 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 86 | mRingingCalls.add(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 87 | updateRinging(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 91 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 92 | @Override |
| 93 | public void onCallRemoved(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 94 | removeFromUnansweredCall(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
| 99 | if (newState != CallState.RINGING) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 100 | removeFromUnansweredCall(call); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onIncomingCallAnswered(Call call) { |
| 106 | onRespondedToIncomingCall(call); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onIncomingCallRejected(Call call) { |
| 111 | onRespondedToIncomingCall(call); |
| 112 | } |
| 113 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 114 | @Override |
| 115 | public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 116 | if (mRingingCalls.contains(oldForegroundCall) || |
| 117 | mRingingCalls.contains(newForegroundCall)) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 118 | updateRinging(); |
| 119 | } |
| 120 | } |
| 121 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 122 | /** |
| 123 | * Silences the ringer for any actively ringing calls. |
| 124 | */ |
| 125 | void silence() { |
| 126 | // Remove all calls from the "ringing" set and then update the ringer. |
| 127 | mRingingCalls.clear(); |
| 128 | updateRinging(); |
| 129 | } |
| 130 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 131 | private void onRespondedToIncomingCall(Call call) { |
| 132 | // Only stop the ringer if this call is the top-most incoming call. |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 133 | if (getTopMostUnansweredCall() == call) { |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 134 | stopRinging(); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 135 | stopCallWaiting(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 136 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 137 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 138 | // We do not remove the call from mRingingCalls until the call state changes from RINGING |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 139 | // or the call is removed. see onCallStateChanged or onCallRemoved. |
| 140 | } |
| 141 | |
| 142 | private Call getTopMostUnansweredCall() { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 143 | return mRingingCalls.isEmpty() ? null : mRingingCalls.get(0); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Removes the specified call from the list of unanswered incoming calls and updates the ringer |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 148 | * based on the new state of {@link #mRingingCalls}. Safe to call with a call that is not |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 149 | * present in the list of incoming calls. |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 150 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 151 | private void removeFromUnansweredCall(Call call) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 152 | mRingingCalls.remove(call); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 153 | updateRinging(); |
| 154 | } |
| 155 | |
| 156 | private void updateRinging() { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 157 | if (mRingingCalls.isEmpty()) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 158 | stopRinging(); |
| 159 | stopCallWaiting(); |
| 160 | } else { |
| 161 | startRingingOrCallWaiting(); |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 165 | private void startRingingOrCallWaiting() { |
| 166 | Call foregroundCall = mCallsManager.getForegroundCall(); |
| 167 | Log.v(this, "startRingingOrCallWaiting, foregroundCall: %s.", foregroundCall); |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 168 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame^] | 169 | if (mRingingCalls.contains(foregroundCall)) { |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 170 | // The foreground call is one of incoming calls so play the ringer out loud. |
| 171 | stopCallWaiting(); |
| 172 | |
| 173 | AudioManager audioManager = |
| 174 | (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 175 | if (audioManager.getStreamVolume(AudioManager.STREAM_RING) > 0) { |
| 176 | Log.v(this, "startRingingOrCallWaiting"); |
| 177 | mCallAudioManager.setIsRinging(true); |
| 178 | |
| 179 | mRingtonePlayer.play(); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 180 | } else { |
| 181 | Log.v(this, "startRingingOrCallWaiting, skipping because volume is 0"); |
| 182 | } |
Evan Charlton | 2186f7f | 2014-04-10 12:00:37 -0700 | [diff] [blame] | 183 | |
| 184 | if (shouldVibrate(TelecommApp.getInstance()) && !mIsVibrating) { |
| 185 | mVibrator.vibrate(VIBRATION_PATTERN, VIBRATION_PATTERN_REPEAT, |
| 186 | AudioManager.STREAM_RING); |
| 187 | mIsVibrating = true; |
| 188 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 189 | } else { |
| 190 | Log.v(this, "Playing call-waiting tone."); |
| 191 | |
| 192 | // All incoming calls are in background so play call waiting. |
| 193 | stopRinging(); |
| 194 | |
| 195 | if (mCallWaitingPlayer == null) { |
| 196 | mCallWaitingPlayer = |
| 197 | mPlayerFactory.createPlayer(InCallTonePlayer.TONE_CALL_WAITING); |
| 198 | mCallWaitingPlayer.startTone(); |
| 199 | } |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 200 | } |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private void stopRinging() { |
| 204 | Log.v(this, "stopRinging"); |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 205 | |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 206 | mRingtonePlayer.stop(); |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 207 | |
| 208 | if (mIsVibrating) { |
| 209 | mVibrator.cancel(); |
| 210 | mIsVibrating = false; |
| 211 | } |
Santos Cordon | 40f78c2 | 2014-04-07 02:11:42 -0700 | [diff] [blame] | 212 | |
| 213 | // Even though stop is asynchronous it's ok to update the audio manager. Things like audio |
| 214 | // focus are voluntary so releasing focus too early is not detrimental. |
| 215 | mCallAudioManager.setIsRinging(false); |
| 216 | } |
| 217 | |
| 218 | private void stopCallWaiting() { |
| 219 | Log.v(this, "stop call waiting."); |
| 220 | if (mCallWaitingPlayer != null) { |
| 221 | mCallWaitingPlayer.stopTone(); |
| 222 | mCallWaitingPlayer = null; |
| 223 | } |
Evan Charlton | 198fde8 | 2014-04-07 10:53:11 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private boolean shouldVibrate(Context context) { |
| 227 | AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 228 | int ringerMode = audioManager.getRingerMode(); |
| 229 | if (getVibrateWhenRinging(context)) { |
| 230 | return ringerMode != AudioManager.RINGER_MODE_SILENT; |
| 231 | } else { |
| 232 | return ringerMode == AudioManager.RINGER_MODE_VIBRATE; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private boolean getVibrateWhenRinging(Context context) { |
| 237 | if (!mVibrator.hasVibrator()) { |
| 238 | return false; |
| 239 | } |
| 240 | return Settings.System.getInt(context.getContentResolver(), |
| 241 | Settings.System.VIBRATE_WHEN_RINGING, 0) != 0; |
Sailesh Nepal | 6aca10a | 2014-03-24 16:11:02 -0700 | [diff] [blame] | 242 | } |
| 243 | } |