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