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 android.os.RemoteException;
20 import android.os.ServiceManager;
21 import android.telephony.TelephonyManager;
22 
23 import com.android.internal.telephony.ITelephonyRegistry;
24 
25 /**
26  * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
27  * changes.
28  */
29 final class PhoneStateBroadcaster extends CallsManagerListenerBase {
30 
31     private final CallsManager mCallsManager;
32     private final ITelephonyRegistry mRegistry;
33     private int mCurrentState = TelephonyManager.CALL_STATE_IDLE;
34 
PhoneStateBroadcaster(CallsManager callsManager)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
onCallStateChanged(Call call, int oldState, int newState)45     public void onCallStateChanged(Call call, int oldState, int newState) {
46         updateStates(call);
47     }
48 
49     @Override
onCallAdded(Call call)50     public void onCallAdded(Call call) {
51         if (call.isExternalCall()) {
52             return;
53         }
54         updateStates(call);
55     }
56 
57     @Override
onCallRemoved(Call call)58     public void onCallRemoved(Call call) {
59         if (call.isExternalCall()) {
60             return;
61         }
62         updateStates(call);
63     }
64 
65     /**
66      * Handles changes to a call's external property.  If the call becomes external, we end up
67      * updating the call state to idle.  If the call becomes non-external, then the call state can
68      * update to off hook.
69      *
70      * @param call The call.
71      * @param isExternalCall {@code True} if the call is external, {@code false} otherwise.
72      */
73     @Override
onExternalCallChanged(Call call, boolean isExternalCall)74     public void onExternalCallChanged(Call call, boolean isExternalCall) {
75         updateStates(call);
76     }
77 
updateStates(Call call)78     private void updateStates(Call call) {
79         // Recalculate the current phone state based on the consolidated state of the remaining
80         // calls in the call list.
81         // Note: CallsManager#hasRingingCall() and CallsManager#getFirstCallWithState(..) do not
82         // consider external calls, so an external call is going to cause the state to be idle.
83         int callState = TelephonyManager.CALL_STATE_IDLE;
84         if (mCallsManager.hasRingingCall()) {
85             callState = TelephonyManager.CALL_STATE_RINGING;
86         } else if (mCallsManager.getFirstCallWithState(CallState.DIALING, CallState.ACTIVE,
87                     CallState.ON_HOLD) != null) {
88             callState = TelephonyManager.CALL_STATE_OFFHOOK;
89         }
90         sendPhoneStateChangedBroadcast(call, callState);
91     }
92 
getCallState()93     int getCallState() {
94         return mCurrentState;
95     }
96 
sendPhoneStateChangedBroadcast(Call call, int phoneState)97     private void sendPhoneStateChangedBroadcast(Call call, int phoneState) {
98         if (phoneState == mCurrentState) {
99             return;
100         }
101 
102         mCurrentState = phoneState;
103 
104         String callHandle = null;
105         if (call.getHandle() != null) {
106             callHandle = call.getHandle().getSchemeSpecificPart();
107         }
108 
109         try {
110             if (mRegistry != null) {
111                 mRegistry.notifyCallState(phoneState, callHandle);
112                 Log.i(this, "Broadcasted state change: %s", mCurrentState);
113             }
114         } catch (RemoteException e) {
115             Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change.");
116         }
117     }
118 }
119