Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 18 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 19 | import android.os.RemoteException; |
| 20 | import android.os.ServiceManager; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 21 | import android.telecom.Log; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 22 | import android.telephony.TelephonyManager; |
| 23 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 24 | import com.android.internal.telephony.ITelephonyRegistry; |
| 25 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state |
| 28 | * changes. |
| 29 | */ |
| 30 | final class PhoneStateBroadcaster extends CallsManagerListenerBase { |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 31 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 32 | private final CallsManager mCallsManager; |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 33 | private final ITelephonyRegistry mRegistry; |
| 34 | private int mCurrentState = TelephonyManager.CALL_STATE_IDLE; |
| 35 | |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 36 | public PhoneStateBroadcaster(CallsManager callsManager) { |
| 37 | mCallsManager = callsManager; |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 38 | mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( |
| 39 | "telephony.registry")); |
| 40 | if (mRegistry == null) { |
| 41 | Log.w(this, "TelephonyRegistry is null"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 42 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 45 | @Override |
| 46 | public void onCallStateChanged(Call call, int oldState, int newState) { |
Tyler Gunn | f15dc33 | 2016-06-07 16:01:41 -0700 | [diff] [blame] | 47 | if (call.isExternalCall()) { |
| 48 | return; |
| 49 | } |
Santos Cordon | 880b983 | 2016-01-22 14:18:48 -0800 | [diff] [blame] | 50 | updateStates(call); |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 51 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 52 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 53 | @Override |
| 54 | public void onCallAdded(Call call) { |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 55 | if (call.isExternalCall()) { |
| 56 | return; |
| 57 | } |
Santos Cordon | 880b983 | 2016-01-22 14:18:48 -0800 | [diff] [blame] | 58 | updateStates(call); |
| 59 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 60 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void onCallRemoved(Call call) { |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 63 | if (call.isExternalCall()) { |
| 64 | return; |
| 65 | } |
| 66 | updateStates(call); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Handles changes to a call's external property. If the call becomes external, we end up |
| 71 | * updating the call state to idle. If the call becomes non-external, then the call state can |
| 72 | * update to off hook. |
| 73 | * |
| 74 | * @param call The call. |
| 75 | * @param isExternalCall {@code True} if the call is external, {@code false} otherwise. |
| 76 | */ |
| 77 | @Override |
| 78 | public void onExternalCallChanged(Call call, boolean isExternalCall) { |
Santos Cordon | 880b983 | 2016-01-22 14:18:48 -0800 | [diff] [blame] | 79 | updateStates(call); |
| 80 | } |
| 81 | |
| 82 | private void updateStates(Call call) { |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 83 | // Recalculate the current phone state based on the consolidated state of the remaining |
| 84 | // calls in the call list. |
Tyler Gunn | 1a40c4f | 2016-04-14 14:29:45 -0700 | [diff] [blame] | 85 | // Note: CallsManager#hasRingingCall() and CallsManager#getFirstCallWithState(..) do not |
| 86 | // consider external calls, so an external call is going to cause the state to be idle. |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 87 | int callState = TelephonyManager.CALL_STATE_IDLE; |
Ihab Awad | 8de7691 | 2015-02-17 12:25:52 -0800 | [diff] [blame] | 88 | if (mCallsManager.hasRingingCall()) { |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 89 | callState = TelephonyManager.CALL_STATE_RINGING; |
Tyler Gunn | 1e37be5 | 2016-07-11 08:54:23 -0700 | [diff] [blame] | 90 | } else if (mCallsManager.getFirstCallWithState(CallState.DIALING, CallState.PULLING, |
| 91 | CallState.ACTIVE, CallState.ON_HOLD) != null) { |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 92 | callState = TelephonyManager.CALL_STATE_OFFHOOK; |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 93 | } |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 94 | sendPhoneStateChangedBroadcast(call, callState); |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 97 | int getCallState() { |
| 98 | return mCurrentState; |
| 99 | } |
| 100 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 101 | private void sendPhoneStateChangedBroadcast(Call call, int phoneState) { |
| 102 | if (phoneState == mCurrentState) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | mCurrentState = phoneState; |
| 107 | |
| 108 | String callHandle = null; |
Tyler Gunn | 054e666 | 2018-05-07 07:50:29 -0700 | [diff] [blame] | 109 | // Only report phone numbers in phone state broadcast for regular mobile calls; do not |
| 110 | // include numbers from 3rd party apps. |
| 111 | if (!call.isSelfManaged() && call.getHandle() != null) { |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 112 | callHandle = call.getHandle().getSchemeSpecificPart(); |
Ihab Awad | 12de549 | 2014-06-19 10:41:41 -0700 | [diff] [blame] | 113 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 114 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 115 | try { |
| 116 | if (mRegistry != null) { |
| 117 | mRegistry.notifyCallState(phoneState, callHandle); |
| 118 | Log.i(this, "Broadcasted state change: %s", mCurrentState); |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 119 | } |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 120 | } catch (RemoteException e) { |
| 121 | Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change."); |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 122 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 123 | } |
| 124 | } |