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