blob: 5e0834f495e01c0180ac0e2fab6d49e6ccd0d7b3 [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 /**
36 * The ID of the current call for which the ringback tone is being played.
37 */
38 private String mCallId;
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.
57 if (newState == CallState.DIALING) {
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
72 if (newForegroundCall != null && newForegroundCall.getState() == CallState.DIALING) {
73 startRingbackForCall(newForegroundCall);
74 }
75 }
76
77 /**
78 * Starts ringback for the specified dialing call as needed.
79 *
80 * @param call The call for which to ringback.
81 */
82 private void startRingbackForCall(Call call) {
83 Preconditions.checkState(call.getState() == CallState.DIALING);
84 ThreadUtil.checkOnMainThread();
85
86 if (mCallId != null) {
87 // We only get here for the foreground call so, there's no reason why there should
88 // exist a current dialing call ID.
89 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
90 }
91
92 mCallId = call.getId();
93 if (mTonePlayer == null) {
94 Log.d(this, "Playing the ringback tone.");
95 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
96 mTonePlayer.startTone();
97 }
98 }
99
100 /**
101 * Stops the ringback for the specified dialing call as needed.
102 *
103 * @param call The call for which to stop ringback.
104 */
105 private void stopRingbackForCall(Call call) {
106 ThreadUtil.checkOnMainThread();
107
108 if (mCallId != null && mCallId.equals(call.getId())) {
109 // The foreground call is no longer dialing or is no longer the foreground call. In
110 // either case, stop the ringback tone.
111 mCallId = null;
112
113 if (mTonePlayer == null) {
114 Log.w(this, "No player found to stop.");
115 } else {
116 Log.i(this, "Stopping the ringback tone.");
117 mTonePlayer.stopTone();
118 mTonePlayer = null;
119 }
120 }
121 }
122}