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