blob: b96eb02b5584a7b27d1a3bebf802de14cfaa2af2 [file] [log] [blame]
Sailesh Nepal810735e2014-03-18 18:15:46 -07001/*
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 Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Sailesh Nepal810735e2014-03-18 18:15:46 -070018
Yorke Leee19d33e2014-09-08 16:07:07 -070019import android.os.RemoteException;
20import android.os.ServiceManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070021import android.telecom.CallState;
Sailesh Nepal810735e2014-03-18 18:15:46 -070022import android.telephony.TelephonyManager;
23
Yorke Leee19d33e2014-09-08 16:07:07 -070024import com.android.internal.telephony.ITelephonyRegistry;
25
Sailesh Nepal810735e2014-03-18 18:15:46 -070026/**
27 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
28 * changes.
29 */
30final class PhoneStateBroadcaster extends CallsManagerListenerBase {
Yorke Leee19d33e2014-09-08 16:07:07 -070031
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 Nepal810735e2014-03-18 18:15:46 -070040 }
Sailesh Nepal810735e2014-03-18 18:15:46 -070041 }
42
Yorke Leee19d33e2014-09-08 16:07:07 -070043 @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 Nepal810735e2014-03-18 18:15:46 -070054
Yorke Leee19d33e2014-09-08 16:07:07 -070055 @Override
56 public void onCallAdded(Call call) {
57 if (call.getState() == CallState.RINGING) {
58 sendPhoneStateChangedBroadcast(call, TelephonyManager.CALL_STATE_RINGING);
59 }
60 };
Sailesh Nepal810735e2014-03-18 18:15:46 -070061
Yorke Leee19d33e2014-09-08 16:07:07 -070062 @Override
63 public void onCallRemoved(Call call) {
Yorke Lee677e8112014-09-27 14:16:47 -070064 // 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 Leee19d33e2014-09-08 16:07:07 -070073 }
Yorke Lee677e8112014-09-27 14:16:47 -070074 sendPhoneStateChangedBroadcast(call, callState);
Yorke Leee19d33e2014-09-08 16:07:07 -070075 }
76
Yorke Leef86db2e2014-09-12 17:56:48 -070077 int getCallState() {
78 return mCurrentState;
79 }
80
Yorke Leee19d33e2014-09-08 16:07:07 -070081 private void sendPhoneStateChangedBroadcast(Call call, int phoneState) {
82 if (phoneState == mCurrentState) {
83 return;
84 }
85
86 mCurrentState = phoneState;
87
88 String callHandle = null;
Ihab Awad12de5492014-06-19 10:41:41 -070089 if (call.getHandle() != null) {
Yorke Leee19d33e2014-09-08 16:07:07 -070090 callHandle = call.getHandle().getSchemeSpecificPart();
Ihab Awad12de5492014-06-19 10:41:41 -070091 }
Sailesh Nepal810735e2014-03-18 18:15:46 -070092
Yorke Leee19d33e2014-09-08 16:07:07 -070093 try {
94 if (mRegistry != null) {
95 mRegistry.notifyCallState(phoneState, callHandle);
96 Log.i(this, "Broadcasted state change: %s", mCurrentState);
Sailesh Nepal53fb8c12014-04-01 23:38:39 -070097 }
Yorke Leee19d33e2014-09-08 16:07:07 -070098 } catch (RemoteException e) {
99 Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change.");
Sailesh Nepal53fb8c12014-04-01 23:38:39 -0700100 }
Sailesh Nepal810735e2014-03-18 18:15:46 -0700101 }
102}