The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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.server; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.net.ConnectivityManager; |
| 26 | import android.net.IConnectivityManager; |
| 27 | import android.net.MobileDataStateTracker; |
| 28 | import android.net.NetworkInfo; |
| 29 | import android.net.NetworkStateTracker; |
| 30 | import android.net.wifi.WifiStateTracker; |
| 31 | import android.os.Binder; |
| 32 | import android.os.Handler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 33 | import android.os.IBinder; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.ServiceManager; |
| 38 | import android.os.SystemProperties; |
| 39 | import android.provider.Settings; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.util.EventLog; |
| 42 | import android.util.Log; |
| 43 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 44 | import com.android.internal.telephony.Phone; |
| 45 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import java.io.FileDescriptor; |
| 47 | import java.io.PrintWriter; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * @hide |
| 53 | */ |
| 54 | public class ConnectivityService extends IConnectivityManager.Stub { |
| 55 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 56 | private static final boolean DBG = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | private static final String TAG = "ConnectivityService"; |
| 58 | |
| 59 | // Event log tags (must be in sync with event-log-tags) |
| 60 | private static final int EVENTLOG_CONNECTIVITY_STATE_CHANGED = 50020; |
| 61 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 62 | // how long to wait before switching back to a radio's default network |
| 63 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 64 | // system property that can override the above value |
| 65 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 66 | "android.telephony.apn-restore"; |
| 67 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Sometimes we want to refer to the individual network state |
| 70 | * trackers separately, and sometimes we just want to treat them |
| 71 | * abstractly. |
| 72 | */ |
| 73 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * A per Net list of the PID's that requested access to the net |
| 77 | * used both as a refcount and for per-PID DNS selection |
| 78 | */ |
| 79 | private List mNetRequestersPids[]; |
| 80 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | private WifiWatchdogService mWifiWatchdogService; |
| 82 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 83 | // priority order of the nettrackers |
| 84 | // (excluding dynamically set mNetworkPreference) |
| 85 | // TODO - move mNetworkTypePreference into this |
| 86 | private int[] mPriorityList; |
| 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | private Context mContext; |
| 89 | private int mNetworkPreference; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 90 | private int mActiveDefaultNetwork = -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | |
| 92 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | |
| 94 | private boolean mTestMode; |
| 95 | private static ConnectivityService sServiceInstance; |
| 96 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 97 | private Handler mHandler; |
| 98 | |
| 99 | // list of DeathRecipients used to make sure features are turned off when |
| 100 | // a process dies |
| 101 | private List mFeatureUsers; |
| 102 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 103 | private boolean mSystemReady; |
| 104 | private ArrayList<Intent> mDeferredBroadcasts; |
| 105 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 106 | private class NetworkAttributes { |
| 107 | /** |
| 108 | * Class for holding settings read from resources. |
| 109 | */ |
| 110 | public String mName; |
| 111 | public int mType; |
| 112 | public int mRadio; |
| 113 | public int mPriority; |
| 114 | public NetworkAttributes(String init) { |
| 115 | String fragments[] = init.split(","); |
| 116 | mName = fragments[0].toLowerCase(); |
| 117 | if (fragments[1].toLowerCase().equals("wifi")) { |
| 118 | mRadio = ConnectivityManager.TYPE_WIFI; |
| 119 | } else { |
| 120 | mRadio = ConnectivityManager.TYPE_MOBILE; |
| 121 | } |
| 122 | if (mName.equals("default")) { |
| 123 | mType = mRadio; |
| 124 | } else if (mName.equals("mms")) { |
| 125 | mType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 126 | } else if (mName.equals("supl")) { |
| 127 | mType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 128 | } else if (mName.equals("dun")) { |
| 129 | mType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 130 | } else if (mName.equals("hipri")) { |
| 131 | mType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 132 | } |
| 133 | mPriority = Integer.parseInt(fragments[2]); |
| 134 | } |
| 135 | public boolean isDefault() { |
| 136 | return (mType == mRadio); |
| 137 | } |
| 138 | } |
| 139 | NetworkAttributes[] mNetAttributes; |
| 140 | |
| 141 | private class RadioAttributes { |
| 142 | public String mName; |
| 143 | public int mPriority; |
| 144 | public int mSimultaneity; |
| 145 | public int mType; |
| 146 | public RadioAttributes(String init) { |
| 147 | String fragments[] = init.split(","); |
| 148 | mName = fragments[0].toLowerCase(); |
| 149 | mPriority = Integer.parseInt(fragments[1]); |
| 150 | mSimultaneity = Integer.parseInt(fragments[2]); |
| 151 | if (mName.equals("wifi")) { |
| 152 | mType = ConnectivityManager.TYPE_WIFI; |
| 153 | } else { |
| 154 | mType = ConnectivityManager.TYPE_MOBILE; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | RadioAttributes[] mRadioAttributes; |
| 159 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | private static class ConnectivityThread extends Thread { |
| 161 | private Context mContext; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 162 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | private ConnectivityThread(Context context) { |
| 164 | super("ConnectivityThread"); |
| 165 | mContext = context; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void run() { |
| 170 | Looper.prepare(); |
| 171 | synchronized (this) { |
| 172 | sServiceInstance = new ConnectivityService(mContext); |
| 173 | notifyAll(); |
| 174 | } |
| 175 | Looper.loop(); |
| 176 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 177 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | public static ConnectivityService getServiceInstance(Context context) { |
| 179 | ConnectivityThread thread = new ConnectivityThread(context); |
| 180 | thread.start(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | synchronized (thread) { |
| 183 | while (sServiceInstance == null) { |
| 184 | try { |
| 185 | // Wait until sServiceInstance has been initialized. |
| 186 | thread.wait(); |
| 187 | } catch (InterruptedException ignore) { |
| 188 | Log.e(TAG, |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 189 | "Unexpected InterruptedException while waiting"+ |
| 190 | " for ConnectivityService thread"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | return sServiceInstance; |
| 196 | } |
| 197 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | public static ConnectivityService getInstance(Context context) { |
| 200 | return ConnectivityThread.getServiceInstance(context); |
| 201 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | private ConnectivityService(Context context) { |
| 204 | if (DBG) Log.v(TAG, "ConnectivityService starting up"); |
| 205 | mContext = context; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 206 | mNetTrackers = new NetworkStateTracker[ |
| 207 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
| 208 | mHandler = new MyHandler(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 209 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | mNetworkPreference = getPersistedNetworkPreference(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 211 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 212 | // Load device network attributes from resources |
| 213 | mNetAttributes = new NetworkAttributes[ |
| 214 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
| 215 | mRadioAttributes = new RadioAttributes[ |
| 216 | ConnectivityManager.MAX_RADIO_TYPE+1]; |
| 217 | String[] naStrings = context.getResources().getStringArray( |
| 218 | com.android.internal.R.array.networkAttributes); |
| 219 | // TODO - what if the setting has gaps/unknown types? |
| 220 | for (String a : naStrings) { |
| 221 | NetworkAttributes n = new NetworkAttributes(a); |
| 222 | mNetAttributes[n.mType] = n; |
| 223 | } |
| 224 | String[] raStrings = context.getResources().getStringArray( |
| 225 | com.android.internal.R.array.radioAttributes); |
| 226 | for (String a : raStrings) { |
| 227 | RadioAttributes r = new RadioAttributes(a); |
| 228 | mRadioAttributes[r.mType] = r; |
| 229 | } |
| 230 | |
| 231 | // high priority first |
| 232 | mPriorityList = new int[naStrings.length]; |
| 233 | { |
| 234 | int priority = 0; //lowest |
| 235 | int nextPos = naStrings.length-1; |
| 236 | while (nextPos>-1) { |
| 237 | for (int i = 0; i < mNetAttributes.length; i++) { |
| 238 | if(mNetAttributes[i].mPriority == priority) { |
| 239 | mPriorityList[nextPos--] = i; |
| 240 | } |
| 241 | } |
| 242 | priority++; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | mNetRequestersPids = |
| 247 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
| 248 | for (int i=0; i<=ConnectivityManager.MAX_NETWORK_TYPE; i++) { |
| 249 | mNetRequestersPids[i] = new ArrayList(); |
| 250 | } |
| 251 | |
| 252 | mFeatureUsers = new ArrayList(); |
| 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | /* |
| 255 | * Create the network state trackers for Wi-Fi and mobile |
| 256 | * data. Maybe this could be done with a factory class, |
| 257 | * but it's not clear that it's worth it, given that |
| 258 | * the number of different network types is not going |
| 259 | * to change very often. |
| 260 | */ |
| 261 | if (DBG) Log.v(TAG, "Starting Wifi Service."); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 262 | WifiStateTracker wst = new WifiStateTracker(context, mHandler); |
| 263 | WifiService wifiService = new WifiService(context, wst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | ServiceManager.addService(Context.WIFI_SERVICE, wifiService); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 265 | mNetTrackers[ConnectivityManager.TYPE_WIFI] = wst; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 267 | mNetTrackers[ConnectivityManager.TYPE_MOBILE] = |
| 268 | new MobileDataStateTracker(context, mHandler, |
| 269 | ConnectivityManager.TYPE_MOBILE, Phone.APN_TYPE_DEFAULT, |
| 270 | "MOBILE"); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 271 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 272 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_MMS] = |
| 273 | new MobileDataStateTracker(context, mHandler, |
| 274 | ConnectivityManager.TYPE_MOBILE_MMS, Phone.APN_TYPE_MMS, |
| 275 | "MOBILE_MMS"); |
| 276 | |
| 277 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_SUPL] = |
| 278 | new MobileDataStateTracker(context, mHandler, |
| 279 | ConnectivityManager.TYPE_MOBILE_SUPL, Phone.APN_TYPE_SUPL, |
| 280 | "MOBILE_SUPL"); |
| 281 | |
| 282 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_DUN] = |
| 283 | new MobileDataStateTracker(context, mHandler, |
| 284 | ConnectivityManager.TYPE_MOBILE_DUN, Phone.APN_TYPE_DUN, |
| 285 | "MOBILE_DUN"); |
| 286 | |
| 287 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI] = |
| 288 | new MobileDataStateTracker(context, mHandler, |
| 289 | ConnectivityManager.TYPE_MOBILE_HIPRI, Phone.APN_TYPE_HIPRI, |
| 290 | "MOBILE_HIPRI"); |
| 291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | mNumDnsEntries = 0; |
| 293 | |
| 294 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 295 | && SystemProperties.get("ro.build.type").equals("eng"); |
| 296 | |
| 297 | for (NetworkStateTracker t : mNetTrackers) |
| 298 | t.startMonitoring(); |
| 299 | |
| 300 | // Constructing this starts it too |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 301 | mWifiWatchdogService = new WifiWatchdogService(context, wst); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 305 | * Sets the preferred network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | * @param preference the new preference |
| 307 | */ |
| 308 | public synchronized void setNetworkPreference(int preference) { |
| 309 | enforceChangePermission(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 310 | if (ConnectivityManager.isNetworkTypeValid(preference) && |
| 311 | mNetAttributes[preference].isDefault()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | if (mNetworkPreference != preference) { |
| 313 | persistNetworkPreference(preference); |
| 314 | mNetworkPreference = preference; |
| 315 | enforcePreference(); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | public int getNetworkPreference() { |
| 321 | enforceAccessPermission(); |
| 322 | return mNetworkPreference; |
| 323 | } |
| 324 | |
| 325 | private void persistNetworkPreference(int networkPreference) { |
| 326 | final ContentResolver cr = mContext.getContentResolver(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 327 | Settings.Secure.putInt(cr, Settings.Secure.NETWORK_PREFERENCE, |
| 328 | networkPreference); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | private int getPersistedNetworkPreference() { |
| 332 | final ContentResolver cr = mContext.getContentResolver(); |
| 333 | |
| 334 | final int networkPrefSetting = Settings.Secure |
| 335 | .getInt(cr, Settings.Secure.NETWORK_PREFERENCE, -1); |
| 336 | if (networkPrefSetting != -1) { |
| 337 | return networkPrefSetting; |
| 338 | } |
| 339 | |
| 340 | return ConnectivityManager.DEFAULT_NETWORK_PREFERENCE; |
| 341 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 344 | * Make the state of network connectivity conform to the preference settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | * In this method, we only tear down a non-preferred network. Establishing |
| 346 | * a connection to the preferred network is taken care of when we handle |
| 347 | * the disconnect event from the non-preferred network |
| 348 | * (see {@link #handleDisconnect(NetworkInfo)}). |
| 349 | */ |
| 350 | private void enforcePreference() { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 351 | if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | return; |
| 353 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 354 | if (!mNetTrackers[mNetworkPreference].isAvailable()) |
| 355 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 357 | for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) { |
| 358 | if (t != mNetworkPreference && |
| 359 | mNetTrackers[t].getNetworkInfo().isConnected()) { |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 360 | if (DBG) { |
| 361 | Log.d(TAG, "tearing down " + |
| 362 | mNetTrackers[t].getNetworkInfo() + |
| 363 | " in enforcePreference"); |
| 364 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 365 | teardown(mNetTrackers[t]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private boolean teardown(NetworkStateTracker netTracker) { |
| 371 | if (netTracker.teardown()) { |
| 372 | netTracker.setTeardownRequested(true); |
| 373 | return true; |
| 374 | } else { |
| 375 | return false; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 381 | * It is assumed that at most one network is active at a time. If more |
| 382 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 383 | * @return the info for the active network, or {@code null} if none is |
| 384 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | */ |
| 386 | public NetworkInfo getActiveNetworkInfo() { |
| 387 | enforceAccessPermission(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 388 | for (int type=0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) { |
| 389 | if (!mNetAttributes[type].isDefault()) { |
| 390 | continue; |
| 391 | } |
| 392 | NetworkStateTracker t = mNetTrackers[type]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | NetworkInfo info = t.getNetworkInfo(); |
| 394 | if (info.isConnected()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 395 | if (DBG && type != mActiveDefaultNetwork) Log.e(TAG, |
| 396 | "connected default network is not " + |
| 397 | "mActiveDefaultNetwork!"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | return info; |
| 399 | } |
| 400 | } |
| 401 | return null; |
| 402 | } |
| 403 | |
| 404 | public NetworkInfo getNetworkInfo(int networkType) { |
| 405 | enforceAccessPermission(); |
| 406 | if (ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 407 | NetworkStateTracker t = mNetTrackers[networkType]; |
| 408 | if (t != null) |
| 409 | return t.getNetworkInfo(); |
| 410 | } |
| 411 | return null; |
| 412 | } |
| 413 | |
| 414 | public NetworkInfo[] getAllNetworkInfo() { |
| 415 | enforceAccessPermission(); |
| 416 | NetworkInfo[] result = new NetworkInfo[mNetTrackers.length]; |
| 417 | int i = 0; |
| 418 | for (NetworkStateTracker t : mNetTrackers) { |
| 419 | result[i++] = t.getNetworkInfo(); |
| 420 | } |
| 421 | return result; |
| 422 | } |
| 423 | |
| 424 | public boolean setRadios(boolean turnOn) { |
| 425 | boolean result = true; |
| 426 | enforceChangePermission(); |
| 427 | for (NetworkStateTracker t : mNetTrackers) { |
| 428 | result = t.setRadio(turnOn) && result; |
| 429 | } |
| 430 | return result; |
| 431 | } |
| 432 | |
| 433 | public boolean setRadio(int netType, boolean turnOn) { |
| 434 | enforceChangePermission(); |
| 435 | if (!ConnectivityManager.isNetworkTypeValid(netType)) { |
| 436 | return false; |
| 437 | } |
| 438 | NetworkStateTracker tracker = mNetTrackers[netType]; |
| 439 | return tracker != null && tracker.setRadio(turnOn); |
| 440 | } |
| 441 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 442 | private class FeatureUser implements IBinder.DeathRecipient { |
| 443 | int mNetworkType; |
| 444 | String mFeature; |
| 445 | IBinder mBinder; |
| 446 | int mPid; |
| 447 | int mUid; |
| 448 | |
| 449 | FeatureUser(int type, String feature, IBinder binder) { |
| 450 | super(); |
| 451 | mNetworkType = type; |
| 452 | mFeature = feature; |
| 453 | mBinder = binder; |
| 454 | mPid = getCallingPid(); |
| 455 | mUid = getCallingUid(); |
| 456 | try { |
| 457 | mBinder.linkToDeath(this, 0); |
| 458 | } catch (RemoteException e) { |
| 459 | binderDied(); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void unlinkDeathRecipient() { |
| 464 | mBinder.unlinkToDeath(this, 0); |
| 465 | } |
| 466 | |
| 467 | public void binderDied() { |
| 468 | Log.d(TAG, "ConnectivityService FeatureUser binderDied(" + |
| 469 | mNetworkType + ", " + mFeature + ", " + mBinder); |
| 470 | stopUsingNetworkFeature(mNetworkType, mFeature, mPid, mUid); |
| 471 | } |
| 472 | |
| 473 | } |
| 474 | |
| 475 | public int startUsingNetworkFeature(int networkType, String feature, |
| 476 | IBinder binder) { |
| 477 | if (DBG) { |
| 478 | Log.d(TAG, "startUsingNetworkFeature for net " + networkType + |
| 479 | ": " + feature); |
| 480 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | enforceChangePermission(); |
| 482 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 483 | return Phone.APN_REQUEST_FAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 485 | |
| 486 | synchronized (mFeatureUsers) { |
| 487 | mFeatureUsers.add(new FeatureUser(networkType, feature, binder)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 489 | |
| 490 | // TODO - move this into the MobileDataStateTracker |
| 491 | int usedNetworkType = networkType; |
| 492 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 493 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 494 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 495 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 496 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 497 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) { |
| 498 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 499 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 500 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 501 | } |
| 502 | } |
| 503 | NetworkStateTracker network = mNetTrackers[usedNetworkType]; |
| 504 | if (network != null) { |
| 505 | if (usedNetworkType != networkType) { |
| 506 | Integer currentPid = new Integer(getCallingPid()); |
| 507 | |
| 508 | NetworkStateTracker radio = mNetTrackers[networkType]; |
| 509 | NetworkInfo ni = network.getNetworkInfo(); |
| 510 | |
| 511 | if (ni.isAvailable() == false) { |
| 512 | if (DBG) Log.d(TAG, "special network not available"); |
| 513 | return Phone.APN_TYPE_NOT_AVAILABLE; |
| 514 | } |
| 515 | |
| 516 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 517 | // this gets used for per-pid dns when connected |
| 518 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 519 | } |
| 520 | |
Robert Greenwalt | a64bf83 | 2009-08-19 20:19:33 -0700 | [diff] [blame] | 521 | if ((ni.isConnectedOrConnecting() == true) && |
| 522 | !network.isTeardownRequested()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 523 | if (ni.isConnected() == true) { |
| 524 | // add the pid-specific dns |
| 525 | handleDnsConfigurationChange(); |
| 526 | if (DBG) Log.d(TAG, "special network already active"); |
| 527 | return Phone.APN_ALREADY_ACTIVE; |
| 528 | } |
| 529 | if (DBG) Log.d(TAG, "special network already connecting"); |
| 530 | return Phone.APN_REQUEST_STARTED; |
| 531 | } |
| 532 | |
| 533 | // check if the radio in play can make another contact |
| 534 | // assume if cannot for now |
| 535 | |
| 536 | // since we have to drop the default on this radio, setup |
| 537 | // an automatic event to switch back |
| 538 | if(mHandler.hasMessages(NetworkStateTracker. |
| 539 | EVENT_RESTORE_DEFAULT_NETWORK, radio) || |
| 540 | radio.getNetworkInfo().isConnectedOrConnecting()) { |
| 541 | mHandler.removeMessages(NetworkStateTracker. |
| 542 | EVENT_RESTORE_DEFAULT_NETWORK, |
| 543 | radio); |
| 544 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 545 | NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK, |
| 546 | radio), getRestoreDefaultNetworkDelay()); |
| 547 | } |
| 548 | if (DBG) Log.d(TAG, "reconnecting to special network"); |
| 549 | network.reconnect(); |
| 550 | return Phone.APN_REQUEST_STARTED; |
| 551 | } else { |
| 552 | return network.startUsingNetworkFeature(feature, |
| 553 | getCallingPid(), getCallingUid()); |
| 554 | } |
| 555 | } |
| 556 | return Phone.APN_TYPE_NOT_AVAILABLE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 560 | return stopUsingNetworkFeature(networkType, feature, getCallingPid(), |
| 561 | getCallingUid()); |
| 562 | } |
| 563 | |
| 564 | private int stopUsingNetworkFeature(int networkType, String feature, |
| 565 | int pid, int uid) { |
| 566 | if (DBG) { |
| 567 | Log.d(TAG, "stopUsingNetworkFeature for net " + networkType + |
| 568 | ": " + feature); |
| 569 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | enforceChangePermission(); |
| 571 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 572 | return -1; |
| 573 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 574 | |
| 575 | synchronized (mFeatureUsers) { |
| 576 | for (int i=0; i < mFeatureUsers.size(); i++) { |
| 577 | FeatureUser u = (FeatureUser)mFeatureUsers.get(i); |
| 578 | if (uid == u.mUid && pid == u.mPid && |
| 579 | networkType == u.mNetworkType && |
| 580 | TextUtils.equals(feature, u.mFeature)) { |
| 581 | u.unlinkDeathRecipient(); |
| 582 | mFeatureUsers.remove(i); |
| 583 | break; |
| 584 | } |
| 585 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 587 | |
| 588 | // TODO - move to MobileDataStateTracker |
| 589 | int usedNetworkType = networkType; |
| 590 | if (networkType == ConnectivityManager.TYPE_MOBILE) { |
| 591 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 592 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 593 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 594 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 595 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN)) { |
| 596 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 597 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 598 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 599 | } |
| 600 | } |
| 601 | NetworkStateTracker tracker = mNetTrackers[usedNetworkType]; |
| 602 | if(usedNetworkType != networkType) { |
| 603 | Integer currentPid = new Integer(pid); |
| 604 | if (mNetRequestersPids[usedNetworkType].remove(currentPid)) { |
| 605 | reassessPidDns(pid, true); |
| 606 | } |
| 607 | if (mNetRequestersPids[usedNetworkType].size() != 0) { |
| 608 | if (DBG) Log.d(TAG, "not tearing down special network - " + |
| 609 | "others still using it"); |
| 610 | return 1; |
| 611 | } |
| 612 | |
| 613 | tracker.teardown(); |
| 614 | NetworkStateTracker radio = mNetTrackers[networkType]; |
| 615 | // Check if we want to revert to the default |
| 616 | if (mHandler.hasMessages(NetworkStateTracker. |
| 617 | EVENT_RESTORE_DEFAULT_NETWORK, radio)) { |
| 618 | mHandler.removeMessages(NetworkStateTracker. |
| 619 | EVENT_RESTORE_DEFAULT_NETWORK, radio); |
| 620 | radio.reconnect(); |
| 621 | } |
| 622 | return 1; |
| 623 | } else { |
| 624 | return tracker.stopUsingNetworkFeature(feature, pid, uid); |
| 625 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Ensure that a network route exists to deliver traffic to the specified |
| 630 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 631 | * @param networkType the type of the network over which traffic to the |
| 632 | * specified host is to be routed |
| 633 | * @param hostAddress the IP address of the host to which the route is |
| 634 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 635 | * @return {@code true} on success, {@code false} on failure |
| 636 | */ |
| 637 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
| 638 | enforceChangePermission(); |
| 639 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 640 | return false; |
| 641 | } |
| 642 | NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 643 | /* |
| 644 | * If there's only one connected network, and it's the one requested, |
| 645 | * then we don't have to do anything - the requested route already |
| 646 | * exists. If it's not the requested network, then it's not possible |
| 647 | * to establish the requested route. Finally, if there is more than |
| 648 | * one connected network, then we must insert an entry in the routing |
| 649 | * table. |
| 650 | */ |
| 651 | if (getNumConnectedNetworks() > 1) { |
| 652 | return tracker.requestRouteToHost(hostAddress); |
| 653 | } else { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 654 | return (mNetAttributes[networkType].isDefault() && |
| 655 | tracker.getNetworkInfo().isConnected()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * @see ConnectivityManager#getBackgroundDataSetting() |
| 661 | */ |
| 662 | public boolean getBackgroundDataSetting() { |
| 663 | return Settings.Secure.getInt(mContext.getContentResolver(), |
| 664 | Settings.Secure.BACKGROUND_DATA, 1) == 1; |
| 665 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 666 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | /** |
| 668 | * @see ConnectivityManager#setBackgroundDataSetting(boolean) |
| 669 | */ |
| 670 | public void setBackgroundDataSetting(boolean allowBackgroundDataUsage) { |
| 671 | mContext.enforceCallingOrSelfPermission( |
| 672 | android.Manifest.permission.CHANGE_BACKGROUND_DATA_SETTING, |
| 673 | "ConnectivityService"); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 674 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 675 | if (getBackgroundDataSetting() == allowBackgroundDataUsage) return; |
| 676 | |
| 677 | Settings.Secure.putInt(mContext.getContentResolver(), |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 678 | Settings.Secure.BACKGROUND_DATA, |
| 679 | allowBackgroundDataUsage ? 1 : 0); |
| 680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | Intent broadcast = new Intent( |
| 682 | ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
| 683 | mContext.sendBroadcast(broadcast); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 684 | } |
| 685 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | private int getNumConnectedNetworks() { |
| 687 | int numConnectedNets = 0; |
| 688 | |
| 689 | for (NetworkStateTracker nt : mNetTrackers) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 690 | if (nt.getNetworkInfo().isConnected() && |
| 691 | !nt.isTeardownRequested()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | ++numConnectedNets; |
| 693 | } |
| 694 | } |
Robert Greenwalt | a64bf83 | 2009-08-19 20:19:33 -0700 | [diff] [blame] | 695 | if (DBG) Log.d(TAG, "numConnectedNets returning "+numConnectedNets); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | return numConnectedNets; |
| 697 | } |
| 698 | |
| 699 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 700 | mContext.enforceCallingOrSelfPermission( |
| 701 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 702 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 706 | mContext.enforceCallingOrSelfPermission( |
| 707 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 708 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 712 | * Handle a {@code DISCONNECTED} event. If this pertains to the non-active |
| 713 | * network, we ignore it. If it is for the active network, we send out a |
| 714 | * broadcast. But first, we check whether it might be possible to connect |
| 715 | * to a different network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | * @param info the {@code NetworkInfo} for the network |
| 717 | */ |
| 718 | private void handleDisconnect(NetworkInfo info) { |
| 719 | |
| 720 | if (DBG) Log.v(TAG, "Handle DISCONNECT for " + info.getTypeName()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 721 | int prevNetType = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 723 | mNetTrackers[prevNetType].setTeardownRequested(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | /* |
| 725 | * If the disconnected network is not the active one, then don't report |
| 726 | * this as a loss of connectivity. What probably happened is that we're |
| 727 | * getting the disconnect for a network that we explicitly disabled |
| 728 | * in accordance with network preference policies. |
| 729 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 730 | if (!mNetAttributes[prevNetType].isDefault()) { |
| 731 | List pids = mNetRequestersPids[prevNetType]; |
| 732 | for (int i = 0; i<pids.size(); i++) { |
| 733 | Integer pid = (Integer)pids.get(i); |
| 734 | // will remove them because the net's no longer connected |
| 735 | // need to do this now as only now do we know the pids and |
| 736 | // can properly null things that are no longer referenced. |
| 737 | reassessPidDns(pid.intValue(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 738 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } |
| 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 742 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 743 | if (info.isFailover()) { |
| 744 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 745 | info.setFailover(false); |
| 746 | } |
| 747 | if (info.getReason() != null) { |
| 748 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 749 | } |
| 750 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 751 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 752 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 753 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 754 | |
| 755 | /* |
| 756 | * If this is a default network, check if other defaults are available |
| 757 | * or active |
| 758 | */ |
| 759 | NetworkStateTracker newNet = null; |
| 760 | if (mNetAttributes[prevNetType].isDefault()) { |
| 761 | if (DBG) Log.d(TAG, "disconnecting a default network"); |
| 762 | if (mActiveDefaultNetwork == prevNetType) { |
| 763 | mActiveDefaultNetwork = -1; |
| 764 | } |
| 765 | |
| 766 | int newType = -1; |
| 767 | int newPriority = -1; |
| 768 | for (int checkType=0; checkType <= |
| 769 | ConnectivityManager.MAX_NETWORK_TYPE; checkType++) { |
| 770 | if (checkType == prevNetType) { |
| 771 | continue; |
| 772 | } |
| 773 | if (mNetAttributes[checkType].isDefault()) { |
| 774 | /* TODO - if we have multiple nets we could use |
| 775 | * we may want to put more thought into which we choose |
| 776 | */ |
| 777 | if (checkType == mNetworkPreference) { |
| 778 | newType = checkType; |
| 779 | break; |
| 780 | } |
| 781 | if (mRadioAttributes[mNetAttributes[checkType].mRadio]. |
| 782 | mPriority > newPriority) { |
| 783 | newType = checkType; |
| 784 | newPriority = mRadioAttributes[mNetAttributes[newType]. |
| 785 | mRadio].mPriority; |
| 786 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 787 | } |
| 788 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 789 | |
| 790 | if (newType != -1) { |
| 791 | newNet = mNetTrackers[newType]; |
| 792 | /** |
| 793 | * See if the other network is available to fail over to. |
| 794 | * If is not available, we enable it anyway, so that it |
| 795 | * will be able to connect when it does become available, |
| 796 | * but we report a total loss of connectivity rather than |
| 797 | * report that we are attempting to fail over. |
| 798 | */ |
| 799 | if (newNet.isAvailable()) { |
| 800 | NetworkInfo switchTo = newNet.getNetworkInfo(); |
| 801 | switchTo.setFailover(true); |
Robert Greenwalt | a64bf83 | 2009-08-19 20:19:33 -0700 | [diff] [blame] | 802 | if (!switchTo.isConnectedOrConnecting() || |
| 803 | newNet.isTeardownRequested()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 804 | newNet.reconnect(); |
| 805 | } |
| 806 | if (DBG) { |
| 807 | if (switchTo.isConnected()) { |
| 808 | Log.v(TAG, "Switching to already connected " + |
| 809 | switchTo.getTypeName()); |
| 810 | } else { |
| 811 | Log.v(TAG, "Attempting to switch to " + |
| 812 | switchTo.getTypeName()); |
| 813 | } |
| 814 | } |
| 815 | intent.putExtra(ConnectivityManager. |
| 816 | EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 817 | } else { |
| 818 | newNet.reconnect(); |
| 819 | } |
| 820 | } else { |
| 821 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, |
| 822 | true); |
| 823 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 824 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 825 | |
| 826 | // do this before we broadcast the change |
| 827 | handleConnectivityChange(); |
| 828 | |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 829 | if (DBG) Log.v(TAG, "Sending DISCONNECT bcast for " + |
| 830 | info.getTypeName() + |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 831 | (newNet == null || !newNet.isAvailable() ? "" : " other=" + |
| 832 | newNet.getNetworkInfo().getTypeName())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 834 | sendStickyBroadcast(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | /* |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 836 | * If the failover network is already connected, then immediately send |
| 837 | * out a followup broadcast indicating successful failover |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 839 | if (newNet != null && newNet.getNetworkInfo().isConnected()) |
| 840 | sendConnectedBroadcast(newNet.getNetworkInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | private void sendConnectedBroadcast(NetworkInfo info) { |
| 844 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 845 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 846 | if (info.isFailover()) { |
| 847 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 848 | info.setFailover(false); |
| 849 | } |
| 850 | if (info.getReason() != null) { |
| 851 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 852 | } |
| 853 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 854 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 855 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 857 | sendStickyBroadcast(intent); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Called when an attempt to fail over to another network has failed. |
| 862 | * @param info the {@link NetworkInfo} for the failed network |
| 863 | */ |
| 864 | private void handleConnectionFailure(NetworkInfo info) { |
| 865 | mNetTrackers[info.getType()].setTeardownRequested(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 867 | String reason = info.getReason(); |
| 868 | String extraInfo = info.getExtraInfo(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 869 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 870 | if (DBG) { |
| 871 | String reasonText; |
| 872 | if (reason == null) { |
| 873 | reasonText = "."; |
| 874 | } else { |
| 875 | reasonText = " (" + reason + ")."; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 877 | Log.v(TAG, "Attempt to connect to " + info.getTypeName() + |
| 878 | " failed" + reasonText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 880 | |
| 881 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 882 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 883 | if (getActiveNetworkInfo() == null) { |
| 884 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 885 | } |
| 886 | if (reason != null) { |
| 887 | intent.putExtra(ConnectivityManager.EXTRA_REASON, reason); |
| 888 | } |
| 889 | if (extraInfo != null) { |
| 890 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo); |
| 891 | } |
| 892 | if (info.isFailover()) { |
| 893 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 894 | info.setFailover(false); |
| 895 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 896 | sendStickyBroadcast(intent); |
| 897 | } |
| 898 | |
| 899 | private void sendStickyBroadcast(Intent intent) { |
| 900 | synchronized(this) { |
| 901 | if (mSystemReady) { |
| 902 | mContext.sendStickyBroadcast(intent); |
| 903 | } else { |
| 904 | if (mDeferredBroadcasts == null) { |
| 905 | mDeferredBroadcasts = new ArrayList<Intent>(); |
| 906 | } |
| 907 | mDeferredBroadcasts.add(intent); |
| 908 | } |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | void systemReady() { |
| 913 | synchronized(this) { |
| 914 | mSystemReady = true; |
| 915 | if (mDeferredBroadcasts != null) { |
| 916 | int count = mDeferredBroadcasts.size(); |
| 917 | for (int i = 0; i < count; i++) { |
| 918 | mContext.sendStickyBroadcast(mDeferredBroadcasts.get(i)); |
| 919 | } |
| 920 | mDeferredBroadcasts = null; |
| 921 | } |
| 922 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | private void handleConnect(NetworkInfo info) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 926 | if (DBG) Log.d(TAG, "Handle CONNECT for " + info.getTypeName()); |
| 927 | |
| 928 | int type = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | |
| 930 | // snapshot isFailover, because sendConnectedBroadcast() resets it |
| 931 | boolean isFailover = info.isFailover(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 932 | NetworkStateTracker thisNet = mNetTrackers[type]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 934 | // if this is a default net and other default is running |
| 935 | // kill the one not preferred |
| 936 | if (mNetAttributes[type].isDefault()) { |
| 937 | if (DBG) Log.d(TAG, "connecting to a default network"); |
| 938 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) { |
| 939 | if ((type != mNetworkPreference && |
| 940 | mNetAttributes[mActiveDefaultNetwork].mPriority > |
| 941 | mNetAttributes[type].mPriority) || |
| 942 | mNetworkPreference == mActiveDefaultNetwork) { |
| 943 | // don't accept this one |
| 944 | if (DBG) Log.v(TAG, "Not broadcasting CONNECT_ACTION " + |
| 945 | "to torn down network " + info.getTypeName()); |
| 946 | teardown(thisNet); |
| 947 | return; |
| 948 | } else { |
| 949 | // tear down the other |
| 950 | NetworkStateTracker otherNet = |
| 951 | mNetTrackers[mActiveDefaultNetwork]; |
| 952 | if (DBG) Log.v(TAG, "Policy requires " + |
| 953 | otherNet.getNetworkInfo().getTypeName() + |
| 954 | " teardown"); |
| 955 | if (!teardown(otherNet)) { |
| 956 | Log.e(TAG, "Network declined teardown request"); |
| 957 | return; |
| 958 | } |
| 959 | if (isFailover) { |
| 960 | otherNet.releaseWakeLock(); |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 | mActiveDefaultNetwork = type; |
| 965 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | thisNet.setTeardownRequested(false); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 967 | if (DBG) Log.d(TAG, "Sending CONNECT bcast for " + info.getTypeName()); |
| 968 | thisNet.updateNetworkSettings(); |
| 969 | handleConnectivityChange(); |
| 970 | sendConnectedBroadcast(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | private void handleScanResultsAvailable(NetworkInfo info) { |
| 974 | int networkType = info.getType(); |
| 975 | if (networkType != ConnectivityManager.TYPE_WIFI) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 976 | if (DBG) Log.v(TAG, "Got ScanResultsAvailable for " + |
| 977 | info.getTypeName() + " network. Don't know how to handle."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 979 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | mNetTrackers[networkType].interpretScanResultsAvailable(); |
| 981 | } |
| 982 | |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 983 | private void handleNotificationChange(boolean visible, int id, |
| 984 | Notification notification) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | NotificationManager notificationManager = (NotificationManager) mContext |
| 986 | .getSystemService(Context.NOTIFICATION_SERVICE); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 987 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | if (visible) { |
| 989 | notificationManager.notify(id, notification); |
| 990 | } else { |
| 991 | notificationManager.cancel(id); |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | /** |
| 996 | * After any kind of change in the connectivity state of any network, |
| 997 | * make sure that anything that depends on the connectivity state of |
| 998 | * more than one network is set up correctly. We're mainly concerned |
| 999 | * with making sure that the list of DNS servers is set up according |
| 1000 | * to which networks are connected, and ensuring that the right routing |
| 1001 | * table entries exist. |
| 1002 | */ |
| 1003 | private void handleConnectivityChange() { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1004 | if (DBG) Log.d(TAG, "handleConnectivityChange"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | /* |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1006 | * If a non-default network is enabled, add the host routes that |
| 1007 | * will allow it's DNS servers to be accessed. Only |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | * If both mobile and wifi are enabled, add the host routes that |
| 1009 | * will allow MMS traffic to pass on the mobile network. But |
| 1010 | * remove the default route for the mobile network, so that there |
| 1011 | * will be only one default route, to ensure that all traffic |
| 1012 | * except MMS will travel via Wi-Fi. |
| 1013 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1014 | handleDnsConfigurationChange(); |
| 1015 | |
| 1016 | for (int netType : mPriorityList) { |
| 1017 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
| 1018 | if (mNetAttributes[netType].isDefault()) { |
| 1019 | mNetTrackers[netType].addDefaultRoute(); |
| 1020 | } else { |
| 1021 | mNetTrackers[netType].addPrivateDnsRoutes(); |
| 1022 | } |
| 1023 | } else { |
| 1024 | if (mNetAttributes[netType].isDefault()) { |
| 1025 | mNetTrackers[netType].removeDefaultRoute(); |
| 1026 | } else { |
| 1027 | mNetTrackers[netType].removePrivateDnsRoutes(); |
| 1028 | } |
| 1029 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1033 | /** |
| 1034 | * Adjust the per-process dns entries (net.dns<x>.<pid>) based |
| 1035 | * on the highest priority active net which this process requested. |
| 1036 | * If there aren't any, clear it out |
| 1037 | */ |
| 1038 | private void reassessPidDns(int myPid, boolean doBump) |
| 1039 | { |
| 1040 | if (DBG) Log.d(TAG, "reassessPidDns for pid " + myPid); |
| 1041 | for(int i : mPriorityList) { |
| 1042 | if (mNetAttributes[i].isDefault()) { |
| 1043 | continue; |
| 1044 | } |
| 1045 | NetworkStateTracker nt = mNetTrackers[i]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1046 | if (nt.getNetworkInfo().isConnected() && |
| 1047 | !nt.isTeardownRequested()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1048 | List pids = mNetRequestersPids[i]; |
| 1049 | for (int j=0; j<pids.size(); j++) { |
| 1050 | Integer pid = (Integer)pids.get(j); |
| 1051 | if (pid.intValue() == myPid) { |
| 1052 | String[] dnsList = nt.getNameServers(); |
| 1053 | writePidDns(dnsList, myPid); |
| 1054 | if (doBump) { |
| 1055 | bumpDns(); |
| 1056 | } |
| 1057 | return; |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | // nothing found - delete |
| 1063 | for (int i = 1; ; i++) { |
| 1064 | String prop = "net.dns" + i + "." + myPid; |
| 1065 | if (SystemProperties.get(prop).length() == 0) { |
| 1066 | if (doBump) { |
| 1067 | bumpDns(); |
| 1068 | } |
| 1069 | return; |
| 1070 | } |
| 1071 | SystemProperties.set(prop, ""); |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | private void writePidDns(String[] dnsList, int pid) { |
| 1076 | int j = 1; |
| 1077 | for (String dns : dnsList) { |
| 1078 | if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) { |
| 1079 | SystemProperties.set("net.dns" + j++ + "." + pid, dns); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | private void bumpDns() { |
| 1085 | /* |
| 1086 | * Bump the property that tells the name resolver library to reread |
| 1087 | * the DNS server list from the properties. |
| 1088 | */ |
| 1089 | String propVal = SystemProperties.get("net.dnschange"); |
| 1090 | int n = 0; |
| 1091 | if (propVal.length() != 0) { |
| 1092 | try { |
| 1093 | n = Integer.parseInt(propVal); |
| 1094 | } catch (NumberFormatException e) {} |
| 1095 | } |
| 1096 | SystemProperties.set("net.dnschange", "" + (n+1)); |
| 1097 | } |
| 1098 | |
| 1099 | private void handleDnsConfigurationChange() { |
| 1100 | if (DBG) Log.d(TAG, "handleDnsConfig Change"); |
| 1101 | // add default net's dns entries |
| 1102 | for (int x = mPriorityList.length-1; x>= 0; x--) { |
| 1103 | int netType = mPriorityList[x]; |
| 1104 | NetworkStateTracker nt = mNetTrackers[netType]; |
| 1105 | if (DBG) Log.d(TAG, " checking " + nt); |
| 1106 | if (nt != null && nt.getNetworkInfo().isConnected() && |
| 1107 | !nt.isTeardownRequested()) { |
| 1108 | if (DBG) Log.d(TAG, " connected"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1109 | String[] dnsList = nt.getNameServers(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1110 | if (mNetAttributes[netType].isDefault()) { |
| 1111 | int j = 1; |
| 1112 | for (String dns : dnsList) { |
| 1113 | if (dns != null && !TextUtils.equals(dns, "0.0.0.0")) { |
Robert Greenwalt | b06324a | 2009-08-25 14:00:10 -0700 | [diff] [blame^] | 1114 | if (DBG) Log.d(TAG, " adding "+dns); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1115 | SystemProperties.set("net.dns" + j++, dns); |
| 1116 | } |
| 1117 | } |
| 1118 | for (int k=j ; k<mNumDnsEntries; k++) { |
Robert Greenwalt | b06324a | 2009-08-25 14:00:10 -0700 | [diff] [blame^] | 1119 | if (DBG) Log.d(TAG, "erasing net.dns" + k); |
| 1120 | SystemProperties.set("net.dns" + k, ""); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1121 | } |
| 1122 | mNumDnsEntries = j; |
| 1123 | } else { |
| 1124 | // set per-pid dns for attached secondary nets |
| 1125 | List pids = mNetRequestersPids[netType]; |
| 1126 | for (int y=0; y< pids.size(); y++) { |
| 1127 | Integer pid = (Integer)pids.get(y); |
| 1128 | writePidDns(dnsList, pid.intValue()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1133 | |
| 1134 | bumpDns(); |
| 1135 | } |
| 1136 | |
| 1137 | private int getRestoreDefaultNetworkDelay() { |
| 1138 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 1139 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 1140 | if(restoreDefaultNetworkDelayStr != null && |
| 1141 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 1142 | try { |
| 1143 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 1144 | } catch (NumberFormatException e) { |
| 1145 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1147 | return RESTORE_DEFAULT_NETWORK_DELAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | @Override |
| 1151 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1152 | if (mContext.checkCallingOrSelfPermission( |
| 1153 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1155 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 1156 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1157 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | return; |
| 1159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | pw.println(); |
| 1161 | for (NetworkStateTracker nst : mNetTrackers) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1162 | if (nst.getNetworkInfo().isConnected()) { |
| 1163 | pw.println("Active network: " + nst.getNetworkInfo(). |
| 1164 | getTypeName()); |
| 1165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | pw.println(nst.getNetworkInfo()); |
| 1167 | pw.println(nst); |
| 1168 | pw.println(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1172 | // must be stateless - things change under us. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1173 | private class MyHandler extends Handler { |
| 1174 | @Override |
| 1175 | public void handleMessage(Message msg) { |
| 1176 | NetworkInfo info; |
| 1177 | switch (msg.what) { |
| 1178 | case NetworkStateTracker.EVENT_STATE_CHANGED: |
| 1179 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1180 | if (DBG) Log.d(TAG, "ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1181 | info.getTypeName() + ": " + |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | info.getState() + "/" + info.getDetailedState()); |
| 1183 | |
| 1184 | // Connectivity state changed: |
| 1185 | // [31-13] Reserved for future use |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1186 | // [12-9] Network subtype (for mobile network, as defined |
| 1187 | // by TelephonyManager) |
| 1188 | // [8-3] Detailed state ordinal (as defined by |
| 1189 | // NetworkInfo.DetailedState) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | // [2-0] Network type (as defined by ConnectivityManager) |
| 1191 | int eventLogParam = (info.getType() & 0x7) | |
| 1192 | ((info.getDetailedState().ordinal() & 0x3f) << 3) | |
| 1193 | (info.getSubtype() << 9); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1194 | EventLog.writeEvent(EVENTLOG_CONNECTIVITY_STATE_CHANGED, |
| 1195 | eventLogParam); |
| 1196 | |
| 1197 | if (info.getDetailedState() == |
| 1198 | NetworkInfo.DetailedState.FAILED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | handleConnectionFailure(info); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1200 | } else if (info.getState() == |
| 1201 | NetworkInfo.State.DISCONNECTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | handleDisconnect(info); |
| 1203 | } else if (info.getState() == NetworkInfo.State.SUSPENDED) { |
| 1204 | // TODO: need to think this over. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1205 | // the logic here is, handle SUSPENDED the same as |
| 1206 | // DISCONNECTED. The only difference being we are |
| 1207 | // broadcasting an intent with NetworkInfo that's |
| 1208 | // suspended. This allows the applications an |
| 1209 | // opportunity to handle DISCONNECTED and SUSPENDED |
| 1210 | // differently, or not. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | handleDisconnect(info); |
| 1212 | } else if (info.getState() == NetworkInfo.State.CONNECTED) { |
| 1213 | handleConnect(info); |
| 1214 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | break; |
| 1216 | |
| 1217 | case NetworkStateTracker.EVENT_SCAN_RESULTS_AVAILABLE: |
| 1218 | info = (NetworkInfo) msg.obj; |
| 1219 | handleScanResultsAvailable(info); |
| 1220 | break; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | case NetworkStateTracker.EVENT_NOTIFICATION_CHANGED: |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1223 | handleNotificationChange(msg.arg1 == 1, msg.arg2, |
| 1224 | (Notification) msg.obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1225 | |
| 1226 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1227 | handleDnsConfigurationChange(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | break; |
| 1229 | |
| 1230 | case NetworkStateTracker.EVENT_ROAMING_CHANGED: |
| 1231 | // fill me in |
| 1232 | break; |
| 1233 | |
| 1234 | case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: |
| 1235 | // fill me in |
| 1236 | break; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1237 | case NetworkStateTracker.EVENT_RESTORE_DEFAULT_NETWORK: |
| 1238 | for (NetworkStateTracker net : mNetTrackers) { |
| 1239 | NetworkInfo i = net.getNetworkInfo(); |
| 1240 | if (i.isConnected() && |
| 1241 | !mNetAttributes[i.getType()].isDefault()) { |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 1242 | if (DBG) { |
| 1243 | Log.d(TAG, "tearing down " + i + |
| 1244 | " to restore the default network"); |
| 1245 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1246 | teardown(net); |
| 1247 | } |
| 1248 | } |
| 1249 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | } |