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