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