ServiceStateTracker.java revision eca208fae6d1b6ae9c8c0e42eee092e86dbddbb7
1/*
2 * Copyright (C) 2006 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 com.android.internal.telephony;
18
19import android.os.AsyncResult;
20import android.os.Handler;
21import android.os.Message;
22import android.os.Registrant;
23import android.os.RegistrantList;
24import android.telephony.ServiceState;
25import android.telephony.SignalStrength;
26
27/**
28 * {@hide}
29 */
30public abstract class ServiceStateTracker extends Handler {
31
32    /**
33     *  Access technology currently in use.
34     */
35    protected static final int DATA_ACCESS_UNKNOWN = 0;
36    protected static final int DATA_ACCESS_GPRS = 1;
37    protected static final int DATA_ACCESS_EDGE = 2;
38    protected static final int DATA_ACCESS_UMTS = 3;
39    protected static final int DATA_ACCESS_CDMA_IS95A = 4;
40    protected static final int DATA_ACCESS_CDMA_IS95B = 5;
41    protected static final int DATA_ACCESS_CDMA_1xRTT = 6;
42    protected static final int DATA_ACCESS_CDMA_EvDo_0 = 7;
43    protected static final int DATA_ACCESS_CDMA_EvDo_A = 8;
44    protected static final int DATA_ACCESS_HSDPA = 9;
45    protected static final int DATA_ACCESS_HSUPA = 10;
46    protected static final int DATA_ACCESS_HSPA = 11;
47    protected static final int DATA_ACCESS_CDMA_EvDo_B = 12;
48
49    protected CommandsInterface cm;
50
51    public ServiceState ss;
52    protected ServiceState newSS;
53
54    public SignalStrength mSignalStrength;
55
56    /* The otaspMode passed to PhoneStateListener#onOtaspChanged */
57    static public final int OTASP_UNINITIALIZED = 0;
58    static public final int OTASP_UNKNOWN = 1;
59    static public final int OTASP_NEEDED = 2;
60    static public final int OTASP_NOT_NEEDED = 3;
61
62    /**
63     * A unique identifier to track requests associated with a poll
64     * and ignore stale responses.  The value is a count-down of
65     * expected responses in this pollingContext.
66     */
67    protected int[] pollingContext;
68    protected boolean mDesiredPowerState;
69
70    /**
71     * By default, strength polling is enabled.  However, if we're
72     * getting unsolicited signal strength updates from the radio, set
73     * value to true and don't bother polling any more.
74     */
75    protected boolean dontPollSignalStrength = false;
76
77    protected RegistrantList networkAttachedRegistrants = new RegistrantList();
78    protected RegistrantList roamingOnRegistrants = new RegistrantList();
79    protected RegistrantList roamingOffRegistrants = new RegistrantList();
80
81    protected  static final boolean DBG = true;
82
83    /** Signal strength poll rate. */
84    protected static final int POLL_PERIOD_MILLIS = 20 * 1000;
85
86    /** Waiting period before recheck gprs and voice registration. */
87    public static final int DEFAULT_GPRS_CHECK_PERIOD_MILLIS = 60 * 1000;
88
89    public static final int DATA_STATE_POLL_SLEEP_MS = 100;
90
91    /** GSM events */
92    protected static final int EVENT_RADIO_STATE_CHANGED               = 1;
93    protected static final int EVENT_NETWORK_STATE_CHANGED             = 2;
94    protected static final int EVENT_GET_SIGNAL_STRENGTH               = 3;
95    protected static final int EVENT_POLL_STATE_REGISTRATION           = 4;
96    protected static final int EVENT_POLL_STATE_GPRS                   = 5;
97    protected static final int EVENT_POLL_STATE_OPERATOR               = 6;
98    protected static final int EVENT_POLL_SIGNAL_STRENGTH              = 10;
99    protected static final int EVENT_NITZ_TIME                         = 11;
100    protected static final int EVENT_SIGNAL_STRENGTH_UPDATE            = 12;
101    protected static final int EVENT_RADIO_AVAILABLE                   = 13;
102    protected static final int EVENT_POLL_STATE_NETWORK_SELECTION_MODE = 14;
103    protected static final int EVENT_GET_LOC_DONE                      = 15;
104    protected static final int EVENT_SIM_RECORDS_LOADED                = 16;
105    protected static final int EVENT_SIM_READY                         = 17;
106    protected static final int EVENT_LOCATION_UPDATES_ENABLED          = 18;
107    protected static final int EVENT_GET_PREFERRED_NETWORK_TYPE        = 19;
108    protected static final int EVENT_SET_PREFERRED_NETWORK_TYPE        = 20;
109    protected static final int EVENT_RESET_PREFERRED_NETWORK_TYPE      = 21;
110    protected static final int EVENT_CHECK_REPORT_GPRS                 = 22;
111    protected static final int EVENT_RESTRICTED_STATE_CHANGED          = 23;
112
113    /** CDMA events */
114    protected static final int EVENT_POLL_STATE_REGISTRATION_CDMA      = 24;
115    protected static final int EVENT_POLL_STATE_OPERATOR_CDMA          = 25;
116    protected static final int EVENT_RUIM_READY                        = 26;
117    protected static final int EVENT_RUIM_RECORDS_LOADED               = 27;
118    protected static final int EVENT_POLL_SIGNAL_STRENGTH_CDMA         = 28;
119    protected static final int EVENT_GET_SIGNAL_STRENGTH_CDMA          = 29;
120    protected static final int EVENT_NETWORK_STATE_CHANGED_CDMA        = 30;
121    protected static final int EVENT_GET_LOC_DONE_CDMA                 = 31;
122    protected static final int EVENT_SIGNAL_STRENGTH_UPDATE_CDMA       = 32;
123    protected static final int EVENT_NV_LOADED                         = 33;
124    protected static final int EVENT_POLL_STATE_CDMA_SUBSCRIPTION      = 34;
125    protected static final int EVENT_NV_READY                          = 35;
126    protected static final int EVENT_ERI_FILE_LOADED                   = 36;
127    protected static final int EVENT_OTA_PROVISION_STATUS_CHANGE       = 37;
128    protected static final int EVENT_SET_RADIO_POWER_OFF               = 38;
129
130    protected static final String TIMEZONE_PROPERTY = "persist.sys.timezone";
131
132    /**
133     * List of ISO codes for countries that can have an offset of
134     * GMT+0 when not in daylight savings time.  This ignores some
135     * small places such as the Canary Islands (Spain) and
136     * Danmarkshavn (Denmark).  The list must be sorted by code.
137    */
138    protected static final String[] GMT_COUNTRY_CODES = {
139        "bf", // Burkina Faso
140        "ci", // Cote d'Ivoire
141        "eh", // Western Sahara
142        "fo", // Faroe Islands, Denmark
143        "gh", // Ghana
144        "gm", // Gambia
145        "gn", // Guinea
146        "gw", // Guinea Bissau
147        "ie", // Ireland
148        "lr", // Liberia
149        "is", // Iceland
150        "ma", // Morocco
151        "ml", // Mali
152        "mr", // Mauritania
153        "pt", // Portugal
154        "sl", // Sierra Leone
155        "sn", // Senegal
156        "st", // Sao Tome and Principe
157        "tg", // Togo
158        "uk", // U.K
159    };
160
161    /** Reason for registration denial. */
162    protected static final String REGISTRATION_DENIED_GEN  = "General";
163    protected static final String REGISTRATION_DENIED_AUTH = "Authentication Failure";
164
165    public ServiceStateTracker() {
166
167    }
168
169    public boolean getDesiredPowerState() {
170        return mDesiredPowerState;
171    }
172
173    /**
174     * Registration point for combined roaming on
175     * combined roaming is true when roaming is true and ONS differs SPN
176     *
177     * @param h handler to notify
178     * @param what what code of message when delivered
179     * @param obj placed in Message.obj
180     */
181    public  void registerForRoamingOn(Handler h, int what, Object obj) {
182        Registrant r = new Registrant(h, what, obj);
183        roamingOnRegistrants.add(r);
184
185        if (ss.getRoaming()) {
186            r.notifyRegistrant();
187        }
188    }
189
190    public  void unregisterForRoamingOn(Handler h) {
191        roamingOnRegistrants.remove(h);
192    }
193
194    /**
195     * Registration point for combined roaming off
196     * combined roaming is true when roaming is true and ONS differs SPN
197     *
198     * @param h handler to notify
199     * @param what what code of message when delivered
200     * @param obj placed in Message.obj
201     */
202    public  void registerForRoamingOff(Handler h, int what, Object obj) {
203        Registrant r = new Registrant(h, what, obj);
204        roamingOffRegistrants.add(r);
205
206        if (!ss.getRoaming()) {
207            r.notifyRegistrant();
208        }
209    }
210
211    public  void unregisterForRoamingOff(Handler h) {
212        roamingOffRegistrants.remove(h);
213    }
214
215    /**
216     * Re-register network by toggling preferred network type.
217     * This is a work-around to deregister and register network since there is
218     * no ril api to set COPS=2 (deregister) only.
219     *
220     * @param onComplete is dispatched when this is complete.  it will be
221     * an AsyncResult, and onComplete.obj.exception will be non-null
222     * on failure.
223     */
224    public void reRegisterNetwork(Message onComplete) {
225        cm.getPreferredNetworkType(
226                obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE, onComplete));
227    }
228
229    public void
230    setRadioPower(boolean power) {
231        mDesiredPowerState = power;
232
233        setPowerStateToDesired();
234    }
235
236    /**
237     * These two flags manage the behavior of the cell lock -- the
238     * lock should be held if either flag is true.  The intention is
239     * to allow temporary acquisition of the lock to get a single
240     * update.  Such a lock grab and release can thus be made to not
241     * interfere with more permanent lock holds -- in other words, the
242     * lock will only be released if both flags are false, and so
243     * releases by temporary users will only affect the lock state if
244     * there is no continuous user.
245     */
246    private boolean mWantContinuousLocationUpdates;
247    private boolean mWantSingleLocationUpdate;
248
249    public void enableSingleLocationUpdate() {
250        if (mWantSingleLocationUpdate || mWantContinuousLocationUpdates) return;
251        mWantSingleLocationUpdate = true;
252        cm.setLocationUpdates(true, obtainMessage(EVENT_LOCATION_UPDATES_ENABLED));
253    }
254
255    public void enableLocationUpdates() {
256        if (mWantSingleLocationUpdate || mWantContinuousLocationUpdates) return;
257        mWantContinuousLocationUpdates = true;
258        cm.setLocationUpdates(true, obtainMessage(EVENT_LOCATION_UPDATES_ENABLED));
259    }
260
261    protected void disableSingleLocationUpdate() {
262        mWantSingleLocationUpdate = false;
263        if (!mWantSingleLocationUpdate && !mWantContinuousLocationUpdates) {
264            cm.setLocationUpdates(false, null);
265        }
266    }
267
268    public void disableLocationUpdates() {
269        mWantContinuousLocationUpdates = false;
270        if (!mWantSingleLocationUpdate && !mWantContinuousLocationUpdates) {
271            cm.setLocationUpdates(false, null);
272        }
273    }
274
275    public abstract void handleMessage(Message msg);
276
277    protected abstract Phone getPhone();
278    protected abstract void handlePollStateResult(int what, AsyncResult ar);
279    protected abstract void updateSpnDisplay();
280    protected abstract void setPowerStateToDesired();
281    protected abstract void log(String s);
282
283    /**
284     * Clean up existing voice and data connection then turn off radio power.
285     *
286     * Hang up the existing voice calls to decrease call drop rate.
287     */
288    protected abstract void powerOffRadioSafely();
289
290    /** Cancel a pending (if any) pollState() operation */
291    protected void cancelPollState() {
292        // This will effectively cancel the rest of the poll requests.
293        pollingContext = new int[1];
294    }
295}
296