TelephonyManager.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.Bundle;
24import android.os.RemoteException;
25import android.os.SystemProperties;
26import android.os.ServiceManager;
27
28import com.android.internal.telephony.*;
29import android.telephony.CellLocation;
30import android.telephony.ServiceState;
31
32import java.util.ArrayList;
33
34/**
35 * Provides access to information about the telephony services on
36 * the device. Applications can use the methods in this class to
37 * determine telephony services and states, as well as to access some
38 * types of subscriber information. Applications can also register
39 * a listener to receive notification of telephony state changes.
40 * <p>
41 * You do not instantiate this class directly; instead, you retrieve
42 * a reference to an instance through
43 * {@link android.content.Context#getSystemService
44 * Context.getSystemService(Context.TELEPHONY_SERVICE)}.
45 * <p>
46 * Note that acess to some telephony information is
47 * permission-protected. Your application cannot access the protected
48 * information unless it has the appropriate permissions declared in
49 * its manifest file. Where permissions apply, they are noted in the
50 * the methods through which you access the protected information.
51 */
52public class TelephonyManager {
53    private static final String TAG = "TelephonyManager";
54
55    private Context mContext;
56    private ITelephonyRegistry mRegistry;
57
58    /** @hide */
59    public TelephonyManager(Context context) {
60        mContext = context;
61        mRegistry = ITelephonyRegistry.Stub.asInterface(ServiceManager.getService(
62                    "telephony.registry"));
63    }
64
65    /** @hide */
66    private TelephonyManager() {
67    }
68
69    private static TelephonyManager sInstance = new TelephonyManager();
70
71    /** @hide */
72    public static TelephonyManager getDefault() {
73        return sInstance;
74    }
75
76    //
77    //
78    // Device Info
79    //
80    //
81
82    /**
83     * Returns the software version number for the device, for example,
84     * the IMEI/SV for GSM phones.
85     *
86     * <p>Requires Permission:
87     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
88     */
89    public String getDeviceSoftwareVersion() {
90        try {
91            return getSubscriberInfo().getDeviceSvn();
92        } catch (RemoteException ex) {
93        }
94        return null;
95    }
96
97    /**
98     * Returns the unique device ID, for example,the IMEI for GSM
99     * phones.
100     *
101     * <p>Requires Permission:
102     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
103     */
104    public String getDeviceId() {
105        try {
106            return getSubscriberInfo().getDeviceId();
107        } catch (RemoteException ex) {
108        }
109        return null;
110    }
111
112    /**
113     * Returns the current location of the device.
114     *
115     * <p>Requires Permission: {@link android.Manifest.permission#ACCESS_COARSE_LOCATION
116     * ACCESS_COARSE_LOCATION}.
117     */
118    public CellLocation getCellLocation() {
119        try {
120            Bundle bundle = getITelephony().getCellLocation();
121            return CellLocation.newFromBundle(bundle);
122        } catch (RemoteException ex) {
123        }
124        return null;
125    }
126
127    /**
128     * Enables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
129     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
130     *
131     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
132     * CONTROL_LOCATION_UPDATES}
133     *
134     * @hide
135     */
136    public void enableLocationUpdates() {
137        try {
138            getITelephony().enableLocationUpdates();
139        } catch (RemoteException ex) {
140        }
141    }
142
143    /**
144     * Disables location update notifications.  {@link PhoneStateListener#onCellLocationChanged
145     * PhoneStateListener.onCellLocationChanged} will be called on location updates.
146     *
147     * <p>Requires Permission: {@link android.Manifest.permission#CONTROL_LOCATION_UPDATES
148     * CONTROL_LOCATION_UPDATES}
149     *
150     * @hide
151     */
152    public void disableLocationUpdates() {
153        try {
154            getITelephony().disableLocationUpdates();
155        } catch (RemoteException ex) {
156        }
157    }
158
159    /**
160     * No phone module
161     */
162    public static final int PHONE_TYPE_NONE = 0;
163
164    /**
165     * GSM phone
166     */
167    public static final int PHONE_TYPE_GSM = 1;
168
169    /**
170     * Returns a constant indicating the device phone type.
171     *
172     * @see #PHONE_TYPE_NONE
173     * @see #PHONE_TYPE_GSM
174     */
175    public int getPhoneType() {
176        // in the future, we should really check this
177        return PHONE_TYPE_GSM;
178    }
179
180    //
181    //
182    // Current Network
183    //
184    //
185
186    /**
187     * Returns the alphabetic name of current registered operator.
188     * <p>
189     * Availability: Only when user is registered to a network
190     */
191    public String getNetworkOperatorName() {
192        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ALPHA);
193    }
194
195    /**
196     * Returns the numeric name (MCC+MNC) of current registered operator.
197     * <p>
198     * Availability: Only when user is registered to a network
199     */
200    public String getNetworkOperator() {
201        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_NUMERIC);
202    }
203
204    /**
205     * Returns true if the device is considered roaming on the current
206     * network, for GSM purposes.
207     * <p>
208     * Availability: Only when user registered to a network
209     */
210    public boolean isNetworkRoaming() {
211        return "true".equals(SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISROAMING));
212    }
213
214    /**
215     * Returns the ISO country code equivilent of the current registered
216     * operator's MCC (Mobile Country Code).
217     * <p>
218     * Availability: Only when user is registered to a network
219     */
220    public String getNetworkCountryIso() {
221        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISO_COUNTRY);
222    }
223
224    /** Network type is unknown */
225    public static final int NETWORK_TYPE_UNKNOWN = 0;
226    /** Current network is GPRS */
227    public static final int NETWORK_TYPE_GPRS = 1;
228    /** Current network is EDGE */
229    public static final int NETWORK_TYPE_EDGE = 2;
230    /** Current network is UMTS */
231    public static final int NETWORK_TYPE_UMTS = 3;
232
233    /**
234     * Returns a constant indicating the radio technology (network type)
235     * currently in use on the device.
236     *
237     * @see #NETWORK_TYPE_UNKNOWN
238     * @see #NETWORK_TYPE_GPRS
239     * @see #NETWORK_TYPE_EDGE
240     * @see #NETWORK_TYPE_UMTS
241     */
242    public int getNetworkType() {
243        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE);
244        if ("GPRS".equals(prop)) {
245            return NETWORK_TYPE_GPRS;
246        }
247        else if ("EDGE".equals(prop)) {
248            return NETWORK_TYPE_EDGE;
249        }
250        else if ("UMTS".equals(prop)) {
251            return NETWORK_TYPE_UMTS;
252        }
253        else {
254            return NETWORK_TYPE_UNKNOWN;
255        }
256    }
257
258    //
259    //
260    // SIM Card
261    //
262    //
263
264    /** SIM card state: Unknown. Signifies that the SIM is in transition
265     *  between states. For example, when the user inputs the SIM pin
266     *  under PIN_REQUIRED state, a query for sim status returns
267     *  this state before turning to SIM_STATE_READY. */
268    public static final int SIM_STATE_UNKNOWN = 0;
269    /** SIM card state: no SIM card is available in the device */
270    public static final int SIM_STATE_ABSENT = 1;
271    /** SIM card state: Locked: requires the user's SIM PIN to unlock */
272    public static final int SIM_STATE_PIN_REQUIRED = 2;
273    /** SIM card state: Locked: requires the user's SIM PUK to unlock */
274    public static final int SIM_STATE_PUK_REQUIRED = 3;
275    /** SIM card state: Locked: requries a network PIN to unlock */
276    public static final int SIM_STATE_NETWORK_LOCKED = 4;
277    /** SIM card state: Ready */
278    public static final int SIM_STATE_READY = 5;
279
280    /**
281     * Returns a constant indicating the state of the
282     * device SIM card.
283     *
284     * @see #SIM_STATE_UNKNOWN
285     * @see #SIM_STATE_ABSENT
286     * @see #SIM_STATE_PIN_REQUIRED
287     * @see #SIM_STATE_PUK_REQUIRED
288     * @see #SIM_STATE_NETWORK_LOCKED
289     * @see #SIM_STATE_READY
290     */
291    public int getSimState() {
292        String prop = SystemProperties.get(TelephonyProperties.PROPERTY_SIM_STATE);
293        if ("ABSENT".equals(prop)) {
294            return SIM_STATE_ABSENT;
295        }
296        else if ("PIN_REQUIRED".equals(prop)) {
297            return SIM_STATE_PIN_REQUIRED;
298        }
299        else if ("PUK_REQUIRED".equals(prop)) {
300            return SIM_STATE_PUK_REQUIRED;
301        }
302        else if ("NETWORK_LOCKED".equals(prop)) {
303            return SIM_STATE_NETWORK_LOCKED;
304        }
305        else if ("READY".equals(prop)) {
306            return SIM_STATE_READY;
307        }
308        else {
309            return SIM_STATE_UNKNOWN;
310        }
311    }
312
313    /**
314     * Returns the MCC+MNC (mobile country code + mobile network code) of the
315     * provider of the SIM. 5 or 6 decimal digits.
316     * <p>
317     * Availability: SIM state must be {@link #SIM_STATE_READY}
318     *
319     * @see #getSimState
320     */
321    public String getSimOperator() {
322        return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC);
323    }
324
325    /**
326     * Returns the Service Provider Name (SPN).
327     * <p>
328     * Availability: SIM state must be {@link #SIM_STATE_READY}
329     *
330     * @see #getSimState
331     */
332    public String getSimOperatorName() {
333        return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ALPHA);
334    }
335
336    /**
337     * Returns the ISO country code equivalent for the SIM provider's country code.
338     */
339    public String getSimCountryIso() {
340        return SystemProperties.get(TelephonyProperties.PROPERTY_SIM_OPERATOR_ISO_COUNTRY);
341    }
342
343    /**
344     * Returns the serial number of the SIM, if applicable.
345     * <p>
346     * Requires Permission:
347     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
348     */
349    public String getSimSerialNumber() {
350        try {
351            return getSubscriberInfo().getSimSerialNumber();
352        } catch (RemoteException ex) {
353        }
354        return null;
355    }
356
357    //
358    //
359    // Subscriber Info
360    //
361    //
362
363    /**
364     * Returns the unique subscriber ID, for example, the IMSI for a GSM phone.
365     * <p>
366     * Requires Permission:
367     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
368     */
369    public String getSubscriberId() {
370        try {
371            return getSubscriberInfo().getSubscriberId();
372        } catch (RemoteException ex) {
373        }
374        return null;
375    }
376
377    /**
378     * Returns the phone number string for line 1, for example, the MSISDN
379     * for a GSM phone.
380     * <p>
381     * Requires Permission:
382     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
383     */
384    public String getLine1Number() {
385        try {
386            return getSubscriberInfo().getLine1Number();
387        } catch (RemoteException ex) {
388        }
389        return null;
390    }
391
392    /**
393     * Returns the alphabetic identifier associated with the line 1 number.
394     * <p>
395     * Requires Permission:
396     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
397     * @hide
398     * nobody seems to call this.
399     */
400    public String getLine1AlphaTag() {
401        try {
402            return getSubscriberInfo().getLine1AlphaTag();
403        } catch (RemoteException ex) {
404        }
405        return null;
406    }
407
408    /**
409     * Returns the voice mail number.
410     * <p>
411     * Requires Permission:
412     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
413     */
414    public String getVoiceMailNumber() {
415        try {
416            return getSubscriberInfo().getVoiceMailNumber();
417        } catch (RemoteException ex) {
418        }
419        return null;
420    }
421
422    /**
423     * Retrieves the alphabetic identifier associated with the voice
424     * mail number.
425     * <p>
426     * Requires Permission:
427     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
428     */
429    public String getVoiceMailAlphaTag() {
430        try {
431            return getSubscriberInfo().getVoiceMailAlphaTag();
432        } catch (RemoteException ex) {
433        }
434        return null;
435    }
436
437    private IPhoneSubInfo getSubscriberInfo() {
438        // get it each time because that process crashes a lot
439        return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService("iphonesubinfo"));
440    }
441
442
443    /** Device call state: No activity. */
444    public static final int CALL_STATE_IDLE = 0;
445    /** Device call state: Ringing. A new call arrived and is
446     *  ringing or waiting. In the latter case, another call is
447     *  already active. */
448    public static final int CALL_STATE_RINGING = 1;
449    /** Device call state: Off-hook. At least one call exists
450      * that is dialing, active, or on hold, and no calls are ringing
451      * or waiting. */
452    public static final int CALL_STATE_OFFHOOK = 2;
453
454    /**
455     * Returns a constant indicating the call state (cellular) on the device.
456     */
457    public int getCallState() {
458        try {
459            return getITelephony().getCallState();
460        } catch (RemoteException ex) {
461            // the phone process is restarting.
462            return CALL_STATE_IDLE;
463        }
464    }
465
466    /** Data connection activity: No traffic. */
467    public static final int DATA_ACTIVITY_NONE = 0x00000000;
468    /** Data connection activity: Currently receiving IP PPP traffic. */
469    public static final int DATA_ACTIVITY_IN = 0x00000001;
470    /** Data connection activity: Currently sending IP PPP traffic. */
471    public static final int DATA_ACTIVITY_OUT = 0x00000002;
472    /** Data connection activity: Currently both sending and receiving
473     *  IP PPP traffic. */
474    public static final int DATA_ACTIVITY_INOUT = DATA_ACTIVITY_IN | DATA_ACTIVITY_OUT;
475
476    /**
477     * Returns a constant indicating the type of activity on a data connection
478     * (cellular).
479     *
480     * @see #DATA_ACTIVITY_NONE
481     * @see #DATA_ACTIVITY_IN
482     * @see #DATA_ACTIVITY_OUT
483     * @see #DATA_ACTIVITY_INOUT
484     */
485    public int getDataActivity() {
486        try {
487            return getITelephony().getDataActivity();
488        } catch (RemoteException ex) {
489            // the phone process is restarting.
490            return DATA_ACTIVITY_NONE;
491        }
492    }
493
494    /** Data connection state: Disconnected. IP traffic not available. */
495    public static final int DATA_DISCONNECTED   = 0;
496    /** Data connection state: Currently setting up a data connection. */
497    public static final int DATA_CONNECTING     = 1;
498    /** Data connection state: Connected. IP traffic should be available. */
499    public static final int DATA_CONNECTED      = 2;
500    /** Data connection state: Suspended. The connection is up, but IP
501     * traffic is temporarily unavailable. For example, in a 2G network,
502     * data activity may be suspended when a voice call arrives. */
503    public static final int DATA_SUSPENDED      = 3;
504
505    /**
506     * Returns a constant indicating the current data connection state
507     * (cellular).
508     *
509     * @see #DATA_DISCONNECTED
510     * @see #DATA_CONNECTING
511     * @see #DATA_CONNECTED
512     * @see #DATA_SUSPENDED
513     */
514    public int getDataState() {
515        try {
516            return getITelephony().getDataState();
517        } catch (RemoteException ex) {
518            // the phone process is restarting.
519            return DATA_DISCONNECTED;
520        }
521    }
522
523    private ITelephony getITelephony() {
524        return ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE));
525    }
526
527    //
528    //
529    // PhoneStateListener
530    //
531    //
532
533    /**
534     * Registers a listener object to receive notification of changes
535     * in specified telephony states.
536     * <p>
537     * To register a listener, pass a {@link PhoneStateListener}
538     * and specify at least one telephony state of interest in
539     * the events argument.
540     *
541     * At registration, and when a specified telephony state
542     * changes, the telephony manager invokes the appropriate
543     * callback method on the listener object and passes the
544     * current (udpated) values.
545     * <p>
546     * To unregister a listener, pass the listener object and set the
547     * events argument to
548     * {@link PhoneStateListener#LISTEN_NONE LISTEN_NONE} (0).
549     *
550     * @param listener The {@link PhoneStateListener} object to register
551     *                 (or unregister)
552     * @param events The telephony state(s) of interest to the listener,
553     *               as a bitwise-OR combination of {@link PhoneStateListener}
554     *               LISTEN_ flags.
555     */
556    public void listen(PhoneStateListener listener, int events) {
557        String pkgForDebug = mContext != null ? mContext.getPackageName() : "<unknown>";
558        try {
559            mRegistry.listen(pkgForDebug, listener.callback, events, true);
560        } catch (RemoteException ex) {
561            // system process dead
562        }
563    }
564}
565
566/*
567 * These have not been implemented since they're not used anywhere in the system
568 * and it's impossible to tell if they're working or not.  The comments in SystemProperties
569 * were wrong in at least one case, so I don't trust them.  They claimed that
570 * PROPERTY_OPERATOR_ISROAMING was "1" if it was true, but it's actually set to "true."
571 */
572
573
574    /* Set to '1' if voice mail is waiting, otherwise false */
575    /*
576    public boolean isVoiceMailWaiting() {
577        return "1".equals(
578                SystemProperties.get(TelephonyProperties.PROPERTY_LINE1_VOICE_MAIL_WAITING));
579    }
580    */
581
582    /* Set to 'true' if unconditional voice call forwarding is enabled
583     *  Availablity: only if configured in SIM; SIM state must be "READY"
584     */
585    /*
586    public boolean isCallForwarding() {
587        SystemProperties.get(TelephonyProperties.PROPERTY_LINE1_VOICE_CALL_FORWARDING);
588    }
589    */
590
591    /* '1' if the current network is the result of a manual network selection.
592     *  Availability: when registered to a network
593     */
594    /*
595    public boolean isNetworkManual() {
596        return SystemProperties.get(TelephonyProperties.PROPERTY_OPERATOR_ISMANUAL);
597    }
598    */
599
600
601/*
602 * These have not been implemented because they're not public IMHO. -joeo
603 */
604
605    /*
606     * Baseband version
607     * Availability: property is available any time radio is on
608     */
609    /*
610    public String getBasebandVersion() {
611        return SystemProperties.get(TelephonyProperties.PROPERTY_BASEBAND_VERSION);
612    }
613    */
614
615    /*
616     * Radio Interface Layer (RIL) library implementation.
617     */
618    /*
619    public String getRilLibrary() {
620        return SystemProperties.get(TelephonyProperties.PROPERTY_RIL_IMPL);
621    }
622    */
623
624