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