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