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