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