Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 1 | /* |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 2 | * Copyright (C) 2014 The Android Open Source Project |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 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 | * |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 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 |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 14 | * limitations under the License |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 18 | |
Tyler Gunn | 1e0aaa9 | 2018-04-10 09:17:18 -0700 | [diff] [blame] | 19 | import static com.android.server.telecom.LogUtils.Events.START_RINBACK; |
| 20 | import static com.android.server.telecom.LogUtils.Events.STOP_RINGBACK; |
| 21 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 22 | import com.android.internal.util.Preconditions; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 23 | import android.telecom.Log; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * Plays ringback tones. Ringback is different from other tones because it operates as the current |
| 27 | * audio for a call, whereas most tones play as simple timed events. This means ringback must be |
| 28 | * able to turn off and on as the user switches between calls. This is why it is implemented as its |
| 29 | * own class. |
| 30 | */ |
Hall Liu | e792b33 | 2016-04-19 14:23:09 -0700 | [diff] [blame] | 31 | public class RingbackPlayer { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 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 | |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 45 | RingbackPlayer(InCallTonePlayer.Factory playerFactory) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 46 | mPlayerFactory = playerFactory; |
| 47 | } |
| 48 | |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Starts ringback for the specified dialing call as needed. |
| 51 | * |
| 52 | * @param call The call for which to ringback. |
| 53 | */ |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 54 | public void startRingbackForCall(Call call) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 55 | Preconditions.checkState(call.getState() == CallState.DIALING); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 56 | |
Evan Charlton | e519d99 | 2014-04-10 09:23:51 -0700 | [diff] [blame] | 57 | if (mCall == call) { |
| 58 | Log.w(this, "Ignoring duplicate requests to ring for %s.", call); |
| 59 | return; |
| 60 | } |
| 61 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 62 | if (mCall != null) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 63 | // 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] | 64 | // exist a current dialing call. |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 65 | Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls."); |
| 66 | } |
| 67 | |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 68 | mCall = call; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 69 | if (mTonePlayer == null) { |
Tyler Gunn | 1e0aaa9 | 2018-04-10 09:17:18 -0700 | [diff] [blame] | 70 | Log.i(this, "Playing the ringback tone for %s.", call); |
| 71 | Log.addEvent(call, START_RINBACK); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 72 | mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK); |
| 73 | mTonePlayer.startTone(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Stops the ringback for the specified dialing call as needed. |
| 79 | * |
| 80 | * @param call The call for which to stop ringback. |
| 81 | */ |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 82 | public void stopRingbackForCall(Call call) { |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 83 | if (mCall == call) { |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 84 | // The foreground call is no longer dialing or is no longer the foreground call. In |
| 85 | // either case, stop the ringback tone. |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 86 | mCall = null; |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 87 | |
| 88 | if (mTonePlayer == null) { |
| 89 | Log.w(this, "No player found to stop."); |
| 90 | } else { |
Evan Charlton | 893f9e3 | 2014-04-09 08:51:52 -0700 | [diff] [blame] | 91 | Log.i(this, "Stopping the ringback tone for %s.", call); |
Tyler Gunn | 1e0aaa9 | 2018-04-10 09:17:18 -0700 | [diff] [blame] | 92 | Log.addEvent(call, STOP_RINGBACK); |
Santos Cordon | a56f276 | 2014-03-24 15:55:53 -0700 | [diff] [blame] | 93 | mTonePlayer.stopTone(); |
| 94 | mTonePlayer = null; |
| 95 | } |
| 96 | } |
| 97 | } |
Hall Liu | e091ab9 | 2015-12-18 17:05:30 -0800 | [diff] [blame] | 98 | } |