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