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