blob: 47b6dfe264414a6d10851cd34978e72bc94c61f6 [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;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070020import android.telecom.Log;
Santos Cordona56f2762014-03-24 15:55:53 -070021
22/**
23 * Plays ringback tones. Ringback is different from other tones because it operates as the current
24 * audio for a call, whereas most tones play as simple timed events. This means ringback must be
25 * able to turn off and on as the user switches between calls. This is why it is implemented as its
26 * own class.
27 */
Hall Liue792b332016-04-19 14:23:09 -070028public class RingbackPlayer {
Santos Cordona56f2762014-03-24 15:55:53 -070029
30 private final InCallTonePlayer.Factory mPlayerFactory;
31
32 /**
Sailesh Nepale59bb192014-04-01 18:33:59 -070033 * The current call for which the ringback tone is being played.
Santos Cordona56f2762014-03-24 15:55:53 -070034 */
Sailesh Nepale59bb192014-04-01 18:33:59 -070035 private Call mCall;
Santos Cordona56f2762014-03-24 15:55:53 -070036
37 /**
38 * The currently active player.
39 */
40 private InCallTonePlayer mTonePlayer;
41
Hall Liue091ab92015-12-18 17:05:30 -080042 RingbackPlayer(InCallTonePlayer.Factory playerFactory) {
Santos Cordona56f2762014-03-24 15:55:53 -070043 mPlayerFactory = playerFactory;
44 }
45
Santos Cordona56f2762014-03-24 15:55:53 -070046 /**
47 * Starts ringback for the specified dialing call as needed.
48 *
49 * @param call The call for which to ringback.
50 */
Hall Liue091ab92015-12-18 17:05:30 -080051 public void startRingbackForCall(Call call) {
Santos Cordona56f2762014-03-24 15:55:53 -070052 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordona56f2762014-03-24 15:55:53 -070053
Evan Charltone519d992014-04-10 09:23:51 -070054 if (mCall == call) {
55 Log.w(this, "Ignoring duplicate requests to ring for %s.", call);
56 return;
57 }
58
Sailesh Nepale59bb192014-04-01 18:33:59 -070059 if (mCall != null) {
Santos Cordona56f2762014-03-24 15:55:53 -070060 // We only get here for the foreground call so, there's no reason why there should
Sailesh Nepale59bb192014-04-01 18:33:59 -070061 // exist a current dialing call.
Santos Cordona56f2762014-03-24 15:55:53 -070062 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
63 }
64
Sailesh Nepale59bb192014-04-01 18:33:59 -070065 mCall = call;
Santos Cordona56f2762014-03-24 15:55:53 -070066 if (mTonePlayer == null) {
Evan Charlton893f9e32014-04-09 08:51:52 -070067 Log.d(this, "Playing the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -070068 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
69 mTonePlayer.startTone();
70 }
71 }
72
73 /**
74 * Stops the ringback for the specified dialing call as needed.
75 *
76 * @param call The call for which to stop ringback.
77 */
Hall Liue091ab92015-12-18 17:05:30 -080078 public void stopRingbackForCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -070079 if (mCall == call) {
Santos Cordona56f2762014-03-24 15:55:53 -070080 // The foreground call is no longer dialing or is no longer the foreground call. In
81 // either case, stop the ringback tone.
Sailesh Nepale59bb192014-04-01 18:33:59 -070082 mCall = null;
Santos Cordona56f2762014-03-24 15:55:53 -070083
84 if (mTonePlayer == null) {
85 Log.w(this, "No player found to stop.");
86 } else {
Evan Charlton893f9e32014-04-09 08:51:52 -070087 Log.i(this, "Stopping the ringback tone for %s.", call);
Santos Cordona56f2762014-03-24 15:55:53 -070088 mTonePlayer.stopTone();
89 mTonePlayer = null;
90 }
91 }
92 }
Hall Liue091ab92015-12-18 17:05:30 -080093}