PhoneStateListener.java revision fb40dd4d00bd3361b2535bc866e6c21eadc52558
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.CellLocation;
24import android.telephony.CellInfo;
25import android.telephony.VoLteServiceState;
26import android.telephony.Rlog;
27import android.telephony.ServiceState;
28import android.telephony.SignalStrength;
29import android.telephony.SubscriptionManager;
30import android.telephony.PreciseCallState;
31import android.telephony.PreciseDataConnectionState;
32
33import com.android.internal.telephony.IPhoneStateListener;
34
35import java.util.List;
36
37/**
38 * A listener class for monitoring changes in specific telephony states
39 * on the device, including service state, signal strength, message
40 * waiting indicator (voicemail), and others.
41 * <p>
42 * Override the methods for the state that you wish to receive updates for, and
43 * pass your PhoneStateListener object, along with bitwise-or of the LISTEN_
44 * flags to {@link TelephonyManager#listen TelephonyManager.listen()}.
45 * <p>
46 * Note that access to some telephony information is
47 * permission-protected. Your application won't receive updates for protected
48 * information unless it has the appropriate permissions declared in
49 * its manifest file. Where permissions apply, they are noted in the
50 * appropriate LISTEN_ flags.
51 */
52public class PhoneStateListener {
53    private static final String TAG = "PhoneStateListener";
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     * Subscription used to listen to the phone state changes
216     * @hide
217     */
218    /** @hide */
219    protected long mSubId = 0;
220
221    private final Handler mHandler;
222
223    public PhoneStateListener() {
224        this(SubscriptionManager.DEFAULT_SUB_ID, Looper.myLooper());
225    }
226
227    /**
228     * @hide
229     */
230    public PhoneStateListener(long subId) {
231        this(subId, Looper.myLooper());
232    }
233
234    /** @hide */
235    public PhoneStateListener(long subId, Looper looper) {
236        Rlog.d(TAG, "ctor: subId=" + subId + " looper=" + looper);
237        mSubId = subId;
238        mHandler = new Handler(looper) {
239            public void handleMessage(Message msg) {
240                Rlog.d(TAG, "mSubId=" + mSubId + " what=0x" + Integer.toHexString(msg.what)
241                 + " msg=" + msg);
242                switch (msg.what) {
243                    case LISTEN_SERVICE_STATE:
244                        PhoneStateListener.this.onServiceStateChanged((ServiceState)msg.obj);
245                        break;
246                    case LISTEN_SIGNAL_STRENGTH:
247                        PhoneStateListener.this.onSignalStrengthChanged(msg.arg1);
248                        break;
249                    case LISTEN_MESSAGE_WAITING_INDICATOR:
250                        PhoneStateListener.this.onMessageWaitingIndicatorChanged(msg.arg1 != 0);
251                        break;
252                    case LISTEN_CALL_FORWARDING_INDICATOR:
253                        PhoneStateListener.this.onCallForwardingIndicatorChanged(msg.arg1 != 0);
254                        break;
255                    case LISTEN_CELL_LOCATION:
256                        PhoneStateListener.this.onCellLocationChanged((CellLocation)msg.obj);
257                        break;
258                    case LISTEN_CALL_STATE:
259                        PhoneStateListener.this.onCallStateChanged(msg.arg1, (String)msg.obj);
260                        break;
261                    case LISTEN_DATA_CONNECTION_STATE:
262                        PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1, msg.arg2);
263                        PhoneStateListener.this.onDataConnectionStateChanged(msg.arg1);
264                        break;
265                    case LISTEN_DATA_ACTIVITY:
266                        PhoneStateListener.this.onDataActivity(msg.arg1);
267                        break;
268                    case LISTEN_SIGNAL_STRENGTHS:
269                        PhoneStateListener.this.onSignalStrengthsChanged((SignalStrength)msg.obj);
270                        break;
271                    case LISTEN_OTASP_CHANGED:
272                        PhoneStateListener.this.onOtaspChanged(msg.arg1);
273                        break;
274                    case LISTEN_CELL_INFO:
275                        PhoneStateListener.this.onCellInfoChanged((List<CellInfo>)msg.obj);
276                        break;
277                    case LISTEN_PRECISE_CALL_STATE:
278                        PhoneStateListener.this.onPreciseCallStateChanged((PreciseCallState)msg.obj);
279                        break;
280                    case LISTEN_PRECISE_DATA_CONNECTION_STATE:
281                        PhoneStateListener.this.onPreciseDataConnectionStateChanged(
282                                (PreciseDataConnectionState)msg.obj);
283                        break;
284                    case LISTEN_DATA_CONNECTION_REAL_TIME_INFO:
285                        PhoneStateListener.this.onDataConnectionRealTimeInfoChanged(
286                                (DataConnectionRealTimeInfo)msg.obj);
287                        break;
288                    case LISTEN_VOLTE_STATE:
289                        PhoneStateListener.this.onVoLteServiceStateChanged((VoLteServiceState)msg.obj);
290                        break;
291                }
292            }
293        };
294    }
295
296    /**
297     * Callback invoked when device service state changes.
298     *
299     * @see ServiceState#STATE_EMERGENCY_ONLY
300     * @see ServiceState#STATE_IN_SERVICE
301     * @see ServiceState#STATE_OUT_OF_SERVICE
302     * @see ServiceState#STATE_POWER_OFF
303     */
304    public void onServiceStateChanged(ServiceState serviceState) {
305        // default implementation empty
306    }
307
308    /**
309     * Callback invoked when network signal strength changes.
310     *
311     * @see ServiceState#STATE_EMERGENCY_ONLY
312     * @see ServiceState#STATE_IN_SERVICE
313     * @see ServiceState#STATE_OUT_OF_SERVICE
314     * @see ServiceState#STATE_POWER_OFF
315     * @deprecated Use {@link #onSignalStrengthsChanged(SignalStrength)}
316     */
317    @Deprecated
318    public void onSignalStrengthChanged(int asu) {
319        // default implementation empty
320    }
321
322    /**
323     * Callback invoked when the message-waiting indicator changes.
324     */
325    public void onMessageWaitingIndicatorChanged(boolean mwi) {
326        // default implementation empty
327    }
328
329    /**
330     * Callback invoked when the call-forwarding indicator changes.
331     */
332    public void onCallForwardingIndicatorChanged(boolean cfi) {
333        // default implementation empty
334    }
335
336    /**
337     * Callback invoked when device cell location changes.
338     */
339    public void onCellLocationChanged(CellLocation location) {
340        // default implementation empty
341    }
342
343    /**
344     * Callback invoked when device call state changes.
345     *
346     * @see TelephonyManager#CALL_STATE_IDLE
347     * @see TelephonyManager#CALL_STATE_RINGING
348     * @see TelephonyManager#CALL_STATE_OFFHOOK
349     */
350    public void onCallStateChanged(int state, String incomingNumber) {
351        // default implementation empty
352    }
353
354    /**
355     * Callback invoked when connection state changes.
356     *
357     * @see TelephonyManager#DATA_DISCONNECTED
358     * @see TelephonyManager#DATA_CONNECTING
359     * @see TelephonyManager#DATA_CONNECTED
360     * @see TelephonyManager#DATA_SUSPENDED
361     */
362    public void onDataConnectionStateChanged(int state) {
363        // default implementation empty
364    }
365
366    /**
367     * same as above, but with the network type.  Both called.
368     */
369    public void onDataConnectionStateChanged(int state, int networkType) {
370    }
371
372    /**
373     * Callback invoked when data activity state changes.
374     *
375     * @see TelephonyManager#DATA_ACTIVITY_NONE
376     * @see TelephonyManager#DATA_ACTIVITY_IN
377     * @see TelephonyManager#DATA_ACTIVITY_OUT
378     * @see TelephonyManager#DATA_ACTIVITY_INOUT
379     * @see TelephonyManager#DATA_ACTIVITY_DORMANT
380     */
381    public void onDataActivity(int direction) {
382        // default implementation empty
383    }
384
385    /**
386     * Callback invoked when network signal strengths changes.
387     *
388     * @see ServiceState#STATE_EMERGENCY_ONLY
389     * @see ServiceState#STATE_IN_SERVICE
390     * @see ServiceState#STATE_OUT_OF_SERVICE
391     * @see ServiceState#STATE_POWER_OFF
392     */
393    public void onSignalStrengthsChanged(SignalStrength signalStrength) {
394        // default implementation empty
395    }
396
397
398    /**
399     * The Over The Air Service Provisioning (OTASP) has changed. Requires
400     * the READ_PHONE_STATE permission.
401     * @param otaspMode is integer <code>OTASP_UNKNOWN=1<code>
402     *   means the value is currently unknown and the system should wait until
403     *   <code>OTASP_NEEDED=2<code> or <code>OTASP_NOT_NEEDED=3<code> is received before
404     *   making the decision to perform OTASP or not.
405     *
406     * @hide
407     */
408    public void onOtaspChanged(int otaspMode) {
409        // default implementation empty
410    }
411
412    /**
413     * Callback invoked when a observed cell info has changed,
414     * or new cells have been added or removed.
415     * @param cellInfo is the list of currently visible cells.
416     */
417    public void onCellInfoChanged(List<CellInfo> cellInfo) {
418    }
419
420    /**
421     * Callback invoked when precise device call state changes.
422     *
423     * @hide
424     */
425    public void onPreciseCallStateChanged(PreciseCallState callState) {
426        // default implementation empty
427    }
428
429    /**
430     * Callback invoked when data connection state changes with precise information.
431     *
432     * @hide
433     */
434    public void onPreciseDataConnectionStateChanged(
435            PreciseDataConnectionState dataConnectionState) {
436        // default implementation empty
437    }
438
439    /**
440     * Callback invoked when data connection state changes with precise information.
441     *
442     * @hide
443     */
444    public void onDataConnectionRealTimeInfoChanged(
445            DataConnectionRealTimeInfo dcRtInfo) {
446        // default implementation empty
447    }
448
449    /**
450     * Callback invoked when the service state of LTE network
451     * related to the VoLTE service has changed.
452     * @param stateInfo is the current LTE network information
453     * @hide
454     */
455    public void onVoLteServiceStateChanged(VoLteServiceState stateInfo) {
456    }
457
458    /**
459     * The callback methods need to be called on the handler thread where
460     * this object was created.  If the binder did that for us it'd be nice.
461     */
462    IPhoneStateListener callback = new IPhoneStateListener.Stub() {
463        public void onServiceStateChanged(ServiceState serviceState) {
464            Message.obtain(mHandler, LISTEN_SERVICE_STATE, 0, 0, serviceState).sendToTarget();
465        }
466
467        public void onSignalStrengthChanged(int asu) {
468            Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTH, asu, 0, null).sendToTarget();
469        }
470
471        public void onMessageWaitingIndicatorChanged(boolean mwi) {
472            Message.obtain(mHandler, LISTEN_MESSAGE_WAITING_INDICATOR, mwi ? 1 : 0, 0, null)
473                    .sendToTarget();
474        }
475
476        public void onCallForwardingIndicatorChanged(boolean cfi) {
477            Message.obtain(mHandler, LISTEN_CALL_FORWARDING_INDICATOR, cfi ? 1 : 0, 0, null)
478                    .sendToTarget();
479        }
480
481        public void onCellLocationChanged(Bundle bundle) {
482            CellLocation location = CellLocation.newFromBundle(bundle);
483            Message.obtain(mHandler, LISTEN_CELL_LOCATION, 0, 0, location).sendToTarget();
484        }
485
486        public void onCallStateChanged(int state, String incomingNumber) {
487            Message.obtain(mHandler, LISTEN_CALL_STATE, state, 0, incomingNumber).sendToTarget();
488        }
489
490        public void onDataConnectionStateChanged(int state, int networkType) {
491            Message.obtain(mHandler, LISTEN_DATA_CONNECTION_STATE, state, networkType).
492                    sendToTarget();
493        }
494
495        public void onDataActivity(int direction) {
496            Message.obtain(mHandler, LISTEN_DATA_ACTIVITY, direction, 0, null).sendToTarget();
497        }
498
499        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
500            Message.obtain(mHandler, LISTEN_SIGNAL_STRENGTHS, 0, 0, signalStrength).sendToTarget();
501        }
502
503        public void onOtaspChanged(int otaspMode) {
504            Message.obtain(mHandler, LISTEN_OTASP_CHANGED, otaspMode, 0).sendToTarget();
505        }
506
507        public void onCellInfoChanged(List<CellInfo> cellInfo) {
508            Message.obtain(mHandler, LISTEN_CELL_INFO, 0, 0, cellInfo).sendToTarget();
509        }
510
511        public void onPreciseCallStateChanged(PreciseCallState callState) {
512            Message.obtain(mHandler, LISTEN_PRECISE_CALL_STATE, 0, 0, callState).sendToTarget();
513        }
514
515        public void onPreciseDataConnectionStateChanged(
516                PreciseDataConnectionState dataConnectionState) {
517            Message.obtain(mHandler, LISTEN_PRECISE_DATA_CONNECTION_STATE, 0, 0,
518                    dataConnectionState).sendToTarget();
519        }
520
521        public void onDataConnectionRealTimeInfoChanged(
522                DataConnectionRealTimeInfo dcRtInfo) {
523            Message.obtain(mHandler, LISTEN_DATA_CONNECTION_REAL_TIME_INFO, 0, 0,
524                    dcRtInfo).sendToTarget();
525        }
526
527        public void onVoLteServiceStateChanged(VoLteServiceState lteState) {
528            Message.obtain(mHandler, LISTEN_VOLTE_STATE, 0, 0, lteState).sendToTarget();
529        }
530    };
531}
532