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; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 21 | import android.telecom.CallState; |
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 | |
| 32 | private final ITelephonyRegistry mRegistry; |
| 33 | private int mCurrentState = TelephonyManager.CALL_STATE_IDLE; |
| 34 | |
| 35 | public PhoneStateBroadcaster() { |
| 36 | mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService( |
| 37 | "telephony.registry")); |
| 38 | if (mRegistry == null) { |
| 39 | Log.w(this, "TelephonyRegistry is null"); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 40 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public void onCallStateChanged(Call call, int oldState, int newState) { |
| 45 | if ((newState == CallState.DIALING || newState == CallState.ACTIVE |
| 46 | || newState == CallState.ON_HOLD) && !CallsManager.getInstance().hasRingingCall()) { |
| 47 | /* |
| 48 | * EXTRA_STATE_RINGING takes precedence over EXTRA_STATE_OFFHOOK, so if there is |
| 49 | * already a ringing call, don't broadcast EXTRA_STATE_OFFHOOK. |
| 50 | */ |
| 51 | sendPhoneStateChangedBroadcast(call, TelephonyManager.CALL_STATE_OFFHOOK); |
| 52 | } |
| 53 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 54 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 55 | @Override |
| 56 | public void onCallAdded(Call call) { |
| 57 | if (call.getState() == CallState.RINGING) { |
| 58 | sendPhoneStateChangedBroadcast(call, TelephonyManager.CALL_STATE_RINGING); |
| 59 | } |
| 60 | }; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 61 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public void onCallRemoved(Call call) { |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 64 | // Recalculate the current phone state based on the consolidated state of the remaining |
| 65 | // calls in the call list. |
| 66 | final CallsManager callsManager = CallsManager.getInstance(); |
| 67 | int callState = TelephonyManager.CALL_STATE_IDLE; |
| 68 | if (callsManager.hasRingingCall()) { |
| 69 | callState = TelephonyManager.CALL_STATE_RINGING; |
| 70 | } else if (callsManager.getFirstCallWithState(CallState.DIALING, CallState.ACTIVE, |
| 71 | CallState.ON_HOLD) != null) { |
| 72 | callState = TelephonyManager.CALL_STATE_OFFHOOK; |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 73 | } |
Yorke Lee | 677e811 | 2014-09-27 14:16:47 -0700 | [diff] [blame] | 74 | sendPhoneStateChangedBroadcast(call, callState); |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Yorke Lee | f86db2e | 2014-09-12 17:56:48 -0700 | [diff] [blame] | 77 | int getCallState() { |
| 78 | return mCurrentState; |
| 79 | } |
| 80 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 81 | private void sendPhoneStateChangedBroadcast(Call call, int phoneState) { |
| 82 | if (phoneState == mCurrentState) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | mCurrentState = phoneState; |
| 87 | |
| 88 | String callHandle = null; |
Ihab Awad | 12de549 | 2014-06-19 10:41:41 -0700 | [diff] [blame] | 89 | if (call.getHandle() != null) { |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 90 | callHandle = call.getHandle().getSchemeSpecificPart(); |
Ihab Awad | 12de549 | 2014-06-19 10:41:41 -0700 | [diff] [blame] | 91 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 92 | |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 93 | try { |
| 94 | if (mRegistry != null) { |
| 95 | mRegistry.notifyCallState(phoneState, callHandle); |
| 96 | Log.i(this, "Broadcasted state change: %s", mCurrentState); |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 97 | } |
Yorke Lee | e19d33e | 2014-09-08 16:07:07 -0700 | [diff] [blame] | 98 | } catch (RemoteException e) { |
| 99 | Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change."); |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 100 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 101 | } |
| 102 | } |