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