blob: bfd5f62c657044ea1d2ebee5c678af3770074029 [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
Sailesh Nepalc92c4362014-07-04 18:33:21 -070063 public void onConnectionServiceChanged(
Evan Charlton893f9e32014-04-09 08:51:52 -070064 Call call,
Sailesh Nepalc92c4362014-07-04 18:33:21 -070065 ConnectionServiceWrapper oldService,
66 ConnectionServiceWrapper newService) {
Evan Charlton893f9e32014-04-09 08:51:52 -070067
Ihab Awadcb387ac2014-05-28 16:49:38 -070068 // Treat as ending or begining dialing based on the state transition.
69 if (shouldStartRinging(call)) {
70 startRingbackForCall(call);
Sailesh Nepalc92c4362014-07-04 18:33:21 -070071 } else if (newService == null) {
Ihab Awadcb387ac2014-05-28 16:49:38 -070072 stopRingbackForCall(call);
73 }
74 }
Evan Charlton893f9e32014-04-09 08:51:52 -070075
Ihab Awadcb387ac2014-05-28 16:49:38 -070076 @Override
77 public void onRequestingRingback(Call call, boolean ignored) {
78 if (shouldStartRinging(call)) {
79 startRingbackForCall(call);
80 } else {
81 stopRingbackForCall(call);
Evan Charlton893f9e32014-04-09 08:51:52 -070082 }
83 }
84
Santos Cordona56f2762014-03-24 15:55:53 -070085 /**
86 * Starts ringback for the specified dialing call as needed.
87 *
88 * @param call The call for which to ringback.
89 */
90 private void startRingbackForCall(Call call) {
91 Preconditions.checkState(call.getState() == CallState.DIALING);
92 ThreadUtil.checkOnMainThread();
93
Evan Charltone519d992014-04-10 09:23:51 -070094 if (mCall == call) {
95 Log.w(this, "Ignoring duplicate requests to ring for %s.", call);
96 return;
97 }
98
Sailesh Nepale59bb192014-04-01 18:33:59 -070099 if (mCall != null) {
Santos Cordona56f2762014-03-24 15:55:53 -0700100 // We only get here for the foreground call so, there's no reason why there should
Sailesh Nepale59bb192014-04-01 18:33:59 -0700101 // exist a current dialing call.
Santos Cordona56f2762014-03-24 15:55:53 -0700102 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
103 }
104
Sailesh Nepale59bb192014-04-01 18:33:59 -0700105 mCall = call;
Santos Cordona56f2762014-03-24 15:55:53 -0700106 if (mTonePlayer == null) {
Evan Charlton893f9e32014-04-09 08:51:52 -0700107 Log.d(this, "Playing the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -0700108 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
109 mTonePlayer.startTone();
110 }
111 }
112
113 /**
114 * Stops the ringback for the specified dialing call as needed.
115 *
116 * @param call The call for which to stop ringback.
117 */
118 private void stopRingbackForCall(Call call) {
119 ThreadUtil.checkOnMainThread();
120
Sailesh Nepale59bb192014-04-01 18:33:59 -0700121 if (mCall == call) {
Santos Cordona56f2762014-03-24 15:55:53 -0700122 // The foreground call is no longer dialing or is no longer the foreground call. In
123 // either case, stop the ringback tone.
Sailesh Nepale59bb192014-04-01 18:33:59 -0700124 mCall = null;
Santos Cordona56f2762014-03-24 15:55:53 -0700125
126 if (mTonePlayer == null) {
127 Log.w(this, "No player found to stop.");
128 } else {
Evan Charlton893f9e32014-04-09 08:51:52 -0700129 Log.i(this, "Stopping the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -0700130 mTonePlayer.stopTone();
131 mTonePlayer = null;
132 }
133 }
134 }
Evan Charlton893f9e32014-04-09 08:51:52 -0700135
Ihab Awadcb387ac2014-05-28 16:49:38 -0700136 private boolean shouldStartRinging(Call call) {
Evan Charlton893f9e32014-04-09 08:51:52 -0700137 return call != null
Ihab Awadcb387ac2014-05-28 16:49:38 -0700138 && mCallsManager.getForegroundCall() == call
139 && call.getState() == CallState.DIALING
140 && call.isRequestingRingback();
Evan Charlton893f9e32014-04-09 08:51:52 -0700141 }
Santos Cordona56f2762014-03-24 15:55:53 -0700142}