PhoneStateListener.java revision 08c7116ab9cd04ad6dd3c04aa1017237e7f409ac
1/* 2 * Copyright (C) 2008 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 android.telephony; 18 19import android.os.Bundle; 20import android.os.Handler; 21import android.os.Looper; 22import android.os.Message; 23import android.telephony.SubscriptionManager; 24import android.telephony.CellLocation; 25import android.telephony.CellInfo; 26import android.telephony.VoLteServiceState; 27import android.telephony.Rlog; 28import android.telephony.ServiceState; 29import android.telephony.SignalStrength; 30import android.telephony.PreciseCallState; 31import android.telephony.PreciseDataConnectionState; 32 33import com.android.internal.telephony.IPhoneStateListener; 34import java.util.List; 35 36/** 37 * A listener class for monitoring changes in specific telephony states 38 * on the device, including service state, signal strength, message 39 * waiting indicator (voicemail), and others. 40 * <p> 41 * Override the methods for the state that you wish to receive updates for, and 42 * pass your PhoneStateListener object, along with bitwise-or of the LISTEN_ 43 * flags to {@link TelephonyManager#listen TelephonyManager.listen()}. 44 * <p> 45 * Note that access to some telephony information is 46 * permission-protected. Your application won't receive updates for protected 47 * information unless it has the appropriate permissions declared in 48 * its manifest file. Where permissions apply, they are noted in the 49 * appropriate LISTEN_ flags. 50 */ 51public class PhoneStateListener { 52 private static final String LOG_TAG = "PhoneStateListener"; 53 private static final boolean DBG = false; // STOPSHIP if true 54 55 /** 56 * Stop listening for updates. 57 */ 58 public static final int LISTEN_NONE = 0; 59 60 /** 61 * Listen for changes to the network service state (cellular). 62 * 63 * @see #onServiceStateChanged 64 * @see ServiceState 65 */ 66 public static final int LISTEN_SERVICE_STATE = 0x00000001; 67 68 /** 69 * Listen for changes to the network signal strength (cellular). 70 * {@more} 71 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 72 * READ_PHONE_STATE} 73 * <p> 74 * 75 * @see #onSignalStrengthChanged 76 * 77 * @deprecated by {@link #LISTEN_SIGNAL_STRENGTHS} 78 */ 79 @Deprecated 80 public static final int LISTEN_SIGNAL_STRENGTH = 0x00000002; 81 82 /** 83 * Listen for changes to the message-waiting indicator. 84 * {@more} 85 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 86 * READ_PHONE_STATE} 87 * <p> 88 * Example: The status bar uses this to determine when to display the 89 * voicemail icon. 90 * 91 * @see #onMessageWaitingIndicatorChanged 92 */ 93 public static final int LISTEN_MESSAGE_WAITING_INDICATOR = 0x00000004; 94 95 /** 96 * Listen for changes to the call-forwarding indicator. 97 * {@more} 98 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 99 * READ_PHONE_STATE} 100 * @see #onCallForwardingIndicatorChanged 101 */ 102 public static final int LISTEN_CALL_FORWARDING_INDICATOR = 0x00000008; 103 104 /** 105 * Listen for changes to the device's cell location. Note that 106 * this will result in frequent callbacks to the listener. 107 * {@more} 108 * Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION 109 * ACCESS_COARSE_LOCATION} 110 * <p> 111 * If you need regular location updates but want more control over 112 * the update interval or location precision, you can set up a listener 113 * through the {@link android.location.LocationManager location manager} 114 * instead. 115 * 116 * @see #onCellLocationChanged 117 */ 118 public static final int LISTEN_CELL_LOCATION = 0x00000010; 119 120 /** 121 * Listen for changes to the device call state. 122 * {@more} 123 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 124 * READ_PHONE_STATE} 125 * @see #onCallStateChanged 126 */ 127 public static final int LISTEN_CALL_STATE = 0x00000020; 128 129 /** 130 * Listen for changes to the data connection state (cellular). 131 * 132 * @see #onDataConnectionStateChanged 133 */ 134 public static final int LISTEN_DATA_CONNECTION_STATE = 0x00000040; 135 136 /** 137 * Listen for changes to the direction of data traffic on the data 138 * connection (cellular). 139 * {@more} 140 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 141 * READ_PHONE_STATE} 142 * Example: The status bar uses this to display the appropriate 143 * data-traffic icon. 144 * 145 * @see #onDataActivity 146 */ 147 public static final int LISTEN_DATA_ACTIVITY = 0x00000080; 148 149 /** 150 * Listen for changes to the network signal strengths (cellular). 151 * <p> 152 * Example: The status bar uses this to control the signal-strength 153 * icon. 154 * 155 * @see #onSignalStrengthsChanged 156 */ 157 public static final int LISTEN_SIGNAL_STRENGTHS = 0x00000100; 158 159 /** 160 * Listen for changes to OTASP mode. 161 * 162 * @see #onOtaspChanged 163 * @hide 164 */ 165 public static final int LISTEN_OTASP_CHANGED = 0x00000200; 166 167 /** 168 * Listen for changes to observed cell info. 169 * 170 * @see #onCellInfoChanged 171 */ 172 public static final int LISTEN_CELL_INFO = 0x00000400; 173 174 /** 175 * Listen for precise changes and fails to the device calls (cellular). 176 * {@more} 177 * Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE 178 * READ_PRECISE_PHONE_STATE} 179 * 180 * @hide 181 */ 182 public static final int LISTEN_PRECISE_CALL_STATE = 0x00000800; 183 184 /** 185 * Listen for precise changes and fails on the data connection (cellular). 186 * {@more} 187 * Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE 188 * READ_PRECISE_PHONE_STATE} 189 * 190 * @see #onPreciseDataConnectionStateChanged 191 * @hide 192 */ 193 public static final int LISTEN_PRECISE_DATA_CONNECTION_STATE = 0x00001000; 194 195 /** 196 * Listen for real time info for all data connections (cellular)). 197 * {@more} 198 * Requires Permission: {@link android.Manifest.permission#READ_PRECISE_PHONE_STATE 199 * READ_PRECISE_PHONE_STATE} 200 * 201 * @see #onDataConnectionRealTimeInfoChanged(DataConnectionRealTimeInfo) 202 * @hide 203 */ 204 public static final int LISTEN_DATA_CONNECTION_REAL_TIME_INFO = 0x00002000; 205 206 /** 207 * Listen for changes to LTE network state 208 * 209 * @see #onLteNetworkStateChanged 210 * @hide 211 */ 212 public static final int LISTEN_VOLTE_STATE = 0x00004000; 213 214 /** 215 * Listen for OEM hook raw event 216 * 217 * @see #onOemHookRawEvent 218 * @hide 219 */ 220 public static final int LISTEN_OEM_HOOK_RAW_EVENT = 0x00008000; 221 222 /* 223 * Subscription used to listen to the phone state changes 224 * @hide 225 */ 226 /** @hide */ 227 protected int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; 228 229 private final Handler mHandler; 230 231 /** 232 * Create a PhoneStateListener for the Phone with the default subscription. 233 * This class requires Looper.myLooper() not return null. 234 */ 235 public PhoneStateListener() { 236 this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, Looper.myLooper()); 237 } 238 239 /** 240 * Create a PhoneStateListener for the Phone with the default subscription 241 * using a particular non-null Looper. 242 * @hide 243 */ 244 public PhoneStateListener(Looper looper) { 245 this(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, looper); 246 } 247 248 /** 249 * Create a PhoneStateListener for the Phone using the specified subscription. 250 * This class requires Looper.myLooper() not return null. To supply your 251 * own non-null Looper use PhoneStateListener(int subId, Looper looper) below. 252 * @hide 253 */ 254 public PhoneStateListener(int subId) { 255 this(subId, Looper.myLooper()); 256 } 257 258 /** 259 * Create a PhoneStateListener for the Phone using the specified subscription 260 * and non-null Looper. 261 * @hide 262 */ 263 public PhoneStateListener(int subId, Looper looper) { 264 if (DBG) log("ctor: subId=" + subId + " looper=" + looper); 265 mSubId = subId; 266 mHandler = new Handler(looper) { 267 public void handleMessage(Message msg) { 268 if (DBG) { 269 log("mSubId=" + mSubId + " what=0x" + Integer.toHexString(msg.what) 270 + " msg=" + msg); 271 } 272 switch (msg.what) { 273 case LISTEN_SERVICE_STATE: 274 PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj); 275 break; 276 case LISTEN_SIGNAL_STRENGTH: 277 PhoneStateListener.this.onSignalStrengthChanged(msg.arg1); 278 break; 279 case LISTEN_MESSAGE_WAITING_INDICATOR: 280 PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0); 281 break; 282 case LISTEN_CALL_FORWARDING_INDICATOR: 283 PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0); 284 break; 285 case LISTEN_CELL_LOCATION: 286 PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj); 287 break; 288 case LISTEN_CALL_STATE: 289 PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj); 290 break; 291 case LISTEN_DATA_CONNECTION_STATE: 292 PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2); 293 PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1); 294 break; 295 case LISTEN_DATA_ACTIVITY: 296 PhoneStateListener.this.onDataActivity(msg.arg1); 297 break; 298 case LISTEN_SIGNAL_STRENGTHS: 299 PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj); 300 break; 301 case LISTEN_OTASP_CHANGED: 302 PhoneStateListener.this.onOtaspChanged(msg.arg1); 303 break; 304 case LISTEN_CELL_INFO: 305 PhoneStateListener.this.onCellInfoChanged((List<CellInfo>)msg.obj); 306 break; 307 case LISTEN_PRECISE_CALL_STATE: 308 PhoneStateListener.this.onPreciseCallStateChanged((PreciseCallState)msg.obj); 309 break; 310 case LISTEN_PRECISE_DATA_CONNECTION_STATE: 311 PhoneStateListener.this.onPreciseDataConnectionStateChanged( 312 (PreciseDataConnectionState)msg.obj); 313 break; 314 case LISTEN_DATA_CONNECTION_REAL_TIME_INFO: 315 PhoneStateListener.this.onDataConnectionRealTimeInfoChanged( 316 (DataConnectionRealTimeInfo)msg.obj); 317 break; 318 case LISTEN_VOLTE_STATE: 319 PhoneStateListener.this.onVoLteServiceStateChanged((VoLteServiceState)msg.obj); 320 break; 321 case LISTEN_OEM_HOOK_RAW_EVENT: 322 PhoneStateListener.this.onOemHookRawEvent((byte[])msg.obj); 323 break; 324 325 } 326 } 327 }; 328 } 329 330 /** 331 * Callback invoked when device service state changes. 332 * 333 * @see ServiceState#STATE_EMERGENCY_ONLY 334 * @see ServiceState#STATE_IN_SERVICE 335 * @see ServiceState#STATE_OUT_OF_SERVICE 336 * @see ServiceState#STATE_POWER_OFF 337 */ 338 public void onServiceStateChanged(ServiceState serviceState) { 339 // default implementation empty 340 } 341 342 /** 343 * Callback invoked when network signal strength changes. 344 * 345 * @see ServiceState#STATE_EMERGENCY_ONLY 346 * @see ServiceState#STATE_IN_SERVICE 347 * @see ServiceState#STATE_OUT_OF_SERVICE 348 * @see ServiceState#STATE_POWER_OFF 349 * @deprecated Use {@link #onSignalStrengthsChanged(SignalStrength)} 350 */ 351 @Deprecated 352 public void onSignalStrengthChanged(int asu) { 353 // default implementation empty 354 } 355 356 /** 357 * Callback invoked when the message-waiting indicator changes. 358 */ 359 public void onMessageWaitingIndicatorChanged(boolean mwi) { 360 // default implementation empty 361 } 362 363 /** 364 * Callback invoked when the call-forwarding indicator changes. 365 */ 366 public void onCallForwardingIndicatorChanged(boolean cfi) { 367 // default implementation empty 368 } 369 370 /** 371 * Callback invoked when device cell location changes. 372 */ 373 public void onCellLocationChanged(CellLocation location) { 374 // default implementation empty 375 } 376 377 /** 378 * Callback invoked when device call state changes. 379 * 380 * @see TelephonyManager#CALL_STATE_IDLE 381 * @see TelephonyManager#CALL_STATE_RINGING 382 * @see TelephonyManager#CALL_STATE_OFFHOOK 383 */ 384 public void onCallStateChanged(int state, String incomingNumber) { 385 // default implementation empty 386 } 387 388 /** 389 * Callback invoked when connection state changes. 390 * 391 * @see TelephonyManager#DATA_DISCONNECTED 392 * @see TelephonyManager#DATA_CONNECTING 393 * @see TelephonyManager#DATA_CONNECTED 394 * @see TelephonyManager#DATA_SUSPENDED 395 */ 396 public void onDataConnectionStateChanged(int state) { 397 // default implementation empty 398 } 399 400 /** 401 * same as above, but with the network type. Both called. 402 */ 403 public void onDataConnectionStateChanged(int state, int networkType) { 404 } 405 406 /** 407 * Callback invoked when data activity state changes. 408 * 409 * @see TelephonyManager#DATA_ACTIVITY_NONE 410 * @see TelephonyManager#DATA_ACTIVITY_IN 411 * @see TelephonyManager#DATA_ACTIVITY_OUT 412 * @see TelephonyManager#DATA_ACTIVITY_INOUT 413 * @see TelephonyManager#DATA_ACTIVITY_DORMANT 414 */ 415 public void onDataActivity(int direction) { 416 // default implementation empty 417 } 418 419 /** 420 * Callback invoked when network signal strengths changes. 421 * 422 * @see ServiceState#STATE_EMERGENCY_ONLY 423 * @see ServiceState#STATE_IN_SERVICE 424 * @see ServiceState#STATE_OUT_OF_SERVICE 425 * @see ServiceState#STATE_POWER_OFF 426 */ 427 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 428 // default implementation empty 429 } 430 431 432 /** 433 * The Over The Air Service Provisioning (OTASP) has changed. Requires 434 * the READ_PHONE_STATE permission. 435 * @param otaspMode is integer <code>OTASP_UNKNOWN=1<code> 436 * means the value is currently unknown and the system should wait until 437 * <code>OTASP_NEEDED=2<code> or <code>OTASP_NOT_NEEDED=3<code> is received before 438 * making the decision to perform OTASP or not. 439 * 440 * @hide 441 */ 442 public void onOtaspChanged(int otaspMode) { 443 // default implementation empty 444 } 445 446 /** 447 * Callback invoked when a observed cell info has changed, 448 * or new cells have been added or removed. 449 * @param cellInfo is the list of currently visible cells. 450 */ 451 public void onCellInfoChanged(List<CellInfo> cellInfo) { 452 } 453 454 /** 455 * Callback invoked when precise device call state changes. 456 * 457 * @hide 458 */ 459 public void onPreciseCallStateChanged(PreciseCallState callState) { 460 // default implementation empty 461 } 462 463 /** 464 * Callback invoked when data connection state changes with precise information. 465 * 466 * @hide 467 */ 468 public void onPreciseDataConnectionStateChanged( 469 PreciseDataConnectionState dataConnectionState) { 470 // default implementation empty 471 } 472 473 /** 474 * Callback invoked when data connection state changes with precise information. 475 * 476 * @hide 477 */ 478 public void onDataConnectionRealTimeInfoChanged( 479 DataConnectionRealTimeInfo dcRtInfo) { 480 // default implementation empty 481 } 482 483 /** 484 * Callback invoked when the service state of LTE network 485 * related to the VoLTE service has changed. 486 * @param stateInfo is the current LTE network information 487 * @hide 488 */ 489 public void onVoLteServiceStateChanged(VoLteServiceState stateInfo) { 490 } 491 492 /** 493 * Callback invoked when OEM hook raw event is received. Requires 494 * the READ_PRIVILEGED_PHONE_STATE permission. 495 * @param rawData is the byte array of the OEM hook raw data. 496 * @hide 497 */ 498 public void onOemHookRawEvent(byte[] rawData) { 499 // default implementation empty 500 } 501 502 /** 503 * The callback methods need to be called on the handler thread where 504 * this object was created. If the binder did that for us it'd be nice. 505 */ 506 IPhoneStateListener callback = new IPhoneStateListener.Stub() { 507 public void onServiceStateChanged(ServiceState serviceState) { 508 Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget(); 509 } 510 511 public void onSignalStrengthChanged(int asu) { 512 Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget(); 513 } 514 515 public void onMessageWaitingIndicatorChanged(boolean mwi) { 516 Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null) 517 .sendToTarget(); 518 } 519 520 public void onCallForwardingIndicatorChanged(boolean cfi) { 521 Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null) 522 .sendToTarget(); 523 } 524 525 public void onCellLocationChanged(Bundle bundle) { 526 CellLocation location = CellLocation.newFromBundle(bundle); 527 Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget(); 528 } 529 530 public void onCallStateChanged(int state, String incomingNumber) { 531 Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget(); 532 } 533 534 public void onDataConnectionStateChanged(int state, int networkType) { 535 Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType). 536 sendToTarget(); 537 } 538 539 public void onDataActivity(int direction) { 540 Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget(); 541 } 542 543 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 544 Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget(); 545 } 546 547 public void onOtaspChanged(int otaspMode) { 548 Message.obtain(mHandler, LISTEN_OTASP_CHANGED, otaspMode, 0).sendToTarget(); 549 } 550 551 public void onCellInfoChanged(List<CellInfo> cellInfo) { 552 Message.obtain(mHandler, LISTEN_CELL_INFO, 0, 0, cellInfo).sendToTarget(); 553 } 554 555 public void onPreciseCallStateChanged(PreciseCallState callState) { 556 Message.obtain(mHandler, LISTEN_PRECISE_CALL_STATE, 0, 0, callState).sendToTarget(); 557 } 558 559 public void onPreciseDataConnectionStateChanged( 560 PreciseDataConnectionState dataConnectionState) { 561 Message.obtain(mHandler, LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0, 562 dataConnectionState).sendToTarget(); 563 } 564 565 public void onDataConnectionRealTimeInfoChanged( 566 DataConnectionRealTimeInfo dcRtInfo) { 567 Message.obtain(mHandler, LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0, 568 dcRtInfo).sendToTarget(); 569 } 570 571 public void onVoLteServiceStateChanged(VoLteServiceState lteState) { 572 Message.obtain(mHandler, LISTEN_VOLTE_STATE, 0, 0, lteState).sendToTarget(); 573 } 574 575 public void onOemHookRawEvent(byte[] rawData) { 576 Message.obtain(mHandler, LISTEN_OEM_HOOK_RAW_EVENT, 0, 0, rawData).sendToTarget(); 577 } 578 }; 579 580 private void log(String s) { 581 Rlog.d(LOG_TAG, s); 582 } 583} 584