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