DataConnectionTracker.java revision 767a662ecde33c3979bf02b793d392aca0403162
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.app.PendingIntent; 20import android.os.AsyncResult; 21import android.os.Handler; 22import android.os.INetStatService; 23import android.os.Message; 24import android.os.RemoteException; 25import android.provider.Settings; 26import android.provider.Settings.SettingNotFoundException; 27import android.util.Log; 28 29/** 30 * {@hide} 31 * 32 */ 33public abstract class DataConnectionTracker extends Handler { 34 private static final boolean DBG = true; 35 36 /** 37 * IDLE: ready to start data connection setup, default state 38 * INITING: state of issued setupDefaultPDP() but not finish yet 39 * CONNECTING: state of issued startPppd() but not finish yet 40 * SCANNING: data connection fails with one apn but other apns are available 41 * ready to start data connection on other apns (before INITING) 42 * CONNECTED: IP connection is setup 43 * DISCONNECTING: Connection.disconnect() has been called, but PDP 44 * context is not yet deactivated 45 * FAILED: data connection fail for all apns settings 46 * 47 * getDataConnectionState() maps State to DataState 48 * FAILED or IDLE : DISCONNECTED 49 * INITING or CONNECTING or SCANNING: CONNECTING 50 * CONNECTED : CONNECTED or DISCONNECTING 51 */ 52 public enum State { 53 IDLE, 54 INITING, 55 CONNECTING, 56 SCANNING, 57 CONNECTED, 58 DISCONNECTING, 59 FAILED 60 } 61 62 public enum Activity { 63 NONE, 64 DATAIN, 65 DATAOUT, 66 DATAINANDOUT 67 } 68 69 //***** Event Codes 70 protected static final int EVENT_DATA_SETUP_COMPLETE = 1; 71 protected static final int EVENT_RADIO_AVAILABLE = 3; 72 protected static final int EVENT_RECORDS_LOADED = 4; 73 protected static final int EVENT_TRY_SETUP_DATA = 5; 74 protected static final int EVENT_DATA_STATE_CHANGED = 6; 75 protected static final int EVENT_POLL_PDP = 7; 76 protected static final int EVENT_GET_PDP_LIST_COMPLETE = 11; 77 protected static final int EVENT_RADIO_OFF_OR_NOT_AVAILABLE = 12; 78 protected static final int EVENT_VOICE_CALL_STARTED = 14; 79 protected static final int EVENT_VOICE_CALL_ENDED = 15; 80 protected static final int EVENT_GPRS_DETACHED = 19; 81 protected static final int EVENT_LINK_STATE_CHANGED = 20; 82 protected static final int EVENT_ROAMING_ON = 21; 83 protected static final int EVENT_ROAMING_OFF = 22; 84 protected static final int EVENT_ENABLE_NEW_APN = 23; 85 protected static final int EVENT_RESTORE_DEFAULT_APN = 24; 86 protected static final int EVENT_DISCONNECT_DONE = 25; 87 protected static final int EVENT_GPRS_ATTACHED = 26; 88 protected static final int EVENT_START_NETSTAT_POLL = 27; 89 protected static final int EVENT_START_RECOVERY = 28; 90 protected static final int EVENT_APN_CHANGED = 29; 91 protected static final int EVENT_CDMA_DATA_DETACHED = 30; 92 protected static final int EVENT_NV_READY = 31; 93 protected static final int EVENT_PS_RESTRICT_ENABLED = 32; 94 protected static final int EVENT_PS_RESTRICT_DISABLED = 33; 95 96 //***** Constants 97 protected static final int RECONNECT_DELAY_INITIAL_MILLIS = 5 * 1000; 98 99 /** Cap out with 1 hour retry interval. */ 100 protected static final int RECONNECT_DELAY_MAX_MILLIS = 60 * 60 * 1000; 101 102 /** Slow poll when attempting connection recovery. */ 103 protected static final int POLL_NETSTAT_SLOW_MILLIS = 5000; 104 /** Default ping deadline, in seconds. */ 105 protected final int DEFAULT_PING_DEADLINE = 5; 106 /** Default max failure count before attempting to network re-registration. */ 107 protected final int DEFAULT_MAX_PDP_RESET_FAIL = 3; 108 109 /** 110 * After detecting a potential connection problem, this is the max number 111 * of subsequent polls before attempting a radio reset. At this point, 112 * poll interval is 5 seconds (POLL_NETSTAT_SLOW_MILLIS), so set this to 113 * poll for about 2 more minutes. 114 */ 115 protected static final int NO_RECV_POLL_LIMIT = 24; 116 117 // 1 sec. default polling interval when screen is on. 118 protected static final int POLL_NETSTAT_MILLIS = 1000; 119 // 10 min. default polling interval when screen is off. 120 protected static final int POLL_NETSTAT_SCREEN_OFF_MILLIS = 1000*60*10; 121 // 2 min for round trip time 122 protected static final int POLL_LONGEST_RTT = 120 * 1000; 123 // 10 for packets without ack 124 protected static final int NUMBER_SENT_PACKETS_OF_HANG = 10; 125 // how long to wait before switching back to default APN 126 protected static final int RESTORE_DEFAULT_APN_DELAY = 1 * 60 * 1000; 127 // system property that can override the above value 128 protected static final String APN_RESTORE_DELAY_PROP_NAME = "android.telephony.apn-restore"; 129 // represents an invalid IP address 130 protected static final String NULL_IP = "0.0.0.0"; 131 132 133 // member variables 134 protected PhoneBase phone; 135 protected Activity activity = Activity.NONE; 136 protected State state = State.IDLE; 137 protected Handler mDataConnectionTracker = null; 138 139 140 protected INetStatService netstat; 141 protected long txPkts, rxPkts, sentSinceLastRecv; 142 protected int netStatPollPeriod; 143 protected int mNoRecvPollCount = 0; 144 protected boolean netStatPollEnabled = false; 145 146 // wifi connection status will be updated by sticky intent 147 protected boolean mIsWifiConnected = false; 148 149 /** Intent sent when the reconnect alarm fires. */ 150 protected PendingIntent mReconnectIntent = null; 151 152 /** CID of active data connection */ 153 protected int cidActive; 154 155 /** 156 * Default constructor 157 */ 158 protected DataConnectionTracker(PhoneBase phone) { 159 super(); 160 this.phone = phone; 161 } 162 163 public Activity getActivity() { 164 return activity; 165 } 166 167 public State getState() { 168 return state; 169 } 170 171 public String getStateInString() { 172 switch (state) { 173 case IDLE: return "IDLE"; 174 case INITING: return "INIT"; 175 case CONNECTING: return "CING"; 176 case SCANNING: return "SCAN"; 177 case CONNECTED: return "CNTD"; 178 case DISCONNECTING: return "DING"; 179 case FAILED: return "FAIL"; 180 default: return "ERRO"; 181 } 182 } 183 184 /** 185 * The data connection is expected to be setup while device 186 * 1. has Icc card 187 * 2. registered for data service 188 * 3. user doesn't explicitly disable data service 189 * 4. wifi is not on 190 * 191 * @return false while no data connection if all above requirements are met. 192 */ 193 public abstract boolean isDataConnectionAsDesired(); 194 195 //The data roaming setting is now located in the shared preferences. 196 // See if the requested preference value is the same as that stored in 197 // the shared values. If it is not, then update it. 198 public void setDataOnRoamingEnabled(boolean enabled) { 199 if (getDataOnRoamingEnabled() != enabled) { 200 Settings.Secure.putInt(phone.getContext().getContentResolver(), 201 Settings.Secure.DATA_ROAMING, enabled ? 1 : 0); 202 } 203 Message roamingMsg = phone.getServiceState().getRoaming() ? 204 obtainMessage(EVENT_ROAMING_ON) : obtainMessage(EVENT_ROAMING_OFF); 205 sendMessage(roamingMsg); 206 } 207 208 //Retrieve the data roaming setting from the shared preferences. 209 public boolean getDataOnRoamingEnabled() { 210 try { 211 return Settings.Secure.getInt(phone.getContext().getContentResolver(), 212 Settings.Secure.DATA_ROAMING) > 0; 213 } catch (SettingNotFoundException snfe) { 214 return false; 215 } 216 } 217 218 // abstract handler methods 219 protected abstract void onTrySetupData(); 220 protected abstract void onRoamingOff(); 221 protected abstract void onRoamingOn(); 222 protected abstract void onRadioAvailable(); 223 protected abstract void onRadioOffOrNotAvailable(); 224 protected abstract void onDataSetupComplete(AsyncResult ar); 225 protected abstract void onDisconnectDone(AsyncResult ar); 226 protected abstract void onVoiceCallStarted(); 227 protected abstract void onVoiceCallEnded(); 228 229 //***** Overridden from Handler 230 public void handleMessage (Message msg) { 231 switch (msg.what) { 232 233 case EVENT_TRY_SETUP_DATA: 234 onTrySetupData(); 235 break; 236 237 case EVENT_ROAMING_OFF: 238 onRoamingOff(); 239 break; 240 241 case EVENT_ROAMING_ON: 242 onRoamingOn(); 243 break; 244 245 case EVENT_RADIO_AVAILABLE: 246 onRadioAvailable(); 247 break; 248 249 case EVENT_RADIO_OFF_OR_NOT_AVAILABLE: 250 onRadioOffOrNotAvailable(); 251 break; 252 253 case EVENT_DATA_SETUP_COMPLETE: 254 cidActive = msg.arg1; 255 onDataSetupComplete((AsyncResult) msg.obj); 256 break; 257 258 case EVENT_DISCONNECT_DONE: 259 onDisconnectDone((AsyncResult) msg.obj); 260 break; 261 262 case EVENT_VOICE_CALL_STARTED: 263 onVoiceCallStarted(); 264 break; 265 266 case EVENT_VOICE_CALL_ENDED: 267 onVoiceCallEnded(); 268 break; 269 270 default: 271 Log.e("DATA", "Unidentified event = " + msg.what); 272 break; 273 } 274 } 275 276 /** 277 * Simply tear down data connections due to radio off 278 * and don't setup again. 279 */ 280 public abstract void cleanConnectionBeforeRadioOff(); 281 282 /** 283 * Report the current state of data connectivity (enabled or disabled) 284 * @return {@code false} if data connectivity has been explicitly disabled, 285 * {@code true} otherwise. 286 */ 287 public abstract boolean getDataEnabled(); 288 289 /** 290 * Report on whether data connectivity is enabled 291 * @return {@code false} if data connectivity has been explicitly disabled, 292 * {@code true} otherwise. 293 */ 294 public abstract boolean getAnyDataEnabled(); 295 296 /** 297 * Prevent mobile data connections from being established, 298 * or once again allow mobile data connections. If the state 299 * toggles, then either tear down or set up data, as 300 * appropriate to match the new state. 301 * @param enable indicates whether to enable ({@code true}) or disable ({@code false}) data 302 * @return {@code true} if the operation succeeded 303 */ 304 public abstract boolean setDataEnabled(boolean enable); 305 306 protected abstract void startNetStatPoll(); 307 308 protected abstract void stopNetStatPoll(); 309 310 protected abstract void restartRadio(); 311 312 protected abstract void log(String s); 313} 314