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.LinkProperties;
     20 import android.net.NetworkCapabilities;
     21 import android.os.Bundle;
     22 import android.os.RemoteException;
     23 import android.os.ServiceManager;
     24 import android.telephony.CellInfo;
     25 import android.telephony.PhysicalChannelConfig;
     26 import android.telephony.PreciseCallState;
     27 import android.telephony.Rlog;
     28 import android.telephony.ServiceState;
     29 import android.telephony.SubscriptionManager;
     30 import android.telephony.TelephonyManager;
     31 import android.telephony.VoLteServiceState;
     32 
     33 import java.util.List;
     34 
     35 /**
     36  * broadcast intents
     37  */
     38 public class DefaultPhoneNotifier implements PhoneNotifier {
     39     private static final String LOG_TAG = "DefaultPhoneNotifier";
     40     private static final boolean DBG = false; // STOPSHIP if true
     41 
     42     protected ITelephonyRegistry mRegistry;
     43 
     44     public DefaultPhoneNotifier() {
     45         mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
     46                     "telephony.registry"));
     47     }
     48 
     49     @Override
     50     public void notifyPhoneState(Phone sender) {
     51         Call ringingCall = sender.getRingingCall();
     52         int subId = sender.getSubId();
     53         int phoneId = sender.getPhoneId();
     54         String incomingNumber = "";
     55         if (ringingCall != null && ringingCall.getEarliestConnection() != null) {
     56             incomingNumber = ringingCall.getEarliestConnection().getAddress();
     57         }
     58         try {
     59             if (mRegistry != null) {
     60                   mRegistry.notifyCallStateForPhoneId(phoneId, subId,
     61                         PhoneConstantConversions.convertCallState(
     62                             sender.getState()), incomingNumber);
     63             }
     64         } catch (RemoteException ex) {
     65             // system process is dead
     66         }
     67     }
     68 
     69     @Override
     70     public void notifyServiceState(Phone sender) {
     71         ServiceState ss = sender.getServiceState();
     72         int phoneId = sender.getPhoneId();
     73         int subId = sender.getSubId();
     74 
     75         Rlog.d(LOG_TAG, "nofityServiceState: mRegistry=" + mRegistry + " ss=" + ss
     76                 + " sender=" + sender + " phondId=" + phoneId + " subId=" + subId);
     77         if (ss == null) {
     78             ss = new ServiceState();
     79             ss.setStateOutOfService();
     80         }
     81         try {
     82             if (mRegistry != null) {
     83                 mRegistry.notifyServiceStateForPhoneId(phoneId, subId, ss);
     84             }
     85         } catch (RemoteException ex) {
     86             // system process is dead
     87         }
     88     }
     89 
     90     @Override
     91     public void notifySignalStrength(Phone sender) {
     92         int phoneId = sender.getPhoneId();
     93         int subId = sender.getSubId();
     94         if (DBG) {
     95             // too chatty to log constantly
     96             Rlog.d(LOG_TAG, "notifySignalStrength: mRegistry=" + mRegistry
     97                     + " ss=" + sender.getSignalStrength() + " sender=" + sender);
     98         }
     99         try {
    100             if (mRegistry != null) {
    101                 mRegistry.notifySignalStrengthForPhoneId(phoneId, subId,
    102                         sender.getSignalStrength());
    103             }
    104         } catch (RemoteException ex) {
    105             // system process is dead
    106         }
    107     }
    108 
    109     @Override
    110     public void notifyMessageWaitingChanged(Phone sender) {
    111         int phoneId = sender.getPhoneId();
    112         int subId = sender.getSubId();
    113 
    114         try {
    115             if (mRegistry != null) {
    116                 mRegistry.notifyMessageWaitingChangedForPhoneId(phoneId, subId,
    117                         sender.getMessageWaitingIndicator());
    118             }
    119         } catch (RemoteException ex) {
    120             // system process is dead
    121         }
    122     }
    123 
    124     @Override
    125     public void notifyCallForwardingChanged(Phone sender) {
    126         int subId = sender.getSubId();
    127         try {
    128             if (mRegistry != null) {
    129                 Rlog.d(LOG_TAG, "notifyCallForwardingChanged: subId=" + subId + ", isCFActive="
    130                         + sender.getCallForwardingIndicator());
    131 
    132                 mRegistry.notifyCallForwardingChangedForSubscriber(subId,
    133                         sender.getCallForwardingIndicator());
    134             }
    135         } catch (RemoteException ex) {
    136             // system process is dead
    137         }
    138     }
    139 
    140     @Override
    141     public void notifyDataActivity(Phone sender) {
    142         int subId = sender.getSubId();
    143         try {
    144             if (mRegistry != null) {
    145                 mRegistry.notifyDataActivityForSubscriber(subId,
    146                         convertDataActivityState(sender.getDataActivityState()));
    147             }
    148         } catch (RemoteException ex) {
    149             // system process is dead
    150         }
    151     }
    152 
    153     @Override
    154     public void notifyDataConnection(Phone sender, String reason, String apnType,
    155             PhoneConstants.DataState state) {
    156         doNotifyDataConnection(sender, reason, apnType, state);
    157     }
    158 
    159     private void doNotifyDataConnection(Phone sender, String reason, String apnType,
    160             PhoneConstants.DataState state) {
    161         int subId = sender.getSubId();
    162         long dds = SubscriptionManager.getDefaultDataSubscriptionId();
    163         if (DBG) log("subId = " + subId + ", DDS = " + dds);
    164 
    165         // TODO
    166         // use apnType as the key to which connection we're talking about.
    167         // pass apnType back up to fetch particular for this one.
    168         TelephonyManager telephony = TelephonyManager.getDefault();
    169         LinkProperties linkProperties = null;
    170         NetworkCapabilities networkCapabilities = null;
    171         boolean roaming = false;
    172 
    173         if (state == PhoneConstants.DataState.CONNECTED) {
    174             linkProperties = sender.getLinkProperties(apnType);
    175             networkCapabilities = sender.getNetworkCapabilities(apnType);
    176         }
    177         ServiceState ss = sender.getServiceState();
    178         if (ss != null) roaming = ss.getDataRoaming();
    179 
    180         try {
    181             if (mRegistry != null) {
    182                 mRegistry.notifyDataConnectionForSubscriber(subId,
    183                     PhoneConstantConversions.convertDataState(state),
    184                         sender.isDataAllowed(), reason,
    185                         sender.getActiveApnHost(apnType),
    186                         apnType,
    187                         linkProperties,
    188                         networkCapabilities,
    189                         ((telephony != null) ? telephony.getDataNetworkType(subId) :
    190                                 TelephonyManager.NETWORK_TYPE_UNKNOWN), roaming);
    191             }
    192         } catch (RemoteException ex) {
    193             // system process is dead
    194         }
    195     }
    196 
    197     @Override
    198     public void notifyDataConnectionFailed(Phone sender, String reason, String apnType) {
    199         int subId = sender.getSubId();
    200         try {
    201             if (mRegistry != null) {
    202                 mRegistry.notifyDataConnectionFailedForSubscriber(subId, reason, apnType);
    203             }
    204         } catch (RemoteException ex) {
    205             // system process is dead
    206         }
    207     }
    208 
    209     @Override
    210     public void notifyCellLocation(Phone sender) {
    211         int subId = sender.getSubId();
    212         Bundle data = new Bundle();
    213         sender.getCellLocation().fillInNotifierBundle(data);
    214         try {
    215             if (mRegistry != null) {
    216                 mRegistry.notifyCellLocationForSubscriber(subId, data);
    217             }
    218         } catch (RemoteException ex) {
    219             // system process is dead
    220         }
    221     }
    222 
    223     @Override
    224     public void notifyCellInfo(Phone sender, List<CellInfo> cellInfo) {
    225         int subId = sender.getSubId();
    226         try {
    227             if (mRegistry != null) {
    228                 mRegistry.notifyCellInfoForSubscriber(subId, cellInfo);
    229             }
    230         } catch (RemoteException ex) {
    231 
    232         }
    233     }
    234 
    235     @Override
    236     public void notifyPhysicalChannelConfiguration(Phone sender,
    237             List<PhysicalChannelConfig> configs) {
    238         int subId = sender.getSubId();
    239         try {
    240             if (mRegistry != null) {
    241                 mRegistry.notifyPhysicalChannelConfigurationForSubscriber(subId, configs);
    242             }
    243         } catch (RemoteException ex) {
    244             // system process is dead
    245         }
    246     }
    247 
    248     @Override
    249     public void notifyOtaspChanged(Phone sender, int otaspMode) {
    250         // FIXME: subId?
    251         try {
    252             if (mRegistry != null) {
    253                 mRegistry.notifyOtaspChanged(otaspMode);
    254             }
    255         } catch (RemoteException ex) {
    256             // system process is dead
    257         }
    258     }
    259 
    260     public void notifyPreciseCallState(Phone sender) {
    261         // FIXME: subId?
    262         Call ringingCall = sender.getRingingCall();
    263         Call foregroundCall = sender.getForegroundCall();
    264         Call backgroundCall = sender.getBackgroundCall();
    265         if (ringingCall != null && foregroundCall != null && backgroundCall != null) {
    266             try {
    267                 mRegistry.notifyPreciseCallState(
    268                         convertPreciseCallState(ringingCall.getState()),
    269                         convertPreciseCallState(foregroundCall.getState()),
    270                         convertPreciseCallState(backgroundCall.getState()));
    271             } catch (RemoteException ex) {
    272                 // system process is dead
    273             }
    274         }
    275     }
    276 
    277     public void notifyDisconnectCause(int cause, int preciseCause) {
    278         // FIXME: subId?
    279         try {
    280             mRegistry.notifyDisconnectCause(cause, preciseCause);
    281         } catch (RemoteException ex) {
    282             // system process is dead
    283         }
    284     }
    285 
    286     public void notifyPreciseDataConnectionFailed(Phone sender, String reason, String apnType,
    287             String apn, String failCause) {
    288         // FIXME: subId?
    289         try {
    290             mRegistry.notifyPreciseDataConnectionFailed(reason, apnType, apn, failCause);
    291         } catch (RemoteException ex) {
    292             // system process is dead
    293         }
    294     }
    295 
    296     @Override
    297     public void notifyVoLteServiceStateChanged(Phone sender, VoLteServiceState lteState) {
    298         // FIXME: subID
    299         try {
    300             mRegistry.notifyVoLteServiceStateChanged(lteState);
    301         } catch (RemoteException ex) {
    302             // system process is dead
    303         }
    304     }
    305 
    306     @Override
    307     public void notifyDataActivationStateChanged(Phone sender, int activationState) {
    308         try {
    309             mRegistry.notifySimActivationStateChangedForPhoneId(sender.getPhoneId(),
    310                     sender.getSubId(), PhoneConstants.SIM_ACTIVATION_TYPE_DATA, activationState);
    311         } catch (RemoteException ex) {
    312             // system process is dead
    313         }
    314     }
    315 
    316     @Override
    317     public void notifyVoiceActivationStateChanged(Phone sender, int activationState) {
    318         try {
    319             mRegistry.notifySimActivationStateChangedForPhoneId(sender.getPhoneId(),
    320                     sender.getSubId(), PhoneConstants.SIM_ACTIVATION_TYPE_VOICE, activationState);
    321         } catch (RemoteException ex) {
    322             // system process is dead
    323         }
    324     }
    325 
    326     @Override
    327     public void notifyUserMobileDataStateChanged(Phone sender, boolean state) {
    328         try {
    329             mRegistry.notifyUserMobileDataStateChangedForPhoneId(
    330                     sender.getPhoneId(), sender.getSubId(), state);
    331         } catch (RemoteException ex) {
    332             // system process is dead
    333         }
    334     }
    335 
    336     @Override
    337     public void notifyOemHookRawEventForSubscriber(int subId, byte[] rawData) {
    338         try {
    339             mRegistry.notifyOemHookRawEventForSubscriber(subId, rawData);
    340         } catch (RemoteException ex) {
    341             // system process is dead
    342         }
    343     }
    344 
    345     /**
    346      * Convert the {@link Phone.DataActivityState} enum into the TelephonyManager.DATA_* constants
    347      * for the public API.
    348      */
    349     public static int convertDataActivityState(Phone.DataActivityState state) {
    350         switch (state) {
    351             case DATAIN:
    352                 return TelephonyManager.DATA_ACTIVITY_IN;
    353             case DATAOUT:
    354                 return TelephonyManager.DATA_ACTIVITY_OUT;
    355             case DATAINANDOUT:
    356                 return TelephonyManager.DATA_ACTIVITY_INOUT;
    357             case DORMANT:
    358                 return TelephonyManager.DATA_ACTIVITY_DORMANT;
    359             default:
    360                 return TelephonyManager.DATA_ACTIVITY_NONE;
    361         }
    362     }
    363 
    364     /**
    365      * Convert the {@link Call.State} enum into the PreciseCallState.PRECISE_CALL_STATE_* constants
    366      * for the public API.
    367      */
    368     public static int convertPreciseCallState(Call.State state) {
    369         switch (state) {
    370             case ACTIVE:
    371                 return PreciseCallState.PRECISE_CALL_STATE_ACTIVE;
    372             case HOLDING:
    373                 return PreciseCallState.PRECISE_CALL_STATE_HOLDING;
    374             case DIALING:
    375                 return PreciseCallState.PRECISE_CALL_STATE_DIALING;
    376             case ALERTING:
    377                 return PreciseCallState.PRECISE_CALL_STATE_ALERTING;
    378             case INCOMING:
    379                 return PreciseCallState.PRECISE_CALL_STATE_INCOMING;
    380             case WAITING:
    381                 return PreciseCallState.PRECISE_CALL_STATE_WAITING;
    382             case DISCONNECTED:
    383                 return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTED;
    384             case DISCONNECTING:
    385                 return PreciseCallState.PRECISE_CALL_STATE_DISCONNECTING;
    386             default:
    387                 return PreciseCallState.PRECISE_CALL_STATE_IDLE;
    388         }
    389     }
    390 
    391     private void log(String s) {
    392         Rlog.d(LOG_TAG, s);
    393     }
    394 }
    395