PhoneStateBroadcaster.java revision 880b98372368b1d2be2ed34edaeb1e2f338d121a
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 17package com.android.server.telecom; 18 19import android.os.RemoteException; 20import android.os.ServiceManager; 21import android.telephony.TelephonyManager; 22 23import com.android.internal.telephony.ITelephonyRegistry; 24 25/** 26 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state 27 * changes. 28 */ 29final class PhoneStateBroadcaster extends CallsManagerListenerBase { 30 31 private final CallsManager mCallsManager; 32 private final ITelephonyRegistry mRegistry; 33 private int mCurrentState = TelephonyManager.CALL_STATE_IDLE; 34 35 public PhoneStateBroadcaster(CallsManager callsManager) { 36 mCallsManager = callsManager; 37 mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( 38 "telephony.registry")); 39 if (mRegistry == null) { 40 Log.w(this, "TelephonyRegistry is null"); 41 } 42 } 43 44 @Override 45 public void onCallStateChanged(Call call, int oldState, int newState) { 46 updateStates(call); 47 } 48 49 @Override 50 public void onCallAdded(Call call) { 51 updateStates(call); 52 } 53 54 @Override 55 public void onCallRemoved(Call call) { 56 updateStates(call); 57 } 58 59 private void updateStates(Call call) { 60 // Recalculate the current phone state based on the consolidated state of the remaining 61 // calls in the call list. 62 int callState = TelephonyManager.CALL_STATE_IDLE; 63 if (mCallsManager.hasRingingCall()) { 64 callState = TelephonyManager.CALL_STATE_RINGING; 65 } else if (mCallsManager.getFirstCallWithState(CallState.DIALING, CallState.ACTIVE, 66 CallState.ON_HOLD) != null) { 67 callState = TelephonyManager.CALL_STATE_OFFHOOK; 68 } 69 sendPhoneStateChangedBroadcast(call, callState); 70 } 71 72 int getCallState() { 73 return mCurrentState; 74 } 75 76 private void sendPhoneStateChangedBroadcast(Call call, int phoneState) { 77 if (phoneState == mCurrentState) { 78 return; 79 } 80 81 mCurrentState = phoneState; 82 83 String callHandle = null; 84 if (call.getHandle() != null) { 85 callHandle = call.getHandle().getSchemeSpecificPart(); 86 } 87 88 try { 89 if (mRegistry != null) { 90 mRegistry.notifyCallState(phoneState, callHandle); 91 Log.i(this, "Broadcasted state change: %s", mCurrentState); 92 } 93 } catch (RemoteException e) { 94 Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change."); 95 } 96 } 97} 98