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