Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2013 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.phone;
     18 
     19 import android.os.AsyncResult;
     20 import android.os.Handler;
     21 import android.os.Message;
     22 import android.os.SystemProperties;
     23 import android.util.Log;
     24 
     25 import com.android.internal.telephony.CallManager;
     26 
     27 import java.util.ArrayList;
     28 import java.util.LinkedList;
     29 import java.util.List;
     30 
     31 
     32 /**
     33  * Dedicated Call state monitoring class.  This class communicates directly with
     34  * the call manager to listen for call state events and notifies registered
     35  * handlers.
     36  * It works as an inverse multiplexor for all classes wanted Call State updates
     37  * so that there exists only one channel to the telephony layer.
     38  *
     39  * TODO: Add manual phone state checks (getState(), etc.).
     40  */
     41 class CallStateMonitor extends Handler {
     42     private static final String LOG_TAG = CallStateMonitor.class.getSimpleName();
     43     private static final boolean DBG =
     44             (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1);
     45 
     46     // Events from the Phone object:
     47     public static final int PHONE_STATE_CHANGED = 1;
     48     public static final int PHONE_NEW_RINGING_CONNECTION = 2;
     49     public static final int PHONE_DISCONNECT = 3;
     50     public static final int PHONE_UNKNOWN_CONNECTION_APPEARED = 4;
     51     public static final int PHONE_INCOMING_RING = 5;
     52     public static final int PHONE_STATE_DISPLAYINFO = 6;
     53     public static final int PHONE_STATE_SIGNALINFO = 7;
     54     public static final int PHONE_CDMA_CALL_WAITING = 8;
     55     public static final int PHONE_ENHANCED_VP_ON = 9;
     56     public static final int PHONE_ENHANCED_VP_OFF = 10;
     57     public static final int PHONE_RINGBACK_TONE = 11;
     58     public static final int PHONE_RESEND_MUTE = 12;
     59     public static final int PHONE_ON_DIAL_CHARS = 13;
     60 
     61     // Other events from call manager
     62     public static final int EVENT_OTA_PROVISION_CHANGE = 20;
     63 
     64     private CallManager callManager;
     65     private ArrayList<Handler> registeredHandlers;
     66 
     67     // Events generated internally:
     68     public CallStateMonitor(CallManager callManager) {
     69         this.callManager = callManager;
     70         registeredHandlers = new ArrayList<Handler>();
     71 
     72         registerForNotifications();
     73     }
     74 
     75     /**
     76      * Register for call state notifications with the CallManager.
     77      */
     78     private void registerForNotifications() {
     79         callManager.registerForNewRingingConnection(this, PHONE_NEW_RINGING_CONNECTION, null);
     80         callManager.registerForPreciseCallStateChanged(this, PHONE_STATE_CHANGED, null);
     81         callManager.registerForDisconnect(this, PHONE_DISCONNECT, null);
     82         callManager.registerForUnknownConnection(this, PHONE_UNKNOWN_CONNECTION_APPEARED, null);
     83         callManager.registerForIncomingRing(this, PHONE_INCOMING_RING, null);
     84         callManager.registerForCdmaOtaStatusChange(this, EVENT_OTA_PROVISION_CHANGE, null);
     85         callManager.registerForCallWaiting(this, PHONE_CDMA_CALL_WAITING, null);
     86         callManager.registerForDisplayInfo(this, PHONE_STATE_DISPLAYINFO, null);
     87         callManager.registerForSignalInfo(this, PHONE_STATE_SIGNALINFO, null);
     88         callManager.registerForInCallVoicePrivacyOn(this, PHONE_ENHANCED_VP_ON, null);
     89         callManager.registerForInCallVoicePrivacyOff(this, PHONE_ENHANCED_VP_OFF, null);
     90         callManager.registerForRingbackTone(this, PHONE_RINGBACK_TONE, null);
     91         callManager.registerForResendIncallMute(this, PHONE_RESEND_MUTE, null);
     92         callManager.registerForPostDialCharacter(this, PHONE_ON_DIAL_CHARS, null);
     93     }
     94 
     95     public void addListener(Handler handler) {
     96         if (handler != null && !registeredHandlers.contains(handler)) {
     97             if (DBG) {
     98                 Log.d(LOG_TAG, "Adding Handler: " + handler);
     99             }
    100             registeredHandlers.add(handler);
    101         }
    102     }
    103 
    104     @Override
    105     public void handleMessage(Message msg) {
    106         if (DBG) {
    107             Log.d(LOG_TAG, "handleMessage(" + msg.what + ")");
    108         }
    109 
    110         for (Handler handler : registeredHandlers) {
    111             handler.handleMessage(msg);
    112         }
    113     }
    114 
    115     /**
    116      * When radio technology changes, we need to to reregister for all the events which are
    117      * all tied to the old radio.
    118      */
    119     public void updateAfterRadioTechnologyChange() {
    120         if (DBG) Log.d(LOG_TAG, "updateCallNotifierRegistrationsAfterRadioTechnologyChange...");
    121 
    122         // Unregister all events from the old obsolete phone
    123         callManager.unregisterForNewRingingConnection(this);
    124         callManager.unregisterForPreciseCallStateChanged(this);
    125         callManager.unregisterForDisconnect(this);
    126         callManager.unregisterForUnknownConnection(this);
    127         callManager.unregisterForIncomingRing(this);
    128         callManager.unregisterForCallWaiting(this);
    129         callManager.unregisterForDisplayInfo(this);
    130         callManager.unregisterForSignalInfo(this);
    131         callManager.unregisterForCdmaOtaStatusChange(this);
    132         callManager.unregisterForRingbackTone(this);
    133         callManager.unregisterForResendIncallMute(this);
    134         callManager.unregisterForInCallVoicePrivacyOn(this);
    135         callManager.unregisterForInCallVoicePrivacyOff(this);
    136         callManager.unregisterForPostDialCharacter(this);
    137 
    138         registerForNotifications();
    139     }
    140 
    141 }
    142