Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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.telecomm.CallState; |
| 20 | |
| 21 | import com.google.common.base.Preconditions; |
| 22 | |
| 23 | /** |
| 24 | * Plays ringback tones. Ringback is different from other tones because it operates as the current |
| 25 | * audio for a call, whereas most tones play as simple timed events. This means ringback must be |
| 26 | * able to turn off and on as the user switches between calls. This is why it is implemented as its |
| 27 | * own class. |
| 28 | */ |
| 29 | class RingbackPlayer extends CallsManagerListenerBase { |
| 30 | |
| 31 | private final CallsManager mCallsManager; |
| 32 | |
| 33 | private final InCallTonePlayer.Factory mPlayerFactory; |
| 34 | |
| 35 | /** |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 36 | * The current call for which the ringback tone is being played. |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 37 | */ |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 38 | private Call mCall; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * The currently active player. |
| 42 | */ |
| 43 | private InCallTonePlayer mTonePlayer; |
| 44 | |
| 45 | RingbackPlayer(CallsManager callsManager, InCallTonePlayer.Factory playerFactory) { |
| 46 | mCallsManager = callsManager; |
| 47 | mPlayerFactory = playerFactory; |
| 48 | } |
| 49 | |
| 50 | /** {@inheritDoc} */ |
| 51 | @Override |
| 52 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
| 53 | // Only operate on the foreground call. |
| 54 | if (mCallsManager.getForegroundCall() == call) { |
| 55 | |
| 56 | // Treat as ending or begining dialing based on the state transition. |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 57 | if (shouldStartRinging(call)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 58 | startRingbackForCall(call); |
| 59 | } else if (oldState == CallState.DIALING) { |
| 60 | stopRingbackForCall(call); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** {@inheritDoc} */ |
| 66 | @Override |
| 67 | public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) { |
| 68 | if (oldForegroundCall != null) { |
| 69 | stopRingbackForCall(oldForegroundCall); |
| 70 | } |
| 71 | |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 72 | if (shouldStartRinging(newForegroundCall)) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 73 | startRingbackForCall(newForegroundCall); |
| 74 | } |
| 75 | } |
| 76 | |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public void onCallServiceChanged( |
| 79 | Call call, |
| 80 | CallServiceWrapper oldCallServiceWrapper, |
| 81 | CallServiceWrapper newCallService) { |
| 82 | |
| 83 | super.onCallServiceChanged(call, oldCallServiceWrapper, newCallService); |
| 84 | // Only operate on the foreground call. |
| 85 | if (mCallsManager.getForegroundCall() == call) { |
| 86 | |
| 87 | // Treat as ending or begining dialing based on the state transition. |
| 88 | if (shouldStartRinging(call)) { |
| 89 | startRingbackForCall(call); |
| 90 | } else if (newCallService == null) { |
| 91 | stopRingbackForCall(call); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Starts ringback for the specified dialing call as needed. |
| 98 | * |
| 99 | * @param call The call for which to ringback. |
| 100 | */ |
| 101 | private void startRingbackForCall(Call call) { |
| 102 | Preconditions.checkState(call.getState() == CallState.DIALING); |
| 103 | ThreadUtil.checkOnMainThread(); |
| 104 | |
Evan Charlton | e519d99 | 2014-04-10 09:23:51 -0700 | [diff] [blame] | 105 | if (mCall == call) { |
| 106 | Log.w(this, "Ignoring duplicate requests to ring for %s.", call); |
| 107 | return; |
| 108 | } |
| 109 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 110 | if (mCall != null) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 111 | // We only get here for the foreground call so, there's no reason why there should |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 112 | // exist a current dialing call. |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 113 | Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls."); |
| 114 | } |
| 115 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 116 | mCall = call; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 117 | if (mTonePlayer == null) { |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 118 | Log.d(this, "Playing the ringback tone for %s.", call); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 119 | mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK); |
| 120 | mTonePlayer.startTone(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Stops the ringback for the specified dialing call as needed. |
| 126 | * |
| 127 | * @param call The call for which to stop ringback. |
| 128 | */ |
| 129 | private void stopRingbackForCall(Call call) { |
| 130 | ThreadUtil.checkOnMainThread(); |
| 131 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 132 | if (mCall == call) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 133 | // The foreground call is no longer dialing or is no longer the foreground call. In |
| 134 | // either case, stop the ringback tone. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 135 | mCall = null; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 136 | |
| 137 | if (mTonePlayer == null) { |
| 138 | Log.w(this, "No player found to stop."); |
| 139 | } else { |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 140 | Log.i(this, "Stopping the ringback tone for %s.", call); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 141 | mTonePlayer.stopTone(); |
| 142 | mTonePlayer = null; |
| 143 | } |
| 144 | } |
| 145 | } |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 146 | |
| 147 | private static boolean shouldStartRinging(Call call) { |
| 148 | return call != null |
Evan Charlton | e519d99 | 2014-04-10 09:23:51 -0700 | [diff] [blame] | 149 | && call.getState() == CallState.DIALING; |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 150 | } |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 151 | } |