blob: 8fd6697fc053a412e39eed10fa8888673685186f [file] [log] [blame]
Santos Cordona56f2762014-03-24 15:55:53 -07001/*
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
17package com.android.telecomm;
18
19import android.telecomm.CallState;
20
21import 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 */
29class RingbackPlayer extends CallsManagerListenerBase {
30
31 private final CallsManager mCallsManager;
32
33 private final InCallTonePlayer.Factory mPlayerFactory;
34
35 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070036 * The current call for which the ringback tone is being played.
Santos Cordona56f2762014-03-24 15:55:53 -070037 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070038 private Call mCall;
Santos Cordona56f2762014-03-24 15:55:53 -070039
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
Santos Cordona56f2762014-03-24 15:55:53 -070052 public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) {
53 if (oldForegroundCall != null) {
54 stopRingbackForCall(oldForegroundCall);
55 }
56
Evan Charlton893f9e32014-04-09 08:51:52 -070057 if (shouldStartRinging(newForegroundCall)) {
Santos Cordona56f2762014-03-24 15:55:53 -070058 startRingbackForCall(newForegroundCall);
59 }
60 }
61
Evan Charlton893f9e32014-04-09 08:51:52 -070062 @Override
63 public void onCallServiceChanged(
64 Call call,
65 CallServiceWrapper oldCallServiceWrapper,
66 CallServiceWrapper newCallService) {
67
68 super.onCallServiceChanged(call, oldCallServiceWrapper, newCallService);
Ihab Awadcb387ac2014-05-28 16:49:38 -070069 // Treat as ending or begining dialing based on the state transition.
70 if (shouldStartRinging(call)) {
71 startRingbackForCall(call);
72 } else if (newCallService == null) {
73 stopRingbackForCall(call);
74 }
75 }
Evan Charlton893f9e32014-04-09 08:51:52 -070076
Ihab Awadcb387ac2014-05-28 16:49:38 -070077 @Override
78 public void onRequestingRingback(Call call, boolean ignored) {
79 if (shouldStartRinging(call)) {
80 startRingbackForCall(call);
81 } else {
82 stopRingbackForCall(call);
Evan Charlton893f9e32014-04-09 08:51:52 -070083 }
84 }
85
Santos Cordona56f2762014-03-24 15:55:53 -070086 /**
87 * Starts ringback for the specified dialing call as needed.
88 *
89 * @param call The call for which to ringback.
90 */
91 private void startRingbackForCall(Call call) {
92 Preconditions.checkState(call.getState() == CallState.DIALING);
93 ThreadUtil.checkOnMainThread();
94
Evan Charltone519d992014-04-10 09:23:51 -070095 if (mCall == call) {
96 Log.w(this, "Ignoring duplicate requests to ring for %s.", call);
97 return;
98 }
99
Sailesh Nepale59bb192014-04-01 18:33:59 -0700100 if (mCall != null) {
Santos Cordona56f2762014-03-24 15:55:53 -0700101 // We only get here for the foreground call so, there's no reason why there should
Sailesh Nepale59bb192014-04-01 18:33:59 -0700102 // exist a current dialing call.
Santos Cordona56f2762014-03-24 15:55:53 -0700103 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
104 }
105
Sailesh Nepale59bb192014-04-01 18:33:59 -0700106 mCall = call;
Santos Cordona56f2762014-03-24 15:55:53 -0700107 if (mTonePlayer == null) {
Evan Charlton893f9e32014-04-09 08:51:52 -0700108 Log.d(this, "Playing the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -0700109 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
110 mTonePlayer.startTone();
111 }
112 }
113
114 /**
115 * Stops the ringback for the specified dialing call as needed.
116 *
117 * @param call The call for which to stop ringback.
118 */
119 private void stopRingbackForCall(Call call) {
120 ThreadUtil.checkOnMainThread();
121
Sailesh Nepale59bb192014-04-01 18:33:59 -0700122 if (mCall == call) {
Santos Cordona56f2762014-03-24 15:55:53 -0700123 // The foreground call is no longer dialing or is no longer the foreground call. In
124 // either case, stop the ringback tone.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700125 mCall = null;
Santos Cordona56f2762014-03-24 15:55:53 -0700126
127 if (mTonePlayer == null) {
128 Log.w(this, "No player found to stop.");
129 } else {
Evan Charlton893f9e32014-04-09 08:51:52 -0700130 Log.i(this, "Stopping the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -0700131 mTonePlayer.stopTone();
132 mTonePlayer = null;
133 }
134 }
135 }
Evan Charlton893f9e32014-04-09 08:51:52 -0700136
Ihab Awadcb387ac2014-05-28 16:49:38 -0700137 private boolean shouldStartRinging(Call call) {
Evan Charlton893f9e32014-04-09 08:51:52 -0700138 return call != null
Ihab Awadcb387ac2014-05-28 16:49:38 -0700139 && mCallsManager.getForegroundCall() == call
140 && call.getState() == CallState.DIALING
141 && call.isRequestingRingback();
Evan Charlton893f9e32014-04-09 08:51:52 -0700142 }
Santos Cordona56f2762014-03-24 15:55:53 -0700143}