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 17 package android.telephony; 18 19 import android.os.Bundle; 20 import android.os.Handler; 21 import android.os.Message; 22 import android.telephony.ServiceState; 23 import android.telephony.SignalStrength; 24 import android.telephony.CellLocation; 25 import android.telephony.CellInfo; 26 import android.util.Log; 27 28 import com.android.internal.telephony.IPhoneStateListener; 29 import com.android.internal.telephony.Phone; 30 31 /** 32 * A listener class for monitoring changes in specific telephony states 33 * on the device, including service state, signal strength, message 34 * waiting indicator (voicemail), and others. 35 * <p> 36 * Override the methods for the state that you wish to receive updates for, and 37 * pass your PhoneStateListener object, along with bitwise-or of the LISTEN_ 38 * flags to {@link TelephonyManager#listen TelephonyManager.listen()}. 39 * <p> 40 * Note that access to some telephony information is 41 * permission-protected. Your application won't receive updates for protected 42 * information unless it has the appropriate permissions declared in 43 * its manifest file. Where permissions apply, they are noted in the 44 * appropriate LISTEN_ flags. 45 */ 46 public class PhoneStateListener { 47 48 /** 49 * Stop listening for updates. 50 */ 51 public static final int LISTEN_NONE = 0; 52 53 /** 54 * Listen for changes to the network service state (cellular). 55 * 56 * @see #onServiceStateChanged 57 * @see ServiceState 58 */ 59 public static final int LISTEN_SERVICE_STATE = 0x00000001; 60 61 /** 62 * Listen for changes to the network signal strength (cellular). 63 * {@more} 64 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 65 * READ_PHONE_STATE} 66 * <p> 67 * 68 * @see #onSignalStrengthChanged 69 * 70 * @deprecated by {@link #LISTEN_SIGNAL_STRENGTHS} 71 */ 72 @Deprecated 73 public static final int LISTEN_SIGNAL_STRENGTH = 0x00000002; 74 75 /** 76 * Listen for changes to the message-waiting indicator. 77 * {@more} 78 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 79 * READ_PHONE_STATE} 80 * <p> 81 * Example: The status bar uses this to determine when to display the 82 * voicemail icon. 83 * 84 * @see #onMessageWaitingIndicatorChanged 85 */ 86 public static final int LISTEN_MESSAGE_WAITING_INDICATOR = 0x00000004; 87 88 /** 89 * Listen for changes to the call-forwarding indicator. 90 * {@more} 91 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 92 * READ_PHONE_STATE} 93 * @see #onCallForwardingIndicatorChanged 94 */ 95 public static final int LISTEN_CALL_FORWARDING_INDICATOR = 0x00000008; 96 97 /** 98 * Listen for changes to the device's cell location. Note that 99 * this will result in frequent callbacks to the listener. 100 * {@more} 101 * Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION 102 * ACCESS_COARSE_LOCATION} 103 * <p> 104 * If you need regular location updates but want more control over 105 * the update interval or location precision, you can set up a listener 106 * through the {@link android.location.LocationManager location manager} 107 * instead. 108 * 109 * @see #onCellLocationChanged 110 */ 111 public static final int LISTEN_CELL_LOCATION = 0x00000010; 112 113 /** 114 * Listen for changes to the device call state. 115 * {@more} 116 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 117 * READ_PHONE_STATE} 118 * @see #onCallStateChanged 119 */ 120 public static final int LISTEN_CALL_STATE = 0x00000020; 121 122 /** 123 * Listen for changes to the data connection state (cellular). 124 * 125 * @see #onDataConnectionStateChanged 126 */ 127 public static final int LISTEN_DATA_CONNECTION_STATE = 0x00000040; 128 129 /** 130 * Listen for changes to the direction of data traffic on the data 131 * connection (cellular). 132 * {@more} 133 * Requires Permission: {@link android.Manifest.permission#READ_PHONE_STATE 134 * READ_PHONE_STATE} 135 * Example: The status bar uses this to display the appropriate 136 * data-traffic icon. 137 * 138 * @see #onDataActivity 139 */ 140 public static final int LISTEN_DATA_ACTIVITY = 0x00000080; 141 142 /** 143 * Listen for changes to the network signal strengths (cellular). 144 * <p> 145 * Example: The status bar uses this to control the signal-strength 146 * icon. 147 * 148 * @see #onSignalStrengthsChanged 149 */ 150 public static final int LISTEN_SIGNAL_STRENGTHS = 0x00000100; 151 152 /** 153 * Listen for changes to OTASP mode. 154 * 155 * @see #onOtaspChanged 156 * @hide 157 */ 158 public static final int LISTEN_OTASP_CHANGED = 0x00000200; 159 160 /** 161 * Listen for changes to observed cell info. 162 * 163 * @see #onCellInfoChanged 164 * @hide pending API review 165 */ 166 public static final int LISTEN_CELL_INFO = 0x00000400; 167 168 public PhoneStateListener() { 169 } 170 171 /** 172 * Callback invoked when device service state changes. 173 * 174 * @see ServiceState#STATE_EMERGENCY_ONLY 175 * @see ServiceState#STATE_IN_SERVICE 176 * @see ServiceState#STATE_OUT_OF_SERVICE 177 * @see ServiceState#STATE_POWER_OFF 178 */ 179 public void onServiceStateChanged(ServiceState serviceState) { 180 // default implementation empty 181 } 182 183 /** 184 * Callback invoked when network signal strength changes. 185 * 186 * @see ServiceState#STATE_EMERGENCY_ONLY 187 * @see ServiceState#STATE_IN_SERVICE 188 * @see ServiceState#STATE_OUT_OF_SERVICE 189 * @see ServiceState#STATE_POWER_OFF 190 * @deprecated Use {@link #onSignalStrengthsChanged(SignalStrength)} 191 */ 192 @Deprecated 193 public void onSignalStrengthChanged(int asu) { 194 // default implementation empty 195 } 196 197 /** 198 * Callback invoked when the message-waiting indicator changes. 199 */ 200 public void onMessageWaitingIndicatorChanged(boolean mwi) { 201 // default implementation empty 202 } 203 204 /** 205 * Callback invoked when the call-forwarding indicator changes. 206 */ 207 public void onCallForwardingIndicatorChanged(boolean cfi) { 208 // default implementation empty 209 } 210 211 /** 212 * Callback invoked when device cell location changes. 213 */ 214 public void onCellLocationChanged(CellLocation location) { 215 // default implementation empty 216 } 217 218 /** 219 * Callback invoked when device call state changes. 220 * 221 * @see TelephonyManager#CALL_STATE_IDLE 222 * @see TelephonyManager#CALL_STATE_RINGING 223 * @see TelephonyManager#CALL_STATE_OFFHOOK 224 */ 225 public void onCallStateChanged(int state, String incomingNumber) { 226 // default implementation empty 227 } 228 229 /** 230 * Callback invoked when connection state changes. 231 * 232 * @see TelephonyManager#DATA_DISCONNECTED 233 * @see TelephonyManager#DATA_CONNECTING 234 * @see TelephonyManager#DATA_CONNECTED 235 * @see TelephonyManager#DATA_SUSPENDED 236 */ 237 public void onDataConnectionStateChanged(int state) { 238 // default implementation empty 239 } 240 241 /** 242 * same as above, but with the network type. Both called. 243 */ 244 public void onDataConnectionStateChanged(int state, int networkType) { 245 } 246 247 /** 248 * Callback invoked when data activity state changes. 249 * 250 * @see TelephonyManager#DATA_ACTIVITY_NONE 251 * @see TelephonyManager#DATA_ACTIVITY_IN 252 * @see TelephonyManager#DATA_ACTIVITY_OUT 253 * @see TelephonyManager#DATA_ACTIVITY_INOUT 254 * @see TelephonyManager#DATA_ACTIVITY_DORMANT 255 */ 256 public void onDataActivity(int direction) { 257 // default implementation empty 258 } 259 260 /** 261 * Callback invoked when network signal strengths changes. 262 * 263 * @see ServiceState#STATE_EMERGENCY_ONLY 264 * @see ServiceState#STATE_IN_SERVICE 265 * @see ServiceState#STATE_OUT_OF_SERVICE 266 * @see ServiceState#STATE_POWER_OFF 267 */ 268 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 269 // default implementation empty 270 } 271 272 273 /** 274 * The Over The Air Service Provisioning (OTASP) has changed. Requires 275 * the READ_PHONE_STATE permission. 276 * @param otaspMode is integer <code>OTASP_UNKNOWN=1<code> 277 * means the value is currently unknown and the system should wait until 278 * <code>OTASP_NEEDED=2<code> or <code>OTASP_NOT_NEEDED=3<code> is received before 279 * making the decisision to perform OTASP or not. 280 * 281 * @hide 282 */ 283 public void onOtaspChanged(int otaspMode) { 284 // default implementation empty 285 } 286 287 /** 288 * Callback invoked when a observed cell info gets changed. 289 * 290 * A notification should be sent when: 291 * 1. a cell is newly-observed. 292 * 2. a observed cell is not visible. 293 * 3. any of the cell info of a observed cell has changed. 294 * 295 * @hide pending API review 296 */ 297 public void onCellInfoChanged(CellInfo cellInfo) { 298 // default implementation empty 299 } 300 301 /** 302 * The callback methods need to be called on the handler thread where 303 * this object was created. If the binder did that for us it'd be nice. 304 */ 305 IPhoneStateListener callback = new IPhoneStateListener.Stub() { 306 public void onServiceStateChanged(ServiceState serviceState) { 307 Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget(); 308 } 309 310 public void onSignalStrengthChanged(int asu) { 311 Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget(); 312 } 313 314 public void onMessageWaitingIndicatorChanged(boolean mwi) { 315 Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null) 316 .sendToTarget(); 317 } 318 319 public void onCallForwardingIndicatorChanged(boolean cfi) { 320 Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null) 321 .sendToTarget(); 322 } 323 324 public void onCellLocationChanged(Bundle bundle) { 325 CellLocation location = CellLocation.newFromBundle(bundle); 326 Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget(); 327 } 328 329 public void onCallStateChanged(int state, String incomingNumber) { 330 Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget(); 331 } 332 333 public void onDataConnectionStateChanged(int state, int networkType) { 334 Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType). 335 sendToTarget(); 336 } 337 338 public void onDataActivity(int direction) { 339 Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget(); 340 } 341 342 public void onSignalStrengthsChanged(SignalStrength signalStrength) { 343 Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget(); 344 } 345 346 public void onOtaspChanged(int otaspMode) { 347 Message.obtain(mHandler, LISTEN_OTASP_CHANGED, otaspMode, 0).sendToTarget(); 348 } 349 350 public void onCellInfoChanged(CellInfo cellInfo) { 351 Message.obtain(mHandler, LISTEN_CELL_INFO, 0, 0).sendToTarget(); 352 } 353 }; 354 355 Handler mHandler = new Handler() { 356 public void handleMessage(Message msg) { 357 //Log.d("TelephonyRegistry", "what=0x" + Integer.toHexString(msg.what) + " msg=" + msg); 358 switch (msg.what) { 359 case LISTEN_SERVICE_STATE: 360 PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj); 361 break; 362 case LISTEN_SIGNAL_STRENGTH: 363 PhoneStateListener.this.onSignalStrengthChanged(msg.arg1); 364 break; 365 case LISTEN_MESSAGE_WAITING_INDICATOR: 366 PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0); 367 break; 368 case LISTEN_CALL_FORWARDING_INDICATOR: 369 PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0); 370 break; 371 case LISTEN_CELL_LOCATION: 372 PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj); 373 break; 374 case LISTEN_CALL_STATE: 375 PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj); 376 break; 377 case LISTEN_DATA_CONNECTION_STATE: 378 PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2); 379 PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1); 380 break; 381 case LISTEN_DATA_ACTIVITY: 382 PhoneStateListener.this.onDataActivity(msg.arg1); 383 break; 384 case LISTEN_SIGNAL_STRENGTHS: 385 PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj); 386 break; 387 case LISTEN_OTASP_CHANGED: 388 PhoneStateListener.this.onOtaspChanged(msg.arg1); 389 break; 390 case LISTEN_CELL_INFO: 391 PhoneStateListener.this.onCellInfoChanged((CellInfo)msg.obj); 392 } 393 } 394 }; 395 } 396