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