Home | History | Annotate | Download | only in telecom
      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 
     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 ((newState == CallState.DIALING || newState == CallState.ACTIVE
     47                 || newState == CallState.ON_HOLD) &&
     48                 !mCallsManager.hasRingingCall()) {
     49             /*
     50              * EXTRA_STATE_RINGING takes precedence over EXTRA_STATE_OFFHOOK, so if there is
     51              * already a ringing call, don't broadcast EXTRA_STATE_OFFHOOK.
     52              */
     53             sendPhoneStateChangedBroadcast(call, TelephonyManager.CALL_STATE_OFFHOOK);
     54         }
     55     }
     56 
     57     @Override
     58     public void onCallAdded(Call call) {
     59         if (call.getState() == CallState.RINGING) {
     60             sendPhoneStateChangedBroadcast(call, TelephonyManager.CALL_STATE_RINGING);
     61         }
     62     };
     63 
     64     @Override
     65     public void onCallRemoved(Call call) {
     66         // Recalculate the current phone state based on the consolidated state of the remaining
     67         // calls in the call list.
     68         int callState = TelephonyManager.CALL_STATE_IDLE;
     69         if (mCallsManager.hasRingingCall()) {
     70             callState = TelephonyManager.CALL_STATE_RINGING;
     71         } else if (mCallsManager.getFirstCallWithState(CallState.DIALING, CallState.ACTIVE,
     72                     CallState.ON_HOLD) != null) {
     73             callState = TelephonyManager.CALL_STATE_OFFHOOK;
     74         }
     75         sendPhoneStateChangedBroadcast(call, callState);
     76     }
     77 
     78     int getCallState() {
     79         return mCurrentState;
     80     }
     81 
     82     private void sendPhoneStateChangedBroadcast(Call call, int phoneState) {
     83         if (phoneState == mCurrentState) {
     84             return;
     85         }
     86 
     87         mCurrentState = phoneState;
     88 
     89         String callHandle = null;
     90         if (call.getHandle() != null) {
     91             callHandle = call.getHandle().getSchemeSpecificPart();
     92         }
     93 
     94         try {
     95             if (mRegistry != null) {
     96                 mRegistry.notifyCallState(phoneState, callHandle);
     97                 Log.i(this, "Broadcasted state change: %s", mCurrentState);
     98             }
     99         } catch (RemoteException e) {
    100             Log.w(this, "RemoteException when notifying TelephonyRegistry of call state change.");
    101         }
    102     }
    103 }
    104