blob: a8af3acbd656a5ce72b44f9d7dd4d0d97d540b75 [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 Gunn1e0aaa92018-04-10 09:17:18 -070019import static com.android.server.telecom.LogUtils.Events.START_RINBACK;
20import static com.android.server.telecom.LogUtils.Events.STOP_RINGBACK;
21
Tyler Gunn91d43cf2014-09-17 12:19:39 -070022import com.android.internal.util.Preconditions;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070023import android.telecom.Log;
Santos Cordona56f2762014-03-24 15:55:53 -070024
25/**
26 * Plays ringback tones. Ringback is different from other tones because it operates as the current
27 * audio for a call, whereas most tones play as simple timed events. This means ringback must be
28 * able to turn off and on as the user switches between calls. This is why it is implemented as its
29 * own class.
30 */
Hall Liue792b332016-04-19 14:23:09 -070031public class RingbackPlayer {
Santos Cordona56f2762014-03-24 15:55:53 -070032
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
Hall Liue091ab92015-12-18 17:05:30 -080045 RingbackPlayer(InCallTonePlayer.Factory playerFactory) {
Santos Cordona56f2762014-03-24 15:55:53 -070046 mPlayerFactory = playerFactory;
47 }
48
Santos Cordona56f2762014-03-24 15:55:53 -070049 /**
50 * Starts ringback for the specified dialing call as needed.
51 *
52 * @param call The call for which to ringback.
53 */
Hall Liue091ab92015-12-18 17:05:30 -080054 public void startRingbackForCall(Call call) {
Santos Cordona56f2762014-03-24 15:55:53 -070055 Preconditions.checkState(call.getState() == CallState.DIALING);
Santos Cordona56f2762014-03-24 15:55:53 -070056
Evan Charltone519d992014-04-10 09:23:51 -070057 if (mCall == call) {
58 Log.w(this, "Ignoring duplicate requests to ring for %s.", call);
59 return;
60 }
61
Sailesh Nepale59bb192014-04-01 18:33:59 -070062 if (mCall != null) {
Santos Cordona56f2762014-03-24 15:55:53 -070063 // We only get here for the foreground call so, there's no reason why there should
Sailesh Nepale59bb192014-04-01 18:33:59 -070064 // exist a current dialing call.
Santos Cordona56f2762014-03-24 15:55:53 -070065 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls.");
66 }
67
Sailesh Nepale59bb192014-04-01 18:33:59 -070068 mCall = call;
Santos Cordona56f2762014-03-24 15:55:53 -070069 if (mTonePlayer == null) {
Tyler Gunn1e0aaa92018-04-10 09:17:18 -070070 Log.i(this, "Playing the ringback tone for %s.", call);
71 Log.addEvent(call, START_RINBACK);
Santos Cordona56f2762014-03-24 15:55:53 -070072 mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
73 mTonePlayer.startTone();
74 }
75 }
76
77 /**
78 * Stops the ringback for the specified dialing call as needed.
79 *
80 * @param call The call for which to stop ringback.
81 */
Hall Liue091ab92015-12-18 17:05:30 -080082 public void stopRingbackForCall(Call call) {
Sailesh Nepale59bb192014-04-01 18:33:59 -070083 if (mCall == call) {
Santos Cordona56f2762014-03-24 15:55:53 -070084 // The foreground call is no longer dialing or is no longer the foreground call. In
85 // either case, stop the ringback tone.
Sailesh Nepale59bb192014-04-01 18:33:59 -070086 mCall = null;
Santos Cordona56f2762014-03-24 15:55:53 -070087
88 if (mTonePlayer == null) {
89 Log.w(this, "No player found to stop.");
90 } else {
Evan Charlton893f9e32014-04-09 08:51:52 -070091 Log.i(this, "Stopping the ringback tone for %s.", call);
Tyler Gunn1e0aaa92018-04-10 09:17:18 -070092 Log.addEvent(call, STOP_RINGBACK);
Santos Cordona56f2762014-03-24 15:55:53 -070093 mTonePlayer.stopTone();
94 mTonePlayer = null;
95 }
96 }
97 }
Hall Liue091ab92015-12-18 17:05:30 -080098}