1 /* 2 * Copyright (C) 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 17 package com.android.server.telecom; 18 19 import static com.android.server.telecom.LogUtils.Events.START_RINBACK; 20 import static com.android.server.telecom.LogUtils.Events.STOP_RINGBACK; 21 22 import com.android.internal.util.Preconditions; 23 import android.telecom.Log; 24 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 */ 31 public class RingbackPlayer { 32 33 private final InCallTonePlayer.Factory mPlayerFactory; 34 35 /** 36 * The current call for which the ringback tone is being played. 37 */ 38 private Call mCall; 39 40 /** 41 * The currently active player. 42 */ 43 private InCallTonePlayer mTonePlayer; 44 RingbackPlayer(InCallTonePlayer.Factory playerFactory)45 RingbackPlayer(InCallTonePlayer.Factory playerFactory) { 46 mPlayerFactory = playerFactory; 47 } 48 49 /** 50 * Starts ringback for the specified dialing call as needed. 51 * 52 * @param call The call for which to ringback. 53 */ startRingbackForCall(Call call)54 public void startRingbackForCall(Call call) { 55 Preconditions.checkState(call.getState() == CallState.DIALING); 56 57 if (mCall == call) { 58 Log.w(this, "Ignoring duplicate requests to ring for %s.", call); 59 return; 60 } 61 62 if (mCall != null) { 63 // We only get here for the foreground call so, there's no reason why there should 64 // exist a current dialing call. 65 Log.wtf(this, "Ringback player thinks there are two foreground-dialing calls."); 66 } 67 68 mCall = call; 69 if (mTonePlayer == null) { 70 Log.i(this, "Playing the ringback tone for %s.", call); 71 Log.addEvent(call, START_RINBACK); 72 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 */ stopRingbackForCall(Call call)82 public void stopRingbackForCall(Call call) { 83 if (mCall == call) { 84 // The foreground call is no longer dialing or is no longer the foreground call. In 85 // either case, stop the ringback tone. 86 mCall = null; 87 88 if (mTonePlayer == null) { 89 Log.w(this, "No player found to stop."); 90 } else { 91 Log.i(this, "Stopping the ringback tone for %s.", call); 92 Log.addEvent(call, STOP_RINGBACK); 93 mTonePlayer.stopTone(); 94 mTonePlayer = null; 95 } 96 } 97 } 98 }