2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.internal.telephony;
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;
30 public abstract class ServiceStateTracker extends Handler {
33 * Access technology currently in use.
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;
49 protected CommandsInterface cm;
51 public ServiceState ss;
52 protected ServiceState newSS;
54 public SignalStrength mSignalStrength;
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;
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.
67 protected int[] pollingContext;
68 protected boolean mDesiredPowerState;
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.
75 protected boolean dontPollSignalStrength = false;
77 protected RegistrantList networkAttachedRegistrants = new RegistrantList();
78 protected RegistrantList roamingOnRegistrants = new RegistrantList();
79 protected RegistrantList roamingOffRegistrants = new RegistrantList();
81 protected static final boolean DBG = true;
83 /** Signal strength poll rate. */
84 protected static final int POLL_PERIOD_MILLIS = 20 * 1000;
86 /** Waiting period before recheck gprs and voice registration. */
87 public static final int DEFAULT_GPRS_CHECK_PERIOD_MILLIS = 60 * 1000;
89 public static final int DATA_STATE_POLL_SLEEP_MS = 100;
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;
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;
130 protected static final String TIMEZONE_PROPERTY = "persist.sys.timezone";
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.
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
146 "gw", // Guinea Bissau
154 "sl", // Sierra Leone
156 "st", // Sao Tome and Principe
161 /** Reason for registration denial. */
162 protected static final String REGISTRATION_DENIED_GEN = "General";
163 protected static final String REGISTRATION_DENIED_AUTH = "Authentication Failure";
165 public ServiceStateTracker() {
169 public boolean getDesiredPowerState() {
170 return mDesiredPowerState;
174 * Registration point for combined roaming on
175 * combined roaming is true when roaming is true and ONS differs SPN
177 * @param h handler to notify
178 * @param what what code of message when delivered
179 * @param obj placed in Message.obj
181 public void registerForRoamingOn(Handler h, int what, Object obj) {
182 Registrant r = new Registrant(h, what, obj);
183 roamingOnRegistrants.add(r);
185 if (ss.getRoaming()) {
186 r.notifyRegistrant();
190 public void unregisterForRoamingOn(Handler h) {
191 roamingOnRegistrants.remove(h);
195 * Registration point for combined roaming off
196 * combined roaming is true when roaming is true and ONS differs SPN
198 * @param h handler to notify
199 * @param what what code of message when delivered
200 * @param obj placed in Message.obj
202 public void registerForRoamingOff(Handler h, int what, Object obj) {
203 Registrant r = new Registrant(h, what, obj);
204 roamingOffRegistrants.add(r);
206 if (!ss.getRoaming()) {
207 r.notifyRegistrant();
211 public void unregisterForRoamingOff(Handler h) {
212 roamingOffRegistrants.remove(h);
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.
220 * @param onComplete is dispatched when this is complete. it will be
221 * an AsyncResult, and onComplete.obj.exception will be non-null
224 public void reRegisterNetwork(Message onComplete) {
225 cm.getPreferredNetworkType(
226 obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE, onComplete));
230 setRadioPower(boolean power) {
231 mDesiredPowerState = power;
233 setPowerStateToDesired();
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.
246 private boolean mWantContinuousLocationUpdates;
247 private boolean mWantSingleLocationUpdate;
249 public void enableSingleLocationUpdate() {
250 if (mWantSingleLocationUpdate || mWantContinuousLocationUpdates) return;
251 mWantSingleLocationUpdate = true;
252 cm.setLocationUpdates(true, obtainMessage(EVENT_LOCATION_UPDATES_ENABLED));
255 public void enableLocationUpdates() {
256 if (mWantSingleLocationUpdate || mWantContinuousLocationUpdates) return;
257 mWantContinuousLocationUpdates = true;
258 cm.setLocationUpdates(true, obtainMessage(EVENT_LOCATION_UPDATES_ENABLED));
261 protected void disableSingleLocationUpdate() {
262 mWantSingleLocationUpdate = false;
263 if (!mWantSingleLocationUpdate && !mWantContinuousLocationUpdates) {
264 cm.setLocationUpdates(false, null);
268 public void disableLocationUpdates() {
269 mWantContinuousLocationUpdates = false;
270 if (!mWantSingleLocationUpdate && !mWantContinuousLocationUpdates) {
271 cm.setLocationUpdates(false, null);
275 public abstract void handleMessage(Message msg);
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);
284 * Clean up existing voice and data connection then turn off radio power.
286 * Hang up the existing voice calls to decrease call drop rate.
288 protected abstract void powerOffRadioSafely();
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];