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 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
| 22 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_BLUETOOTH; |
| 24 | import static android.net.ConnectivityManager.TYPE_DUMMY; |
| 25 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
| 26 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 27 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 28 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
| 29 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
| 31 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 32 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 33 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 34 | import android.app.Notification; |
| 35 | import android.app.NotificationManager; |
| 36 | import android.app.PendingIntent; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 37 | import android.bluetooth.BluetoothTetheringDataTracker; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.content.ContentResolver; |
| 40 | import android.content.Context; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 41 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 43 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 45 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 46 | import android.database.ContentObserver; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 47 | import android.net.CaptivePortalTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.net.ConnectivityManager; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 49 | import android.net.DummyDataStateTracker; |
Benoit Goby | 1997069 | 2010-12-22 14:29:40 -0800 | [diff] [blame] | 50 | import android.net.EthernetDataTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 52 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 53 | import android.net.INetworkPolicyListener; |
| 54 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 55 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 56 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 57 | import android.net.LinkProperties; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 58 | import android.net.Uri; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 59 | import android.net.LinkProperties.CompareResult; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 61 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 63 | import android.net.NetworkInfo.DetailedState; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 64 | import android.net.NetworkInfo.State; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 65 | import android.net.NetworkQuotaInfo; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 66 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 68 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 69 | import android.net.Proxy; |
| 70 | import android.net.ProxyProperties; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 71 | import android.net.RouteInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.net.wifi.WifiStateTracker; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 73 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 74 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | import android.os.Binder; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 76 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 78 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 79 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 80 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.os.Looper; |
| 82 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 83 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 84 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 85 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 86 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 87 | import android.os.RemoteException; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 88 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 90 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 92 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 94 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 95 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 96 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 97 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 98 | import android.util.Slog; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 99 | import android.util.SparseIntArray; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 100 | import android.util.SparseArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 102 | import com.android.internal.R; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 103 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 104 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 105 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 106 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 107 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 108 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 109 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 110 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 111 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 112 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 113 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 114 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 115 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 116 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 117 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 118 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 119 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 120 | import com.android.internal.annotations.GuardedBy; |
| 121 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 122 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | import java.io.FileDescriptor; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 125 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 127 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 128 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 129 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 130 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 131 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 132 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 133 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 134 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 135 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 136 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 137 | import java.util.GregorianCalendar; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 138 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 139 | import java.util.List; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 140 | import java.util.Random; |
| 141 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * @hide |
| 145 | */ |
| 146 | public class ConnectivityService extends IConnectivityManager.Stub { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 147 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
Robert Greenwalt | ba175a5 | 2010-10-05 19:12:26 -0700 | [diff] [blame] | 149 | private static final boolean DBG = true; |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 150 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 152 | private static final boolean LOGD_RULES = false; |
| 153 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 154 | // TODO: create better separation between radio types and network types |
| 155 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 156 | // how long to wait before switching back to a radio's default network |
| 157 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 158 | // system property that can override the above value |
| 159 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 160 | "android.telephony.apn-restore"; |
| 161 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 162 | // Default value if FAIL_FAST_TIME_MS is not set |
| 163 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 164 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 165 | private static final String FAIL_FAST_TIME_MS = |
| 166 | "persist.radio.fail_fast_time_ms"; |
| 167 | |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 168 | // used in recursive route setting to add gateways for the host for which |
| 169 | // a host route was requested. |
| 170 | private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10; |
| 171 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 172 | private Tethering mTethering; |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 173 | private boolean mTetheringConfigValid = false; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 174 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 175 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 176 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 177 | @GuardedBy("mVpns") |
| 178 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 179 | private VpnCallback mVpnCallback = new VpnCallback(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 181 | private boolean mLockdownEnabled; |
| 182 | private LockdownVpnTracker mLockdownTracker; |
| 183 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 184 | private Nat464Xlat mClat; |
| 185 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 186 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 187 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 188 | /** Currently active network rules by UID. */ |
| 189 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 190 | /** Set of ifaces that are costly. */ |
| 191 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | /** |
| 194 | * Sometimes we want to refer to the individual network state |
| 195 | * trackers separately, and sometimes we just want to treat them |
| 196 | * abstractly. |
| 197 | */ |
| 198 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 199 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 200 | /* Handles captive portal check on a network */ |
| 201 | private CaptivePortalTracker mCaptivePortalTracker; |
| 202 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 203 | /** |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 204 | * The link properties that define the current links |
| 205 | */ |
| 206 | private LinkProperties mCurrentLinkProperties[]; |
| 207 | |
| 208 | /** |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 209 | * A per Net list of the PID's that requested access to the net |
| 210 | * used both as a refcount and for per-PID DNS selection |
| 211 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 212 | private List<Integer> mNetRequestersPids[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 213 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 214 | // priority order of the nettrackers |
| 215 | // (excluding dynamically set mNetworkPreference) |
| 216 | // TODO - move mNetworkTypePreference into this |
| 217 | private int[] mPriorityList; |
| 218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | private Context mContext; |
| 220 | private int mNetworkPreference; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 221 | private int mActiveDefaultNetwork = -1; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 222 | // 0 is full bad, 100 is full good |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 223 | private int mDefaultInetCondition = 0; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 224 | private int mDefaultInetConditionPublished = 0; |
| 225 | private boolean mInetConditionChangeInFlight = false; |
| 226 | private int mDefaultConnectionSequence = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 228 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 229 | private int mNumDnsEntries; |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 230 | private boolean mDnsOverridden = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | |
| 232 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 233 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 235 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 236 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 237 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 238 | private static final int ENABLED = 1; |
| 239 | private static final int DISABLED = 0; |
| 240 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 241 | private static final boolean ADD = true; |
| 242 | private static final boolean REMOVE = false; |
| 243 | |
| 244 | private static final boolean TO_DEFAULT_TABLE = true; |
| 245 | private static final boolean TO_SECONDARY_TABLE = false; |
| 246 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 247 | /** |
| 248 | * used internally as a delayed event to make us switch back to the |
| 249 | * default network |
| 250 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 251 | private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * used internally to change our mobile data enabled flag |
| 255 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 256 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * used internally to change our network preference setting |
| 260 | * arg1 = networkType to prefer |
| 261 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 262 | private static final int EVENT_SET_NETWORK_PREFERENCE = 3; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 263 | |
| 264 | /** |
| 265 | * used internally to synchronize inet condition reports |
| 266 | * arg1 = networkType |
| 267 | * arg2 = condition (0 bad, 100 good) |
| 268 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 269 | private static final int EVENT_INET_CONDITION_CHANGE = 4; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * used internally to mark the end of inet condition hold periods |
| 273 | * arg1 = networkType |
| 274 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 275 | private static final int EVENT_INET_CONDITION_HOLD_END = 5; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 276 | |
| 277 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 278 | * used internally to set enable/disable cellular data |
| 279 | * arg1 = ENBALED or DISABLED |
| 280 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 281 | private static final int EVENT_SET_MOBILE_DATA = 7; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 282 | |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 283 | /** |
| 284 | * used internally to clear a wakelock when transitioning |
| 285 | * from one net to another |
| 286 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 287 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 288 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 289 | /** |
| 290 | * used internally to reload global proxy settings |
| 291 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 292 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 293 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 294 | /** |
| 295 | * used internally to set external dependency met/unmet |
| 296 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 297 | * arg2 = NetworkType |
| 298 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 299 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 300 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 301 | /** |
| 302 | * used internally to restore DNS properties back to the |
| 303 | * default network |
| 304 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 305 | private static final int EVENT_RESTORE_DNS = 11; |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 306 | |
Wink Saville | 2b30afd | 2011-08-05 11:40:22 -0700 | [diff] [blame] | 307 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 308 | * used internally to send a sticky broadcast delayed. |
| 309 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 310 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 12; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 311 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 312 | /** |
| 313 | * Used internally to |
| 314 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 315 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 316 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 13; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 317 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 318 | private static final int EVENT_VPN_STATE_CHANGED = 14; |
| 319 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 320 | /** |
| 321 | * Used internally to disable fail fast of mobile data |
| 322 | */ |
| 323 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 15; |
| 324 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 325 | /** Handler used for internal events. */ |
| 326 | private InternalHandler mHandler; |
| 327 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
| 328 | private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 329 | |
| 330 | // list of DeathRecipients used to make sure features are turned off when |
| 331 | // a process dies |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 332 | private List<FeatureUser> mFeatureUsers; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 333 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 334 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 335 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 336 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 337 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 338 | private String mNetTransitionWakeLockCausedBy = ""; |
| 339 | private int mNetTransitionWakeLockSerialNumber; |
| 340 | private int mNetTransitionWakeLockTimeout; |
| 341 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 342 | private InetAddress mDefaultDns; |
| 343 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 344 | // this collection is used to refcount the added routes - if there are none left |
| 345 | // it's time to remove the route from the route table |
| 346 | private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>(); |
| 347 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 348 | // used in DBG mode to track inet condition reports |
| 349 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 350 | private ArrayList mInetLog; |
| 351 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 352 | // track the current default http proxy - tell the world if we get a new one (real change) |
| 353 | private ProxyProperties mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 354 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 355 | private boolean mDefaultProxyDisabled = false; |
| 356 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 357 | // track the global proxy. |
| 358 | private ProxyProperties mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 359 | |
| 360 | private SettingsObserver mSettingsObserver; |
| 361 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 362 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 363 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 364 | |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 365 | private static class RadioAttributes { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 366 | public int mSimultaneity; |
| 367 | public int mType; |
| 368 | public RadioAttributes(String init) { |
| 369 | String fragments[] = init.split(","); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 370 | mType = Integer.parseInt(fragments[0]); |
| 371 | mSimultaneity = Integer.parseInt(fragments[1]); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | RadioAttributes[] mRadioAttributes; |
| 375 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 376 | // the set of network types that can only be enabled by system/sig apps |
| 377 | List mProtectedNetworks; |
| 378 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 379 | private DataConnectionStats mDataConnectionStats; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 380 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 381 | |
| 382 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 383 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 384 | public ConnectivityService(Context context, INetworkManagementService netd, |
| 385 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 386 | // Currently, omitting a NetworkFactory will create one internally |
| 387 | // TODO: create here when we have cleaner WiMAX support |
| 388 | this(context, netd, statsService, policyManager, null); |
| 389 | } |
| 390 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 391 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 392 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 393 | NetworkFactory netFactory) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 394 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 395 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 396 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 397 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 398 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 399 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 400 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 401 | if (netFactory == null) { |
| 402 | netFactory = new DefaultNetworkFactory(context, mTrackerHandler); |
| 403 | } |
| 404 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 405 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 406 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 407 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 408 | Settings.Secure.ANDROID_ID); |
| 409 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 410 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 411 | SystemProperties.set("net.hostname", name); |
| 412 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 415 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 416 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 417 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 418 | if (dns == null || dns.length() == 0) { |
| 419 | dns = context.getResources().getString( |
| 420 | com.android.internal.R.string.config_default_dns_server); |
| 421 | } |
| 422 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 423 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 424 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 425 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 428 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 429 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 430 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 431 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 432 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 433 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 434 | try { |
| 435 | mPolicyManager.registerListener(mPolicyListener); |
| 436 | } catch (RemoteException e) { |
| 437 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 438 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 442 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 443 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 444 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 445 | com.android.internal.R.integer.config_networkTransitionTimeout); |
| 446 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 447 | mNetTrackers = new NetworkStateTracker[ |
| 448 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 449 | mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 450 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 451 | mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1]; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 452 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 453 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 454 | // Load device network attributes from resources |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 455 | String[] raStrings = context.getResources().getStringArray( |
| 456 | com.android.internal.R.array.radioAttributes); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 457 | for (String raString : raStrings) { |
| 458 | RadioAttributes r = new RadioAttributes(raString); |
| 459 | if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 460 | loge("Error in radioAttributes - ignoring attempt to define type " + r.mType); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 461 | continue; |
| 462 | } |
| 463 | if (mRadioAttributes[r.mType] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 464 | loge("Error in radioAttributes - ignoring attempt to redefine type " + |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 465 | r.mType); |
| 466 | continue; |
| 467 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 468 | mRadioAttributes[r.mType] = r; |
| 469 | } |
| 470 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 471 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 472 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 473 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 474 | String[] naStrings = context.getResources().getStringArray( |
| 475 | com.android.internal.R.array.networkAttributes); |
| 476 | for (String naString : naStrings) { |
| 477 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 478 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 479 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 480 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 481 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 482 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 483 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 484 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 485 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 486 | n.type); |
| 487 | continue; |
| 488 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 489 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 490 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 491 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 492 | continue; |
| 493 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 494 | if (mRadioAttributes[n.radio] == null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 495 | loge("Error in networkAttributes - ignoring attempt to use undefined " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 496 | "radio " + n.radio + " in network type " + n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 497 | continue; |
| 498 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 499 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 500 | mNetworksDefined++; |
| 501 | } catch(Exception e) { |
| 502 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 506 | mProtectedNetworks = new ArrayList<Integer>(); |
| 507 | int[] protectedNetworks = context.getResources().getIntArray( |
| 508 | com.android.internal.R.array.config_protectedNetworks); |
| 509 | for (int p : protectedNetworks) { |
| 510 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 511 | mProtectedNetworks.add(p); |
| 512 | } else { |
| 513 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 514 | } |
| 515 | } |
| 516 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 517 | // high priority first |
| 518 | mPriorityList = new int[mNetworksDefined]; |
| 519 | { |
| 520 | int insertionPoint = mNetworksDefined-1; |
| 521 | int currentLowest = 0; |
| 522 | int nextLowest = 0; |
| 523 | while (insertionPoint > -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 524 | for (NetworkConfig na : mNetConfigs) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 525 | if (na == null) continue; |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 526 | if (na.priority < currentLowest) continue; |
| 527 | if (na.priority > currentLowest) { |
| 528 | if (na.priority < nextLowest || nextLowest == 0) { |
| 529 | nextLowest = na.priority; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 530 | } |
| 531 | continue; |
| 532 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 533 | mPriorityList[insertionPoint--] = na.type; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 534 | } |
| 535 | currentLowest = nextLowest; |
| 536 | nextLowest = 0; |
| 537 | } |
| 538 | } |
| 539 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 540 | // Update mNetworkPreference according to user mannually first then overlay config.xml |
| 541 | mNetworkPreference = getPersistedNetworkPreference(); |
| 542 | if (mNetworkPreference == -1) { |
| 543 | for (int n : mPriorityList) { |
| 544 | if (mNetConfigs[n].isDefault() && ConnectivityManager.isNetworkTypeValid(n)) { |
| 545 | mNetworkPreference = n; |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | if (mNetworkPreference == -1) { |
| 550 | throw new IllegalStateException( |
| 551 | "You should set at least one default Network in config.xml!"); |
| 552 | } |
| 553 | } |
| 554 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 555 | mNetRequestersPids = |
| 556 | (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 557 | for (int i : mPriorityList) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 558 | mNetRequestersPids[i] = new ArrayList<Integer>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 561 | mFeatureUsers = new ArrayList<FeatureUser>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 562 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 563 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 564 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 565 | |
| 566 | // Create and start trackers for hard-coded networks |
| 567 | for (int targetNetworkType : mPriorityList) { |
| 568 | final NetworkConfig config = mNetConfigs[targetNetworkType]; |
| 569 | final NetworkStateTracker tracker; |
| 570 | try { |
| 571 | tracker = netFactory.createTracker(targetNetworkType, config); |
| 572 | mNetTrackers[targetNetworkType] = tracker; |
| 573 | } catch (IllegalArgumentException e) { |
| 574 | Slog.e(TAG, "Problem creating " + getNetworkTypeName(targetNetworkType) |
| 575 | + " tracker: " + e); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 576 | continue; |
| 577 | } |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 578 | |
| 579 | tracker.startMonitoring(context, mTrackerHandler); |
| 580 | if (config.isDefault()) { |
| 581 | tracker.reconnect(); |
Robert Greenwalt | 6537b02 | 2011-11-10 16:55:20 -0800 | [diff] [blame] | 582 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 583 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 584 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 585 | mTethering = new Tethering(mContext, mNetd, statsService, this, mHandler.getLooper()); |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 586 | mTetheringConfigValid = ((mTethering.getTetherableUsbRegexs().length != 0 || |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 587 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 588 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 589 | mTethering.getUpstreamIfaceTypes().length != 0); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 590 | //set up the listener for user state for creating user VPNs |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 591 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 592 | IntentFilter intentFilter = new IntentFilter(); |
| 593 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 594 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 595 | mContext.registerReceiverAsUser( |
| 596 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 597 | mClat = new Nat464Xlat(mContext, mNetd, this, mTrackerHandler); |
| 598 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 599 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 600 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 601 | mNetd.registerObserver(mDataActivityObserver); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 602 | mNetd.registerObserver(mClat); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 603 | } catch (RemoteException e) { |
| 604 | loge("Error registering observer :" + e); |
| 605 | } |
| 606 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 607 | if (DBG) { |
| 608 | mInetLog = new ArrayList(); |
| 609 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 610 | |
| 611 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 612 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 613 | |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 614 | mCaptivePortalTracker = CaptivePortalTracker.makeCaptivePortalTracker(mContext, this); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 615 | loadGlobalProxy(); |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 616 | |
| 617 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 618 | mDataConnectionStats.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 620 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 621 | /** |
| 622 | * Factory that creates {@link NetworkStateTracker} instances using given |
| 623 | * {@link NetworkConfig}. |
| 624 | */ |
| 625 | public interface NetworkFactory { |
| 626 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config); |
| 627 | } |
| 628 | |
| 629 | private static class DefaultNetworkFactory implements NetworkFactory { |
| 630 | private final Context mContext; |
| 631 | private final Handler mTrackerHandler; |
| 632 | |
| 633 | public DefaultNetworkFactory(Context context, Handler trackerHandler) { |
| 634 | mContext = context; |
| 635 | mTrackerHandler = trackerHandler; |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config) { |
| 640 | switch (config.radio) { |
| 641 | case TYPE_WIFI: |
| 642 | return new WifiStateTracker(targetNetworkType, config.name); |
| 643 | case TYPE_MOBILE: |
| 644 | return new MobileDataStateTracker(targetNetworkType, config.name); |
| 645 | case TYPE_DUMMY: |
| 646 | return new DummyDataStateTracker(targetNetworkType, config.name); |
| 647 | case TYPE_BLUETOOTH: |
| 648 | return BluetoothTetheringDataTracker.getInstance(); |
| 649 | case TYPE_WIMAX: |
| 650 | return makeWimaxStateTracker(mContext, mTrackerHandler); |
| 651 | case TYPE_ETHERNET: |
| 652 | return EthernetDataTracker.getInstance(); |
| 653 | default: |
| 654 | throw new IllegalArgumentException( |
| 655 | "Trying to create a NetworkStateTracker for an unknown radio type: " |
| 656 | + config.radio); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Loads external WiMAX library and registers as system service, returning a |
| 663 | * {@link NetworkStateTracker} for WiMAX. Caller is still responsible for |
| 664 | * invoking {@link NetworkStateTracker#startMonitoring(Context, Handler)}. |
| 665 | */ |
| 666 | private static NetworkStateTracker makeWimaxStateTracker( |
| 667 | Context context, Handler trackerHandler) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 668 | // Initialize Wimax |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 669 | DexClassLoader wimaxClassLoader; |
| 670 | Class wimaxStateTrackerClass = null; |
| 671 | Class wimaxServiceClass = null; |
| 672 | Class wimaxManagerClass; |
| 673 | String wimaxJarLocation; |
| 674 | String wimaxLibLocation; |
| 675 | String wimaxManagerClassName; |
| 676 | String wimaxServiceClassName; |
| 677 | String wimaxStateTrackerClassName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 679 | NetworkStateTracker wimaxStateTracker = null; |
| 680 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 681 | boolean isWimaxEnabled = context.getResources().getBoolean( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 682 | com.android.internal.R.bool.config_wimaxEnabled); |
| 683 | |
| 684 | if (isWimaxEnabled) { |
| 685 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 686 | wimaxJarLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 687 | com.android.internal.R.string.config_wimaxServiceJarLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 688 | wimaxLibLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 689 | com.android.internal.R.string.config_wimaxNativeLibLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 690 | wimaxManagerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 691 | com.android.internal.R.string.config_wimaxManagerClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 692 | wimaxServiceClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 693 | com.android.internal.R.string.config_wimaxServiceClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 694 | wimaxStateTrackerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 695 | com.android.internal.R.string.config_wimaxStateTrackerClassname); |
| 696 | |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 697 | if (DBG) log("wimaxJarLocation: " + wimaxJarLocation); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 698 | wimaxClassLoader = new DexClassLoader(wimaxJarLocation, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 699 | new ContextWrapper(context).getCacheDir().getAbsolutePath(), |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 700 | wimaxLibLocation, ClassLoader.getSystemClassLoader()); |
| 701 | |
| 702 | try { |
| 703 | wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName); |
| 704 | wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName); |
| 705 | wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName); |
| 706 | } catch (ClassNotFoundException ex) { |
| 707 | loge("Exception finding Wimax classes: " + ex.toString()); |
| 708 | return null; |
| 709 | } |
| 710 | } catch(Resources.NotFoundException ex) { |
| 711 | loge("Wimax Resources does not exist!!! "); |
| 712 | return null; |
| 713 | } |
| 714 | |
| 715 | try { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 716 | if (DBG) log("Starting Wimax Service... "); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 717 | |
| 718 | Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor |
| 719 | (new Class[] {Context.class, Handler.class}); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 720 | wimaxStateTracker = (NetworkStateTracker) wmxStTrkrConst.newInstance( |
| 721 | context, trackerHandler); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 722 | |
| 723 | Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor |
| 724 | (new Class[] {Context.class, wimaxStateTrackerClass}); |
| 725 | wmxSrvConst.setAccessible(true); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 726 | IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(context, wimaxStateTracker); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 727 | wmxSrvConst.setAccessible(false); |
| 728 | |
| 729 | ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker); |
| 730 | |
| 731 | } catch(Exception ex) { |
| 732 | loge("Exception creating Wimax classes: " + ex.toString()); |
| 733 | return null; |
| 734 | } |
| 735 | } else { |
| 736 | loge("Wimax is not enabled or not added to the network attributes!!! "); |
| 737 | return null; |
| 738 | } |
| 739 | |
| 740 | return wimaxStateTracker; |
| 741 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 742 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 744 | * Sets the preferred network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | * @param preference the new preference |
| 746 | */ |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 747 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | enforceChangePermission(); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 749 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 750 | mHandler.sendMessage( |
| 751 | mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | public int getNetworkPreference() { |
| 755 | enforceAccessPermission(); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 756 | int preference; |
| 757 | synchronized(this) { |
| 758 | preference = mNetworkPreference; |
| 759 | } |
| 760 | return preference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 763 | private void handleSetNetworkPreference(int preference) { |
| 764 | if (ConnectivityManager.isNetworkTypeValid(preference) && |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 765 | mNetConfigs[preference] != null && |
| 766 | mNetConfigs[preference].isDefault()) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 767 | if (mNetworkPreference != preference) { |
| 768 | final ContentResolver cr = mContext.getContentResolver(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 769 | Settings.Global.putInt(cr, Settings.Global.NETWORK_PREFERENCE, preference); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 770 | synchronized(this) { |
| 771 | mNetworkPreference = preference; |
| 772 | } |
| 773 | enforcePreference(); |
| 774 | } |
| 775 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 777 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 778 | private int getConnectivityChangeDelay() { |
| 779 | final ContentResolver cr = mContext.getContentResolver(); |
| 780 | |
| 781 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 782 | int defaultDelay = SystemProperties.getInt( |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 783 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 784 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 785 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 786 | defaultDelay); |
| 787 | } |
| 788 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | private int getPersistedNetworkPreference() { |
| 790 | final ContentResolver cr = mContext.getContentResolver(); |
| 791 | |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 792 | final int networkPrefSetting = Settings.Global |
| 793 | .getInt(cr, Settings.Global.NETWORK_PREFERENCE, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 795 | return networkPrefSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 799 | * 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] | 800 | * In this method, we only tear down a non-preferred network. Establishing |
| 801 | * a connection to the preferred network is taken care of when we handle |
| 802 | * the disconnect event from the non-preferred network |
| 803 | * (see {@link #handleDisconnect(NetworkInfo)}). |
| 804 | */ |
| 805 | private void enforcePreference() { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 806 | if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 807 | return; |
| 808 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 809 | if (!mNetTrackers[mNetworkPreference].isAvailable()) |
| 810 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 812 | for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 813 | if (t != mNetworkPreference && mNetTrackers[t] != null && |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 814 | mNetTrackers[t].getNetworkInfo().isConnected()) { |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 815 | if (DBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 816 | log("tearing down " + mNetTrackers[t].getNetworkInfo() + |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 817 | " in enforcePreference"); |
| 818 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 819 | teardown(mNetTrackers[t]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | private boolean teardown(NetworkStateTracker netTracker) { |
| 825 | if (netTracker.teardown()) { |
| 826 | netTracker.setTeardownRequested(true); |
| 827 | return true; |
| 828 | } else { |
| 829 | return false; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 834 | * Check if UID should be blocked from using the network represented by the |
| 835 | * given {@link NetworkStateTracker}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 836 | */ |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 837 | private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) { |
| 838 | final String iface = tracker.getLinkProperties().getInterfaceName(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 839 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 840 | final boolean networkCostly; |
| 841 | final int uidRules; |
| 842 | synchronized (mRulesLock) { |
| 843 | networkCostly = mMeteredIfaces.contains(iface); |
| 844 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 845 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 846 | |
| 847 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | // no restrictive rules; network is visible |
| 852 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 856 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 857 | * {@link DetailedState#BLOCKED} based on |
| 858 | * {@link #isNetworkBlocked(NetworkStateTracker, int)}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 859 | */ |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 860 | private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) { |
| 861 | NetworkInfo info = tracker.getNetworkInfo(); |
| 862 | if (isNetworkBlocked(tracker, uid)) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 863 | // network is blocked; clone and override state |
| 864 | info = new NetworkInfo(info); |
| 865 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 866 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 867 | if (mLockdownTracker != null) { |
| 868 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 869 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 870 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 874 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 875 | * It is assumed that at most one network is active at a time. If more |
| 876 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 877 | * @return the info for the active network, or {@code null} if none is |
| 878 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 880 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 882 | enforceAccessPermission(); |
| 883 | final int uid = Binder.getCallingUid(); |
| 884 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 887 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 888 | enforceAccessPermission(); |
| 889 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
| 890 | final NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork]; |
| 891 | if (tracker != null) { |
| 892 | return tracker.getNetworkInfo(); |
| 893 | } |
| 894 | } |
| 895 | return null; |
| 896 | } |
| 897 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 898 | @Override |
| 899 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 900 | enforceConnectivityInternalPermission(); |
| 901 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
| 902 | } |
| 903 | |
| 904 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | public NetworkInfo getNetworkInfo(int networkType) { |
| 906 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 907 | final int uid = Binder.getCallingUid(); |
| 908 | return getNetworkInfo(networkType, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | } |
| 910 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 911 | private NetworkInfo getNetworkInfo(int networkType, int uid) { |
| 912 | NetworkInfo info = null; |
| 913 | if (isNetworkTypeValid(networkType)) { |
| 914 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 915 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 916 | info = getFilteredNetworkInfo(tracker, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | return info; |
| 920 | } |
| 921 | |
| 922 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | public NetworkInfo[] getAllNetworkInfo() { |
| 924 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 925 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 926 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 927 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 928 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 929 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 930 | result.add(getFilteredNetworkInfo(tracker, uid)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 931 | } |
| 932 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 933 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 934 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | } |
| 936 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 937 | @Override |
| 938 | public boolean isNetworkSupported(int networkType) { |
| 939 | enforceAccessPermission(); |
| 940 | return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null)); |
| 941 | } |
| 942 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 943 | /** |
| 944 | * Return LinkProperties for the active (i.e., connected) default |
| 945 | * network interface. It is assumed that at most one default network |
| 946 | * is active at a time. If more than one is active, it is indeterminate |
| 947 | * which will be returned. |
| 948 | * @return the ip properties for the active network, or {@code null} if |
| 949 | * none is active |
| 950 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 951 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 952 | public LinkProperties getActiveLinkProperties() { |
Robert Greenwalt | 5991158 | 2011-05-20 12:23:41 -0700 | [diff] [blame] | 953 | return getLinkProperties(mActiveDefaultNetwork); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 956 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 957 | public LinkProperties getLinkProperties(int networkType) { |
| 958 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 959 | if (isNetworkTypeValid(networkType)) { |
| 960 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 961 | if (tracker != null) { |
| 962 | return tracker.getLinkProperties(); |
| 963 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 964 | } |
| 965 | return null; |
| 966 | } |
| 967 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 968 | @Override |
| 969 | public NetworkState[] getAllNetworkState() { |
| 970 | enforceAccessPermission(); |
| 971 | final int uid = Binder.getCallingUid(); |
| 972 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 973 | synchronized (mRulesLock) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 974 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 975 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 976 | final NetworkInfo info = getFilteredNetworkInfo(tracker, uid); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 977 | result.add(new NetworkState( |
| 978 | info, tracker.getLinkProperties(), tracker.getLinkCapabilities())); |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | return result.toArray(new NetworkState[result.size()]); |
| 983 | } |
| 984 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 985 | private NetworkState getNetworkStateUnchecked(int networkType) { |
| 986 | if (isNetworkTypeValid(networkType)) { |
| 987 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 988 | if (tracker != null) { |
| 989 | return new NetworkState(tracker.getNetworkInfo(), tracker.getLinkProperties(), |
| 990 | tracker.getLinkCapabilities()); |
| 991 | } |
| 992 | } |
| 993 | return null; |
| 994 | } |
| 995 | |
| 996 | @Override |
| 997 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 998 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 999 | |
| 1000 | final long token = Binder.clearCallingIdentity(); |
| 1001 | try { |
| 1002 | final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork); |
| 1003 | if (state != null) { |
| 1004 | try { |
| 1005 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1006 | } catch (RemoteException e) { |
| 1007 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1008 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1009 | return null; |
| 1010 | } finally { |
| 1011 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1012 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1015 | @Override |
| 1016 | public boolean isActiveNetworkMetered() { |
| 1017 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1018 | final long token = Binder.clearCallingIdentity(); |
| 1019 | try { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1020 | return isNetworkMeteredUnchecked(mActiveDefaultNetwork); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1021 | } finally { |
| 1022 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1023 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1026 | private boolean isNetworkMeteredUnchecked(int networkType) { |
| 1027 | final NetworkState state = getNetworkStateUnchecked(networkType); |
| 1028 | if (state != null) { |
| 1029 | try { |
| 1030 | return mPolicyManager.isNetworkMetered(state); |
| 1031 | } catch (RemoteException e) { |
| 1032 | } |
| 1033 | } |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | public boolean setRadios(boolean turnOn) { |
| 1038 | boolean result = true; |
| 1039 | enforceChangePermission(); |
| 1040 | for (NetworkStateTracker t : mNetTrackers) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1041 | if (t != null) result = t.setRadio(turnOn) && result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | return result; |
| 1044 | } |
| 1045 | |
| 1046 | public boolean setRadio(int netType, boolean turnOn) { |
| 1047 | enforceChangePermission(); |
| 1048 | if (!ConnectivityManager.isNetworkTypeValid(netType)) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | NetworkStateTracker tracker = mNetTrackers[netType]; |
| 1052 | return tracker != null && tracker.setRadio(turnOn); |
| 1053 | } |
| 1054 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1055 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1056 | @Override |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1057 | public void interfaceClassDataActivityChanged(String label, boolean active) { |
| 1058 | int deviceType = Integer.parseInt(label); |
| 1059 | sendDataActivityBroadcast(deviceType, active); |
| 1060 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1061 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1062 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1063 | /** |
| 1064 | * Used to notice when the calling process dies so we can self-expire |
| 1065 | * |
| 1066 | * Also used to know if the process has cleaned up after itself when |
| 1067 | * our auto-expire timer goes off. The timer has a link to an object. |
| 1068 | * |
| 1069 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1070 | private class FeatureUser implements IBinder.DeathRecipient { |
| 1071 | int mNetworkType; |
| 1072 | String mFeature; |
| 1073 | IBinder mBinder; |
| 1074 | int mPid; |
| 1075 | int mUid; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1076 | long mCreateTime; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1077 | |
| 1078 | FeatureUser(int type, String feature, IBinder binder) { |
| 1079 | super(); |
| 1080 | mNetworkType = type; |
| 1081 | mFeature = feature; |
| 1082 | mBinder = binder; |
| 1083 | mPid = getCallingPid(); |
| 1084 | mUid = getCallingUid(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1085 | mCreateTime = System.currentTimeMillis(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1086 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1087 | try { |
| 1088 | mBinder.linkToDeath(this, 0); |
| 1089 | } catch (RemoteException e) { |
| 1090 | binderDied(); |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | void unlinkDeathRecipient() { |
| 1095 | mBinder.unlinkToDeath(this, 0); |
| 1096 | } |
| 1097 | |
| 1098 | public void binderDied() { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1099 | log("ConnectivityService FeatureUser binderDied(" + |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1100 | mNetworkType + ", " + mFeature + ", " + mBinder + "), created " + |
| 1101 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1102 | stopUsingNetworkFeature(this, false); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1105 | public void expire() { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1106 | if (VDBG) { |
| 1107 | log("ConnectivityService FeatureUser expire(" + |
| 1108 | mNetworkType + ", " + mFeature + ", " + mBinder +"), created " + |
| 1109 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
| 1110 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1111 | stopUsingNetworkFeature(this, false); |
| 1112 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1113 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1114 | public boolean isSameUser(FeatureUser u) { |
| 1115 | if (u == null) return false; |
| 1116 | |
| 1117 | return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature); |
| 1118 | } |
| 1119 | |
| 1120 | public boolean isSameUser(int pid, int uid, int networkType, String feature) { |
| 1121 | if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) && |
| 1122 | TextUtils.equals(mFeature, feature)) { |
| 1123 | return true; |
| 1124 | } |
| 1125 | return false; |
| 1126 | } |
| 1127 | |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1128 | public String toString() { |
| 1129 | return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " + |
| 1130 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"; |
| 1131 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1134 | // javadoc from interface |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1135 | public int startUsingNetworkFeature(int networkType, String feature, |
| 1136 | IBinder binder) { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1137 | long startTime = 0; |
| 1138 | if (DBG) { |
| 1139 | startTime = SystemClock.elapsedRealtime(); |
| 1140 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1141 | if (VDBG) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1142 | log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid=" |
| 1143 | + Binder.getCallingUid()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | enforceChangePermission(); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1146 | try { |
| 1147 | if (!ConnectivityManager.isNetworkTypeValid(networkType) || |
| 1148 | mNetConfigs[networkType] == null) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1149 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1150 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1151 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1152 | FeatureUser f = new FeatureUser(networkType, feature, binder); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1153 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1154 | // TODO - move this into individual networktrackers |
| 1155 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1156 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1157 | if (mLockdownEnabled) { |
| 1158 | // Since carrier APNs usually aren't available from VPN |
| 1159 | // endpoint, mark them as unavailable. |
| 1160 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
| 1161 | } |
| 1162 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1163 | if (mProtectedNetworks.contains(usedNetworkType)) { |
| 1164 | enforceConnectivityInternalPermission(); |
| 1165 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1166 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1167 | // if UID is restricted, don't allow them to bring up metered APNs |
| 1168 | final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType); |
| 1169 | final int uidRules; |
| 1170 | synchronized (mRulesLock) { |
| 1171 | uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL); |
| 1172 | } |
| 1173 | if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1174 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1175 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1176 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1177 | NetworkStateTracker network = mNetTrackers[usedNetworkType]; |
| 1178 | if (network != null) { |
| 1179 | Integer currentPid = new Integer(getCallingPid()); |
| 1180 | if (usedNetworkType != networkType) { |
| 1181 | NetworkInfo ni = network.getNetworkInfo(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1182 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1183 | if (ni.isAvailable() == false) { |
| 1184 | if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 1185 | if (DBG) log("special network not available ni=" + ni.getTypeName()); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1186 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1187 | } else { |
| 1188 | // else make the attempt anyway - probably giving REQUEST_STARTED below |
| 1189 | if (DBG) { |
| 1190 | log("special network not available, but try anyway ni=" + |
| 1191 | ni.getTypeName()); |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | } |
| 1195 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1196 | int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1197 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1198 | synchronized(this) { |
| 1199 | boolean addToList = true; |
| 1200 | if (restoreTimer < 0) { |
| 1201 | // In case there is no timer is specified for the feature, |
| 1202 | // make sure we don't add duplicate entry with the same request. |
| 1203 | for (FeatureUser u : mFeatureUsers) { |
| 1204 | if (u.isSameUser(f)) { |
| 1205 | // Duplicate user is found. Do not add. |
| 1206 | addToList = false; |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1210 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1211 | |
| 1212 | if (addToList) mFeatureUsers.add(f); |
| 1213 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1214 | // this gets used for per-pid dns when connected |
| 1215 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1216 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1217 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1218 | |
| 1219 | if (restoreTimer >= 0) { |
| 1220 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1221 | EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer); |
| 1222 | } |
| 1223 | |
| 1224 | if ((ni.isConnectedOrConnecting() == true) && |
| 1225 | !network.isTeardownRequested()) { |
| 1226 | if (ni.isConnected() == true) { |
| 1227 | final long token = Binder.clearCallingIdentity(); |
| 1228 | try { |
| 1229 | // add the pid-specific dns |
| 1230 | handleDnsConfigurationChange(usedNetworkType); |
| 1231 | if (VDBG) log("special network already active"); |
| 1232 | } finally { |
| 1233 | Binder.restoreCallingIdentity(token); |
| 1234 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1235 | return PhoneConstants.APN_ALREADY_ACTIVE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1236 | } |
| 1237 | if (VDBG) log("special network already connecting"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1238 | return PhoneConstants.APN_REQUEST_STARTED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | // check if the radio in play can make another contact |
| 1242 | // assume if cannot for now |
| 1243 | |
| 1244 | if (DBG) { |
| 1245 | log("startUsingNetworkFeature reconnecting to " + networkType + ": " + |
| 1246 | feature); |
| 1247 | } |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1248 | if (network.reconnect()) { |
| 1249 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1250 | } else { |
| 1251 | return PhoneConstants.APN_REQUEST_FAILED; |
| 1252 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1253 | } else { |
| 1254 | // need to remember this unsupported request so we respond appropriately on stop |
| 1255 | synchronized(this) { |
| 1256 | mFeatureUsers.add(f); |
| 1257 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1258 | // this gets used for per-pid dns when connected |
| 1259 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1260 | } |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1261 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1262 | return -1; |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1263 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1264 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1265 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1266 | } finally { |
| 1267 | if (DBG) { |
| 1268 | final long execTime = SystemClock.elapsedRealtime() - startTime; |
| 1269 | if (execTime > 250) { |
| 1270 | loge("startUsingNetworkFeature took too long: " + execTime + "ms"); |
| 1271 | } else { |
| 1272 | if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms"); |
| 1273 | } |
| 1274 | } |
| 1275 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1278 | // javadoc from interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1279 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1280 | enforceChangePermission(); |
| 1281 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1282 | int pid = getCallingPid(); |
| 1283 | int uid = getCallingUid(); |
| 1284 | |
| 1285 | FeatureUser u = null; |
| 1286 | boolean found = false; |
| 1287 | |
| 1288 | synchronized(this) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1289 | for (FeatureUser x : mFeatureUsers) { |
| 1290 | if (x.isSameUser(pid, uid, networkType, feature)) { |
| 1291 | u = x; |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1292 | found = true; |
| 1293 | break; |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | if (found && u != null) { |
| 1298 | // stop regardless of how many other time this proc had called start |
| 1299 | return stopUsingNetworkFeature(u, true); |
| 1300 | } else { |
| 1301 | // none found! |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1302 | if (VDBG) log("stopUsingNetworkFeature - not a live request, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1303 | return 1; |
| 1304 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1307 | private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) { |
| 1308 | int networkType = u.mNetworkType; |
| 1309 | String feature = u.mFeature; |
| 1310 | int pid = u.mPid; |
| 1311 | int uid = u.mUid; |
| 1312 | |
| 1313 | NetworkStateTracker tracker = null; |
| 1314 | boolean callTeardown = false; // used to carry our decision outside of sync block |
| 1315 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1316 | if (VDBG) { |
| 1317 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1318 | } |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1319 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1321 | if (DBG) { |
| 1322 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1323 | ", net is invalid"); |
| 1324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | return -1; |
| 1326 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1327 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1328 | // need to link the mFeatureUsers list with the mNetRequestersPids state in this |
| 1329 | // sync block |
| 1330 | synchronized(this) { |
| 1331 | // check if this process still has an outstanding start request |
| 1332 | if (!mFeatureUsers.contains(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1333 | if (VDBG) { |
| 1334 | log("stopUsingNetworkFeature: this process has no outstanding requests" + |
| 1335 | ", ignoring"); |
| 1336 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1337 | return 1; |
| 1338 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1339 | u.unlinkDeathRecipient(); |
| 1340 | mFeatureUsers.remove(mFeatureUsers.indexOf(u)); |
| 1341 | // If we care about duplicate requests, check for that here. |
| 1342 | // |
| 1343 | // This is done to support the extension of a request - the app |
| 1344 | // can request we start the network feature again and renew the |
| 1345 | // auto-shutoff delay. Normal "stop" calls from the app though |
| 1346 | // do not pay attention to duplicate requests - in effect the |
| 1347 | // API does not refcount and a single stop will counter multiple starts. |
| 1348 | if (ignoreDups == false) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1349 | for (FeatureUser x : mFeatureUsers) { |
| 1350 | if (x.isSameUser(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1351 | if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1352 | return 1; |
| 1353 | } |
| 1354 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1355 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1356 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 1357 | // TODO - move to individual network trackers |
| 1358 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
| 1359 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1360 | tracker = mNetTrackers[usedNetworkType]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1361 | if (tracker == null) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1362 | if (DBG) { |
| 1363 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1364 | " no known tracker for used net type " + usedNetworkType); |
| 1365 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1366 | return -1; |
| 1367 | } |
| 1368 | if (usedNetworkType != networkType) { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1369 | Integer currentPid = new Integer(pid); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1370 | mNetRequestersPids[usedNetworkType].remove(currentPid); |
Robert Greenwalt | 9055ae3 | 2013-02-12 17:18:25 -0800 | [diff] [blame] | 1371 | |
| 1372 | final long token = Binder.clearCallingIdentity(); |
| 1373 | try { |
| 1374 | reassessPidDns(pid, true); |
| 1375 | } finally { |
| 1376 | Binder.restoreCallingIdentity(token); |
| 1377 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1378 | flushVmDnsCache(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1379 | if (mNetRequestersPids[usedNetworkType].size() != 0) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1380 | if (VDBG) { |
| 1381 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1382 | " others still using it"); |
| 1383 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1384 | return 1; |
| 1385 | } |
| 1386 | callTeardown = true; |
Robert Greenwalt | 19b9ab4 | 2011-01-10 11:58:31 -0800 | [diff] [blame] | 1387 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1388 | if (DBG) { |
| 1389 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1390 | " not a known feature - dropping"); |
| 1391 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1392 | } |
| 1393 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1394 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1395 | if (callTeardown) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1396 | if (DBG) { |
| 1397 | log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature); |
| 1398 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1399 | tracker.teardown(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1400 | return 1; |
| 1401 | } else { |
Robert Greenwalt | 02648a4 | 2010-05-18 10:52:51 -0700 | [diff] [blame] | 1402 | return -1; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1403 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1407 | * @deprecated use requestRouteToHostAddress instead |
| 1408 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1409 | * Ensure that a network route exists to deliver traffic to the specified |
| 1410 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1411 | * @param networkType the type of the network over which traffic to the |
| 1412 | * specified host is to be routed |
| 1413 | * @param hostAddress the IP address of the host to which the route is |
| 1414 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1415 | * @return {@code true} on success, {@code false} on failure |
| 1416 | */ |
| 1417 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1418 | InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); |
| 1419 | |
| 1420 | if (inetAddress == null) { |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
| 1424 | return requestRouteToHostAddress(networkType, inetAddress.getAddress()); |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * Ensure that a network route exists to deliver traffic to the specified |
| 1429 | * host via the specified network interface. |
| 1430 | * @param networkType the type of the network over which traffic to the |
| 1431 | * specified host is to be routed |
| 1432 | * @param hostAddress the IP address of the host to which the route is |
| 1433 | * desired |
| 1434 | * @return {@code true} on success, {@code false} on failure |
| 1435 | */ |
| 1436 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1438 | if (mProtectedNetworks.contains(networkType)) { |
| 1439 | enforceConnectivityInternalPermission(); |
| 1440 | } |
| 1441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1443 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | return false; |
| 1445 | } |
| 1446 | NetworkStateTracker tracker = mNetTrackers[networkType]; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 1447 | DetailedState netState = tracker.getNetworkInfo().getDetailedState(); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1448 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 1449 | if (tracker == null || (netState != DetailedState.CONNECTED && |
| 1450 | netState != DetailedState.CAPTIVE_PORTAL_CHECK) || |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1451 | tracker.isTeardownRequested()) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1452 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1453 | log("requestRouteToHostAddress on down network " |
| 1454 | + "(" + networkType + ") - dropped" |
| 1455 | + " tracker=" + tracker |
| 1456 | + " netState=" + netState |
| 1457 | + " isTeardownRequested=" |
| 1458 | + ((tracker != null) ? tracker.isTeardownRequested() : "tracker:null")); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1459 | } |
| 1460 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1462 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1463 | try { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1464 | InetAddress addr = InetAddress.getByAddress(hostAddress); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1465 | LinkProperties lp = tracker.getLinkProperties(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1466 | boolean ok = addRouteToAddress(lp, addr); |
| 1467 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1468 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1469 | } catch (UnknownHostException e) { |
| 1470 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1471 | } finally { |
| 1472 | Binder.restoreCallingIdentity(token); |
| 1473 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1474 | if (DBG) log("requestRouteToHostAddress X bottom return false"); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1475 | return false; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1478 | private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) { |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1479 | return modifyRoute(p, r, 0, ADD, toDefaultTable); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 1480 | } |
| 1481 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1482 | private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable) { |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1483 | return modifyRoute(p, r, 0, REMOVE, toDefaultTable); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1486 | private boolean addRouteToAddress(LinkProperties lp, InetAddress addr) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1487 | return modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1491 | return modifyRouteToAddress(lp, addr, REMOVE, TO_DEFAULT_TABLE); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1494 | private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd, |
| 1495 | boolean toDefaultTable) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1496 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1497 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1498 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1499 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1500 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1501 | if (bestRoute.getGateway().equals(addr)) { |
| 1502 | // if there is no better route, add the implied hostroute for our gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1503 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1504 | } else { |
| 1505 | // if we will connect to this through another route, add a direct route |
| 1506 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1507 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1508 | } |
| 1509 | } |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1510 | return modifyRoute(lp, bestRoute, 0, doAdd, toDefaultTable); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1513 | private boolean modifyRoute(LinkProperties lp, RouteInfo r, int cycleCount, boolean doAdd, |
| 1514 | boolean toDefaultTable) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1515 | if ((lp == null) || (r == null)) { |
| 1516 | if (DBG) log("modifyRoute got unexpected null: " + lp + ", " + r); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1517 | return false; |
| 1518 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1519 | |
| 1520 | if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1521 | loge("Error modifying route - too much recursion"); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1522 | return false; |
| 1523 | } |
| 1524 | |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1525 | String ifaceName = r.getInterface(); |
| 1526 | if(ifaceName == null) { |
| 1527 | loge("Error modifying route - no interface name"); |
| 1528 | return false; |
| 1529 | } |
Robert Greenwalt | ca441ee | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 1530 | if (r.hasGateway()) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1531 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), r.getGateway()); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1532 | if (bestRoute != null) { |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1533 | if (bestRoute.getGateway().equals(r.getGateway())) { |
| 1534 | // if there is no better route, add the implied hostroute for our gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1535 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), ifaceName); |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1536 | } else { |
| 1537 | // if we will connect to our gateway through another route, add a direct |
| 1538 | // route to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1539 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), |
| 1540 | bestRoute.getGateway(), |
| 1541 | ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1542 | } |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1543 | modifyRoute(lp, bestRoute, cycleCount+1, doAdd, toDefaultTable); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1544 | } |
| 1545 | } |
| 1546 | if (doAdd) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1547 | if (VDBG) log("Adding " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1548 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1549 | if (toDefaultTable) { |
| 1550 | mAddedRoutes.add(r); // only track default table - only one apps can effect |
| 1551 | mNetd.addRoute(ifaceName, r); |
| 1552 | } else { |
| 1553 | mNetd.addSecondaryRoute(ifaceName, r); |
| 1554 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1555 | } catch (Exception e) { |
| 1556 | // never crash - catch them all |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1557 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1558 | return false; |
| 1559 | } |
| 1560 | } else { |
| 1561 | // if we remove this one and there are no more like it, then refcount==0 and |
| 1562 | // we can remove it from the table |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1563 | if (toDefaultTable) { |
| 1564 | mAddedRoutes.remove(r); |
| 1565 | if (mAddedRoutes.contains(r) == false) { |
| 1566 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 1567 | try { |
| 1568 | mNetd.removeRoute(ifaceName, r); |
| 1569 | } catch (Exception e) { |
| 1570 | // never crash - catch them all |
Robert Greenwalt | 2473a4ba | 2012-04-23 18:00:37 -0700 | [diff] [blame] | 1571 | if (VDBG) loge("Exception trying to remove a route: " + e); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1572 | return false; |
| 1573 | } |
| 1574 | } else { |
| 1575 | if (VDBG) log("not removing " + r + " as it's still in use"); |
| 1576 | } |
| 1577 | } else { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1578 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1579 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1580 | mNetd.removeSecondaryRoute(ifaceName, r); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1581 | } catch (Exception e) { |
| 1582 | // never crash - catch them all |
Robert Greenwalt | 2473a4ba | 2012-04-23 18:00:37 -0700 | [diff] [blame] | 1583 | if (VDBG) loge("Exception trying to remove a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1584 | return false; |
| 1585 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1592 | * @see ConnectivityManager#getMobileDataEnabled() |
| 1593 | */ |
| 1594 | public boolean getMobileDataEnabled() { |
Wink Saville | e798268 | 2010-12-07 10:31:02 -0800 | [diff] [blame] | 1595 | // TODO: This detail should probably be in DataConnectionTracker's |
| 1596 | // which is where we store the value and maybe make this |
| 1597 | // asynchronous. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1598 | enforceAccessPermission(); |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 1599 | boolean retVal = Settings.Global.getInt(mContext.getContentResolver(), |
| 1600 | Settings.Global.MOBILE_DATA, 1) == 1; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1601 | if (VDBG) log("getMobileDataEnabled returning " + retVal); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1602 | return retVal; |
| 1603 | } |
| 1604 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1605 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1606 | enforceConnectivityInternalPermission(); |
| 1607 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1608 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1609 | (met ? ENABLED : DISABLED), networkType)); |
| 1610 | } |
| 1611 | |
| 1612 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1613 | if (mNetTrackers[networkType] != null) { |
| 1614 | if (DBG) { |
| 1615 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1616 | } |
| 1617 | mNetTrackers[networkType].setDependencyMet(met); |
| 1618 | } |
| 1619 | } |
| 1620 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1621 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1622 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1623 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1624 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1625 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1626 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1627 | } |
| 1628 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1629 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1630 | // skip update when we've already applied rules |
| 1631 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1632 | if (oldRules == uidRules) return; |
| 1633 | |
| 1634 | mUidRules.put(uid, uidRules); |
| 1635 | } |
| 1636 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1637 | // TODO: notify UID when it has requested targeted updates |
| 1638 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1639 | |
| 1640 | @Override |
| 1641 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1642 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1643 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1644 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | synchronized (mRulesLock) { |
| 1648 | mMeteredIfaces.clear(); |
| 1649 | for (String iface : meteredIfaces) { |
| 1650 | mMeteredIfaces.add(iface); |
| 1651 | } |
| 1652 | } |
| 1653 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1654 | |
| 1655 | @Override |
| 1656 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1657 | // caller is NPMS, since we only register with them |
| 1658 | if (LOGD_RULES) { |
| 1659 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1660 | } |
| 1661 | |
| 1662 | // kick off connectivity change broadcast for active network, since |
| 1663 | // global background policy change is radical. |
| 1664 | final int networkType = mActiveDefaultNetwork; |
| 1665 | if (isNetworkTypeValid(networkType)) { |
| 1666 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1667 | if (tracker != null) { |
| 1668 | final NetworkInfo info = tracker.getNetworkInfo(); |
| 1669 | if (info != null && info.isConnected()) { |
| 1670 | sendConnectedBroadcast(info); |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1675 | }; |
| 1676 | |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1677 | /** |
| 1678 | * @see ConnectivityManager#setMobileDataEnabled(boolean) |
| 1679 | */ |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1680 | public void setMobileDataEnabled(boolean enabled) { |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1681 | enforceChangePermission(); |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1682 | if (DBG) log("setMobileDataEnabled(" + enabled + ")"); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1683 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1684 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA, |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1685 | (enabled ? ENABLED : DISABLED), 0)); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | private void handleSetMobileData(boolean enabled) { |
Wink Saville | e798268 | 2010-12-07 10:31:02 -0800 | [diff] [blame] | 1689 | if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1690 | if (VDBG) { |
| 1691 | log(mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1692 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1693 | mNetTrackers[ConnectivityManager.TYPE_MOBILE].setUserDataEnable(enabled); |
| 1694 | } |
tk.mun | b97d34c | 2012-01-06 10:43:52 +0900 | [diff] [blame] | 1695 | if (mNetTrackers[ConnectivityManager.TYPE_WIMAX] != null) { |
| 1696 | if (VDBG) { |
| 1697 | log(mNetTrackers[ConnectivityManager.TYPE_WIMAX].toString() + enabled); |
| 1698 | } |
| 1699 | mNetTrackers[ConnectivityManager.TYPE_WIMAX].setUserDataEnable(enabled); |
| 1700 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | @Override |
| 1704 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1705 | // only someone like NPMS should only be calling us |
| 1706 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1707 | |
| 1708 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1709 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 1710 | } |
| 1711 | |
| 1712 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
| 1713 | if (isNetworkTypeValid(networkType)) { |
| 1714 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1715 | if (tracker != null) { |
| 1716 | tracker.setPolicyDataEnable(enabled); |
| 1717 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1718 | } |
| 1719 | } |
| 1720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1721 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1722 | mContext.enforceCallingOrSelfPermission( |
| 1723 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1724 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1728 | mContext.enforceCallingOrSelfPermission( |
| 1729 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1730 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | } |
| 1732 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1733 | // TODO Make this a special check when it goes public |
| 1734 | private void enforceTetherChangePermission() { |
| 1735 | mContext.enforceCallingOrSelfPermission( |
| 1736 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1737 | "ConnectivityService"); |
| 1738 | } |
| 1739 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1740 | private void enforceTetherAccessPermission() { |
| 1741 | mContext.enforceCallingOrSelfPermission( |
| 1742 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1743 | "ConnectivityService"); |
| 1744 | } |
| 1745 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1746 | private void enforceConnectivityInternalPermission() { |
| 1747 | mContext.enforceCallingOrSelfPermission( |
| 1748 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1749 | "ConnectivityService"); |
| 1750 | } |
| 1751 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 1752 | private void enforceMarkNetworkSocketPermission() { |
| 1753 | //Media server special case |
| 1754 | if (Binder.getCallingUid() == Process.MEDIA_UID) { |
| 1755 | return; |
| 1756 | } |
| 1757 | mContext.enforceCallingOrSelfPermission( |
| 1758 | android.Manifest.permission.MARK_NETWORK_SOCKET, |
| 1759 | "ConnectivityService"); |
| 1760 | } |
| 1761 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1762 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1763 | * Handle a {@code DISCONNECTED} event. If this pertains to the non-active |
| 1764 | * network, we ignore it. If it is for the active network, we send out a |
| 1765 | * broadcast. But first, we check whether it might be possible to connect |
| 1766 | * to a different network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1767 | * @param info the {@code NetworkInfo} for the network |
| 1768 | */ |
| 1769 | private void handleDisconnect(NetworkInfo info) { |
| 1770 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1771 | int prevNetType = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1773 | mNetTrackers[prevNetType].setTeardownRequested(false); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1774 | |
| 1775 | // Remove idletimer previously setup in {@code handleConnect} |
| 1776 | removeDataActivityTracking(prevNetType); |
| 1777 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | /* |
| 1779 | * If the disconnected network is not the active one, then don't report |
| 1780 | * this as a loss of connectivity. What probably happened is that we're |
| 1781 | * getting the disconnect for a network that we explicitly disabled |
| 1782 | * in accordance with network preference policies. |
| 1783 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1784 | if (!mNetConfigs[prevNetType].isDefault()) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1785 | List<Integer> pids = mNetRequestersPids[prevNetType]; |
| 1786 | for (Integer pid : pids) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1787 | // will remove them because the net's no longer connected |
| 1788 | // need to do this now as only now do we know the pids and |
| 1789 | // can properly null things that are no longer referenced. |
| 1790 | reassessPidDns(pid.intValue(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1791 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1795 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1796 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1797 | if (info.isFailover()) { |
| 1798 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1799 | info.setFailover(false); |
| 1800 | } |
| 1801 | if (info.getReason() != null) { |
| 1802 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1803 | } |
| 1804 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1805 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1806 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1808 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1809 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1810 | tryFailover(prevNetType); |
| 1811 | if (mActiveDefaultNetwork != -1) { |
| 1812 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 1813 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 1814 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 1815 | mDefaultInetConditionPublished = 0; // we're not connected anymore |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 1816 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 1817 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1818 | } |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 1819 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 1820 | |
| 1821 | // Reset interface if no other connections are using the same interface |
| 1822 | boolean doReset = true; |
| 1823 | LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties(); |
| 1824 | if (linkProperties != null) { |
| 1825 | String oldIface = linkProperties.getInterfaceName(); |
| 1826 | if (TextUtils.isEmpty(oldIface) == false) { |
| 1827 | for (NetworkStateTracker networkStateTracker : mNetTrackers) { |
| 1828 | if (networkStateTracker == null) continue; |
| 1829 | NetworkInfo networkInfo = networkStateTracker.getNetworkInfo(); |
| 1830 | if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) { |
| 1831 | LinkProperties l = networkStateTracker.getLinkProperties(); |
| 1832 | if (l == null) continue; |
| 1833 | if (oldIface.equals(l.getInterfaceName())) { |
| 1834 | doReset = false; |
| 1835 | break; |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | } |
| 1841 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1842 | // do this before we broadcast the change |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 1843 | handleConnectivityChange(prevNetType, doReset); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1844 | |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1845 | final Intent immediateIntent = new Intent(intent); |
| 1846 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 1847 | sendStickyBroadcast(immediateIntent); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1848 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1849 | /* |
| 1850 | * If the failover network is already connected, then immediately send |
| 1851 | * out a followup broadcast indicating successful failover |
| 1852 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1853 | if (mActiveDefaultNetwork != -1) { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1854 | sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), |
| 1855 | getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1856 | } |
| 1857 | } |
| 1858 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1859 | private void tryFailover(int prevNetType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1860 | /* |
Robert Greenwalt | bff9018 | 2011-01-06 15:41:07 -0800 | [diff] [blame] | 1861 | * If this is a default network, check if other defaults are available. |
| 1862 | * Try to reconnect on all available and let them hash it out when |
| 1863 | * more than one connects. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1864 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1865 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1866 | if (mActiveDefaultNetwork == prevNetType) { |
| 1867 | mActiveDefaultNetwork = -1; |
| 1868 | } |
| 1869 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1870 | // don't signal a reconnect for anything lower or equal priority than our |
| 1871 | // current connected default |
| 1872 | // TODO - don't filter by priority now - nice optimization but risky |
| 1873 | // int currentPriority = -1; |
| 1874 | // if (mActiveDefaultNetwork != -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1875 | // currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority; |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1876 | // } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1877 | for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1878 | if (checkType == prevNetType) continue; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1879 | if (mNetConfigs[checkType] == null) continue; |
| 1880 | if (!mNetConfigs[checkType].isDefault()) continue; |
Robert Greenwalt | 424781e | 2011-11-11 09:56:03 -0800 | [diff] [blame] | 1881 | if (mNetTrackers[checkType] == null) continue; |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 1882 | |
| 1883 | // Enabling the isAvailable() optimization caused mobile to not get |
| 1884 | // selected if it was in the middle of error handling. Specifically |
| 1885 | // a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL |
| 1886 | // would not be available and we wouldn't get connected to anything. |
| 1887 | // So removing the isAvailable() optimization below for now. TODO: This |
| 1888 | // optimization should work and we need to investigate why it doesn't work. |
| 1889 | // This could be related to how DEACTIVATE_DATA_CALL is reporting its |
| 1890 | // complete before it is really complete. |
| 1891 | // if (!mNetTrackers[checkType].isAvailable()) continue; |
| 1892 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1893 | // if (currentPriority >= mNetConfigs[checkType].mPriority) continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1894 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1895 | NetworkStateTracker checkTracker = mNetTrackers[checkType]; |
| 1896 | NetworkInfo checkInfo = checkTracker.getNetworkInfo(); |
| 1897 | if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) { |
| 1898 | checkInfo.setFailover(true); |
| 1899 | checkTracker.reconnect(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1900 | } |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 1901 | if (DBG) log("Attempting to switch to " + checkInfo.getTypeName()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1902 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1904 | } |
| 1905 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1906 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1907 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1908 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1909 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1912 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1913 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1914 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1917 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1918 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1919 | } |
| 1920 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1921 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1922 | if (mLockdownTracker != null) { |
| 1923 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 1924 | } |
| 1925 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1926 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1927 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1928 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1929 | if (info.isFailover()) { |
| 1930 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1931 | info.setFailover(false); |
| 1932 | } |
| 1933 | if (info.getReason() != null) { |
| 1934 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1935 | } |
| 1936 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1937 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1938 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1940 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1941 | return intent; |
| 1942 | } |
| 1943 | |
| 1944 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1945 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1946 | } |
| 1947 | |
| 1948 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 1949 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } |
| 1951 | |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1952 | private void sendDataActivityBroadcast(int deviceType, boolean active) { |
| 1953 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1954 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1955 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1956 | final long ident = Binder.clearCallingIdentity(); |
| 1957 | try { |
| 1958 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1959 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 1960 | } finally { |
| 1961 | Binder.restoreCallingIdentity(ident); |
| 1962 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1965 | /** |
| 1966 | * Called when an attempt to fail over to another network has failed. |
| 1967 | * @param info the {@link NetworkInfo} for the failed network |
| 1968 | */ |
| 1969 | private void handleConnectionFailure(NetworkInfo info) { |
| 1970 | mNetTrackers[info.getType()].setTeardownRequested(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1971 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1972 | String reason = info.getReason(); |
| 1973 | String extraInfo = info.getExtraInfo(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1974 | |
Robert Greenwalt | 572172b | 2010-10-08 16:35:52 -0700 | [diff] [blame] | 1975 | String reasonText; |
| 1976 | if (reason == null) { |
| 1977 | reasonText = "."; |
| 1978 | } else { |
| 1979 | reasonText = " (" + reason + ")."; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1981 | loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1982 | |
| 1983 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1984 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1985 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1986 | if (getActiveNetworkInfo() == null) { |
| 1987 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 1988 | } |
| 1989 | if (reason != null) { |
| 1990 | intent.putExtra(ConnectivityManager.EXTRA_REASON, reason); |
| 1991 | } |
| 1992 | if (extraInfo != null) { |
| 1993 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo); |
| 1994 | } |
| 1995 | if (info.isFailover()) { |
| 1996 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1997 | info.setFailover(false); |
| 1998 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 1999 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2000 | if (mNetConfigs[info.getType()].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2001 | tryFailover(info.getType()); |
| 2002 | if (mActiveDefaultNetwork != -1) { |
| 2003 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2004 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 2005 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2006 | mDefaultInetConditionPublished = 0; |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2007 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2008 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2009 | } |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2010 | |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2011 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2012 | |
| 2013 | final Intent immediateIntent = new Intent(intent); |
| 2014 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 2015 | sendStickyBroadcast(immediateIntent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2016 | sendStickyBroadcast(intent); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2017 | /* |
| 2018 | * If the failover network is already connected, then immediately send |
| 2019 | * out a followup broadcast indicating successful failover |
| 2020 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2021 | if (mActiveDefaultNetwork != -1) { |
| 2022 | sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2023 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | private void sendStickyBroadcast(Intent intent) { |
| 2027 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2028 | if (!mSystemReady) { |
| 2029 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2030 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2031 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2032 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2033 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2034 | } |
| 2035 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2036 | final long ident = Binder.clearCallingIdentity(); |
| 2037 | try { |
| 2038 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2039 | } finally { |
| 2040 | Binder.restoreCallingIdentity(ident); |
| 2041 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2045 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 2046 | if (delayMs <= 0) { |
| 2047 | sendStickyBroadcast(intent); |
| 2048 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2049 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2050 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 2051 | + intent.getAction()); |
| 2052 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2053 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 2054 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 2055 | } |
| 2056 | } |
| 2057 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2058 | void systemReady() { |
| 2059 | synchronized(this) { |
| 2060 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2061 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2062 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2063 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2064 | } |
| 2065 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2066 | // load the global proxy at startup |
| 2067 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2068 | |
| 2069 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 2070 | // for user to unlock device. |
| 2071 | if (!updateLockdownVpn()) { |
| 2072 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 2073 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 2074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | } |
| 2076 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2077 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 2078 | @Override |
| 2079 | public void onReceive(Context context, Intent intent) { |
| 2080 | // Try creating lockdown tracker, since user present usually means |
| 2081 | // unlocked keystore. |
| 2082 | if (updateLockdownVpn()) { |
| 2083 | mContext.unregisterReceiver(this); |
| 2084 | } |
| 2085 | } |
| 2086 | }; |
| 2087 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2088 | private boolean isNewNetTypePreferredOverCurrentNetType(int type) { |
| 2089 | if ((type != mNetworkPreference && |
| 2090 | mNetConfigs[mActiveDefaultNetwork].priority > |
| 2091 | mNetConfigs[type].priority) || |
| 2092 | mNetworkPreference == mActiveDefaultNetwork) return false; |
| 2093 | return true; |
| 2094 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2095 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2096 | private void handleConnect(NetworkInfo info) { |
| 2097 | final int newNetType = info.getType(); |
| 2098 | |
| 2099 | setupDataActivityTracking(newNetType); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2101 | // snapshot isFailover, because sendConnectedBroadcast() resets it |
| 2102 | boolean isFailover = info.isFailover(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2103 | final NetworkStateTracker thisNet = mNetTrackers[newNetType]; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2104 | final String thisIface = thisNet.getLinkProperties().getInterfaceName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2105 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2106 | // if this is a default net and other default is running |
| 2107 | // kill the one not preferred |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2108 | if (mNetConfigs[newNetType].isDefault()) { |
| 2109 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { |
| 2110 | if (isNewNetTypePreferredOverCurrentNetType(newNetType)) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2111 | // tear down the other |
| 2112 | NetworkStateTracker otherNet = |
| 2113 | mNetTrackers[mActiveDefaultNetwork]; |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2114 | if (DBG) { |
| 2115 | log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2116 | " teardown"); |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2117 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2118 | if (!teardown(otherNet)) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2119 | loge("Network declined teardown request"); |
Robert Greenwalt | 27725e8 | 2011-03-29 11:36:28 -0700 | [diff] [blame] | 2120 | teardown(thisNet); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2121 | return; |
| 2122 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2123 | } else { |
| 2124 | // don't accept this one |
| 2125 | if (VDBG) { |
| 2126 | log("Not broadcasting CONNECT_ACTION " + |
| 2127 | "to torn down network " + info.getTypeName()); |
| 2128 | } |
| 2129 | teardown(thisNet); |
| 2130 | return; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | synchronized (ConnectivityService.this) { |
| 2134 | // have a new default network, release the transition wakelock in a second |
| 2135 | // if it's held. The second pause is to allow apps to reconnect over the |
| 2136 | // new network |
| 2137 | if (mNetTransitionWakeLock.isHeld()) { |
| 2138 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 2139 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2140 | mNetTransitionWakeLockSerialNumber, 0), |
| 2141 | 1000); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2142 | } |
| 2143 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2144 | mActiveDefaultNetwork = newNetType; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2145 | // this will cause us to come up initially as unconnected and switching |
| 2146 | // to connected after our normal pause unless somebody reports us as reall |
| 2147 | // disconnected |
| 2148 | mDefaultInetConditionPublished = 0; |
| 2149 | mDefaultConnectionSequence++; |
| 2150 | mInetConditionChangeInFlight = false; |
| 2151 | // Don't do this - if we never sign in stay, grey |
| 2152 | //reportNetworkCondition(mActiveDefaultNetwork, 100); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2153 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2154 | thisNet.setTeardownRequested(false); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2155 | updateNetworkSettings(thisNet); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2156 | handleConnectivityChange(newNetType, false); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2157 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2158 | |
| 2159 | // notify battery stats service about this network |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2160 | if (thisIface != null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2161 | try { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2162 | BatteryStatsService.getService().noteNetworkInterfaceType(thisIface, newNetType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2163 | } catch (RemoteException e) { |
| 2164 | // ignored; service lives in system_server |
| 2165 | } |
| 2166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2167 | } |
| 2168 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2169 | private void handleCaptivePortalTrackerCheck(NetworkInfo info) { |
| 2170 | if (DBG) log("Captive portal check " + info); |
| 2171 | int type = info.getType(); |
| 2172 | final NetworkStateTracker thisNet = mNetTrackers[type]; |
| 2173 | if (mNetConfigs[type].isDefault()) { |
| 2174 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != type) { |
| 2175 | if (isNewNetTypePreferredOverCurrentNetType(type)) { |
| 2176 | if (DBG) log("Captive check on " + info.getTypeName()); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2177 | mCaptivePortalTracker.detectCaptivePortal(new NetworkInfo(info)); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2178 | return; |
| 2179 | } else { |
| 2180 | if (DBG) log("Tear down low priority net " + info.getTypeName()); |
| 2181 | teardown(thisNet); |
| 2182 | return; |
| 2183 | } |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | thisNet.captivePortalCheckComplete(); |
| 2188 | } |
| 2189 | |
| 2190 | /** @hide */ |
| 2191 | public void captivePortalCheckComplete(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2192 | enforceConnectivityInternalPermission(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2193 | mNetTrackers[info.getType()].captivePortalCheckComplete(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | /** |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2197 | * Setup data activity tracking for the given network interface. |
| 2198 | * |
| 2199 | * Every {@code setupDataActivityTracking} should be paired with a |
| 2200 | * {@link removeDataActivityTracking} for cleanup. |
| 2201 | */ |
| 2202 | private void setupDataActivityTracking(int type) { |
| 2203 | final NetworkStateTracker thisNet = mNetTrackers[type]; |
| 2204 | final String iface = thisNet.getLinkProperties().getInterfaceName(); |
| 2205 | |
| 2206 | final int timeout; |
| 2207 | |
| 2208 | if (ConnectivityManager.isNetworkTypeMobile(type)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2209 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2210 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2211 | 0); |
| 2212 | // Canonicalize mobile network type |
| 2213 | type = ConnectivityManager.TYPE_MOBILE; |
| 2214 | } else if (ConnectivityManager.TYPE_WIFI == type) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2215 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2216 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2217 | 0); |
| 2218 | } else { |
| 2219 | // do not track any other networks |
| 2220 | timeout = 0; |
| 2221 | } |
| 2222 | |
| 2223 | if (timeout > 0 && iface != null) { |
| 2224 | try { |
| 2225 | mNetd.addIdleTimer(iface, timeout, Integer.toString(type)); |
| 2226 | } catch (RemoteException e) { |
| 2227 | } |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | /** |
| 2232 | * Remove data activity tracking when network disconnects. |
| 2233 | */ |
| 2234 | private void removeDataActivityTracking(int type) { |
| 2235 | final NetworkStateTracker net = mNetTrackers[type]; |
| 2236 | final String iface = net.getLinkProperties().getInterfaceName(); |
| 2237 | |
| 2238 | if (iface != null && (ConnectivityManager.isNetworkTypeMobile(type) || |
| 2239 | ConnectivityManager.TYPE_WIFI == type)) { |
| 2240 | try { |
| 2241 | // the call fails silently if no idletimer setup for this interface |
| 2242 | mNetd.removeIdleTimer(iface); |
| 2243 | } catch (RemoteException e) { |
| 2244 | } |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /** |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2249 | * After a change in the connectivity state of a network. We're mainly |
| 2250 | * concerned with making sure that the list of DNS servers is set up |
| 2251 | * according to which networks are connected, and ensuring that the |
| 2252 | * right routing table entries exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2253 | */ |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2254 | private void handleConnectivityChange(int netType, boolean doReset) { |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2255 | int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0; |
| 2256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2257 | /* |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2258 | * If a non-default network is enabled, add the host routes that |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2259 | * will allow it's DNS servers to be accessed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2260 | */ |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2261 | handleDnsConfigurationChange(netType); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2262 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2263 | LinkProperties curLp = mCurrentLinkProperties[netType]; |
| 2264 | LinkProperties newLp = null; |
| 2265 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2266 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2267 | newLp = mNetTrackers[netType].getLinkProperties(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2268 | if (VDBG) { |
| 2269 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2270 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2271 | "\n curLp=" + curLp + |
| 2272 | "\n newLp=" + newLp); |
| 2273 | } |
| 2274 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2275 | if (curLp != null) { |
| 2276 | if (curLp.isIdenticalInterfaceName(newLp)) { |
| 2277 | CompareResult<LinkAddress> car = curLp.compareAddresses(newLp); |
| 2278 | if ((car.removed.size() != 0) || (car.added.size() != 0)) { |
| 2279 | for (LinkAddress linkAddr : car.removed) { |
| 2280 | if (linkAddr.getAddress() instanceof Inet4Address) { |
| 2281 | resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES; |
| 2282 | } |
| 2283 | if (linkAddr.getAddress() instanceof Inet6Address) { |
| 2284 | resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES; |
| 2285 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2286 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2287 | if (DBG) { |
| 2288 | log("handleConnectivityChange: addresses changed" + |
| 2289 | " linkProperty[" + netType + "]:" + " resetMask=" + resetMask + |
| 2290 | "\n car=" + car); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2291 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2292 | } else { |
| 2293 | if (DBG) { |
| 2294 | log("handleConnectivityChange: address are the same reset per doReset" + |
| 2295 | " linkProperty[" + netType + "]:" + |
| 2296 | " resetMask=" + resetMask); |
| 2297 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2298 | } |
| 2299 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2300 | resetMask = NetworkUtils.RESET_ALL_ADDRESSES; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2301 | if (DBG) { |
| 2302 | log("handleConnectivityChange: interface not not equivalent reset both" + |
| 2303 | " linkProperty[" + netType + "]:" + |
| 2304 | " resetMask=" + resetMask); |
| 2305 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2306 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2307 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2308 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2309 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2310 | } |
| 2311 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2312 | if (VDBG) { |
| 2313 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2314 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2315 | "\n curLp=" + curLp + |
| 2316 | "\n newLp= null"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2319 | mCurrentLinkProperties[netType] = newLp; |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2320 | boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault()); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2321 | |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2322 | if (resetMask != 0 || resetDns) { |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2323 | if (curLp != null) { |
| 2324 | for (String iface : curLp.getAllInterfaceNames()) { |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2325 | if (TextUtils.isEmpty(iface) == false) { |
| 2326 | if (resetMask != 0) { |
| 2327 | if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")"); |
| 2328 | NetworkUtils.resetConnections(iface, resetMask); |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2329 | |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2330 | // Tell VPN the interface is down. It is a temporary |
| 2331 | // but effective fix to make VPN aware of the change. |
| 2332 | if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2333 | synchronized(mVpns) { |
| 2334 | for (int i = 0; i < mVpns.size(); i++) { |
| 2335 | mVpns.valueAt(i).interfaceStatusChanged(iface, false); |
| 2336 | } |
| 2337 | } |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2338 | } |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2339 | } |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2340 | if (resetDns) { |
| 2341 | flushVmDnsCache(); |
| 2342 | if (VDBG) log("resetting DNS cache for " + iface); |
| 2343 | try { |
| 2344 | mNetd.flushInterfaceDnsCache(iface); |
| 2345 | } catch (Exception e) { |
| 2346 | // never crash - catch them all |
| 2347 | if (DBG) loge("Exception resetting dns cache: " + e); |
| 2348 | } |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2349 | } |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2350 | } else { |
| 2351 | loge("Can't reset connection for type "+netType); |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2352 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | } |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2356 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2357 | // Update 464xlat state. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2358 | NetworkStateTracker tracker = mNetTrackers[netType]; |
| 2359 | if (mClat.requiresClat(netType, tracker)) { |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 2360 | // If the connection was previously using clat, but is not using it now, stop the clat |
| 2361 | // daemon. Normally, this happens automatically when the connection disconnects, but if |
| 2362 | // the disconnect is not reported, or if the connection's LinkProperties changed for |
| 2363 | // some other reason (e.g., handoff changes the IP addresses on the link), it would |
| 2364 | // still be running. If it's not running, then stopping it is a no-op. |
| 2365 | if (Nat464Xlat.isRunningClat(curLp) && !Nat464Xlat.isRunningClat(newLp)) { |
| 2366 | mClat.stopClat(); |
| 2367 | } |
| 2368 | // If the link requires clat to be running, then start the daemon now. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2369 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
| 2370 | mClat.startClat(tracker); |
| 2371 | } else { |
| 2372 | mClat.stopClat(); |
| 2373 | } |
| 2374 | } |
| 2375 | |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2376 | // TODO: Temporary notifying upstread change to Tethering. |
| 2377 | // @see bug/4455071 |
| 2378 | /** Notify TetheringService if interface name has been changed. */ |
| 2379 | if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(), |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 2380 | PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) { |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2381 | if (isTetheringSupported()) { |
| 2382 | mTethering.handleTetherIfaceChange(); |
| 2383 | } |
| 2384 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2385 | } |
| 2386 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2387 | /** |
| 2388 | * Add and remove routes using the old properties (null if not previously connected), |
| 2389 | * new properties (null if becoming disconnected). May even be double null, which |
| 2390 | * is a noop. |
| 2391 | * Uses isLinkDefault to determine if default routes should be set or conversely if |
| 2392 | * host routes should be set to the dns servers |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2393 | * returns a boolean indicating the routes changed |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2394 | */ |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2395 | private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp, |
| 2396 | boolean isLinkDefault) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2397 | Collection<RouteInfo> routesToAdd = null; |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2398 | CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>(); |
| 2399 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2400 | if (curLp != null) { |
| 2401 | // check for the delta between the current set and the new |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2402 | routeDiff = curLp.compareRoutes(newLp); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2403 | dnsDiff = curLp.compareDnses(newLp); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2404 | } else if (newLp != null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 2405 | routeDiff.added = newLp.getAllRoutes(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2406 | dnsDiff.added = newLp.getDnses(); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2407 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2408 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2409 | boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0); |
| 2410 | |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2411 | for (RouteInfo r : routeDiff.removed) { |
| 2412 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 2413 | removeRoute(curLp, r, TO_DEFAULT_TABLE); |
| 2414 | } |
| 2415 | if (isLinkDefault == false) { |
| 2416 | // remove from a secondary route table |
| 2417 | removeRoute(curLp, r, TO_SECONDARY_TABLE); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2418 | } |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2419 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2420 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2421 | if (!isLinkDefault) { |
| 2422 | // handle DNS routes |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2423 | if (routesChanged) { |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2424 | // routes changed - remove all old dns entries and add new |
| 2425 | if (curLp != null) { |
| 2426 | for (InetAddress oldDns : curLp.getDnses()) { |
| 2427 | removeRouteToAddress(curLp, oldDns); |
| 2428 | } |
| 2429 | } |
| 2430 | if (newLp != null) { |
| 2431 | for (InetAddress newDns : newLp.getDnses()) { |
| 2432 | addRouteToAddress(newLp, newDns); |
| 2433 | } |
Robert Greenwalt | edcb4f9 | 2011-03-22 18:47:42 -0700 | [diff] [blame] | 2434 | } |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2435 | } else { |
| 2436 | // no change in routes, check for change in dns themselves |
| 2437 | for (InetAddress oldDns : dnsDiff.removed) { |
| 2438 | removeRouteToAddress(curLp, oldDns); |
| 2439 | } |
| 2440 | for (InetAddress newDns : dnsDiff.added) { |
| 2441 | addRouteToAddress(newLp, newDns); |
| 2442 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2443 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2444 | } |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2445 | |
| 2446 | for (RouteInfo r : routeDiff.added) { |
| 2447 | if (isLinkDefault || ! r.isDefaultRoute()) { |
| 2448 | addRoute(newLp, r, TO_DEFAULT_TABLE); |
| 2449 | } else { |
| 2450 | // add to a secondary route table |
| 2451 | addRoute(newLp, r, TO_SECONDARY_TABLE); |
| 2452 | |
| 2453 | // many radios add a default route even when we don't want one. |
| 2454 | // remove the default route unless somebody else has asked for it |
| 2455 | String ifaceName = newLp.getInterfaceName(); |
| 2456 | if (TextUtils.isEmpty(ifaceName) == false && mAddedRoutes.contains(r) == false) { |
| 2457 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 2458 | try { |
| 2459 | mNetd.removeRoute(ifaceName, r); |
| 2460 | } catch (Exception e) { |
| 2461 | // never crash - catch them all |
| 2462 | if (DBG) loge("Exception trying to remove a route: " + e); |
| 2463 | } |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2468 | return routesChanged; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2472 | /** |
| 2473 | * Reads the network specific TCP buffer sizes from SystemProperties |
| 2474 | * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system |
| 2475 | * wide use |
| 2476 | */ |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2477 | private void updateNetworkSettings(NetworkStateTracker nt) { |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2478 | String key = nt.getTcpBufferSizesPropName(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2479 | String bufferSizes = key == null ? null : SystemProperties.get(key); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2480 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2481 | if (TextUtils.isEmpty(bufferSizes)) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2482 | if (VDBG) log(key + " not found in system properties. Using defaults"); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2483 | |
| 2484 | // Setting to default values so we won't be stuck to previous values |
| 2485 | key = "net.tcp.buffersize.default"; |
| 2486 | bufferSizes = SystemProperties.get(key); |
| 2487 | } |
| 2488 | |
| 2489 | // Set values in kernel |
| 2490 | if (bufferSizes.length() != 0) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2491 | if (VDBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2492 | log("Setting TCP values: [" + bufferSizes |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2493 | + "] which comes from [" + key + "]"); |
| 2494 | } |
| 2495 | setBufferSize(bufferSizes); |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max] |
| 2501 | * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem |
| 2502 | * |
| 2503 | * @param bufferSizes in the format of "readMin, readInitial, readMax, |
| 2504 | * writeMin, writeInitial, writeMax" |
| 2505 | */ |
| 2506 | private void setBufferSize(String bufferSizes) { |
| 2507 | try { |
| 2508 | String[] values = bufferSizes.split(","); |
| 2509 | |
| 2510 | if (values.length == 6) { |
| 2511 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 2512 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 2513 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 2514 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 2515 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 2516 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 2517 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2518 | } else { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2519 | loge("Invalid buffersize string: " + bufferSizes); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2520 | } |
| 2521 | } catch (IOException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2522 | loge("Can't set tcp buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2526 | /** |
| 2527 | * Adjust the per-process dns entries (net.dns<x>.<pid>) based |
| 2528 | * on the highest priority active net which this process requested. |
| 2529 | * If there aren't any, clear it out |
| 2530 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2531 | private void reassessPidDns(int pid, boolean doBump) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2532 | { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2533 | if (VDBG) log("reassessPidDns for pid " + pid); |
| 2534 | Integer myPid = new Integer(pid); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2535 | for(int i : mPriorityList) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2536 | if (mNetConfigs[i].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2537 | continue; |
| 2538 | } |
| 2539 | NetworkStateTracker nt = mNetTrackers[i]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2540 | if (nt.getNetworkInfo().isConnected() && |
| 2541 | !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2542 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 2543 | if (p == null) continue; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2544 | if (mNetRequestersPids[i].contains(myPid)) { |
| 2545 | try { |
| 2546 | mNetd.setDnsInterfaceForPid(p.getInterfaceName(), pid); |
| 2547 | } catch (Exception e) { |
| 2548 | Slog.e(TAG, "exception reasseses pid dns: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2549 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2550 | return; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2551 | } |
| 2552 | } |
| 2553 | } |
| 2554 | // nothing found - delete |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2555 | try { |
| 2556 | mNetd.clearDnsInterfaceForPid(pid); |
| 2557 | } catch (Exception e) { |
| 2558 | Slog.e(TAG, "exception clear interface from pid: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2559 | } |
| 2560 | } |
| 2561 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2562 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 2563 | /* |
| 2564 | * Tell the VMs to toss their DNS caches |
| 2565 | */ |
| 2566 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 2567 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 2568 | /* |
| 2569 | * Connectivity events can happen before boot has completed ... |
| 2570 | */ |
| 2571 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2572 | final long ident = Binder.clearCallingIdentity(); |
| 2573 | try { |
| 2574 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2575 | } finally { |
| 2576 | Binder.restoreCallingIdentity(ident); |
| 2577 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2580 | // Caller must grab mDnsLock. |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2581 | private void updateDnsLocked(String network, String iface, |
Lorenzo Colitti | c1358b2 | 2011-09-28 22:31:45 -0700 | [diff] [blame] | 2582 | Collection<InetAddress> dnses, String domains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2583 | int last = 0; |
| 2584 | if (dnses.size() == 0 && mDefaultDns != null) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2585 | dnses = new ArrayList(); |
| 2586 | dnses.add(mDefaultDns); |
| 2587 | if (DBG) { |
| 2588 | loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2589 | } |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2592 | try { |
| 2593 | mNetd.setDnsServersForInterface(iface, NetworkUtils.makeStrings(dnses), domains); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 2594 | for (InetAddress dns : dnses) { |
| 2595 | ++last; |
| 2596 | String key = "net.dns" + last; |
| 2597 | String value = dns.getHostAddress(); |
| 2598 | SystemProperties.set(key, value); |
| 2599 | } |
| 2600 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 2601 | String key = "net.dns" + i; |
| 2602 | SystemProperties.set(key, ""); |
| 2603 | } |
| 2604 | mNumDnsEntries = last; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2605 | } catch (Exception e) { |
| 2606 | if (DBG) loge("exception setting default dns interface: " + e); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2607 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2610 | private void handleDnsConfigurationChange(int netType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2611 | // add default net's dns entries |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2612 | NetworkStateTracker nt = mNetTrackers[netType]; |
| 2613 | if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2614 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2615 | if (p == null) return; |
| 2616 | Collection<InetAddress> dnses = p.getDnses(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2617 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2618 | String network = nt.getNetworkInfo().getTypeName(); |
| 2619 | synchronized (mDnsLock) { |
| 2620 | if (!mDnsOverridden) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2621 | updateDnsLocked(network, p.getInterfaceName(), dnses, p.getDomains()); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 2622 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2623 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2624 | } else { |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2625 | try { |
Robert Greenwalt | c59c6da | 2011-07-27 10:00:36 -0700 | [diff] [blame] | 2626 | mNetd.setDnsServersForInterface(p.getInterfaceName(), |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 2627 | NetworkUtils.makeStrings(dnses), p.getDomains()); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2628 | } catch (Exception e) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 2629 | if (DBG) loge("exception setting dns servers: " + e); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2630 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2631 | // set per-pid dns for attached secondary nets |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2632 | List<Integer> pids = mNetRequestersPids[netType]; |
| 2633 | for (Integer pid : pids) { |
| 2634 | try { |
| 2635 | mNetd.setDnsInterfaceForPid(p.getInterfaceName(), pid); |
| 2636 | } catch (Exception e) { |
| 2637 | Slog.e(TAG, "exception setting interface for pid: " + e); |
| 2638 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2639 | } |
| 2640 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2641 | flushVmDnsCache(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2642 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2643 | } |
| 2644 | |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2645 | private int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2646 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 2647 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2648 | if(restoreDefaultNetworkDelayStr != null && |
| 2649 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2650 | try { |
| 2651 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 2652 | } catch (NumberFormatException e) { |
| 2653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2655 | // if the system property isn't set, use the value for the apn type |
| 2656 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2657 | |
| 2658 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2659 | (mNetConfigs[networkType] != null)) { |
| 2660 | ret = mNetConfigs[networkType].restoreTime; |
| 2661 | } |
| 2662 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2666 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2667 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2668 | if (mContext.checkCallingOrSelfPermission( |
| 2669 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2670 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2671 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 2672 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 2673 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2676 | |
| 2677 | // TODO: add locking to get atomic snapshot |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2678 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2679 | for (int i = 0; i < mNetTrackers.length; i++) { |
| 2680 | final NetworkStateTracker nst = mNetTrackers[i]; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2681 | if (nst != null) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2682 | pw.println("NetworkStateTracker for " + getNetworkTypeName(i) + ":"); |
| 2683 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2684 | if (nst.getNetworkInfo().isConnected()) { |
| 2685 | pw.println("Active network: " + nst.getNetworkInfo(). |
| 2686 | getTypeName()); |
| 2687 | } |
| 2688 | pw.println(nst.getNetworkInfo()); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2689 | pw.println(nst.getLinkProperties()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2690 | pw.println(nst); |
| 2691 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2692 | pw.decreaseIndent(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2693 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2694 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2695 | |
| 2696 | pw.println("Network Requester Pids:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2697 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2698 | for (int net : mPriorityList) { |
| 2699 | String pidString = net + ": "; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2700 | for (Integer pid : mNetRequestersPids[net]) { |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2701 | pidString = pidString + pid.toString() + ", "; |
| 2702 | } |
| 2703 | pw.println(pidString); |
| 2704 | } |
| 2705 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2706 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2707 | |
| 2708 | pw.println("FeatureUsers:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2709 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2710 | for (Object requester : mFeatureUsers) { |
| 2711 | pw.println(requester.toString()); |
| 2712 | } |
| 2713 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2714 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2715 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2716 | synchronized (this) { |
| 2717 | pw.println("NetworkTranstionWakeLock is currently " + |
| 2718 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 2719 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 2720 | } |
| 2721 | pw.println(); |
| 2722 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2723 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2724 | |
| 2725 | if (mInetLog != null) { |
| 2726 | pw.println(); |
| 2727 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2728 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2729 | for(int i = 0; i < mInetLog.size(); i++) { |
| 2730 | pw.println(mInetLog.get(i)); |
| 2731 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2732 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2733 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2734 | } |
| 2735 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2736 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2737 | private class NetworkStateTrackerHandler extends Handler { |
| 2738 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 2739 | super(looper); |
| 2740 | } |
| 2741 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2742 | @Override |
| 2743 | public void handleMessage(Message msg) { |
| 2744 | NetworkInfo info; |
| 2745 | switch (msg.what) { |
| 2746 | case NetworkStateTracker.EVENT_STATE_CHANGED: |
| 2747 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2748 | int type = info.getType(); |
| 2749 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2750 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2751 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
| 2752 | (state == NetworkInfo.State.DISCONNECTED)) { |
| 2753 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2754 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2755 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 2758 | EventLogTags.writeConnectivityStateChanged( |
| 2759 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2760 | |
| 2761 | if (info.getDetailedState() == |
| 2762 | NetworkInfo.DetailedState.FAILED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2763 | handleConnectionFailure(info); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2764 | } else if (info.getDetailedState() == |
| 2765 | DetailedState.CAPTIVE_PORTAL_CHECK) { |
| 2766 | handleCaptivePortalTrackerCheck(info); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2767 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2768 | handleDisconnect(info); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2769 | } else if (state == NetworkInfo.State.SUSPENDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2770 | // TODO: need to think this over. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2771 | // the logic here is, handle SUSPENDED the same as |
| 2772 | // DISCONNECTED. The only difference being we are |
| 2773 | // broadcasting an intent with NetworkInfo that's |
| 2774 | // suspended. This allows the applications an |
| 2775 | // opportunity to handle DISCONNECTED and SUSPENDED |
| 2776 | // differently, or not. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2777 | handleDisconnect(info); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2778 | } else if (state == NetworkInfo.State.CONNECTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2779 | handleConnect(info); |
| 2780 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2781 | if (mLockdownTracker != null) { |
| 2782 | mLockdownTracker.onNetworkInfoChanged(info); |
| 2783 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2785 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2786 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2787 | // TODO: Temporary allowing network configuration |
| 2788 | // change not resetting sockets. |
| 2789 | // @see bug/4455071 |
| 2790 | handleConnectivityChange(info.getType(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2791 | break; |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 2792 | case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: |
| 2793 | info = (NetworkInfo) msg.obj; |
| 2794 | type = info.getType(); |
| 2795 | updateNetworkSettings(mNetTrackers[type]); |
| 2796 | break; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2797 | } |
| 2798 | } |
| 2799 | } |
| 2800 | |
| 2801 | private class InternalHandler extends Handler { |
| 2802 | public InternalHandler(Looper looper) { |
| 2803 | super(looper); |
| 2804 | } |
| 2805 | |
| 2806 | @Override |
| 2807 | public void handleMessage(Message msg) { |
| 2808 | NetworkInfo info; |
| 2809 | switch (msg.what) { |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 2810 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2811 | String causedBy = null; |
| 2812 | synchronized (ConnectivityService.this) { |
| 2813 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 2814 | mNetTransitionWakeLock.isHeld()) { |
| 2815 | mNetTransitionWakeLock.release(); |
| 2816 | causedBy = mNetTransitionWakeLockCausedBy; |
| 2817 | } |
| 2818 | } |
| 2819 | if (causedBy != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2820 | log("NetTransition Wakelock for " + causedBy + " released by timeout"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2821 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 2822 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2823 | case EVENT_RESTORE_DEFAULT_NETWORK: |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 2824 | FeatureUser u = (FeatureUser)msg.obj; |
| 2825 | u.expire(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2826 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2827 | case EVENT_INET_CONDITION_CHANGE: |
| 2828 | { |
| 2829 | int netType = msg.arg1; |
| 2830 | int condition = msg.arg2; |
| 2831 | handleInetConditionChange(netType, condition); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2832 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2833 | } |
| 2834 | case EVENT_INET_CONDITION_HOLD_END: |
| 2835 | { |
| 2836 | int netType = msg.arg1; |
| 2837 | int sequence = msg.arg2; |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 2838 | handleInetConditionHoldEnd(netType, sequence); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2839 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2840 | } |
| 2841 | case EVENT_SET_NETWORK_PREFERENCE: |
| 2842 | { |
| 2843 | int preference = msg.arg1; |
| 2844 | handleSetNetworkPreference(preference); |
| 2845 | break; |
| 2846 | } |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2847 | case EVENT_SET_MOBILE_DATA: |
| 2848 | { |
| 2849 | boolean enabled = (msg.arg1 == ENABLED); |
| 2850 | handleSetMobileData(enabled); |
| 2851 | break; |
| 2852 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2853 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: |
| 2854 | { |
| 2855 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2856 | break; |
| 2857 | } |
| 2858 | case EVENT_SET_DEPENDENCY_MET: |
| 2859 | { |
| 2860 | boolean met = (msg.arg1 == ENABLED); |
| 2861 | handleSetDependencyMet(msg.arg2, met); |
| 2862 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2863 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2864 | case EVENT_RESTORE_DNS: |
| 2865 | { |
| 2866 | if (mActiveDefaultNetwork != -1) { |
| 2867 | handleDnsConfigurationChange(mActiveDefaultNetwork); |
| 2868 | } |
| 2869 | break; |
| 2870 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2871 | case EVENT_SEND_STICKY_BROADCAST_INTENT: |
| 2872 | { |
| 2873 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2874 | sendStickyBroadcast(intent); |
| 2875 | break; |
| 2876 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2877 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 2878 | final int networkType = msg.arg1; |
| 2879 | final boolean enabled = msg.arg2 == ENABLED; |
| 2880 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2881 | break; |
| 2882 | } |
| 2883 | case EVENT_VPN_STATE_CHANGED: { |
| 2884 | if (mLockdownTracker != null) { |
| 2885 | mLockdownTracker.onVpnStateChanged((NetworkInfo) msg.obj); |
| 2886 | } |
| 2887 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2888 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2889 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 2890 | int tag = mEnableFailFastMobileDataTag.get(); |
| 2891 | if (msg.arg1 == tag) { |
| 2892 | MobileDataStateTracker mobileDst = |
| 2893 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 2894 | if (mobileDst != null) { |
| 2895 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 2896 | } |
| 2897 | } else { |
| 2898 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 2899 | + " != tag:" + tag); |
| 2900 | } |
| 2901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | } |
| 2903 | } |
| 2904 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2905 | |
| 2906 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2907 | public int tether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2908 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2909 | |
| 2910 | if (isTetheringSupported()) { |
| 2911 | return mTethering.tether(iface); |
| 2912 | } else { |
| 2913 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2914 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2915 | } |
| 2916 | |
| 2917 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2918 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2919 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2920 | |
| 2921 | if (isTetheringSupported()) { |
| 2922 | return mTethering.untether(iface); |
| 2923 | } else { |
| 2924 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2925 | } |
| 2926 | } |
| 2927 | |
| 2928 | // javadoc from interface |
| 2929 | public int getLastTetherError(String iface) { |
| 2930 | enforceTetherAccessPermission(); |
| 2931 | |
| 2932 | if (isTetheringSupported()) { |
| 2933 | return mTethering.getLastTetherError(iface); |
| 2934 | } else { |
| 2935 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2936 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | // TODO - proper iface API for selection by property, inspection, etc |
| 2940 | public String[] getTetherableUsbRegexs() { |
| 2941 | enforceTetherAccessPermission(); |
| 2942 | if (isTetheringSupported()) { |
| 2943 | return mTethering.getTetherableUsbRegexs(); |
| 2944 | } else { |
| 2945 | return new String[0]; |
| 2946 | } |
| 2947 | } |
| 2948 | |
| 2949 | public String[] getTetherableWifiRegexs() { |
| 2950 | enforceTetherAccessPermission(); |
| 2951 | if (isTetheringSupported()) { |
| 2952 | return mTethering.getTetherableWifiRegexs(); |
| 2953 | } else { |
| 2954 | return new String[0]; |
| 2955 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2956 | } |
| 2957 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2958 | public String[] getTetherableBluetoothRegexs() { |
| 2959 | enforceTetherAccessPermission(); |
| 2960 | if (isTetheringSupported()) { |
| 2961 | return mTethering.getTetherableBluetoothRegexs(); |
| 2962 | } else { |
| 2963 | return new String[0]; |
| 2964 | } |
| 2965 | } |
| 2966 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2967 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2968 | enforceTetherChangePermission(); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2969 | if (isTetheringSupported()) { |
| 2970 | return mTethering.setUsbTethering(enable); |
| 2971 | } else { |
| 2972 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2973 | } |
| 2974 | } |
| 2975 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2976 | // TODO - move iface listing, queries, etc to new module |
| 2977 | // javadoc from interface |
| 2978 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2979 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2980 | return mTethering.getTetherableIfaces(); |
| 2981 | } |
| 2982 | |
| 2983 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2984 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2985 | return mTethering.getTetheredIfaces(); |
| 2986 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2987 | |
Jeff Sharkey | cdd02c5d | 2011-09-16 01:52:49 -0700 | [diff] [blame] | 2988 | @Override |
| 2989 | public String[] getTetheredIfacePairs() { |
| 2990 | enforceTetherAccessPermission(); |
| 2991 | return mTethering.getTetheredIfacePairs(); |
| 2992 | } |
| 2993 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2994 | public String[] getTetheringErroredIfaces() { |
| 2995 | enforceTetherAccessPermission(); |
| 2996 | return mTethering.getErroredIfaces(); |
| 2997 | } |
| 2998 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2999 | // if ro.tether.denied = true we default to no tethering |
| 3000 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3001 | // had previously been turned off. |
| 3002 | public boolean isTetheringSupported() { |
| 3003 | enforceTetherAccessPermission(); |
| 3004 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 3005 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
| 3006 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 3007 | return tetherEnabledInSettings && mTetheringConfigValid; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3008 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3009 | |
| 3010 | // An API NetworkStateTrackers can call when they lose their network. |
| 3011 | // This will automatically be cleared after X seconds or a network becomes CONNECTED, |
| 3012 | // whichever happens first. The timer is started by the first caller and not |
| 3013 | // restarted by subsequent callers. |
| 3014 | public void requestNetworkTransitionWakelock(String forWhom) { |
| 3015 | enforceConnectivityInternalPermission(); |
| 3016 | synchronized (this) { |
| 3017 | if (mNetTransitionWakeLock.isHeld()) return; |
| 3018 | mNetTransitionWakeLockSerialNumber++; |
| 3019 | mNetTransitionWakeLock.acquire(); |
| 3020 | mNetTransitionWakeLockCausedBy = forWhom; |
| 3021 | } |
| 3022 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 3023 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3024 | mNetTransitionWakeLockSerialNumber, 0), |
| 3025 | mNetTransitionWakeLockTimeout); |
| 3026 | return; |
| 3027 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3028 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3029 | // 100 percent is full good, 0 is full bad. |
| 3030 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3031 | if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")"); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3032 | mContext.enforceCallingOrSelfPermission( |
| 3033 | android.Manifest.permission.STATUS_BAR, |
| 3034 | "ConnectivityService"); |
| 3035 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3036 | if (DBG) { |
| 3037 | int pid = getCallingPid(); |
| 3038 | int uid = getCallingUid(); |
| 3039 | String s = pid + "(" + uid + ") reports inet is " + |
| 3040 | (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " + |
| 3041 | "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime(); |
| 3042 | mInetLog.add(s); |
| 3043 | while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) { |
| 3044 | mInetLog.remove(0); |
| 3045 | } |
| 3046 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3047 | mHandler.sendMessage(mHandler.obtainMessage( |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3048 | EVENT_INET_CONDITION_CHANGE, networkType, percentage)); |
| 3049 | } |
| 3050 | |
| 3051 | private void handleInetConditionChange(int netType, int condition) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3052 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3053 | if (DBG) log("handleInetConditionChange: no active default network - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3054 | return; |
| 3055 | } |
| 3056 | if (mActiveDefaultNetwork != netType) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3057 | if (DBG) log("handleInetConditionChange: net=" + netType + |
| 3058 | " != default=" + mActiveDefaultNetwork + " - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3059 | return; |
| 3060 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3061 | if (VDBG) { |
| 3062 | log("handleInetConditionChange: net=" + |
| 3063 | netType + ", condition=" + condition + |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3064 | ",mActiveDefaultNetwork=" + mActiveDefaultNetwork); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3065 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3066 | mDefaultInetCondition = condition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3067 | int delay; |
| 3068 | if (mInetConditionChangeInFlight == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3069 | if (VDBG) log("handleInetConditionChange: starting a change hold"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3070 | // setup a new hold to debounce this |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3071 | if (mDefaultInetCondition > 50) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3072 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3073 | Settings.Global.INET_CONDITION_DEBOUNCE_UP_DELAY, 500); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3074 | } else { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3075 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3076 | Settings.Global.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3077 | } |
| 3078 | mInetConditionChangeInFlight = true; |
| 3079 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END, |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3080 | mActiveDefaultNetwork, mDefaultConnectionSequence), delay); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3081 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3082 | // we've set the new condition, when this hold ends that will get picked up |
| 3083 | if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3087 | private void handleInetConditionHoldEnd(int netType, int sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3088 | if (DBG) { |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3089 | log("handleInetConditionHoldEnd: net=" + netType + |
| 3090 | ", condition=" + mDefaultInetCondition + |
| 3091 | ", published condition=" + mDefaultInetConditionPublished); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3092 | } |
| 3093 | mInetConditionChangeInFlight = false; |
| 3094 | |
| 3095 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3096 | if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3097 | return; |
| 3098 | } |
| 3099 | if (mDefaultConnectionSequence != sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3100 | if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3101 | return; |
| 3102 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3103 | // TODO: Figure out why this optimization sometimes causes a |
| 3104 | // change in mDefaultInetCondition to be missed and the |
| 3105 | // UI to not be updated. |
| 3106 | //if (mDefaultInetConditionPublished == mDefaultInetCondition) { |
| 3107 | // if (DBG) log("no change in condition - aborting"); |
| 3108 | // return; |
| 3109 | //} |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3110 | NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
| 3111 | if (networkInfo.isConnected() == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3112 | if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3113 | return; |
| 3114 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3115 | mDefaultInetConditionPublished = mDefaultInetCondition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3116 | sendInetConditionBroadcast(networkInfo); |
| 3117 | return; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3118 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3119 | |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3120 | public ProxyProperties getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3121 | // this information is already available as a world read/writable jvm property |
| 3122 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3123 | // of proxy info to all the JVMs. |
| 3124 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3125 | synchronized (mProxyLock) { |
| 3126 | if (mGlobalProxy != null) return mGlobalProxy; |
| 3127 | return (mDefaultProxyDisabled ? null : mDefaultProxy); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3128 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | public void setGlobalProxy(ProxyProperties proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3132 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3133 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3134 | if (proxyProperties == mGlobalProxy) return; |
| 3135 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 3136 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 3137 | |
| 3138 | String host = ""; |
| 3139 | int port = 0; |
| 3140 | String exclList = ""; |
| 3141 | if (proxyProperties != null && !TextUtils.isEmpty(proxyProperties.getHost())) { |
| 3142 | mGlobalProxy = new ProxyProperties(proxyProperties); |
| 3143 | host = mGlobalProxy.getHost(); |
| 3144 | port = mGlobalProxy.getPort(); |
| 3145 | exclList = mGlobalProxy.getExclusionList(); |
| 3146 | } else { |
| 3147 | mGlobalProxy = null; |
| 3148 | } |
| 3149 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3150 | final long token = Binder.clearCallingIdentity(); |
| 3151 | try { |
| 3152 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 3153 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 3154 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 3155 | exclList); |
| 3156 | } finally { |
| 3157 | Binder.restoreCallingIdentity(token); |
| 3158 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
| 3161 | if (mGlobalProxy == null) { |
| 3162 | proxyProperties = mDefaultProxy; |
| 3163 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3164 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3165 | } |
| 3166 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3167 | private void loadGlobalProxy() { |
| 3168 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3169 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 3170 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 3171 | String exclList = Settings.Global.getString(res, |
| 3172 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3173 | if (!TextUtils.isEmpty(host)) { |
| 3174 | ProxyProperties proxyProperties = new ProxyProperties(host, port, exclList); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3175 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3176 | mGlobalProxy = proxyProperties; |
| 3177 | } |
| 3178 | } |
| 3179 | } |
| 3180 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3181 | public ProxyProperties getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3182 | // this information is already available as a world read/writable jvm property |
| 3183 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3184 | // of proxy info to all the JVMs. |
| 3185 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3186 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3187 | return mGlobalProxy; |
| 3188 | } |
| 3189 | } |
| 3190 | |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3191 | private void handleApplyDefaultProxy(ProxyProperties proxy) { |
| 3192 | if (proxy != null && TextUtils.isEmpty(proxy.getHost())) { |
| 3193 | proxy = null; |
| 3194 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3195 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3196 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3197 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3198 | mDefaultProxy = proxy; |
| 3199 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3200 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3201 | if (!mDefaultProxyDisabled) { |
| 3202 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3203 | } |
| 3204 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3208 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 3209 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3210 | if (!TextUtils.isEmpty(proxy)) { |
| 3211 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3212 | if (data.length == 0) { |
| 3213 | return; |
| 3214 | } |
| 3215 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3216 | String proxyHost = data[0]; |
| 3217 | int proxyPort = 8080; |
| 3218 | if (data.length > 1) { |
| 3219 | try { |
| 3220 | proxyPort = Integer.parseInt(data[1]); |
| 3221 | } catch (NumberFormatException e) { |
| 3222 | return; |
| 3223 | } |
| 3224 | } |
| 3225 | ProxyProperties p = new ProxyProperties(data[0], proxyPort, ""); |
| 3226 | setGlobalProxy(p); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3227 | } else { |
| 3228 | setGlobalProxy(null); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | private void sendProxyBroadcast(ProxyProperties proxy) { |
Robert Greenwalt | 55985be | 2010-12-23 15:51:10 -0800 | [diff] [blame] | 3233 | if (proxy == null) proxy = new ProxyProperties("", 0, ""); |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3234 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3235 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3236 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3237 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3238 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3239 | final long ident = Binder.clearCallingIdentity(); |
| 3240 | try { |
| 3241 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3242 | } finally { |
| 3243 | Binder.restoreCallingIdentity(ident); |
| 3244 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | private static class SettingsObserver extends ContentObserver { |
| 3248 | private int mWhat; |
| 3249 | private Handler mHandler; |
| 3250 | SettingsObserver(Handler handler, int what) { |
| 3251 | super(handler); |
| 3252 | mHandler = handler; |
| 3253 | mWhat = what; |
| 3254 | } |
| 3255 | |
| 3256 | void observe(Context context) { |
| 3257 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3258 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 3259 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | @Override |
| 3263 | public void onChange(boolean selfChange) { |
| 3264 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 3265 | } |
| 3266 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3267 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3268 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3269 | Slog.d(TAG, s); |
| 3270 | } |
| 3271 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3272 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3273 | Slog.e(TAG, s); |
| 3274 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3275 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3276 | int convertFeatureToNetworkType(int networkType, String feature) { |
| 3277 | int usedNetworkType = networkType; |
| 3278 | |
| 3279 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 3280 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 3281 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 3282 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 3283 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 3284 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) || |
| 3285 | TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 3286 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 3287 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 3288 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 3289 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) { |
| 3290 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA; |
| 3291 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) { |
| 3292 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS; |
| 3293 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) { |
| 3294 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS; |
| 3295 | } else { |
| 3296 | Slog.e(TAG, "Can't match any mobile netTracker!"); |
| 3297 | } |
| 3298 | } else if (networkType == ConnectivityManager.TYPE_WIFI) { |
| 3299 | if (TextUtils.equals(feature, "p2p")) { |
| 3300 | usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P; |
| 3301 | } else { |
| 3302 | Slog.e(TAG, "Can't match any wifi netTracker!"); |
| 3303 | } |
| 3304 | } else { |
| 3305 | Slog.e(TAG, "Unexpected network type"); |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3306 | } |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3307 | return usedNetworkType; |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3308 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3309 | |
| 3310 | private static <T> T checkNotNull(T value, String message) { |
| 3311 | if (value == null) { |
| 3312 | throw new NullPointerException(message); |
| 3313 | } |
| 3314 | return value; |
| 3315 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3316 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3317 | /** |
| 3318 | * Protect a socket from VPN routing rules. This method is used by |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3319 | * VpnBuilder and not available in ConnectivityManager. Permissions |
| 3320 | * are checked in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3321 | * @hide |
| 3322 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3323 | @Override |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3324 | public boolean protectVpn(ParcelFileDescriptor socket) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3325 | throwIfLockdownEnabled(); |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3326 | try { |
| 3327 | int type = mActiveDefaultNetwork; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3328 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3329 | if (ConnectivityManager.isNetworkTypeValid(type) && mNetTrackers[type] != null) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3330 | synchronized(mVpns) { |
| 3331 | mVpns.get(user).protect(socket, |
| 3332 | mNetTrackers[type].getLinkProperties().getInterfaceName()); |
| 3333 | } |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3334 | return true; |
| 3335 | } |
| 3336 | } catch (Exception e) { |
| 3337 | // ignore |
| 3338 | } finally { |
| 3339 | try { |
| 3340 | socket.close(); |
| 3341 | } catch (Exception e) { |
| 3342 | // ignore |
| 3343 | } |
| 3344 | } |
| 3345 | return false; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3346 | } |
| 3347 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3348 | /** |
| 3349 | * Prepare for a VPN application. This method is used by VpnDialogs |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3350 | * and not available in ConnectivityManager. Permissions are checked |
| 3351 | * in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3352 | * @hide |
| 3353 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3354 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 3355 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3356 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3357 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3358 | synchronized(mVpns) { |
| 3359 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 3360 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3361 | } |
| 3362 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 3363 | @Override |
| 3364 | public void markSocketAsUser(ParcelFileDescriptor socket, int uid) { |
| 3365 | enforceMarkNetworkSocketPermission(); |
| 3366 | final long token = Binder.clearCallingIdentity(); |
| 3367 | try { |
| 3368 | int mark = mNetd.getMarkForUid(uid); |
| 3369 | // Clear the mark on the socket if no mark is needed to prevent socket reuse issues |
| 3370 | if (mark == -1) { |
| 3371 | mark = 0; |
| 3372 | } |
| 3373 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 3374 | } catch (RemoteException e) { |
| 3375 | } finally { |
| 3376 | Binder.restoreCallingIdentity(token); |
| 3377 | } |
| 3378 | } |
| 3379 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3380 | /** |
| 3381 | * Configure a TUN interface and return its file descriptor. Parameters |
| 3382 | * are encoded and opaque to this class. This method is used by VpnBuilder |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3383 | * and not available in ConnectivityManager. Permissions are checked in |
| 3384 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3385 | * @hide |
| 3386 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3387 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3388 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3389 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3390 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3391 | synchronized(mVpns) { |
| 3392 | return mVpns.get(user).establish(config); |
| 3393 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3394 | } |
| 3395 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3396 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3397 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 3398 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3399 | */ |
| 3400 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3401 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3402 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3403 | final LinkProperties egress = getActiveLinkProperties(); |
| 3404 | if (egress == null) { |
| 3405 | throw new IllegalStateException("Missing active network connection"); |
| 3406 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3407 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3408 | synchronized(mVpns) { |
| 3409 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 3410 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | /** |
| 3414 | * Return the information of the ongoing legacy VPN. This method is used |
| 3415 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 3416 | * are checked in Vpn class. |
| 3417 | * @hide |
| 3418 | */ |
| 3419 | @Override |
| 3420 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3421 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3422 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3423 | synchronized(mVpns) { |
| 3424 | return mVpns.get(user).getLegacyVpnInfo(); |
| 3425 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3428 | /** |
| 3429 | * Callback for VPN subsystem. Currently VPN is not adapted to the service |
| 3430 | * through NetworkStateTracker since it works differently. For example, it |
| 3431 | * needs to override DNS servers but never takes the default routes. It |
| 3432 | * relies on another data network, and it could keep existing connections |
| 3433 | * alive after reconnecting, switching between networks, or even resuming |
| 3434 | * from deep sleep. Calls from applications should be done synchronously |
| 3435 | * to avoid race conditions. As these are all hidden APIs, refactoring can |
| 3436 | * be done whenever a better abstraction is developed. |
| 3437 | */ |
| 3438 | public class VpnCallback { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3439 | private VpnCallback() { |
| 3440 | } |
| 3441 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3442 | public void onStateChanged(NetworkInfo info) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3443 | mHandler.obtainMessage(EVENT_VPN_STATE_CHANGED, info).sendToTarget(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3446 | public void override(String iface, List<String> dnsServers, List<String> searchDomains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3447 | if (dnsServers == null) { |
| 3448 | restore(); |
| 3449 | return; |
| 3450 | } |
| 3451 | |
| 3452 | // Convert DNS servers into addresses. |
| 3453 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 3454 | for (String address : dnsServers) { |
| 3455 | // Double check the addresses and remove invalid ones. |
| 3456 | try { |
| 3457 | addresses.add(InetAddress.parseNumericAddress(address)); |
| 3458 | } catch (Exception e) { |
| 3459 | // ignore |
| 3460 | } |
| 3461 | } |
| 3462 | if (addresses.isEmpty()) { |
| 3463 | restore(); |
| 3464 | return; |
| 3465 | } |
| 3466 | |
| 3467 | // Concatenate search domains into a string. |
| 3468 | StringBuilder buffer = new StringBuilder(); |
| 3469 | if (searchDomains != null) { |
| 3470 | for (String domain : searchDomains) { |
| 3471 | buffer.append(domain).append(' '); |
| 3472 | } |
| 3473 | } |
| 3474 | String domains = buffer.toString().trim(); |
| 3475 | |
| 3476 | // Apply DNS changes. |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3477 | synchronized (mDnsLock) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3478 | updateDnsLocked("VPN", iface, addresses, domains); |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3479 | mDnsOverridden = true; |
| 3480 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3481 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3482 | // Temporarily disable the default proxy (not global). |
| 3483 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3484 | mDefaultProxyDisabled = true; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3485 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3486 | sendProxyBroadcast(null); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | // TODO: support proxy per network. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3491 | } |
| 3492 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3493 | public void restore() { |
| 3494 | synchronized (mDnsLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3495 | if (mDnsOverridden) { |
| 3496 | mDnsOverridden = false; |
| 3497 | mHandler.sendEmptyMessage(EVENT_RESTORE_DNS); |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3498 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3499 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3500 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3501 | mDefaultProxyDisabled = false; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3502 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3503 | sendProxyBroadcast(mDefaultProxy); |
| 3504 | } |
| 3505 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3506 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3507 | |
| 3508 | public void protect(ParcelFileDescriptor socket) { |
| 3509 | try { |
| 3510 | final int mark = mNetd.getMarkForProtect(); |
| 3511 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 3512 | } catch (RemoteException e) { |
| 3513 | } |
| 3514 | } |
| 3515 | |
| 3516 | public void setRoutes(String interfaze, List<RouteInfo> routes) { |
| 3517 | for (RouteInfo route : routes) { |
| 3518 | try { |
| 3519 | mNetd.setMarkedForwardingRoute(interfaze, route); |
| 3520 | } catch (RemoteException e) { |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | public void setMarkedForwarding(String interfaze) { |
| 3526 | try { |
| 3527 | mNetd.setMarkedForwarding(interfaze); |
| 3528 | } catch (RemoteException e) { |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | public void clearMarkedForwarding(String interfaze) { |
| 3533 | try { |
| 3534 | mNetd.clearMarkedForwarding(interfaze); |
| 3535 | } catch (RemoteException e) { |
| 3536 | } |
| 3537 | } |
| 3538 | |
| 3539 | public void addUserForwarding(String interfaze, int uid) { |
| 3540 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 3541 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
| 3542 | addUidForwarding(interfaze, uidStart, uidEnd); |
| 3543 | } |
| 3544 | |
| 3545 | public void clearUserForwarding(String interfaze, int uid) { |
| 3546 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 3547 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
| 3548 | clearUidForwarding(interfaze, uidStart, uidEnd); |
| 3549 | } |
| 3550 | |
| 3551 | public void addUidForwarding(String interfaze, int uidStart, int uidEnd) { |
| 3552 | try { |
| 3553 | mNetd.setUidRangeRoute(interfaze,uidStart, uidEnd); |
| 3554 | mNetd.setDnsInterfaceForUidRange(interfaze, uidStart, uidEnd); |
| 3555 | } catch (RemoteException e) { |
| 3556 | } |
| 3557 | |
| 3558 | } |
| 3559 | |
| 3560 | public void clearUidForwarding(String interfaze, int uidStart, int uidEnd) { |
| 3561 | try { |
| 3562 | mNetd.clearUidRangeRoute(interfaze, uidStart, uidEnd); |
| 3563 | mNetd.clearDnsInterfaceForUidRange(uidStart, uidEnd); |
| 3564 | } catch (RemoteException e) { |
| 3565 | } |
| 3566 | |
| 3567 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3568 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3569 | |
| 3570 | @Override |
| 3571 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 3572 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 3573 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 3574 | return false; |
| 3575 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3576 | |
| 3577 | // Tear down existing lockdown if profile was removed |
| 3578 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 3579 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 3580 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3581 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 3582 | return false; |
| 3583 | } |
| 3584 | |
| 3585 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 3586 | final VpnProfile profile = VpnProfile.decode( |
| 3587 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3588 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3589 | synchronized(mVpns) { |
| 3590 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 3591 | profile)); |
| 3592 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3593 | } else { |
| 3594 | setLockdownTracker(null); |
| 3595 | } |
| 3596 | |
| 3597 | return true; |
| 3598 | } |
| 3599 | |
| 3600 | /** |
| 3601 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 3602 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 3603 | */ |
| 3604 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 3605 | // Shutdown any existing tracker |
| 3606 | final LockdownVpnTracker existing = mLockdownTracker; |
| 3607 | mLockdownTracker = null; |
| 3608 | if (existing != null) { |
| 3609 | existing.shutdown(); |
| 3610 | } |
| 3611 | |
| 3612 | try { |
| 3613 | if (tracker != null) { |
| 3614 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 3615 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3616 | mLockdownTracker = tracker; |
| 3617 | mLockdownTracker.init(); |
| 3618 | } else { |
| 3619 | mNetd.setFirewallEnabled(false); |
| 3620 | } |
| 3621 | } catch (RemoteException e) { |
| 3622 | // ignored; NMS lives inside system_server |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | private void throwIfLockdownEnabled() { |
| 3627 | if (mLockdownEnabled) { |
| 3628 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 3629 | } |
| 3630 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3631 | |
| 3632 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 3633 | enforceConnectivityInternalPermission(); |
| 3634 | |
| 3635 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 3636 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 3637 | } |
| 3638 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 3639 | |
| 3640 | public int findConnectionTypeForIface(String iface) { |
| 3641 | enforceConnectivityInternalPermission(); |
| 3642 | |
| 3643 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
| 3644 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3645 | if (tracker != null) { |
| 3646 | LinkProperties lp = tracker.getLinkProperties(); |
| 3647 | if (lp != null && iface.equals(lp.getInterfaceName())) { |
| 3648 | return tracker.getNetworkInfo().getType(); |
| 3649 | } |
| 3650 | } |
| 3651 | } |
| 3652 | return ConnectivityManager.TYPE_NONE; |
| 3653 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3654 | |
| 3655 | /** |
| 3656 | * Have mobile data fail fast if enabled. |
| 3657 | * |
| 3658 | * @param enabled DctConstants.ENABLED/DISABLED |
| 3659 | */ |
| 3660 | private void setEnableFailFastMobileData(int enabled) { |
| 3661 | int tag; |
| 3662 | |
| 3663 | if (enabled == DctConstants.ENABLED) { |
| 3664 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 3665 | } else { |
| 3666 | tag = mEnableFailFastMobileDataTag.get(); |
| 3667 | } |
| 3668 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 3669 | enabled)); |
| 3670 | } |
| 3671 | |
| 3672 | @Override |
Wink Saville | c2fee24 | 2013-07-19 09:27:56 -0700 | [diff] [blame^] | 3673 | public int checkMobileProvisioning(final boolean sendNotification, int suggestedTimeOutMs, |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3674 | final ResultReceiver resultReceiver) { |
| 3675 | log("checkMobileProvisioning: E sendNotification=" + sendNotification |
| 3676 | + " suggestedTimeOutMs=" + suggestedTimeOutMs |
| 3677 | + " resultReceiver=" + resultReceiver); |
| 3678 | enforceChangePermission(); |
| 3679 | |
| 3680 | int timeOutMs = suggestedTimeOutMs; |
| 3681 | if (suggestedTimeOutMs > CheckMp.MAX_TIMEOUT_MS) { |
| 3682 | timeOutMs = CheckMp.MAX_TIMEOUT_MS; |
| 3683 | } |
| 3684 | |
Wink Saville | 68e6c64 | 2013-07-02 10:55:14 -0700 | [diff] [blame] | 3685 | // Check that mobile networks are supported |
| 3686 | if (!isNetworkSupported(ConnectivityManager.TYPE_MOBILE) |
| 3687 | || !isNetworkSupported(ConnectivityManager.TYPE_MOBILE_HIPRI)) { |
| 3688 | log("checkMobileProvisioning: X no mobile network"); |
| 3689 | if (resultReceiver != null) { |
| 3690 | resultReceiver.send(ConnectivityManager.CMP_RESULT_CODE_NO_CONNECTION, null); |
| 3691 | } |
| 3692 | return timeOutMs; |
| 3693 | } |
| 3694 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3695 | final long token = Binder.clearCallingIdentity(); |
| 3696 | try { |
| 3697 | CheckMp checkMp = new CheckMp(mContext, this); |
| 3698 | CheckMp.CallBack cb = new CheckMp.CallBack() { |
| 3699 | @Override |
| 3700 | void onComplete(Integer result) { |
| 3701 | log("CheckMp.onComplete: result=" + result); |
| 3702 | if (resultReceiver != null) { |
| 3703 | log("CheckMp.onComplete: send result"); |
| 3704 | resultReceiver.send(result, null); |
| 3705 | } |
Wink Saville | c2fee24 | 2013-07-19 09:27:56 -0700 | [diff] [blame^] | 3706 | if (!sendNotification) { |
| 3707 | log("CheckMp.onComplete: done, not sending notification"); |
| 3708 | return; |
| 3709 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3710 | NetworkInfo ni = |
| 3711 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI].getNetworkInfo(); |
| 3712 | switch(result) { |
| 3713 | case ConnectivityManager.CMP_RESULT_CODE_CONNECTABLE: |
| 3714 | case ConnectivityManager.CMP_RESULT_CODE_NO_CONNECTION: { |
| 3715 | log("CheckMp.onComplete: ignore, connected or no connection"); |
| 3716 | break; |
| 3717 | } |
| 3718 | case ConnectivityManager.CMP_RESULT_CODE_REDIRECTED: { |
| 3719 | log("CheckMp.onComplete: warm sim"); |
| 3720 | String url = getProvisioningUrl(); |
| 3721 | if (TextUtils.isEmpty(url)) { |
| 3722 | url = mContext.getResources() |
| 3723 | .getString(R.string.mobile_redirected_provisioning_url); |
| 3724 | } |
| 3725 | if (TextUtils.isEmpty(url) == false) { |
| 3726 | log("CheckMp.onComplete: warm sim (redirected), url=" + url); |
| 3727 | setNotificationVisible(true, ni, url); |
| 3728 | } else { |
| 3729 | log("CheckMp.onComplete: warm sim (redirected), no url"); |
| 3730 | } |
| 3731 | break; |
| 3732 | } |
| 3733 | case ConnectivityManager.CMP_RESULT_CODE_NO_DNS: |
| 3734 | case ConnectivityManager.CMP_RESULT_CODE_NO_TCP_CONNECTION: { |
| 3735 | String url = getProvisioningUrl(); |
| 3736 | if (TextUtils.isEmpty(url) == false) { |
| 3737 | log("CheckMp.onComplete: warm sim (no dns/tcp), url=" + url); |
| 3738 | setNotificationVisible(true, ni, url); |
| 3739 | } else { |
| 3740 | log("CheckMp.onComplete: warm sim (no dns/tcp), no url"); |
| 3741 | } |
| 3742 | break; |
| 3743 | } |
| 3744 | default: { |
| 3745 | loge("CheckMp.onComplete: ignore unexpected result=" + result); |
| 3746 | break; |
| 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | }; |
| 3751 | CheckMp.Params params = |
| 3752 | new CheckMp.Params(checkMp.getDefaultUrl(), timeOutMs, cb); |
| 3753 | log("checkMobileProvisioning: params=" + params); |
| 3754 | setNotificationVisible(false, null, null); |
| 3755 | checkMp.execute(params); |
| 3756 | } finally { |
| 3757 | Binder.restoreCallingIdentity(token); |
| 3758 | log("checkMobileProvisioning: X"); |
| 3759 | } |
| 3760 | return timeOutMs; |
| 3761 | } |
| 3762 | |
| 3763 | static class CheckMp extends |
| 3764 | AsyncTask<CheckMp.Params, Void, Integer> { |
| 3765 | private static final String CHECKMP_TAG = "CheckMp"; |
| 3766 | public static final int MAX_TIMEOUT_MS = 60000; |
| 3767 | private static final int SOCKET_TIMEOUT_MS = 5000; |
| 3768 | private Context mContext; |
| 3769 | private ConnectivityService mCs; |
| 3770 | private TelephonyManager mTm; |
| 3771 | private Params mParams; |
| 3772 | |
| 3773 | /** |
| 3774 | * Parameters for AsyncTask.execute |
| 3775 | */ |
| 3776 | static class Params { |
| 3777 | private String mUrl; |
| 3778 | private long mTimeOutMs; |
| 3779 | private CallBack mCb; |
| 3780 | |
| 3781 | Params(String url, long timeOutMs, CallBack cb) { |
| 3782 | mUrl = url; |
| 3783 | mTimeOutMs = timeOutMs; |
| 3784 | mCb = cb; |
| 3785 | } |
| 3786 | |
| 3787 | @Override |
| 3788 | public String toString() { |
| 3789 | return "{" + " url=" + mUrl + " mTimeOutMs=" + mTimeOutMs + " mCb=" + mCb + "}"; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | /** |
| 3794 | * The call back object passed in Params. onComplete will be called |
| 3795 | * on the main thread. |
| 3796 | */ |
| 3797 | abstract static class CallBack { |
| 3798 | // Called on the main thread. |
| 3799 | abstract void onComplete(Integer result); |
| 3800 | } |
| 3801 | |
| 3802 | public CheckMp(Context context, ConnectivityService cs) { |
| 3803 | mContext = context; |
| 3804 | mCs = cs; |
| 3805 | |
| 3806 | // Setup access to TelephonyService we'll be using. |
| 3807 | mTm = (TelephonyManager) mContext.getSystemService( |
| 3808 | Context.TELEPHONY_SERVICE); |
| 3809 | } |
| 3810 | |
| 3811 | /** |
| 3812 | * Get the default url to use for the test. |
| 3813 | */ |
| 3814 | public String getDefaultUrl() { |
| 3815 | // See http://go/clientsdns for usage approval |
| 3816 | String server = Settings.Global.getString(mContext.getContentResolver(), |
| 3817 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 3818 | if (server == null) { |
| 3819 | server = "clients3.google.com"; |
| 3820 | } |
| 3821 | return "http://" + server + "/generate_204"; |
| 3822 | } |
| 3823 | |
| 3824 | /** |
| 3825 | * Detect if its possible to connect to the http url. DNS based detection techniques |
| 3826 | * do not work at all hotspots. The best way to check is to perform a request to |
| 3827 | * a known address that fetches the data we expect. |
| 3828 | */ |
| 3829 | private synchronized Integer isMobileOk(Params params) { |
| 3830 | Integer result = ConnectivityManager.CMP_RESULT_CODE_NO_CONNECTION; |
| 3831 | Uri orgUri = Uri.parse(params.mUrl); |
| 3832 | Random rand = new Random(); |
| 3833 | mParams = params; |
| 3834 | |
| 3835 | try { |
| 3836 | if (mCs.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) { |
| 3837 | log("isMobileOk: not mobile capable"); |
| 3838 | result = ConnectivityManager.CMP_RESULT_CODE_NO_CONNECTION; |
| 3839 | return result; |
| 3840 | } |
| 3841 | |
| 3842 | // Enable fail fast as we'll do retries here and use a |
| 3843 | // hipri connection so the default connection stays active. |
| 3844 | log("isMobileOk: start hipri url=" + params.mUrl); |
| 3845 | mCs.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 3846 | mCs.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 3847 | Phone.FEATURE_ENABLE_HIPRI, new Binder()); |
| 3848 | |
| 3849 | // Continue trying to connect until time has run out |
| 3850 | long endTime = SystemClock.elapsedRealtime() + params.mTimeOutMs; |
| 3851 | while(SystemClock.elapsedRealtime() < endTime) { |
| 3852 | try { |
| 3853 | // Wait for hipri to connect. |
| 3854 | // TODO: Don't poll and handle situation where hipri fails |
| 3855 | // because default is retrying. See b/9569540 |
| 3856 | NetworkInfo.State state = mCs |
| 3857 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 3858 | if (state != NetworkInfo.State.CONNECTED) { |
| 3859 | log("isMobileOk: not connected ni=" + |
| 3860 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
| 3861 | sleep(1); |
| 3862 | result = ConnectivityManager.CMP_RESULT_CODE_NO_CONNECTION; |
| 3863 | continue; |
| 3864 | } |
| 3865 | |
| 3866 | // Get of the addresses associated with the url host. We need to use the |
| 3867 | // address otherwise HttpURLConnection object will use the name to get |
| 3868 | // the addresses and is will try every address but that will bypass the |
| 3869 | // route to host we setup and the connection could succeed as the default |
| 3870 | // interface might be connected to the internet via wifi or other interface. |
| 3871 | InetAddress[] addresses; |
| 3872 | try { |
| 3873 | addresses = InetAddress.getAllByName(orgUri.getHost()); |
| 3874 | } catch (UnknownHostException e) { |
| 3875 | log("isMobileOk: UnknownHostException"); |
| 3876 | result = ConnectivityManager.CMP_RESULT_CODE_NO_DNS; |
| 3877 | return result; |
| 3878 | } |
| 3879 | log("isMobileOk: addresses=" + inetAddressesToString(addresses)); |
| 3880 | |
| 3881 | // Get the type of addresses supported by this link |
| 3882 | LinkProperties lp = mCs.getLinkProperties( |
| 3883 | ConnectivityManager.TYPE_MOBILE_HIPRI); |
| 3884 | boolean linkHasIpv4 = hasIPv4Address(lp); |
| 3885 | boolean linkHasIpv6 = hasIPv6Address(lp); |
| 3886 | log("isMobileOk: linkHasIpv4=" + linkHasIpv4 |
| 3887 | + " linkHasIpv6=" + linkHasIpv6); |
| 3888 | |
| 3889 | // Loop through at most 3 valid addresses or all of the address or until |
| 3890 | // we run out of time |
| 3891 | int loops = Math.min(3, addresses.length); |
| 3892 | for(int validAddr=0, addrTried=0; |
| 3893 | (validAddr < loops) && (addrTried < addresses.length) |
| 3894 | && (SystemClock.elapsedRealtime() < endTime); |
| 3895 | addrTried ++) { |
| 3896 | |
| 3897 | // Choose the address at random but make sure its type is supported |
| 3898 | InetAddress hostAddr = addresses[rand.nextInt(addresses.length)]; |
| 3899 | if (((hostAddr instanceof Inet4Address) && linkHasIpv4) |
| 3900 | || ((hostAddr instanceof Inet6Address) && linkHasIpv6)) { |
| 3901 | // Valid address, so use it |
| 3902 | validAddr += 1; |
| 3903 | } else { |
| 3904 | // Invalid address so try next address |
| 3905 | continue; |
| 3906 | } |
| 3907 | |
| 3908 | // Make a route to host so we check the specific interface. |
| 3909 | if (mCs.requestRouteToHostAddress(ConnectivityManager.TYPE_MOBILE_HIPRI, |
| 3910 | hostAddr.getAddress())) { |
| 3911 | // Wait a short time to be sure the route is established ?? |
| 3912 | log("isMobileOk:" |
| 3913 | + " wait to establish route to hostAddr=" + hostAddr); |
| 3914 | sleep(3); |
| 3915 | } else { |
| 3916 | log("isMobileOk:" |
| 3917 | + " could not establish route to hostAddr=" + hostAddr); |
| 3918 | continue; |
| 3919 | } |
| 3920 | |
| 3921 | // Rewrite the url to have numeric address to use the specific route. |
| 3922 | // I also set the "Connection" to "Close" as by default "Keep-Alive" |
| 3923 | // is used which is useless in this case. |
| 3924 | URL newUrl = new URL(orgUri.getScheme() + "://" |
| 3925 | + hostAddr.getHostAddress() + orgUri.getPath()); |
| 3926 | log("isMobileOk: newUrl=" + newUrl); |
| 3927 | |
| 3928 | HttpURLConnection urlConn = null; |
| 3929 | try { |
| 3930 | // Open the connection set the request header and get the response |
| 3931 | urlConn = (HttpURLConnection) newUrl.openConnection( |
| 3932 | java.net.Proxy.NO_PROXY); |
| 3933 | urlConn.setInstanceFollowRedirects(false); |
| 3934 | urlConn.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 3935 | urlConn.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 3936 | urlConn.setUseCaches(false); |
| 3937 | urlConn.setAllowUserInteraction(false); |
| 3938 | urlConn.setRequestProperty("Connection", "close"); |
| 3939 | int responseCode = urlConn.getResponseCode(); |
| 3940 | if (responseCode == 204) { |
| 3941 | result = ConnectivityManager.CMP_RESULT_CODE_CONNECTABLE; |
| 3942 | } else { |
| 3943 | result = ConnectivityManager.CMP_RESULT_CODE_REDIRECTED; |
| 3944 | } |
| 3945 | log("isMobileOk: connected responseCode=" + responseCode); |
| 3946 | urlConn.disconnect(); |
| 3947 | urlConn = null; |
| 3948 | return result; |
| 3949 | } catch (Exception e) { |
| 3950 | log("isMobileOk: HttpURLConnection Exception e=" + e); |
| 3951 | if (urlConn != null) { |
| 3952 | urlConn.disconnect(); |
| 3953 | urlConn = null; |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | result = ConnectivityManager.CMP_RESULT_CODE_NO_TCP_CONNECTION; |
| 3958 | log("isMobileOk: loops|timed out"); |
| 3959 | return result; |
| 3960 | } catch (Exception e) { |
| 3961 | log("isMobileOk: Exception e=" + e); |
| 3962 | continue; |
| 3963 | } |
| 3964 | } |
| 3965 | log("isMobileOk: timed out"); |
| 3966 | } finally { |
| 3967 | log("isMobileOk: F stop hipri"); |
| 3968 | mCs.setEnableFailFastMobileData(DctConstants.DISABLED); |
| 3969 | mCs.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 3970 | Phone.FEATURE_ENABLE_HIPRI); |
| 3971 | log("isMobileOk: X result=" + result); |
| 3972 | } |
| 3973 | return result; |
| 3974 | } |
| 3975 | |
| 3976 | @Override |
| 3977 | protected Integer doInBackground(Params... params) { |
| 3978 | return isMobileOk(params[0]); |
| 3979 | } |
| 3980 | |
| 3981 | @Override |
| 3982 | protected void onPostExecute(Integer result) { |
| 3983 | log("onPostExecute: result=" + result); |
| 3984 | if ((mParams != null) && (mParams.mCb != null)) { |
| 3985 | mParams.mCb.onComplete(result); |
| 3986 | } |
| 3987 | } |
| 3988 | |
| 3989 | private String inetAddressesToString(InetAddress[] addresses) { |
| 3990 | StringBuffer sb = new StringBuffer(); |
| 3991 | boolean firstTime = true; |
| 3992 | for(InetAddress addr : addresses) { |
| 3993 | if (firstTime) { |
| 3994 | firstTime = false; |
| 3995 | } else { |
| 3996 | sb.append(","); |
| 3997 | } |
| 3998 | sb.append(addr); |
| 3999 | } |
| 4000 | return sb.toString(); |
| 4001 | } |
| 4002 | |
| 4003 | private void printNetworkInfo() { |
| 4004 | boolean hasIccCard = mTm.hasIccCard(); |
| 4005 | int simState = mTm.getSimState(); |
| 4006 | log("hasIccCard=" + hasIccCard |
| 4007 | + " simState=" + simState); |
| 4008 | NetworkInfo[] ni = mCs.getAllNetworkInfo(); |
| 4009 | if (ni != null) { |
| 4010 | log("ni.length=" + ni.length); |
| 4011 | for (NetworkInfo netInfo: ni) { |
| 4012 | log("netInfo=" + netInfo.toString()); |
| 4013 | } |
| 4014 | } else { |
| 4015 | log("no network info ni=null"); |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | /** |
| 4020 | * Sleep for a few seconds then return. |
| 4021 | * @param seconds |
| 4022 | */ |
| 4023 | private static void sleep(int seconds) { |
| 4024 | try { |
| 4025 | Thread.sleep(seconds * 1000); |
| 4026 | } catch (InterruptedException e) { |
| 4027 | e.printStackTrace(); |
| 4028 | } |
| 4029 | } |
| 4030 | |
| 4031 | public boolean hasIPv4Address(LinkProperties lp) { |
| 4032 | return lp.hasIPv4Address(); |
| 4033 | } |
| 4034 | |
| 4035 | // Not implemented in LinkProperties, do it here. |
| 4036 | public boolean hasIPv6Address(LinkProperties lp) { |
| 4037 | for (LinkAddress address : lp.getLinkAddresses()) { |
| 4038 | if (address.getAddress() instanceof Inet6Address) { |
| 4039 | return true; |
| 4040 | } |
| 4041 | } |
| 4042 | return false; |
| 4043 | } |
| 4044 | |
| 4045 | private void log(String s) { |
| 4046 | Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s); |
| 4047 | } |
| 4048 | } |
| 4049 | |
| 4050 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 4051 | |
| 4052 | private void setNotificationVisible(boolean visible, NetworkInfo networkInfo, String url) { |
| 4053 | log("setNotificationVisible: E visible=" + visible + " ni=" + networkInfo + " url=" + url); |
| 4054 | |
| 4055 | Resources r = Resources.getSystem(); |
| 4056 | NotificationManager notificationManager = (NotificationManager) mContext |
| 4057 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 4058 | |
| 4059 | if (visible) { |
| 4060 | CharSequence title; |
| 4061 | CharSequence details; |
| 4062 | int icon; |
| 4063 | switch (networkInfo.getType()) { |
| 4064 | case ConnectivityManager.TYPE_WIFI: |
| 4065 | log("setNotificationVisible: TYPE_WIFI"); |
| 4066 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 4067 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 4068 | networkInfo.getExtraInfo()); |
| 4069 | icon = R.drawable.stat_notify_wifi_in_range; |
| 4070 | break; |
| 4071 | case ConnectivityManager.TYPE_MOBILE: |
| 4072 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
| 4073 | log("setNotificationVisible: TYPE_MOBILE|HIPRI"); |
| 4074 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4075 | // TODO: Change this to pull from NetworkInfo once a printable |
| 4076 | // name has been added to it |
| 4077 | details = mTelephonyManager.getNetworkOperatorName(); |
| 4078 | icon = R.drawable.stat_notify_rssi_in_range; |
| 4079 | break; |
| 4080 | default: |
| 4081 | log("setNotificationVisible: other type=" + networkInfo.getType()); |
| 4082 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4083 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 4084 | networkInfo.getExtraInfo()); |
| 4085 | icon = R.drawable.stat_notify_rssi_in_range; |
| 4086 | break; |
| 4087 | } |
| 4088 | |
| 4089 | Notification notification = new Notification(); |
| 4090 | notification.when = 0; |
| 4091 | notification.icon = icon; |
| 4092 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
| 4093 | Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 4094 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4095 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4096 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
| 4097 | notification.tickerText = title; |
| 4098 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
| 4099 | |
| 4100 | log("setNotificaitionVisible: notify notificaiton=" + notification); |
| 4101 | notificationManager.notify(NOTIFICATION_ID, 1, notification); |
| 4102 | } else { |
| 4103 | log("setNotificaitionVisible: cancel"); |
| 4104 | notificationManager.cancel(NOTIFICATION_ID, 1); |
| 4105 | } |
| 4106 | log("setNotificationVisible: X visible=" + visible + " ni=" + networkInfo + " url=" + url); |
| 4107 | } |
| 4108 | |
| 4109 | private String getProvisioningUrl() { |
| 4110 | String url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4111 | log("getProvisioningUrl: mobile_provisioning_url=" + url); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4112 | |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4113 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4114 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4115 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 4116 | if (TextUtils.isEmpty(phoneNumber)) { |
| 4117 | phoneNumber = "0000000000"; |
| 4118 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4119 | url = String.format(url, |
| 4120 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 4121 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4122 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4125 | return url; |
| 4126 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4127 | |
| 4128 | private void onUserStart(int userId) { |
| 4129 | synchronized(mVpns) { |
| 4130 | Vpn userVpn = mVpns.get(userId); |
| 4131 | if (userVpn != null) { |
| 4132 | loge("Starting user already has a VPN"); |
| 4133 | return; |
| 4134 | } |
| 4135 | userVpn = new Vpn(mContext, mVpnCallback, mNetd, this, userId); |
| 4136 | mVpns.put(userId, userVpn); |
| 4137 | userVpn.startMonitoring(mContext, mTrackerHandler); |
| 4138 | } |
| 4139 | } |
| 4140 | |
| 4141 | private void onUserStop(int userId) { |
| 4142 | synchronized(mVpns) { |
| 4143 | Vpn userVpn = mVpns.get(userId); |
| 4144 | if (userVpn == null) { |
| 4145 | loge("Stopping user has no VPN"); |
| 4146 | return; |
| 4147 | } |
| 4148 | mVpns.delete(userId); |
| 4149 | } |
| 4150 | } |
| 4151 | |
| 4152 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 4153 | @Override |
| 4154 | public void onReceive(Context context, Intent intent) { |
| 4155 | final String action = intent.getAction(); |
| 4156 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 4157 | if (userId == UserHandle.USER_NULL) return; |
| 4158 | |
| 4159 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 4160 | onUserStart(userId); |
| 4161 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 4162 | onUserStop(userId); |
| 4163 | } |
| 4164 | } |
| 4165 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4166 | } |