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