Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright (C) 2006 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.internal.telephony;
     18 
     19 import android.net.LinkCapabilities;
     20 import android.net.LinkProperties;
     21 import android.os.Bundle;
     22 import android.os.RemoteException;
     23 import android.os.ServiceManager;
     24 import android.telephony.ServiceState;
     25 import android.telephony.TelephonyManager;
     26 import android.util.Log;
     27 
     28 import com.android.internal.telephony.ITelephonyRegistry;
     29 
     30 /**
     31  * broadcast intents
     32  */
     33 public class DefaultPhoneNotifier implements PhoneNotifier {
     34 
     35     static final String LOG_TAG = "GSM";
     36     private static final boolean DBG = true;
     37     private ITelephonyRegistry mRegistry;
     38 
     39     /*package*/
     40     DefaultPhoneNotifier() {
     41         mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
     42                     "telephony.registry"));
     43     }
     44 
     45     public void notifyPhoneState(Phone sender) {
     46         Call ringingCall = sender.getRingingCall();
     47         String incomingNumber = "";
     48         if (ringingCall != null && ringingCall.getEarliestConnection() != null){
     49             incomingNumber = ringingCall.getEarliestConnection().getAddress();
     50         }
     51         try {
     52             mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber);
     53         } catch (RemoteException ex) {
     54             // system process is dead
     55         }
     56     }
     57 
     58     public void notifyServiceState(Phone sender) {
     59         ServiceState ss = sender.getServiceState();
     60         if (ss == null) {
     61             ss = new ServiceState();
     62             ss.setStateOutOfService();
     63         }
     64         try {
     65             mRegistry.notifyServiceState(ss);
     66         } catch (RemoteException ex) {
     67             // system process is dead
     68         }
     69     }
     70 
     71     public void notifySignalStrength(Phone sender) {
     72         try {
     73             mRegistry.notifySignalStrength(sender.getSignalStrength());
     74         } catch (RemoteException ex) {
     75             // system process is dead
     76         }
     77     }
     78 
     79     public void notifyMessageWaitingChanged(Phone sender) {
     80         try {
     81             mRegistry.notifyMessageWaitingChanged(sender.getMessageWaitingIndicator());
     82         } catch (RemoteException ex) {
     83             // system process is dead
     84         }
     85     }
     86 
     87     public void notifyCallForwardingChanged(Phone sender) {
     88         try {
     89             mRegistry.notifyCallForwardingChanged(sender.getCallForwardingIndicator());
     90         } catch (RemoteException ex) {
     91             // system process is dead
     92         }
     93     }
     94 
     95     public void notifyDataActivity(Phone sender) {
     96         try {
     97             mRegistry.notifyDataActivity(convertDataActivityState(sender.getDataActivityState()));
     98         } catch (RemoteException ex) {
     99             // system process is dead
    100         }
    101     }
    102 
    103     public void notifyDataConnection(Phone sender, String reason, String apnType,
    104             Phone.DataState state) {
    105         doNotifyDataConnection(sender, reason, apnType, state);
    106     }
    107 
    108     private void doNotifyDataConnection(Phone sender, String reason, String apnType,
    109             Phone.DataState state) {
    110         // TODO
    111         // use apnType as the key to which connection we're talking about.
    112         // pass apnType back up to fetch particular for this one.
    113         TelephonyManager telephony = TelephonyManager.getDefault();
    114         LinkProperties linkProperties = null;
    115         LinkCapabilities linkCapabilities = null;
    116         boolean roaming = false;
    117 
    118         if (state == Phone.DataState.CONNECTED) {
    119             linkProperties = sender.getLinkProperties(apnType);
    120             linkCapabilities = sender.getLinkCapabilities(apnType);
    121         }
    122         ServiceState ss = sender.getServiceState();
    123         if (ss != null) roaming = ss.getRoaming();
    124 
    125         try {
    126             mRegistry.notifyDataConnection(
    127                     convertDataState(state),
    128                     sender.isDataConnectivityPossible(apnType), reason,
    129                     sender.getActiveApnHost(apnType),
    130                     apnType,
    131                     linkProperties,
    132                     linkCapabilities,
    133                     ((telephony!=null) ? telephony.getNetworkType() :
    134                     TelephonyManager.NETWORK_TYPE_UNKNOWN),
    135                     roaming);
    136         } catch (RemoteException ex) {
    137             // system process is dead
    138         }
    139     }
    140 
    141     public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
    142         try {
    143             mRegistry.notifyDataConnectionFailed(reason, apnType);
    144         } catch (RemoteException ex) {
    145             // system process is dead
    146         }
    147     }
    148 
    149     public void notifyCellLocation(Phone sender) {
    150         Bundle data = new Bundle();
    151         sender.getCellLocation().fillInNotifierBundle(data);
    152         try {
    153             mRegistry.notifyCellLocation(data);
    154         } catch (RemoteException ex) {
    155             // system process is dead
    156         }
    157     }
    158 
    159     public void notifyOtaspChanged(Phone sender, int otaspMode) {
    160         try {
    161             mRegistry.notifyOtaspChanged(otaspMode);
    162         } catch (RemoteException ex) {
    163             // system process is dead
    164         }
    165     }
    166 
    167     private void log(String s) {
    168         Log.d(LOG_TAG, "[PhoneNotifier] " + s);
    169     }
    170 
    171     /**
    172      * Convert the {@link State} enum into the TelephonyManager.CALL_STATE_* constants
    173      * for the public API.
    174      */
    175     public static int convertCallState(Phone.State state) {
    176         switch (state) {
    177             case RINGING:
    178                 return TelephonyManager.CALL_STATE_RINGING;
    179             case OFFHOOK:
    180                 return TelephonyManager.CALL_STATE_OFFHOOK;
    181             default:
    182                 return TelephonyManager.CALL_STATE_IDLE;
    183         }
    184     }
    185 
    186     /**
    187      * Convert the TelephonyManager.CALL_STATE_* constants into the {@link State} enum
    188      * for the public API.
    189      */
    190     public static Phone.State convertCallState(int state) {
    191         switch (state) {
    192             case TelephonyManager.CALL_STATE_RINGING:
    193                 return Phone.State.RINGING;
    194             case TelephonyManager.CALL_STATE_OFFHOOK:
    195                 return Phone.State.OFFHOOK;
    196             default:
    197                 return Phone.State.IDLE;
    198         }
    199     }
    200 
    201     /**
    202      * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
    203      * for the public API.
    204      */
    205     public static int convertDataState(Phone.DataState state) {
    206         switch (state) {
    207             case CONNECTING:
    208                 return TelephonyManager.DATA_CONNECTING;
    209             case CONNECTED:
    210                 return TelephonyManager.DATA_CONNECTED;
    211             case SUSPENDED:
    212                 return TelephonyManager.DATA_SUSPENDED;
    213             default:
    214                 return TelephonyManager.DATA_DISCONNECTED;
    215         }
    216     }
    217 
    218     /**
    219      * Convert the TelephonyManager.DATA_* constants into {@link DataState} enum
    220      * for the public API.
    221      */
    222     public static Phone.DataState convertDataState(int state) {
    223         switch (state) {
    224             case TelephonyManager.DATA_CONNECTING:
    225                 return Phone.DataState.CONNECTING;
    226             case TelephonyManager.DATA_CONNECTED:
    227                 return Phone.DataState.CONNECTED;
    228             case TelephonyManager.DATA_SUSPENDED:
    229                 return Phone.DataState.SUSPENDED;
    230             default:
    231                 return Phone.DataState.DISCONNECTED;
    232         }
    233     }
    234 
    235     /**
    236      * Convert the {@link DataState} enum into the TelephonyManager.DATA_* constants
    237      * for the public API.
    238      */
    239     public static int convertDataActivityState(Phone.DataActivityState state) {
    240         switch (state) {
    241             case DATAIN:
    242                 return TelephonyManager.DATA_ACTIVITY_IN;
    243             case DATAOUT:
    244                 return TelephonyManager.DATA_ACTIVITY_OUT;
    245             case DATAINANDOUT:
    246                 return TelephonyManager.DATA_ACTIVITY_INOUT;
    247             case DORMANT:
    248                 return TelephonyManager.DATA_ACTIVITY_DORMANT;
    249             default:
    250                 return TelephonyManager.DATA_ACTIVITY_NONE;
    251         }
    252     }
    253 
    254     /**
    255      * Convert the TelephonyManager.DATA_* constants into the {@link DataState} enum
    256      * for the public API.
    257      */
    258     public static Phone.DataActivityState convertDataActivityState(int state) {
    259         switch (state) {
    260             case TelephonyManager.DATA_ACTIVITY_IN:
    261                 return Phone.DataActivityState.DATAIN;
    262             case TelephonyManager.DATA_ACTIVITY_OUT:
    263                 return Phone.DataActivityState.DATAOUT;
    264             case TelephonyManager.DATA_ACTIVITY_INOUT:
    265                 return Phone.DataActivityState.DATAINANDOUT;
    266             case TelephonyManager.DATA_ACTIVITY_DORMANT:
    267                 return Phone.DataActivityState.DORMANT;
    268             default:
    269                 return Phone.DataActivityState.NONE;
    270         }
    271     }
    272 }
    273