blob: bfaf8a20a406ee5be15e3c9679539a98c52eb0c1 [file] [log] [blame]
Santos Cordona56f2762014-03-24 15:55:53 -07001/*
Hall Liue091ab92015-12-18 17:05:30 -08002 * Copyright (C) 2014 The Android Open Source Project
Santos Cordona56f2762014-03-24 15:55:53 -07003 *
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 Liue091ab92015-12-18 17:05:30 -08008 * http://www.apache.org/licenses/LICENSE-2.0
Santos Cordona56f2762014-03-24 15:55:53 -07009 *
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 Liue091ab92015-12-18 17:05:30 -080014 * limitations under the License
Santos Cordona56f2762014-03-24 15:55:53 -070015 */
16
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Santos Cordona56f2762014-03-24 15:55:53 -070018
Tyler Gunn91d43cf2014-09-17 12:19:39 -070019import com.android.internal.util.Preconditions;
Santos Cordona56f2762014-03-24 15:55:53 -070020
21/**
22 * Plays ringback tones. Ringback is different from other tones because it operates as the current
23 * audio for a call, whereas most tones play as simple timed events. This means ringback must be
24 * able to turn off and on as the user switches between calls. This is why it is implemented as its
25 * own class.
26 */
Hall Liue792b332016-04-19 14:23:09 -070027public class RingbackPlayer {
Santos Cordona56f2762014-03-24 15:55:53 -070028
29 private final InCallTonePlayer.Factory mPlayerFactory;
30
31 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070032 * The current call for which the ringback tone is being played.
Santos Cordona56f2762014-03-24 15:55:53 -070033 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070034 private Call mCall;
Santos Cordona56f2762014-03-24 15:55:53 -070035
36 /**
37 * The currently active player.
38 */
39 private InCallTonePlayer mTonePlayer;
40
Hall Liue091ab92015-12-18 17:05:30 -080041 RingbackPlayer(InCallTonePlayer.Factory playerFactory) {
Santos Cordona56f2762014-03-24 15:55:53 -070042 mPlayerFactory = playerFactory;
43 }
44
Santos Cordona56f2762014-03-24 15:55:53 -070045 /**
46 * Starts ringback for the specified dialing call as needed.
47 *
48 * @param call The call for which to ringback.
49 */
Hall Liue091ab92015-12-18 17:05:30 -080050 public void startRingbackForCall(Call call) {
Santos Cordona56f2762014-03-24 15:55:53 -070051 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordona56f2762014-03-24 15:55:53 -070052
Evan Charltone519d992014-04-10 09:23:51 -070053 if (mCall == call) {
54 Log.w(this, "Ignoring duplicate requests to ring for %s.", call);
55 return;
56 }
57
Sailesh Nepale59bb192014-04-01 18:33:59 -070058 if (mCall != null) {
Santos Cordona56f2762014-03-24 15:55:53 -070059 // We only get here for the foreground call so, there's no reason why there should
Sailesh Nepale59bb192014-04-01 18:33:59 -070060 // exist a current dialing call.
Santos Cordona56f2762014-03-24 15:55:53 -070061 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
62 }
63
Sailesh Nepale59bb192014-04-01 18:33:59 -070064 mCall = call;
Santos Cordona56f2762014-03-24 15:55:53 -070065 if (mTonePlayer == null) {
Evan Charlton893f9e32014-04-09 08:51:52 -070066 Log.d(this, "Playing the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -070067 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
68 mTonePlayer.startTone();
69 }
70 }
71
72 /**
73 * Stops the ringback for the specified dialing call as needed.
74 *
75 * @param call The call for which to stop ringback.
76 */
Hall Liue091ab92015-12-18 17:05:30 -080077 public void stopRingbackForCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -070078 if (mCall == call) {
Santos Cordona56f2762014-03-24 15:55:53 -070079 // The foreground call is no longer dialing or is no longer the foreground call. In
80 // either case, stop the ringback tone.
Sailesh Nepale59bb192014-04-01 18:33:59 -070081 mCall = null;
Santos Cordona56f2762014-03-24 15:55:53 -070082
83 if (mTonePlayer == null) {
84 Log.w(this, "No player found to stop.");
85 } else {
Evan Charlton893f9e32014-04-09 08:51:52 -070086 Log.i(this, "Stopping the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -070087 mTonePlayer.stopTone();
88 mTonePlayer = null;
89 }
90 }
91 }
Hall Liue091ab92015-12-18 17:05:30 -080092}