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; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 25 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TYPE_MOBILE_MMS; |
| 27 | import static android.net.ConnectivityManager.TYPE_MOBILE_SUPL; |
| 28 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 29 | import static android.net.ConnectivityManager.TYPE_MOBILE_FOTA; |
| 30 | import static android.net.ConnectivityManager.TYPE_MOBILE_IMS; |
| 31 | import static android.net.ConnectivityManager.TYPE_MOBILE_CBS; |
| 32 | import static android.net.ConnectivityManager.TYPE_MOBILE_IA; |
| 33 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 34 | import static android.net.ConnectivityManager.TYPE_NONE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 35 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 36 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 37 | import static android.net.ConnectivityManager.TYPE_PROXY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 38 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 39 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
| 40 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 42 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 43 | import android.app.AlarmManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 44 | import android.app.Notification; |
| 45 | import android.app.NotificationManager; |
| 46 | import android.app.PendingIntent; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 47 | import android.content.ActivityNotFoundException; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 48 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.content.ContentResolver; |
| 50 | import android.content.Context; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 51 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 53 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.content.pm.PackageManager; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 55 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 56 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 57 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.net.ConnectivityManager; |
| 59 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 60 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 61 | import android.net.INetworkPolicyListener; |
| 62 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 63 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 64 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 65 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 66 | import android.net.LinkProperties.CompareResult; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 67 | import android.net.LinkQualityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 69 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 70 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 71 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 72 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 74 | import android.net.NetworkInfo.DetailedState; |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 75 | import android.net.NetworkFactory; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 76 | import android.net.NetworkMisc; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 77 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 78 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 79 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 81 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 82 | import android.net.Proxy; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 83 | import android.net.ProxyDataTracker; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 84 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 85 | import android.net.RouteInfo; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 86 | import android.net.SamplingDataTracker; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 87 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 88 | import android.net.Uri; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 89 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 90 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.os.Binder; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 92 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 93 | import android.os.Bundle; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 94 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 96 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 97 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 98 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.os.Looper; |
| 100 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 101 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 102 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 103 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 104 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 105 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 107 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 109 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 110 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 112 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 113 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 114 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 115 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 116 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 117 | import android.util.SparseArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 118 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 119 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 121 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 122 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 123 | import com.android.internal.app.IBatteryStats; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 124 | import com.android.internal.net.LegacyVpnInfo; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 125 | import com.android.internal.net.NetworkStatsFactory; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 126 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 127 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 128 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 129 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 130 | import com.android.internal.telephony.PhoneConstants; |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 131 | import com.android.internal.telephony.TelephonyIntents; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 132 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 133 | import com.android.internal.util.IndentingPrintWriter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 134 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 135 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 136 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 137 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 138 | import com.android.server.connectivity.NetworkAgentInfo; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 139 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 140 | import com.android.server.connectivity.PacManager; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 141 | import com.android.server.connectivity.PermissionMonitor; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 142 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 143 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 144 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 145 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 146 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 147 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 148 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 149 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 150 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 151 | import org.xmlpull.v1.XmlPullParser; |
| 152 | import org.xmlpull.v1.XmlPullParserException; |
| 153 | |
| 154 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 156 | import java.io.FileNotFoundException; |
| 157 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 158 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 160 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 161 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 162 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 163 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 164 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 165 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 166 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 167 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 168 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 169 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 170 | import java.util.GregorianCalendar; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 171 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 172 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 173 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 174 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 175 | import java.util.Objects; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 176 | import java.util.Random; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 177 | import java.util.concurrent.atomic.AtomicBoolean; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 178 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 180 | import javax.net.ssl.HostnameVerifier; |
| 181 | import javax.net.ssl.HttpsURLConnection; |
| 182 | import javax.net.ssl.SSLSession; |
| 183 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | /** |
| 185 | * @hide |
| 186 | */ |
| 187 | public class ConnectivityService extends IConnectivityManager.Stub { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 188 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Robert Greenwalt | a7d31bf | 2014-07-23 12:42:47 -0700 | [diff] [blame] | 190 | private static final boolean DBG = true; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 191 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 193 | // network sampling debugging |
| 194 | private static final boolean SAMPLE_DBG = false; |
| 195 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 196 | private static final boolean LOGD_RULES = false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 197 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 198 | // TODO: create better separation between radio types and network types |
| 199 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 200 | // how long to wait before switching back to a radio's default network |
| 201 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 202 | // system property that can override the above value |
| 203 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 204 | "android.telephony.apn-restore"; |
| 205 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 206 | // Default value if FAIL_FAST_TIME_MS is not set |
| 207 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 208 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 209 | private static final String FAIL_FAST_TIME_MS = |
| 210 | "persist.radio.fail_fast_time_ms"; |
| 211 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 212 | private static final String ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED = |
| 213 | "android.net.ConnectivityService.action.PKT_CNT_SAMPLE_INTERVAL_ELAPSED"; |
| 214 | |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 215 | private static final int SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE = 0; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 216 | |
| 217 | private PendingIntent mSampleIntervalElapsedIntent; |
| 218 | |
| 219 | // Set network sampling interval at 12 minutes, this way, even if the timers get |
| 220 | // aggregated, it will fire at around 15 minutes, which should allow us to |
| 221 | // aggregate this timer with other timers (specially the socket keep alive timers) |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 222 | private static final int DEFAULT_SAMPLING_INTERVAL_IN_SECONDS = (SAMPLE_DBG ? 30 : 12 * 60); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 223 | |
| 224 | // start network sampling a minute after booting ... |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 225 | private static final int DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS = (SAMPLE_DBG ? 30 : 60); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 226 | |
| 227 | AlarmManager mAlarmManager; |
| 228 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 229 | private Tethering mTethering; |
| 230 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 231 | private final PermissionMonitor mPermissionMonitor; |
| 232 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 233 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 234 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 235 | @GuardedBy("mVpns") |
| 236 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 237 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 238 | private boolean mLockdownEnabled; |
| 239 | private LockdownVpnTracker mLockdownTracker; |
| 240 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 241 | private Nat464Xlat mClat; |
| 242 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 243 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 244 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 245 | /** Currently active network rules by UID. */ |
| 246 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 247 | /** Set of ifaces that are costly. */ |
| 248 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 249 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | /** |
| 251 | * Sometimes we want to refer to the individual network state |
| 252 | * trackers separately, and sometimes we just want to treat them |
| 253 | * abstractly. |
| 254 | */ |
| 255 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | private Context mContext; |
| 258 | private int mNetworkPreference; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 259 | private int mActiveDefaultNetwork = TYPE_NONE; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 260 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 261 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 263 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 264 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | |
| 266 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 267 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 269 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 270 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 271 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 272 | private String mCurrentTcpBufferSizes; |
| 273 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 274 | private static final int ENABLED = 1; |
| 275 | private static final int DISABLED = 0; |
| 276 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 277 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 278 | * used internally to change our mobile data enabled flag |
| 279 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 280 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 281 | |
| 282 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 283 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 284 | * from one net to another. Clear happens when we get a new |
| 285 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 286 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 287 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 288 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 289 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 290 | /** |
| 291 | * used internally to reload global proxy settings |
| 292 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 293 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 294 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 295 | /** |
| 296 | * used internally to set external dependency met/unmet |
| 297 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 298 | * arg2 = NetworkType |
| 299 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 300 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 301 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 302 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 303 | * used internally to send a sticky broadcast delayed. |
| 304 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 305 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 11; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 306 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 307 | /** |
| 308 | * Used internally to |
| 309 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 310 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 311 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 12; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 312 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 313 | /** |
| 314 | * Used internally to disable fail fast of mobile data |
| 315 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 316 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 14; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 317 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 318 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 319 | * used internally to indicate that data sampling interval is up |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 320 | */ |
| 321 | private static final int EVENT_SAMPLE_INTERVAL_ELAPSED = 15; |
| 322 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 323 | /** |
| 324 | * PAC manager has received new port. |
| 325 | */ |
| 326 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 327 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 328 | /** |
| 329 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 330 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 331 | */ |
| 332 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 333 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 334 | /** |
| 335 | * used internally when registering NetworkAgents |
| 336 | * obj = Messenger |
| 337 | */ |
| 338 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 339 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 340 | /** |
| 341 | * used to add a network request |
| 342 | * includes a NetworkRequestInfo |
| 343 | */ |
| 344 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 345 | |
| 346 | /** |
| 347 | * indicates a timeout period is over - check if we had a network yet or not |
| 348 | * and if not, call the timeout calback (but leave the request live until they |
| 349 | * cancel it. |
| 350 | * includes a NetworkRequestInfo |
| 351 | */ |
| 352 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 353 | |
| 354 | /** |
| 355 | * used to add a network listener - no request |
| 356 | * includes a NetworkRequestInfo |
| 357 | */ |
| 358 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 359 | |
| 360 | /** |
| 361 | * used to remove a network request, either a listener or a real request |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 362 | * arg1 = UID of caller |
| 363 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 364 | */ |
| 365 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 366 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 367 | /** |
| 368 | * used internally when registering NetworkFactories |
| 369 | * obj = Messenger |
| 370 | */ |
| 371 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 372 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 373 | /** |
| 374 | * used internally to expire a wakelock when transitioning |
| 375 | * from one net to another. Expire happens when we fail to find |
| 376 | * a new network (typically after 1 minute) - |
| 377 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 378 | * a replacement network. |
| 379 | */ |
| 380 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 381 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Used internally to indicate the system is ready. |
| 384 | */ |
| 385 | private static final int EVENT_SYSTEM_READY = 25; |
| 386 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 387 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 388 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 389 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 390 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 391 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 392 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 393 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 394 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 395 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 396 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 397 | private String mNetTransitionWakeLockCausedBy = ""; |
| 398 | private int mNetTransitionWakeLockSerialNumber; |
| 399 | private int mNetTransitionWakeLockTimeout; |
| 400 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 401 | private InetAddress mDefaultDns; |
| 402 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 403 | // used in DBG mode to track inet condition reports |
| 404 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 405 | private ArrayList mInetLog; |
| 406 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 407 | // track the current default http proxy - tell the world if we get a new one (real change) |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 408 | private volatile ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 409 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 410 | private boolean mDefaultProxyDisabled = false; |
| 411 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 412 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 413 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 414 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 415 | private PacManager mPacManager = null; |
| 416 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 417 | private SettingsObserver mSettingsObserver; |
| 418 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 419 | private UserManager mUserManager; |
| 420 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 421 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 422 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 423 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 424 | // the set of network types that can only be enabled by system/sig apps |
| 425 | List mProtectedNetworks; |
| 426 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 427 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 428 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 429 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 430 | |
| 431 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 432 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 433 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
| 434 | private final static int MIN_NET_ID = 100; // some reserved marks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 435 | private final static int MAX_NET_ID = 65535; |
| 436 | private int mNextNetId = MIN_NET_ID; |
| 437 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 438 | // sequence number of NetworkRequests |
| 439 | private int mNextNetworkRequestId = 1; |
| 440 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 441 | /** |
| 442 | * Implements support for the legacy "one network per network type" model. |
| 443 | * |
| 444 | * We used to have a static array of NetworkStateTrackers, one for each |
| 445 | * network type, but that doesn't work any more now that we can have, |
| 446 | * for example, more that one wifi network. This class stores all the |
| 447 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 448 | * API will only see the first one. |
| 449 | * |
| 450 | * It serves two main purposes: |
| 451 | * |
| 452 | * 1. Provide information about "the network for a given type" (since this |
| 453 | * API only supports one). |
| 454 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 455 | * the first network for a given type changes, or if the default network |
| 456 | * changes. |
| 457 | */ |
| 458 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 459 | |
| 460 | private static final boolean DBG = true; |
| 461 | private static final boolean VDBG = false; |
| 462 | private static final String TAG = "CSLegacyTypeTracker"; |
| 463 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 464 | /** |
| 465 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 466 | * Each list holds references to all NetworkAgentInfos that are used to |
| 467 | * satisfy requests for that network type. |
| 468 | * |
| 469 | * This array is built out at startup such that an unsupported network |
| 470 | * doesn't get an ArrayList instance, making this a tristate: |
| 471 | * unsupported, supported but not active and active. |
| 472 | * |
| 473 | * The actual lists are populated when we scan the network types that |
| 474 | * are supported on this device. |
| 475 | */ |
| 476 | private ArrayList<NetworkAgentInfo> mTypeLists[]; |
| 477 | |
| 478 | public LegacyTypeTracker() { |
| 479 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 480 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 481 | } |
| 482 | |
| 483 | public void addSupportedType(int type) { |
| 484 | if (mTypeLists[type] != null) { |
| 485 | throw new IllegalStateException( |
| 486 | "legacy list for type " + type + "already initialized"); |
| 487 | } |
| 488 | mTypeLists[type] = new ArrayList<NetworkAgentInfo>(); |
| 489 | } |
| 490 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 491 | public boolean isTypeSupported(int type) { |
| 492 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 493 | } |
| 494 | |
| 495 | public NetworkAgentInfo getNetworkForType(int type) { |
| 496 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 497 | return mTypeLists[type].get(0); |
| 498 | } else { |
| 499 | return null; |
| 500 | } |
| 501 | } |
| 502 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 503 | private void maybeLogBroadcast(NetworkAgentInfo nai, boolean connected, int type) { |
| 504 | if (DBG) { |
| 505 | log("Sending " + (connected ? "connected" : "disconnected") + |
| 506 | " broadcast for type " + type + " " + nai.name() + |
| 507 | " isDefaultNetwork=" + isDefaultNetwork(nai)); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 512 | public void add(int type, NetworkAgentInfo nai) { |
| 513 | if (!isTypeSupported(type)) { |
| 514 | return; // Invalid network type. |
| 515 | } |
| 516 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 517 | |
| 518 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 519 | if (list.contains(nai)) { |
| 520 | loge("Attempting to register duplicate agent for type " + type + ": " + nai); |
| 521 | return; |
| 522 | } |
| 523 | |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 524 | list.add(nai); |
| 525 | |
| 526 | // Send a broadcast if this is the first network of its type or if it's the default. |
| 527 | if (list.size() == 1 || isDefaultNetwork(nai)) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 528 | maybeLogBroadcast(nai, true, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 529 | sendLegacyNetworkBroadcast(nai, true, type); |
| 530 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 531 | } |
| 532 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 533 | /** Removes the given network from the specified legacy type list. */ |
| 534 | public void remove(int type, NetworkAgentInfo nai) { |
| 535 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 536 | if (list == null || list.isEmpty()) { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | boolean wasFirstNetwork = list.get(0).equals(nai); |
| 541 | |
| 542 | if (!list.remove(nai)) { |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | if (wasFirstNetwork || isDefaultNetwork(nai)) { |
| 547 | maybeLogBroadcast(nai, false, type); |
| 548 | sendLegacyNetworkBroadcast(nai, false, type); |
| 549 | } |
| 550 | |
| 551 | if (!list.isEmpty() && wasFirstNetwork) { |
| 552 | if (DBG) log("Other network available for type " + type + |
| 553 | ", sending connected broadcast"); |
| 554 | maybeLogBroadcast(list.get(0), false, type); |
| 555 | sendLegacyNetworkBroadcast(list.get(0), false, type); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** Removes the given network from all legacy type lists. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 560 | public void remove(NetworkAgentInfo nai) { |
| 561 | if (VDBG) log("Removing agent " + nai); |
| 562 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 563 | remove(type, nai); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 564 | } |
| 565 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 566 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 567 | private String naiToString(NetworkAgentInfo nai) { |
| 568 | String name = (nai != null) ? nai.name() : "null"; |
| 569 | String state = (nai.networkInfo != null) ? |
| 570 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 571 | "???/???"; |
| 572 | return name + " " + state; |
| 573 | } |
| 574 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 575 | public void dump(IndentingPrintWriter pw) { |
| 576 | for (int type = 0; type < mTypeLists.length; type++) { |
| 577 | if (mTypeLists[type] == null) continue; |
| 578 | pw.print(type + " "); |
| 579 | pw.increaseIndent(); |
| 580 | if (mTypeLists[type].size() == 0) pw.println("none"); |
| 581 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 582 | pw.println(naiToString(nai)); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 583 | } |
| 584 | pw.decreaseIndent(); |
| 585 | } |
| 586 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 587 | |
| 588 | // This class needs its own log method because it has a different TAG. |
| 589 | private void log(String s) { |
| 590 | Slog.d(TAG, s); |
| 591 | } |
| 592 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 593 | } |
| 594 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 595 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 596 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 597 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 598 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 599 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 600 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 601 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 602 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 603 | mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 604 | NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), |
| 605 | NetworkRequestInfo.REQUEST); |
| 606 | mNetworkRequests.put(mDefaultRequest, nri); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 607 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 608 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 609 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 610 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 611 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 612 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 613 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 614 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 615 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 616 | Settings.Secure.ANDROID_ID); |
| 617 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 618 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 619 | SystemProperties.set("net.hostname", name); |
| 620 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 623 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 624 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 625 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 626 | if (dns == null || dns.length() == 0) { |
| 627 | dns = context.getResources().getString( |
| 628 | com.android.internal.R.string.config_default_dns_server); |
| 629 | } |
| 630 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 631 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 632 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 633 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 636 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 637 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 638 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 639 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 640 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 641 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 642 | try { |
| 643 | mPolicyManager.registerListener(mPolicyListener); |
| 644 | } catch (RemoteException e) { |
| 645 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 646 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 650 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 651 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 652 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 653 | com.android.internal.R.integer.config_networkTransitionTimeout); |
| 654 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 655 | mNetTrackers = new NetworkStateTracker[ |
| 656 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 657 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 658 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 659 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 660 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 661 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 662 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 663 | String[] naStrings = context.getResources().getStringArray( |
| 664 | com.android.internal.R.array.networkAttributes); |
| 665 | for (String naString : naStrings) { |
| 666 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 667 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 668 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 669 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 670 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 671 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 672 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 673 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 674 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 675 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 676 | n.type); |
| 677 | continue; |
| 678 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 679 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 680 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 681 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 682 | continue; |
| 683 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 684 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 685 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 686 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 687 | mNetworksDefined++; |
| 688 | } catch(Exception e) { |
| 689 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 690 | } |
| 691 | } |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 692 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 693 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 694 | mProtectedNetworks = new ArrayList<Integer>(); |
| 695 | int[] protectedNetworks = context.getResources().getIntArray( |
| 696 | com.android.internal.R.array.config_protectedNetworks); |
| 697 | for (int p : protectedNetworks) { |
| 698 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 699 | mProtectedNetworks.add(p); |
| 700 | } else { |
| 701 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 702 | } |
| 703 | } |
| 704 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 705 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 706 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 707 | |
Robert Greenwalt | fab50167 | 2014-07-23 11:44:01 -0700 | [diff] [blame] | 708 | mTethering = new Tethering(mContext, mNetd, statsService, mHandler.getLooper()); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 709 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 710 | mPermissionMonitor = new PermissionMonitor(mContext, mNetd); |
| 711 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 712 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 713 | IntentFilter intentFilter = new IntentFilter(); |
| 714 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 715 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 716 | mContext.registerReceiverAsUser( |
| 717 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 718 | mClat = new Nat464Xlat(mContext, mNetd, this, mTrackerHandler); |
| 719 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 720 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 721 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 722 | mNetd.registerObserver(mDataActivityObserver); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 723 | mNetd.registerObserver(mClat); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 724 | } catch (RemoteException e) { |
| 725 | loge("Error registering observer :" + e); |
| 726 | } |
| 727 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 728 | if (DBG) { |
| 729 | mInetLog = new ArrayList(); |
| 730 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 731 | |
| 732 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 733 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 734 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 735 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 736 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 737 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 738 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 739 | |
| 740 | IntentFilter filter = new IntentFilter(); |
| 741 | filter.addAction(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 742 | mContext.registerReceiver( |
| 743 | new BroadcastReceiver() { |
| 744 | @Override |
| 745 | public void onReceive(Context context, Intent intent) { |
| 746 | String action = intent.getAction(); |
| 747 | if (action.equals(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED)) { |
| 748 | mHandler.sendMessage(mHandler.obtainMessage |
| 749 | (EVENT_SAMPLE_INTERVAL_ELAPSED)); |
| 750 | } |
| 751 | } |
| 752 | }, |
| 753 | new IntentFilter(filter)); |
| 754 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 755 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 756 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 757 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 759 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 760 | private synchronized int nextNetworkRequestId() { |
| 761 | return mNextNetworkRequestId++; |
| 762 | } |
| 763 | |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 764 | private void assignNextNetId(NetworkAgentInfo nai) { |
| 765 | synchronized (mNetworkForNetId) { |
| 766 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 767 | int netId = mNextNetId; |
| 768 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 769 | // Make sure NetID unused. http://b/16815182 |
| 770 | if (mNetworkForNetId.get(netId) == null) { |
| 771 | nai.network = new Network(netId); |
| 772 | mNetworkForNetId.put(netId, nai); |
| 773 | return; |
| 774 | } |
| 775 | } |
| 776 | } |
| 777 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 780 | private int getConnectivityChangeDelay() { |
| 781 | final ContentResolver cr = mContext.getContentResolver(); |
| 782 | |
| 783 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 784 | int defaultDelay = SystemProperties.getInt( |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 785 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 786 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 787 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 788 | defaultDelay); |
| 789 | } |
| 790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | private boolean teardown(NetworkStateTracker netTracker) { |
| 792 | if (netTracker.teardown()) { |
| 793 | netTracker.setTeardownRequested(true); |
| 794 | return true; |
| 795 | } else { |
| 796 | return false; |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | /** |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 801 | * Check if UID should be blocked from using the network represented by the given networkType. |
| 802 | * @deprecated Uses mLegacyTypeTracker; cannot deal with multiple Networks of the same type. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 803 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 804 | private boolean isNetworkBlocked(int networkType, int uid) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 805 | return isNetworkWithLinkPropertiesBlocked(getLinkPropertiesForType(networkType), uid); |
| 806 | } |
| 807 | |
| 808 | /** |
| 809 | * Check if UID should be blocked from using the network represented by the given |
| 810 | * NetworkAgentInfo. |
| 811 | */ |
| 812 | private boolean isNetworkBlocked(NetworkAgentInfo nai, int uid) { |
| 813 | return isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 818 | */ |
| 819 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 820 | final boolean networkCostly; |
| 821 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 822 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 823 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 824 | synchronized (mRulesLock) { |
| 825 | networkCostly = mMeteredIfaces.contains(iface); |
| 826 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 827 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 828 | |
| 829 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | // no restrictive rules; network is visible |
| 834 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 838 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 839 | * {@link DetailedState#BLOCKED} based on |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 840 | * {@link #isNetworkBlocked}. |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 841 | * @deprecated Uses mLegacyTypeTracker; cannot deal with multiple Networks of the same type. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 842 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 843 | private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) { |
| 844 | NetworkInfo info = getNetworkInfoForType(networkType); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 845 | return getFilteredNetworkInfo(info, networkType, uid); |
| 846 | } |
| 847 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 848 | /* |
| 849 | * @deprecated Uses mLegacyTypeTracker; cannot deal with multiple Networks of the same type. |
| 850 | */ |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 851 | private NetworkInfo getFilteredNetworkInfo(NetworkInfo info, int networkType, int uid) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 852 | if (isNetworkBlocked(networkType, uid)) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 853 | // network is blocked; clone and override state |
| 854 | info = new NetworkInfo(info); |
| 855 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Robert Greenwalt | 83f0f75 | 2014-08-01 16:56:05 -0700 | [diff] [blame] | 856 | if (VDBG) log("returning Blocked NetworkInfo"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 857 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 858 | if (mLockdownTracker != null) { |
| 859 | info = mLockdownTracker.augmentNetworkInfo(info); |
Robert Greenwalt | 83f0f75 | 2014-08-01 16:56:05 -0700 | [diff] [blame] | 860 | if (VDBG) log("returning Locked NetworkInfo"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 861 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 862 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 865 | private NetworkInfo getFilteredNetworkInfo(NetworkAgentInfo nai, int uid) { |
| 866 | NetworkInfo info = nai.networkInfo; |
| 867 | if (isNetworkBlocked(nai, uid)) { |
| 868 | // network is blocked; clone and override state |
| 869 | info = new NetworkInfo(info); |
| 870 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 871 | if (DBG) log("returning Blocked NetworkInfo"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 872 | } |
| 873 | if (mLockdownTracker != null) { |
| 874 | info = mLockdownTracker.augmentNetworkInfo(info); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 875 | if (DBG) log("returning Locked NetworkInfo"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 876 | } |
| 877 | return info; |
| 878 | } |
| 879 | |
| 880 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 882 | * It is assumed that at most one network is active at a time. If more |
| 883 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 884 | * @return the info for the active network, or {@code null} if none is |
| 885 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 886 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 887 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 889 | enforceAccessPermission(); |
| 890 | final int uid = Binder.getCallingUid(); |
| 891 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 894 | /** |
| 895 | * Find the first Provisioning network. |
| 896 | * |
| 897 | * @return NetworkInfo or null if none. |
| 898 | */ |
| 899 | private NetworkInfo getProvisioningNetworkInfo() { |
| 900 | enforceAccessPermission(); |
| 901 | |
| 902 | // Find the first Provisioning Network |
| 903 | NetworkInfo provNi = null; |
| 904 | for (NetworkInfo ni : getAllNetworkInfo()) { |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 905 | if (ni.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 906 | provNi = ni; |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | if (DBG) log("getProvisioningNetworkInfo: X provNi=" + provNi); |
| 911 | return provNi; |
| 912 | } |
| 913 | |
| 914 | /** |
| 915 | * Find the first Provisioning network or the ActiveDefaultNetwork |
| 916 | * if there is no Provisioning network |
| 917 | * |
| 918 | * @return NetworkInfo or null if none. |
| 919 | */ |
| 920 | @Override |
| 921 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 922 | enforceAccessPermission(); |
| 923 | |
| 924 | NetworkInfo provNi = getProvisioningNetworkInfo(); |
| 925 | if (provNi == null) { |
| 926 | final int uid = Binder.getCallingUid(); |
| 927 | provNi = getNetworkInfo(mActiveDefaultNetwork, uid); |
| 928 | } |
| 929 | if (DBG) log("getProvisioningOrActiveNetworkInfo: X provNi=" + provNi); |
| 930 | return provNi; |
| 931 | } |
| 932 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 933 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 934 | enforceAccessPermission(); |
| 935 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 936 | return getNetworkInfoForType(mActiveDefaultNetwork); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 937 | } |
| 938 | return null; |
| 939 | } |
| 940 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 941 | @Override |
| 942 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 943 | enforceConnectivityInternalPermission(); |
| 944 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
| 945 | } |
| 946 | |
| 947 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 948 | public NetworkInfo getNetworkInfo(int networkType) { |
| 949 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 950 | final int uid = Binder.getCallingUid(); |
| 951 | return getNetworkInfo(networkType, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 954 | private NetworkInfo getNetworkInfo(int networkType, int uid) { |
| 955 | NetworkInfo info = null; |
| 956 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 957 | if (getNetworkInfoForType(networkType) != null) { |
| 958 | info = getFilteredNetworkInfo(networkType, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | return info; |
| 962 | } |
| 963 | |
| 964 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 965 | public NetworkInfo getNetworkInfoForNetwork(Network network) { |
| 966 | enforceAccessPermission(); |
| 967 | if (network == null) return null; |
| 968 | |
| 969 | final int uid = Binder.getCallingUid(); |
| 970 | NetworkAgentInfo nai = null; |
| 971 | synchronized (mNetworkForNetId) { |
| 972 | nai = mNetworkForNetId.get(network.netId); |
| 973 | } |
| 974 | if (nai == null) return null; |
| 975 | synchronized (nai) { |
| 976 | if (nai.networkInfo == null) return null; |
| 977 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 978 | return getFilteredNetworkInfo(nai, uid); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
| 982 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | public NetworkInfo[] getAllNetworkInfo() { |
| 984 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 985 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 986 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 987 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 988 | networkType++) { |
| 989 | if (getNetworkInfoForType(networkType) != null) { |
| 990 | result.add(getFilteredNetworkInfo(networkType, uid)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 991 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 992 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 993 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | } |
| 995 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 996 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 997 | public Network getNetworkForType(int networkType) { |
| 998 | enforceAccessPermission(); |
| 999 | final int uid = Binder.getCallingUid(); |
| 1000 | if (isNetworkBlocked(networkType, uid)) { |
| 1001 | return null; |
| 1002 | } |
| 1003 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1004 | return (nai == null) ? null : nai.network; |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1008 | public Network[] getAllNetworks() { |
| 1009 | enforceAccessPermission(); |
| 1010 | final ArrayList<Network> result = new ArrayList(); |
| 1011 | synchronized (mNetworkForNetId) { |
| 1012 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
| 1013 | result.add(new Network(mNetworkForNetId.valueAt(i).network)); |
| 1014 | } |
| 1015 | } |
| 1016 | return result.toArray(new Network[result.size()]); |
| 1017 | } |
| 1018 | |
| 1019 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1020 | public boolean isNetworkSupported(int networkType) { |
| 1021 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1022 | return (isNetworkTypeValid(networkType) && (getNetworkInfoForType(networkType) != null)); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1025 | /** |
| 1026 | * Return LinkProperties for the active (i.e., connected) default |
| 1027 | * network interface. It is assumed that at most one default network |
| 1028 | * is active at a time. If more than one is active, it is indeterminate |
| 1029 | * which will be returned. |
| 1030 | * @return the ip properties for the active network, or {@code null} if |
| 1031 | * none is active |
| 1032 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1033 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1034 | public LinkProperties getActiveLinkProperties() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1035 | return getLinkPropertiesForType(mActiveDefaultNetwork); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1038 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1039 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1040 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1041 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1042 | return getLinkPropertiesForTypeInternal(networkType); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1043 | } |
| 1044 | return null; |
| 1045 | } |
| 1046 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1047 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1048 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1049 | public LinkProperties getLinkProperties(Network network) { |
| 1050 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1051 | NetworkAgentInfo nai = null; |
| 1052 | synchronized (mNetworkForNetId) { |
| 1053 | nai = mNetworkForNetId.get(network.netId); |
| 1054 | } |
| 1055 | |
| 1056 | if (nai != null) { |
| 1057 | synchronized (nai) { |
| 1058 | return new LinkProperties(nai.linkProperties); |
| 1059 | } |
| 1060 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1061 | return null; |
| 1062 | } |
| 1063 | |
| 1064 | @Override |
| 1065 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1066 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1067 | NetworkAgentInfo nai = null; |
| 1068 | synchronized (mNetworkForNetId) { |
| 1069 | nai = mNetworkForNetId.get(network.netId); |
| 1070 | } |
| 1071 | if (nai != null) { |
| 1072 | synchronized (nai) { |
| 1073 | return new NetworkCapabilities(nai.networkCapabilities); |
| 1074 | } |
| 1075 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1076 | return null; |
| 1077 | } |
| 1078 | |
| 1079 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1080 | public NetworkState[] getAllNetworkState() { |
| 1081 | enforceAccessPermission(); |
| 1082 | final int uid = Binder.getCallingUid(); |
| 1083 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1084 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1085 | networkType++) { |
| 1086 | if (getNetworkInfoForType(networkType) != null) { |
| 1087 | final NetworkInfo info = getFilteredNetworkInfo(networkType, uid); |
| 1088 | final LinkProperties lp = getLinkPropertiesForTypeInternal(networkType); |
| 1089 | final NetworkCapabilities netcap = getNetworkCapabilitiesForType(networkType); |
| 1090 | result.add(new NetworkState(info, lp, netcap)); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | return result.toArray(new NetworkState[result.size()]); |
| 1094 | } |
| 1095 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1096 | private NetworkState getNetworkStateUnchecked(int networkType) { |
| 1097 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1098 | NetworkInfo info = getNetworkInfoForType(networkType); |
| 1099 | if (info != null) { |
| 1100 | return new NetworkState(info, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1101 | getLinkPropertiesForTypeInternal(networkType), |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1102 | getNetworkCapabilitiesForType(networkType)); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | return null; |
| 1106 | } |
| 1107 | |
| 1108 | @Override |
| 1109 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1110 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1111 | |
| 1112 | final long token = Binder.clearCallingIdentity(); |
| 1113 | try { |
| 1114 | final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork); |
| 1115 | if (state != null) { |
| 1116 | try { |
| 1117 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1118 | } catch (RemoteException e) { |
| 1119 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1120 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1121 | return null; |
| 1122 | } finally { |
| 1123 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1124 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1125 | } |
| 1126 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1127 | @Override |
| 1128 | public boolean isActiveNetworkMetered() { |
| 1129 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1130 | final long token = Binder.clearCallingIdentity(); |
| 1131 | try { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1132 | return isNetworkMeteredUnchecked(mActiveDefaultNetwork); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1133 | } finally { |
| 1134 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1135 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1138 | private boolean isNetworkMeteredUnchecked(int networkType) { |
| 1139 | final NetworkState state = getNetworkStateUnchecked(networkType); |
| 1140 | if (state != null) { |
| 1141 | try { |
| 1142 | return mPolicyManager.isNetworkMetered(state); |
| 1143 | } catch (RemoteException e) { |
| 1144 | } |
| 1145 | } |
| 1146 | return false; |
| 1147 | } |
| 1148 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1149 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1150 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1151 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1152 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1153 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1154 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1155 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1156 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1157 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | * Ensure that a network route exists to deliver traffic to the specified |
| 1159 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1160 | * @param networkType the type of the network over which traffic to the |
| 1161 | * specified host is to be routed |
| 1162 | * @param hostAddress the IP address of the host to which the route is |
| 1163 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | * @return {@code true} on success, {@code false} on failure |
| 1165 | */ |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1166 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1167 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1168 | if (mProtectedNetworks.contains(networkType)) { |
| 1169 | enforceConnectivityInternalPermission(); |
| 1170 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1171 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1172 | InetAddress addr; |
| 1173 | try { |
| 1174 | addr = InetAddress.getByAddress(hostAddress); |
| 1175 | } catch (UnknownHostException e) { |
| 1176 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1177 | return false; |
| 1178 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1179 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1180 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1181 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1184 | |
| 1185 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1186 | if (nai == null) { |
| 1187 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1188 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1189 | } else { |
| 1190 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1191 | } |
| 1192 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1193 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1194 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1195 | DetailedState netState; |
| 1196 | synchronized (nai) { |
| 1197 | netState = nai.networkInfo.getDetailedState(); |
| 1198 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1199 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1200 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1201 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1202 | log("requestRouteToHostAddress on down network " |
| 1203 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1204 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1205 | } |
| 1206 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1208 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1209 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1210 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1211 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1212 | LinkProperties lp; |
| 1213 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1214 | synchronized (nai) { |
| 1215 | lp = nai.linkProperties; |
| 1216 | netId = nai.network.netId; |
| 1217 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1218 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1219 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1220 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1221 | } finally { |
| 1222 | Binder.restoreCallingIdentity(token); |
| 1223 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1226 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1227 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1228 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1229 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1230 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1231 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1232 | if (bestRoute.getGateway().equals(addr)) { |
| 1233 | // 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] | 1234 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1235 | } else { |
| 1236 | // if we will connect to this through another route, add a direct route |
| 1237 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1238 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1241 | if (DBG) log("Adding " + bestRoute + " for interface " + bestRoute.getInterface()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1242 | try { |
| 1243 | mNetd.addLegacyRouteForNetId(netId, bestRoute, uid); |
| 1244 | } catch (Exception e) { |
| 1245 | // never crash - catch them all |
| 1246 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1247 | return false; |
| 1248 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1249 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1252 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1253 | enforceConnectivityInternalPermission(); |
| 1254 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1255 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1256 | (met ? ENABLED : DISABLED), networkType)); |
| 1257 | } |
| 1258 | |
| 1259 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1260 | if (mNetTrackers[networkType] != null) { |
| 1261 | if (DBG) { |
| 1262 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1263 | } |
| 1264 | mNetTrackers[networkType].setDependencyMet(met); |
| 1265 | } |
| 1266 | } |
| 1267 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1268 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1269 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1270 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1271 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1272 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1273 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1276 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1277 | // skip update when we've already applied rules |
| 1278 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1279 | if (oldRules == uidRules) return; |
| 1280 | |
| 1281 | mUidRules.put(uid, uidRules); |
| 1282 | } |
| 1283 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1284 | // TODO: notify UID when it has requested targeted updates |
| 1285 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1286 | |
| 1287 | @Override |
| 1288 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1289 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1290 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1291 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | synchronized (mRulesLock) { |
| 1295 | mMeteredIfaces.clear(); |
| 1296 | for (String iface : meteredIfaces) { |
| 1297 | mMeteredIfaces.add(iface); |
| 1298 | } |
| 1299 | } |
| 1300 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1301 | |
| 1302 | @Override |
| 1303 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1304 | // caller is NPMS, since we only register with them |
| 1305 | if (LOGD_RULES) { |
| 1306 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1307 | } |
| 1308 | |
| 1309 | // kick off connectivity change broadcast for active network, since |
| 1310 | // global background policy change is radical. |
| 1311 | final int networkType = mActiveDefaultNetwork; |
| 1312 | if (isNetworkTypeValid(networkType)) { |
| 1313 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1314 | if (tracker != null) { |
| 1315 | final NetworkInfo info = tracker.getNetworkInfo(); |
| 1316 | if (info != null && info.isConnected()) { |
| 1317 | sendConnectedBroadcast(info); |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1322 | }; |
| 1323 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1324 | @Override |
| 1325 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1326 | // only someone like NPMS should only be calling us |
| 1327 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1328 | |
| 1329 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1330 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 1331 | } |
| 1332 | |
| 1333 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1334 | // TODO - handle this passing to factories |
| 1335 | // if (isNetworkTypeValid(networkType)) { |
| 1336 | // final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1337 | // if (tracker != null) { |
| 1338 | // tracker.setPolicyDataEnable(enabled); |
| 1339 | // } |
| 1340 | // } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1343 | private void enforceInternetPermission() { |
| 1344 | mContext.enforceCallingOrSelfPermission( |
| 1345 | android.Manifest.permission.INTERNET, |
| 1346 | "ConnectivityService"); |
| 1347 | } |
| 1348 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1350 | mContext.enforceCallingOrSelfPermission( |
| 1351 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1352 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1356 | mContext.enforceCallingOrSelfPermission( |
| 1357 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1358 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1361 | private void enforceTetherAccessPermission() { |
| 1362 | mContext.enforceCallingOrSelfPermission( |
| 1363 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1364 | "ConnectivityService"); |
| 1365 | } |
| 1366 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1367 | private void enforceConnectivityInternalPermission() { |
| 1368 | mContext.enforceCallingOrSelfPermission( |
| 1369 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1370 | "ConnectivityService"); |
| 1371 | } |
| 1372 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1373 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1374 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1375 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1376 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1379 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1380 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 1381 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1384 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1385 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1386 | } |
| 1387 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1388 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1389 | if (mLockdownTracker != null) { |
| 1390 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 1391 | } |
| 1392 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1393 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1394 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1395 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | if (info.isFailover()) { |
| 1397 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1398 | info.setFailover(false); |
| 1399 | } |
| 1400 | if (info.getReason() != null) { |
| 1401 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1402 | } |
| 1403 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1404 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1405 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1407 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1408 | return intent; |
| 1409 | } |
| 1410 | |
| 1411 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1412 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1413 | } |
| 1414 | |
| 1415 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 1416 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1419 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1420 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1421 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1422 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1423 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1424 | final long ident = Binder.clearCallingIdentity(); |
| 1425 | try { |
| 1426 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1427 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 1428 | } finally { |
| 1429 | Binder.restoreCallingIdentity(ident); |
| 1430 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1433 | private void sendStickyBroadcast(Intent intent) { |
| 1434 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1435 | if (!mSystemReady) { |
| 1436 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1437 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1438 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1439 | if (DBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1440 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1443 | final long ident = Binder.clearCallingIdentity(); |
| 1444 | try { |
| 1445 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 1446 | } finally { |
| 1447 | Binder.restoreCallingIdentity(ident); |
| 1448 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1452 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 1453 | if (delayMs <= 0) { |
| 1454 | sendStickyBroadcast(intent); |
| 1455 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1456 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1457 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 1458 | + intent.getAction()); |
| 1459 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1460 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1461 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 1462 | } |
| 1463 | } |
| 1464 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1465 | void systemReady() { |
Robert Greenwalt | f9b5c26 | 2014-07-27 09:41:34 -0700 | [diff] [blame] | 1466 | // start network sampling .. |
| 1467 | Intent intent = new Intent(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 1468 | intent.setPackage(mContext.getPackageName()); |
| 1469 | |
| 1470 | mSampleIntervalElapsedIntent = PendingIntent.getBroadcast(mContext, |
| 1471 | SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE, intent, 0); |
| 1472 | setAlarm(DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS * 1000, mSampleIntervalElapsedIntent); |
| 1473 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1474 | loadGlobalProxy(); |
| 1475 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1476 | synchronized(this) { |
| 1477 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1478 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1479 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1480 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1481 | } |
| 1482 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1483 | // load the global proxy at startup |
| 1484 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1485 | |
| 1486 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 1487 | // for user to unlock device. |
| 1488 | if (!updateLockdownVpn()) { |
| 1489 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 1490 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 1491 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1492 | |
| 1493 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 1494 | |
| 1495 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | } |
| 1497 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1498 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 1499 | @Override |
| 1500 | public void onReceive(Context context, Intent intent) { |
| 1501 | // Try creating lockdown tracker, since user present usually means |
| 1502 | // unlocked keystore. |
| 1503 | if (updateLockdownVpn()) { |
| 1504 | mContext.unregisterReceiver(this); |
| 1505 | } |
| 1506 | } |
| 1507 | }; |
| 1508 | |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 1509 | /** @hide */ |
| 1510 | @Override |
| 1511 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 1512 | enforceConnectivityInternalPermission(); |
| 1513 | if (DBG) log("captivePortalCheckCompleted: ni=" + info + " captive=" + isCaptivePortal); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1514 | // mNetTrackers[info.getType()].captivePortalCheckCompleted(isCaptivePortal); |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 1515 | } |
| 1516 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1517 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1518 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1519 | * |
| 1520 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1521 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1522 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1523 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1524 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1525 | |
| 1526 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1527 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1528 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1529 | if (networkAgent.networkCapabilities.hasTransport( |
| 1530 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1531 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1532 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 1533 | 5); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1534 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1535 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 1536 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1537 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1538 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1539 | 0); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1540 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1541 | } else { |
| 1542 | // do not track any other networks |
| 1543 | timeout = 0; |
| 1544 | } |
| 1545 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1546 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1547 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1548 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1549 | } catch (Exception e) { |
| 1550 | // You shall not crash! |
| 1551 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * Remove data activity tracking when network disconnects. |
| 1558 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1559 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1560 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 1561 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1562 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1563 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 1564 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1565 | try { |
| 1566 | // the call fails silently if no idletimer setup for this interface |
| 1567 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1568 | } catch (Exception e) { |
| 1569 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1575 | * Reads the network specific MTU size from reources. |
| 1576 | * and set it on it's iface. |
| 1577 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1578 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 1579 | final String iface = newLp.getInterfaceName(); |
| 1580 | final int mtu = newLp.getMtu(); |
| 1581 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 1582 | if (VDBG) log("identical MTU - not setting"); |
| 1583 | return; |
| 1584 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1585 | |
Robert Greenwalt | 4307403 | 2014-08-15 17:53:05 -0700 | [diff] [blame] | 1586 | if (LinkProperties.isValidMtu(mtu, newLp.hasGlobalIPv6Address()) == false) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1587 | loge("Unexpected mtu value: " + mtu + ", " + iface); |
| 1588 | return; |
| 1589 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1590 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 1591 | // Cannot set MTU without interface name |
| 1592 | if (TextUtils.isEmpty(iface)) { |
| 1593 | loge("Setting MTU size with null iface."); |
| 1594 | return; |
| 1595 | } |
| 1596 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1597 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1598 | if (DBG) log("Setting MTU size: " + iface + ", " + mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1599 | mNetd.setMtu(iface, mtu); |
| 1600 | } catch (Exception e) { |
| 1601 | Slog.e(TAG, "exception in setMtu()" + e); |
| 1602 | } |
| 1603 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1604 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1605 | private static final String DEFAULT_TCP_BUFFER_SIZES = "4096,87380,110208,4096,16384,110208"; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1606 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1607 | private void updateTcpBufferSizes(NetworkAgentInfo nai) { |
| 1608 | if (isDefaultNetwork(nai) == false) { |
| 1609 | return; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1612 | String tcpBufferSizes = nai.linkProperties.getTcpBufferSizes(); |
| 1613 | String[] values = null; |
| 1614 | if (tcpBufferSizes != null) { |
| 1615 | values = tcpBufferSizes.split(","); |
| 1616 | } |
| 1617 | |
| 1618 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1619 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1620 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 1621 | values = tcpBufferSizes.split(","); |
| 1622 | } |
| 1623 | |
| 1624 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 1625 | |
| 1626 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1627 | if (DBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1628 | |
| 1629 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
| 1630 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 1631 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 1632 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 1633 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 1634 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 1635 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
| 1636 | mCurrentTcpBufferSizes = tcpBufferSizes; |
| 1637 | } catch (IOException e) { |
| 1638 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1639 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1640 | |
| 1641 | final String defaultRwndKey = "net.tcp.default_init_rwnd"; |
| 1642 | int defaultRwndValue = SystemProperties.getInt(defaultRwndKey, 0); |
| 1643 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 1644 | Settings.Global.TCP_DEFAULT_INIT_RWND, defaultRwndValue); |
| 1645 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 1646 | if (rwndValue != 0) { |
| 1647 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 1648 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1651 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1652 | /* |
| 1653 | * Tell the VMs to toss their DNS caches |
| 1654 | */ |
| 1655 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 1656 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 1657 | /* |
| 1658 | * Connectivity events can happen before boot has completed ... |
| 1659 | */ |
| 1660 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1661 | final long ident = Binder.clearCallingIdentity(); |
| 1662 | try { |
| 1663 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1664 | } finally { |
| 1665 | Binder.restoreCallingIdentity(ident); |
| 1666 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1669 | @Override |
| 1670 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1671 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 1672 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 1673 | if(restoreDefaultNetworkDelayStr != null && |
| 1674 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 1675 | try { |
| 1676 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 1677 | } catch (NumberFormatException e) { |
| 1678 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1679 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 1680 | // if the system property isn't set, use the value for the apn type |
| 1681 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 1682 | |
| 1683 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 1684 | (mNetConfigs[networkType] != null)) { |
| 1685 | ret = mNetConfigs[networkType].restoreTime; |
| 1686 | } |
| 1687 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1691 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1692 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1693 | if (mContext.checkCallingOrSelfPermission( |
| 1694 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1696 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 1697 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1698 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | return; |
| 1700 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1701 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1702 | pw.println("NetworkFactories for:"); |
| 1703 | pw.increaseIndent(); |
| 1704 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 1705 | pw.println(nfi.name); |
| 1706 | } |
| 1707 | pw.decreaseIndent(); |
| 1708 | pw.println(); |
| 1709 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1710 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 1711 | pw.print("Active default network: "); |
| 1712 | if (defaultNai == null) { |
| 1713 | pw.println("none"); |
| 1714 | } else { |
| 1715 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1716 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1717 | pw.println(); |
| 1718 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1719 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1720 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1721 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1722 | pw.println(nai.toString()); |
| 1723 | pw.increaseIndent(); |
| 1724 | pw.println("Requests:"); |
| 1725 | pw.increaseIndent(); |
| 1726 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 1727 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1728 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1729 | pw.decreaseIndent(); |
| 1730 | pw.println("Lingered:"); |
| 1731 | pw.increaseIndent(); |
| 1732 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 1733 | pw.decreaseIndent(); |
| 1734 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1735 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1736 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1737 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1738 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1739 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1740 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1741 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 1742 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1743 | } |
| 1744 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1745 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1746 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 1747 | pw.print("mActiveDefaultNetwork: " + mActiveDefaultNetwork); |
| 1748 | if (mActiveDefaultNetwork != TYPE_NONE) { |
| 1749 | NetworkInfo activeNetworkInfo = getActiveNetworkInfo(); |
| 1750 | if (activeNetworkInfo != null) { |
| 1751 | pw.print(" " + activeNetworkInfo.getState() + |
| 1752 | "/" + activeNetworkInfo.getDetailedState()); |
| 1753 | } |
| 1754 | } |
| 1755 | pw.println(); |
| 1756 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1757 | pw.println("mLegacyTypeTracker:"); |
| 1758 | pw.increaseIndent(); |
| 1759 | mLegacyTypeTracker.dump(pw); |
| 1760 | pw.decreaseIndent(); |
| 1761 | pw.println(); |
| 1762 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1763 | synchronized (this) { |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1764 | pw.println("NetworkTransitionWakeLock is currently " + |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1765 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 1766 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 1767 | } |
| 1768 | pw.println(); |
| 1769 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1770 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1771 | |
| 1772 | if (mInetLog != null) { |
| 1773 | pw.println(); |
| 1774 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1775 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1776 | for(int i = 0; i < mInetLog.size(); i++) { |
| 1777 | pw.println(mInetLog.get(i)); |
| 1778 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1779 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1780 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1783 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, String msg) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1784 | if (nai.network == null) return false; |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1785 | final NetworkAgentInfo officialNai; |
| 1786 | synchronized (mNetworkForNetId) { |
| 1787 | officialNai = mNetworkForNetId.get(nai.network.netId); |
| 1788 | } |
| 1789 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 1790 | if (officialNai != null || VDBG) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1791 | loge(msg + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1792 | " - " + nai); |
| 1793 | } |
| 1794 | return false; |
| 1795 | } |
| 1796 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1797 | private boolean isRequest(NetworkRequest request) { |
| 1798 | return mNetworkRequests.get(request).isRequest; |
| 1799 | } |
| 1800 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1801 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1802 | private class NetworkStateTrackerHandler extends Handler { |
| 1803 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 1804 | super(looper); |
| 1805 | } |
| 1806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1807 | @Override |
| 1808 | public void handleMessage(Message msg) { |
| 1809 | NetworkInfo info; |
| 1810 | switch (msg.what) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1811 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1812 | handleAsyncChannelHalfConnect(msg); |
| 1813 | break; |
| 1814 | } |
| 1815 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 1816 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1817 | if (nai != null) nai.asyncChannel.disconnect(); |
| 1818 | break; |
| 1819 | } |
| 1820 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 1821 | handleAsyncChannelDisconnected(msg); |
| 1822 | break; |
| 1823 | } |
| 1824 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
| 1825 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1826 | if (nai == null) { |
| 1827 | loge("EVENT_NETWORK_CAPABILITIES_CHANGED from unknown NetworkAgent"); |
| 1828 | } else { |
| 1829 | updateCapabilities(nai, (NetworkCapabilities)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1830 | } |
| 1831 | break; |
| 1832 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1833 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
| 1834 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1835 | if (nai == null) { |
| 1836 | loge("NetworkAgent not found for EVENT_NETWORK_PROPERTIES_CHANGED"); |
| 1837 | } else { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1838 | if (VDBG) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1839 | log("Update of LinkProperties for " + nai.name() + |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1840 | "; created=" + nai.created); |
| 1841 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1842 | LinkProperties oldLp = nai.linkProperties; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1843 | synchronized (nai) { |
| 1844 | nai.linkProperties = (LinkProperties)msg.obj; |
| 1845 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 1846 | if (nai.created) updateLinkProperties(nai, oldLp); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1847 | } |
| 1848 | break; |
| 1849 | } |
| 1850 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
| 1851 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1852 | if (nai == null) { |
| 1853 | loge("EVENT_NETWORK_INFO_CHANGED from unknown NetworkAgent"); |
| 1854 | break; |
| 1855 | } |
| 1856 | info = (NetworkInfo) msg.obj; |
| 1857 | updateNetworkInfo(nai, info); |
| 1858 | break; |
| 1859 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1860 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
| 1861 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1862 | if (nai == null) { |
| 1863 | loge("EVENT_NETWORK_SCORE_CHANGED from unknown NetworkAgent"); |
| 1864 | break; |
| 1865 | } |
| 1866 | Integer score = (Integer) msg.obj; |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 1867 | if (score != null) updateNetworkScore(nai, score.intValue()); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1868 | break; |
| 1869 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1870 | case NetworkAgent.EVENT_UID_RANGES_ADDED: { |
| 1871 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1872 | if (nai == null) { |
| 1873 | loge("EVENT_UID_RANGES_ADDED from unknown NetworkAgent"); |
| 1874 | break; |
| 1875 | } |
| 1876 | try { |
| 1877 | mNetd.addVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 1878 | } catch (Exception e) { |
| 1879 | // Never crash! |
| 1880 | loge("Exception in addVpnUidRanges: " + e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 1881 | } |
| 1882 | break; |
| 1883 | } |
| 1884 | case NetworkAgent.EVENT_UID_RANGES_REMOVED: { |
| 1885 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1886 | if (nai == null) { |
| 1887 | loge("EVENT_UID_RANGES_REMOVED from unknown NetworkAgent"); |
| 1888 | break; |
| 1889 | } |
| 1890 | try { |
| 1891 | mNetd.removeVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 1892 | } catch (Exception e) { |
| 1893 | // Never crash! |
| 1894 | loge("Exception in removeVpnUidRanges: " + e); |
| 1895 | } |
| 1896 | break; |
| 1897 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 1898 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
| 1899 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1900 | if (nai == null) { |
| 1901 | loge("EVENT_SET_EXPLICITLY_SELECTED from unknown NetworkAgent"); |
| 1902 | break; |
| 1903 | } |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 1904 | if (nai.created && !nai.networkMisc.explicitlySelected) { |
| 1905 | loge("ERROR: created network explicitly selected."); |
| 1906 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 1907 | nai.networkMisc.explicitlySelected = true; |
| 1908 | break; |
| 1909 | } |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1910 | case NetworkMonitor.EVENT_NETWORK_TESTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1911 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1912 | if (isLiveNetworkAgent(nai, "EVENT_NETWORK_VALIDATED")) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1913 | boolean valid = (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID); |
| 1914 | if (valid) { |
| 1915 | if (DBG) log("Validated " + nai.name()); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 1916 | final boolean previouslyValidated = nai.validated; |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1917 | final int previousScore = nai.getCurrentScore(); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1918 | nai.validated = true; |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 1919 | rematchNetworkAndRequests(nai, !previouslyValidated); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1920 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 1921 | if (nai.getCurrentScore() != previousScore) { |
| 1922 | sendUpdatedScoreToFactories(nai); |
| 1923 | } |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1924 | } |
| 1925 | updateInetCondition(nai, valid); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 1926 | // Let the NetworkAgent know the state of its network |
| 1927 | nai.asyncChannel.sendMessage( |
| 1928 | android.net.NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
| 1929 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
| 1930 | 0, null); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1931 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1932 | break; |
| 1933 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 1934 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1935 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1936 | if (isLiveNetworkAgent(nai, "EVENT_NETWORK_LINGER_COMPLETE")) { |
| 1937 | handleLingerComplete(nai); |
| 1938 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1939 | break; |
| 1940 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 1941 | case NetworkMonitor.EVENT_PROVISIONING_NOTIFICATION: { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 1942 | if (msg.arg1 == 0) { |
| 1943 | setProvNotificationVisibleIntent(false, msg.arg2, 0, null, null); |
| 1944 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 1945 | NetworkAgentInfo nai = null; |
| 1946 | synchronized (mNetworkForNetId) { |
| 1947 | nai = mNetworkForNetId.get(msg.arg2); |
| 1948 | } |
| 1949 | if (nai == null) { |
| 1950 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 1951 | break; |
| 1952 | } |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 1953 | setProvNotificationVisibleIntent(true, msg.arg2, nai.networkInfo.getType(), |
| 1954 | nai.networkInfo.getExtraInfo(), (PendingIntent)msg.obj); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 1955 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 1956 | break; |
| 1957 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1958 | case NetworkStateTracker.EVENT_STATE_CHANGED: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1959 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 1960 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 1961 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1962 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 1963 | (state == NetworkInfo.State.DISCONNECTED) || |
| 1964 | (state == NetworkInfo.State.SUSPENDED)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1965 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1966 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 1967 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1968 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 1970 | EventLogTags.writeConnectivityStateChanged( |
| 1971 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1972 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1973 | if (info.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1974 | /** |
| 1975 | * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING |
| 1976 | * for now its an in between network, its a network that |
| 1977 | * is actually a default network but we don't want it to be |
| 1978 | * announced as such to keep background applications from |
| 1979 | * trying to use it. It turns out that some still try so we |
| 1980 | * take the additional step of clearing any default routes |
| 1981 | * to the link that may have incorrectly setup by the lower |
| 1982 | * levels. |
| 1983 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1984 | LinkProperties lp = getLinkPropertiesForTypeInternal(info.getType()); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1985 | if (DBG) { |
| 1986 | log("EVENT_STATE_CHANGED: connected to provisioning network, lp=" + lp); |
| 1987 | } |
| 1988 | |
| 1989 | // Clear any default routes setup by the radio so |
| 1990 | // any activity by applications trying to use this |
| 1991 | // connection will fail until the provisioning network |
| 1992 | // is enabled. |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1993 | /* |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1994 | for (RouteInfo r : lp.getRoutes()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1995 | removeRoute(lp, r, TO_DEFAULT_TABLE, |
| 1996 | mNetTrackers[info.getType()].getNetwork().netId); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1997 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1998 | */ |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 1999 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2000 | } else if (state == NetworkInfo.State.SUSPENDED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 2001 | } else if (state == NetworkInfo.State.CONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2002 | // handleConnect(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2003 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2004 | if (mLockdownTracker != null) { |
| 2005 | mLockdownTracker.onNetworkInfoChanged(info); |
| 2006 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2008 | } |
| 2009 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: { |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2010 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2011 | // TODO: Temporary allowing network configuration |
| 2012 | // change not resetting sockets. |
| 2013 | // @see bug/4455071 |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2014 | /* |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2015 | handleConnectivityChange(info.getType(), mCurrentLinkProperties[info.getType()], |
| 2016 | false); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2017 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2018 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2019 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2020 | } |
| 2021 | } |
| 2022 | } |
| 2023 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2024 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2025 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2026 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2027 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2028 | if (VDBG) log("NetworkFactory connected"); |
| 2029 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2030 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2031 | if (nri.isRequest == false) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2032 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2033 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2034 | (nai != null ? nai.getCurrentScore() : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2035 | } |
| 2036 | } else { |
| 2037 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2038 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2039 | } |
| 2040 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2041 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2042 | if (VDBG) log("NetworkAgent connected"); |
| 2043 | // A network agent has requested a connection. Establish the connection. |
| 2044 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2045 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2046 | } else { |
| 2047 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2048 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2049 | if (nai != null) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2050 | synchronized (mNetworkForNetId) { |
| 2051 | mNetworkForNetId.remove(nai.network.netId); |
| 2052 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2053 | // Just in case. |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2054 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2055 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2060 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2061 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2062 | if (DBG) { |
| 2063 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 2064 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2065 | // A network agent has disconnected. |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 2066 | if (nai.created) { |
| 2067 | // Tell netd to clean up the configuration for this network |
| 2068 | // (routing rules, DNS, etc). |
| 2069 | try { |
| 2070 | mNetd.removeNetwork(nai.network.netId); |
| 2071 | } catch (Exception e) { |
| 2072 | loge("Exception removing network: " + e); |
| 2073 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2074 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2075 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2076 | // because they lost all their requests or because their score isn't good) |
| 2077 | // then they would disconnect organically, report their new state and then |
| 2078 | // disconnect the channel. |
| 2079 | if (nai.networkInfo.isConnected()) { |
| 2080 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2081 | null, null); |
| 2082 | } |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2083 | if (isDefaultNetwork(nai)) { |
| 2084 | mDefaultInetConditionPublished = 0; |
| 2085 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2086 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2087 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 2088 | mNetworkAgentInfos.remove(msg.replyTo); |
| 2089 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2090 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2091 | synchronized (mNetworkForNetId) { |
| 2092 | mNetworkForNetId.remove(nai.network.netId); |
| 2093 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2094 | // Since we've lost the network, go through all the requests that |
| 2095 | // it was satisfying and see if any other factory can satisfy them. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2096 | // TODO: This logic may be better replaced with a call to rematchAllNetworksAndRequests |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2097 | final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2098 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2099 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 2100 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
| 2101 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2102 | if (DBG) { |
| 2103 | log("Checking for replacement network to handle request " + request ); |
| 2104 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2105 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2106 | sendUpdatedScoreToFactories(request, 0); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2107 | NetworkAgentInfo alternative = null; |
| 2108 | for (Map.Entry entry : mNetworkAgentInfos.entrySet()) { |
| 2109 | NetworkAgentInfo existing = (NetworkAgentInfo)entry.getValue(); |
| 2110 | if (existing.networkInfo.isConnected() && |
| 2111 | request.networkCapabilities.satisfiedByNetworkCapabilities( |
| 2112 | existing.networkCapabilities) && |
| 2113 | (alternative == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2114 | alternative.getCurrentScore() < existing.getCurrentScore())) { |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2115 | alternative = existing; |
| 2116 | } |
| 2117 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2118 | if (alternative != null) { |
| 2119 | if (DBG) log(" found replacement in " + alternative.name()); |
| 2120 | if (!toActivate.contains(alternative)) { |
| 2121 | toActivate.add(alternative); |
| 2122 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2123 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 2127 | removeDataActivityTracking(nai); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2128 | mActiveDefaultNetwork = ConnectivityManager.TYPE_NONE; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2129 | requestNetworkTransitionWakelock(nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2130 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2131 | for (NetworkAgentInfo networkToActivate : toActivate) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2132 | networkToActivate.networkLingered.clear(); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2133 | networkToActivate.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2134 | rematchNetworkAndRequests(networkToActivate, false); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2135 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2139 | private void handleRegisterNetworkRequest(Message msg) { |
| 2140 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 2141 | final NetworkCapabilities newCap = nri.request.networkCapabilities; |
| 2142 | int score = 0; |
| 2143 | |
| 2144 | // Check for the best currently alive network that satisfies this request |
| 2145 | NetworkAgentInfo bestNetwork = null; |
| 2146 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2147 | if (DBG) log("handleRegisterNetworkRequest checking " + network.name()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2148 | if (newCap.satisfiedByNetworkCapabilities(network.networkCapabilities)) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2149 | if (DBG) log("apparently satisfied. currentScore=" + network.getCurrentScore()); |
| 2150 | if ((bestNetwork == null) || |
| 2151 | bestNetwork.getCurrentScore() < network.getCurrentScore()) { |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 2152 | if (!nri.isRequest) { |
| 2153 | // Not setting bestNetwork here as a listening NetworkRequest may be |
| 2154 | // satisfied by multiple Networks. Instead the request is added to |
| 2155 | // each satisfying Network and notified about each. |
| 2156 | network.addRequest(nri.request); |
| 2157 | notifyNetworkCallback(network, nri); |
| 2158 | } else { |
| 2159 | bestNetwork = network; |
| 2160 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | if (bestNetwork != null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2165 | if (DBG) log("using " + bestNetwork.name()); |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 2166 | if (bestNetwork.networkInfo.isConnected()) { |
Paul Jensen | e585bfe | 2014-07-02 12:02:59 -0400 | [diff] [blame] | 2167 | // Cancel any lingering so the linger timeout doesn't teardown this network |
| 2168 | // even though we have a request for it. |
| 2169 | bestNetwork.networkLingered.clear(); |
| 2170 | bestNetwork.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 2171 | } |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2172 | // TODO: This logic may be better replaced with a call to rematchNetworkAndRequests |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2173 | bestNetwork.addRequest(nri.request); |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 2174 | mNetworkForRequestId.put(nri.request.requestId, bestNetwork); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2175 | notifyNetworkCallback(bestNetwork, nri); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2176 | score = bestNetwork.getCurrentScore(); |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 2177 | if (nri.request.legacyType != TYPE_NONE) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2178 | mLegacyTypeTracker.add(nri.request.legacyType, bestNetwork); |
| 2179 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2180 | } |
| 2181 | mNetworkRequests.put(nri.request, nri); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2182 | if (nri.isRequest) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2183 | if (DBG) log("sending new NetworkRequest to factories"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2184 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2185 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2186 | 0, nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2187 | } |
| 2188 | } |
| 2189 | } |
| 2190 | |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2191 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
| 2192 | NetworkRequestInfo nri = mNetworkRequests.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2193 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 2194 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2195 | if (DBG) log("Attempt to release unowned NetworkRequest " + request); |
| 2196 | return; |
| 2197 | } |
| 2198 | if (DBG) log("releasing NetworkRequest " + request); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 2199 | nri.unlinkDeathRecipient(); |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2200 | mNetworkRequests.remove(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2201 | if (nri.isRequest) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2202 | // Find all networks that are satisfying this request and remove the request |
| 2203 | // from their request lists. |
| 2204 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2205 | if (nai.networkRequests.get(nri.request.requestId) != null) { |
| 2206 | nai.networkRequests.remove(nri.request.requestId); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2207 | if (DBG) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2208 | log(" Removing from current network " + nai.name() + |
| 2209 | ", leaving " + nai.networkRequests.size() + |
| 2210 | " requests."); |
| 2211 | } |
| 2212 | // check if has any requests remaining and if not, |
| 2213 | // disconnect (unless it's a VPN). |
| 2214 | boolean keep = nai.isVPN(); |
| 2215 | for (int i = 0; i < nai.networkRequests.size() && !keep; i++) { |
| 2216 | NetworkRequest r = nai.networkRequests.valueAt(i); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2217 | if (isRequest(r)) keep = true; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2218 | } |
| 2219 | if (!keep) { |
| 2220 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
| 2221 | nai.asyncChannel.disconnect(); |
| 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | // Maintain the illusion. When this request arrived, we might have preteneded |
| 2227 | // that a network connected to serve it, even though the network was already |
| 2228 | // connected. Now that this request has gone away, we might have to pretend |
| 2229 | // that the network disconnected. LegacyTypeTracker will generate that |
| 2230 | // phatom disconnect for this type. |
| 2231 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
| 2232 | if (nai != null) { |
| 2233 | mNetworkForRequestId.remove(nri.request.requestId); |
| 2234 | if (nri.request.legacyType != TYPE_NONE) { |
| 2235 | mLegacyTypeTracker.remove(nri.request.legacyType, nai); |
| 2236 | } |
| 2237 | } |
| 2238 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2239 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2240 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 2241 | nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2242 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2243 | } else { |
| 2244 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 2245 | // if this listen request applies and remove it. |
| 2246 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2247 | nai.networkRequests.remove(nri.request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 2251 | } |
| 2252 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2253 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2254 | private class InternalHandler extends Handler { |
| 2255 | public InternalHandler(Looper looper) { |
| 2256 | super(looper); |
| 2257 | } |
| 2258 | |
| 2259 | @Override |
| 2260 | public void handleMessage(Message msg) { |
| 2261 | NetworkInfo info; |
| 2262 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2263 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2264 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2265 | String causedBy = null; |
| 2266 | synchronized (ConnectivityService.this) { |
| 2267 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 2268 | mNetTransitionWakeLock.isHeld()) { |
| 2269 | mNetTransitionWakeLock.release(); |
| 2270 | causedBy = mNetTransitionWakeLockCausedBy; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2271 | } else { |
| 2272 | break; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2273 | } |
| 2274 | } |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2275 | if (msg.what == EVENT_EXPIRE_NET_TRANSITION_WAKELOCK) { |
| 2276 | log("Failed to find a new network - expiring NetTransition Wakelock"); |
| 2277 | } else { |
| 2278 | log("NetTransition Wakelock (" + (causedBy == null ? "unknown" : causedBy) + |
| 2279 | " cleared because we found a replacement network"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2280 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 2281 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2282 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2283 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2284 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2285 | break; |
| 2286 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2287 | case EVENT_SET_DEPENDENCY_MET: { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2288 | boolean met = (msg.arg1 == ENABLED); |
| 2289 | handleSetDependencyMet(msg.arg2, met); |
| 2290 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2291 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2292 | case EVENT_SEND_STICKY_BROADCAST_INTENT: { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2293 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2294 | sendStickyBroadcast(intent); |
| 2295 | break; |
| 2296 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2297 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 2298 | final int networkType = msg.arg1; |
| 2299 | final boolean enabled = msg.arg2 == ENABLED; |
| 2300 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2301 | break; |
| 2302 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2303 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 2304 | int tag = mEnableFailFastMobileDataTag.get(); |
| 2305 | if (msg.arg1 == tag) { |
| 2306 | MobileDataStateTracker mobileDst = |
| 2307 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 2308 | if (mobileDst != null) { |
| 2309 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 2310 | } |
| 2311 | } else { |
| 2312 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 2313 | + " != tag:" + tag); |
| 2314 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2315 | break; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2316 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2317 | case EVENT_SAMPLE_INTERVAL_ELAPSED: { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2318 | handleNetworkSamplingTimeout(); |
| 2319 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2320 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2321 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2322 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2323 | break; |
| 2324 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2325 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2326 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 2327 | break; |
| 2328 | } |
| 2329 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 2330 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2331 | break; |
| 2332 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2333 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 2334 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 2335 | break; |
| 2336 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2337 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 2338 | case EVENT_REGISTER_NETWORK_LISTENER: { |
| 2339 | handleRegisterNetworkRequest(msg); |
| 2340 | break; |
| 2341 | } |
| 2342 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2343 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2344 | break; |
| 2345 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2346 | case EVENT_SYSTEM_READY: { |
| 2347 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2348 | nai.networkMonitor.systemReady = true; |
| 2349 | } |
| 2350 | break; |
| 2351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2352 | } |
| 2353 | } |
| 2354 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2355 | |
| 2356 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2357 | public int tether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2358 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2359 | if (isTetheringSupported()) { |
| 2360 | return mTethering.tether(iface); |
| 2361 | } else { |
| 2362 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2363 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2367 | public int untether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2368 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2369 | |
| 2370 | if (isTetheringSupported()) { |
| 2371 | return mTethering.untether(iface); |
| 2372 | } else { |
| 2373 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | // javadoc from interface |
| 2378 | public int getLastTetherError(String iface) { |
| 2379 | enforceTetherAccessPermission(); |
| 2380 | |
| 2381 | if (isTetheringSupported()) { |
| 2382 | return mTethering.getLastTetherError(iface); |
| 2383 | } else { |
| 2384 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2385 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2386 | } |
| 2387 | |
| 2388 | // TODO - proper iface API for selection by property, inspection, etc |
| 2389 | public String[] getTetherableUsbRegexs() { |
| 2390 | enforceTetherAccessPermission(); |
| 2391 | if (isTetheringSupported()) { |
| 2392 | return mTethering.getTetherableUsbRegexs(); |
| 2393 | } else { |
| 2394 | return new String[0]; |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | public String[] getTetherableWifiRegexs() { |
| 2399 | enforceTetherAccessPermission(); |
| 2400 | if (isTetheringSupported()) { |
| 2401 | return mTethering.getTetherableWifiRegexs(); |
| 2402 | } else { |
| 2403 | return new String[0]; |
| 2404 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2407 | public String[] getTetherableBluetoothRegexs() { |
| 2408 | enforceTetherAccessPermission(); |
| 2409 | if (isTetheringSupported()) { |
| 2410 | return mTethering.getTetherableBluetoothRegexs(); |
| 2411 | } else { |
| 2412 | return new String[0]; |
| 2413 | } |
| 2414 | } |
| 2415 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2416 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2417 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2418 | if (isTetheringSupported()) { |
| 2419 | return mTethering.setUsbTethering(enable); |
| 2420 | } else { |
| 2421 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2422 | } |
| 2423 | } |
| 2424 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2425 | // TODO - move iface listing, queries, etc to new module |
| 2426 | // javadoc from interface |
| 2427 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2428 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2429 | return mTethering.getTetherableIfaces(); |
| 2430 | } |
| 2431 | |
| 2432 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2433 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2434 | return mTethering.getTetheredIfaces(); |
| 2435 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2436 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2437 | public String[] getTetheringErroredIfaces() { |
| 2438 | enforceTetherAccessPermission(); |
| 2439 | return mTethering.getErroredIfaces(); |
| 2440 | } |
| 2441 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2442 | public String[] getTetheredDhcpRanges() { |
| 2443 | enforceConnectivityInternalPermission(); |
| 2444 | return mTethering.getTetheredDhcpRanges(); |
| 2445 | } |
| 2446 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2447 | // if ro.tether.denied = true we default to no tethering |
| 2448 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 2449 | // had previously been turned off. |
| 2450 | public boolean isTetheringSupported() { |
| 2451 | enforceTetherAccessPermission(); |
| 2452 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2453 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 2454 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0) |
| 2455 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 2456 | return tetherEnabledInSettings && ((mTethering.getTetherableUsbRegexs().length != 0 || |
| 2457 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 2458 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 2459 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2460 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2461 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 2462 | // Called when we lose the default network and have no replacement yet. |
| 2463 | // This will automatically be cleared after X seconds or a new default network |
| 2464 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 2465 | // first caller and not restarted by subsequent callers. |
| 2466 | private void requestNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2467 | int serialNum = 0; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2468 | synchronized (this) { |
| 2469 | if (mNetTransitionWakeLock.isHeld()) return; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2470 | serialNum = ++mNetTransitionWakeLockSerialNumber; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2471 | mNetTransitionWakeLock.acquire(); |
| 2472 | mNetTransitionWakeLockCausedBy = forWhom; |
| 2473 | } |
| 2474 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2475 | EVENT_EXPIRE_NET_TRANSITION_WAKELOCK, serialNum, 0), |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2476 | mNetTransitionWakeLockTimeout); |
| 2477 | return; |
| 2478 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 2479 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2480 | // 100 percent is full good, 0 is full bad. |
| 2481 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2482 | if (percentage > 50) return; // don't handle good network reports |
| 2483 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 2484 | if (nai != null) reportBadNetwork(nai.network); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2487 | public void reportBadNetwork(Network network) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2488 | enforceAccessPermission(); |
| 2489 | enforceInternetPermission(); |
| 2490 | |
| 2491 | if (network == null) return; |
| 2492 | |
| 2493 | final int uid = Binder.getCallingUid(); |
| 2494 | NetworkAgentInfo nai = null; |
| 2495 | synchronized (mNetworkForNetId) { |
| 2496 | nai = mNetworkForNetId.get(network.netId); |
| 2497 | } |
| 2498 | if (nai == null) return; |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2499 | if (DBG) log("reportBadNetwork(" + nai.name() + ") by " + uid); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2500 | synchronized (nai) { |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2501 | // Validating an uncreated network could result in a call to rematchNetworkAndRequests() |
| 2502 | // which isn't meant to work on uncreated networks. |
| 2503 | if (!nai.created) return; |
| 2504 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2505 | if (isNetworkBlocked(nai, uid)) return; |
| 2506 | |
| 2507 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid); |
| 2508 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2509 | } |
| 2510 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2511 | public ProxyInfo getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2512 | // this information is already available as a world read/writable jvm property |
| 2513 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2514 | // of proxy info to all the JVMs. |
| 2515 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2516 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2517 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2518 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 2519 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2520 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2521 | } |
| 2522 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2523 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2524 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2525 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2526 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2527 | if (proxyProperties == mGlobalProxy) return; |
| 2528 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 2529 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 2530 | |
| 2531 | String host = ""; |
| 2532 | int port = 0; |
| 2533 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2534 | String pacFileUrl = ""; |
| 2535 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2536 | (proxyProperties.getPacFileUrl() != null))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2537 | if (!proxyProperties.isValid()) { |
| 2538 | if (DBG) |
| 2539 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2540 | return; |
| 2541 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2542 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2543 | host = mGlobalProxy.getHost(); |
| 2544 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2545 | exclList = mGlobalProxy.getExclusionListAsString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2546 | if (proxyProperties.getPacFileUrl() != null) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2547 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2548 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2549 | } else { |
| 2550 | mGlobalProxy = null; |
| 2551 | } |
| 2552 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2553 | final long token = Binder.clearCallingIdentity(); |
| 2554 | try { |
| 2555 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 2556 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 2557 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 2558 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2559 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2560 | } finally { |
| 2561 | Binder.restoreCallingIdentity(token); |
| 2562 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2563 | |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 2564 | if (mGlobalProxy == null) { |
| 2565 | proxyProperties = mDefaultProxy; |
| 2566 | } |
| 2567 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2568 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2571 | private void loadGlobalProxy() { |
| 2572 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2573 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 2574 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 2575 | String exclList = Settings.Global.getString(res, |
| 2576 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2577 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 2578 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2579 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2580 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2581 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2582 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2583 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2584 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2585 | if (!proxyProperties.isValid()) { |
| 2586 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2587 | return; |
| 2588 | } |
| 2589 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2590 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2591 | mGlobalProxy = proxyProperties; |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2596 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2597 | // this information is already available as a world read/writable jvm property |
| 2598 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2599 | // of proxy info to all the JVMs. |
| 2600 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2601 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2602 | return mGlobalProxy; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2606 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2607 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2608 | && (proxy.getPacFileUrl() == null)) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2609 | proxy = null; |
| 2610 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2611 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2612 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2613 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 2614 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2615 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 2616 | return; |
| 2617 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 2618 | |
| 2619 | // This call could be coming from the PacManager, containing the port of the local |
| 2620 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 2621 | // global (to get the correct local port), and send a broadcast. |
| 2622 | // TODO: Switch PacManager to have its own message to send back rather than |
| 2623 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
| 2624 | if ((mGlobalProxy != null) && (proxy != null) && (proxy.getPacFileUrl() != null) |
| 2625 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 2626 | mGlobalProxy = proxy; |
| 2627 | sendProxyBroadcast(mGlobalProxy); |
| 2628 | return; |
| 2629 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2630 | mDefaultProxy = proxy; |
| 2631 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2632 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2633 | if (!mDefaultProxyDisabled) { |
| 2634 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2635 | } |
| 2636 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2640 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 2641 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2642 | if (!TextUtils.isEmpty(proxy)) { |
| 2643 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 2644 | if (data.length == 0) { |
| 2645 | return; |
| 2646 | } |
| 2647 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2648 | String proxyHost = data[0]; |
| 2649 | int proxyPort = 8080; |
| 2650 | if (data.length > 1) { |
| 2651 | try { |
| 2652 | proxyPort = Integer.parseInt(data[1]); |
| 2653 | } catch (NumberFormatException e) { |
| 2654 | return; |
| 2655 | } |
| 2656 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2657 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2658 | setGlobalProxy(p); |
| 2659 | } |
| 2660 | } |
| 2661 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2662 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 2663 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 2664 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2665 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2666 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 2667 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 2668 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2669 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2670 | final long ident = Binder.clearCallingIdentity(); |
| 2671 | try { |
| 2672 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2673 | } finally { |
| 2674 | Binder.restoreCallingIdentity(ident); |
| 2675 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | private static class SettingsObserver extends ContentObserver { |
| 2679 | private int mWhat; |
| 2680 | private Handler mHandler; |
| 2681 | SettingsObserver(Handler handler, int what) { |
| 2682 | super(handler); |
| 2683 | mHandler = handler; |
| 2684 | mWhat = what; |
| 2685 | } |
| 2686 | |
| 2687 | void observe(Context context) { |
| 2688 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2689 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 2690 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2691 | } |
| 2692 | |
| 2693 | @Override |
| 2694 | public void onChange(boolean selfChange) { |
| 2695 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 2696 | } |
| 2697 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2698 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2699 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2700 | Slog.d(TAG, s); |
| 2701 | } |
| 2702 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2703 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2704 | Slog.e(TAG, s); |
| 2705 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2706 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 2707 | int convertFeatureToNetworkType(int networkType, String feature) { |
| 2708 | int usedNetworkType = networkType; |
| 2709 | |
| 2710 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 2711 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 2712 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 2713 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 2714 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 2715 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) || |
| 2716 | TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 2717 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 2718 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 2719 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 2720 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) { |
| 2721 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA; |
| 2722 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) { |
| 2723 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS; |
| 2724 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) { |
| 2725 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 2726 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_EMERGENCY)) { |
| 2727 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_EMERGENCY; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 2728 | } else { |
| 2729 | Slog.e(TAG, "Can't match any mobile netTracker!"); |
| 2730 | } |
| 2731 | } else if (networkType == ConnectivityManager.TYPE_WIFI) { |
| 2732 | if (TextUtils.equals(feature, "p2p")) { |
| 2733 | usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P; |
| 2734 | } else { |
| 2735 | Slog.e(TAG, "Can't match any wifi netTracker!"); |
| 2736 | } |
| 2737 | } else { |
| 2738 | Slog.e(TAG, "Unexpected network type"); |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 2739 | } |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 2740 | return usedNetworkType; |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 2741 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2742 | |
| 2743 | private static <T> T checkNotNull(T value, String message) { |
| 2744 | if (value == null) { |
| 2745 | throw new NullPointerException(message); |
| 2746 | } |
| 2747 | return value; |
| 2748 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2749 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2750 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2751 | * Prepare for a VPN application. This method is used by VpnDialogs |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2752 | * and not available in ConnectivityManager. Permissions are checked |
| 2753 | * in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2754 | * @hide |
| 2755 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2756 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 2757 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2758 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2759 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2760 | synchronized(mVpns) { |
| 2761 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 2762 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2763 | } |
| 2764 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2765 | /** |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 2766 | * Set whether the current VPN package has the ability to launch VPNs without |
| 2767 | * user intervention. This method is used by system UIs and not available |
| 2768 | * in ConnectivityManager. Permissions are checked in Vpn class. |
| 2769 | * @hide |
| 2770 | */ |
| 2771 | @Override |
| 2772 | public void setVpnPackageAuthorization(boolean authorized) { |
| 2773 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2774 | synchronized(mVpns) { |
| 2775 | mVpns.get(user).setPackageAuthorization(authorized); |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2780 | * Configure a TUN interface and return its file descriptor. Parameters |
| 2781 | * 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] | 2782 | * and not available in ConnectivityManager. Permissions are checked in |
| 2783 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2784 | * @hide |
| 2785 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2786 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 2787 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2788 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2789 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2790 | synchronized(mVpns) { |
| 2791 | return mVpns.get(user).establish(config); |
| 2792 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2795 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2796 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 2797 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2798 | */ |
| 2799 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2800 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2801 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 2802 | final LinkProperties egress = getActiveLinkProperties(); |
| 2803 | if (egress == null) { |
| 2804 | throw new IllegalStateException("Missing active network connection"); |
| 2805 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2806 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2807 | synchronized(mVpns) { |
| 2808 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 2809 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | /** |
| 2813 | * Return the information of the ongoing legacy VPN. This method is used |
| 2814 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 2815 | * are checked in Vpn class. |
| 2816 | * @hide |
| 2817 | */ |
| 2818 | @Override |
| 2819 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2820 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2821 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2822 | synchronized(mVpns) { |
| 2823 | return mVpns.get(user).getLegacyVpnInfo(); |
| 2824 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 2827 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 2828 | * Returns the information of the ongoing VPN. This method is used by VpnDialogs and |
| 2829 | * not available in ConnectivityManager. |
| 2830 | * Permissions are checked in Vpn class. |
| 2831 | * @hide |
| 2832 | */ |
| 2833 | @Override |
| 2834 | public VpnConfig getVpnConfig() { |
| 2835 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2836 | synchronized(mVpns) { |
| 2837 | return mVpns.get(user).getVpnConfig(); |
| 2838 | } |
| 2839 | } |
| 2840 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2841 | @Override |
| 2842 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 2843 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 2844 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 2845 | return false; |
| 2846 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2847 | |
| 2848 | // Tear down existing lockdown if profile was removed |
| 2849 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 2850 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 2851 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2852 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 2853 | return false; |
| 2854 | } |
| 2855 | |
| 2856 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 2857 | final VpnProfile profile = VpnProfile.decode( |
| 2858 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2859 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 2860 | synchronized(mVpns) { |
| 2861 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 2862 | profile)); |
| 2863 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2864 | } else { |
| 2865 | setLockdownTracker(null); |
| 2866 | } |
| 2867 | |
| 2868 | return true; |
| 2869 | } |
| 2870 | |
| 2871 | /** |
| 2872 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 2873 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 2874 | */ |
| 2875 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 2876 | // Shutdown any existing tracker |
| 2877 | final LockdownVpnTracker existing = mLockdownTracker; |
| 2878 | mLockdownTracker = null; |
| 2879 | if (existing != null) { |
| 2880 | existing.shutdown(); |
| 2881 | } |
| 2882 | |
| 2883 | try { |
| 2884 | if (tracker != null) { |
| 2885 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 2886 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2887 | mLockdownTracker = tracker; |
| 2888 | mLockdownTracker.init(); |
| 2889 | } else { |
| 2890 | mNetd.setFirewallEnabled(false); |
| 2891 | } |
| 2892 | } catch (RemoteException e) { |
| 2893 | // ignored; NMS lives inside system_server |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 | private void throwIfLockdownEnabled() { |
| 2898 | if (mLockdownEnabled) { |
| 2899 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 2900 | } |
| 2901 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 2902 | |
| 2903 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 2904 | enforceConnectivityInternalPermission(); |
| 2905 | |
| 2906 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 2907 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 2908 | } |
| 2909 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 2910 | |
| 2911 | public int findConnectionTypeForIface(String iface) { |
| 2912 | enforceConnectivityInternalPermission(); |
| 2913 | |
| 2914 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
Lorenzo Colitti | 1c3a35b | 2014-08-19 21:31:08 -0700 | [diff] [blame] | 2915 | |
| 2916 | synchronized(mNetworkForNetId) { |
| 2917 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
| 2918 | NetworkAgentInfo nai = mNetworkForNetId.valueAt(i); |
| 2919 | LinkProperties lp = nai.linkProperties; |
| 2920 | if (lp != null && iface.equals(lp.getInterfaceName()) && nai.networkInfo != null) { |
| 2921 | return nai.networkInfo.getType(); |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | return ConnectivityManager.TYPE_NONE; |
| 2926 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2927 | |
| 2928 | /** |
| 2929 | * Have mobile data fail fast if enabled. |
| 2930 | * |
| 2931 | * @param enabled DctConstants.ENABLED/DISABLED |
| 2932 | */ |
| 2933 | private void setEnableFailFastMobileData(int enabled) { |
| 2934 | int tag; |
| 2935 | |
| 2936 | if (enabled == DctConstants.ENABLED) { |
| 2937 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 2938 | } else { |
| 2939 | tag = mEnableFailFastMobileDataTag.get(); |
| 2940 | } |
| 2941 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 2942 | enabled)); |
| 2943 | } |
| 2944 | |
| 2945 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2946 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2947 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 2948 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 2952 | private volatile boolean mIsNotificationVisible = false; |
| 2953 | |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2954 | private void setProvNotificationVisible(boolean visible, int networkType, String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2955 | if (DBG) { |
| 2956 | log("setProvNotificationVisible: E visible=" + visible + " networkType=" + networkType |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2957 | + " action=" + action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2958 | } |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2959 | Intent intent = new Intent(action); |
| 2960 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2961 | // Concatenate the range of types onto the range of NetIDs. |
| 2962 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2963 | setProvNotificationVisibleIntent(visible, id, networkType, null, pendingIntent); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2964 | } |
| 2965 | |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2966 | /** |
| 2967 | * Show or hide network provisioning notificaitons. |
| 2968 | * |
| 2969 | * @param id an identifier that uniquely identifies this notification. This must match |
| 2970 | * between show and hide calls. We use the NetID value but for legacy callers |
| 2971 | * we concatenate the range of types with the range of NetIDs. |
| 2972 | */ |
| 2973 | private void setProvNotificationVisibleIntent(boolean visible, int id, int networkType, |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2974 | String extraInfo, PendingIntent intent) { |
| 2975 | if (DBG) { |
| 2976 | log("setProvNotificationVisibleIntent: E visible=" + visible + " networkType=" + |
| 2977 | networkType + " extraInfo=" + extraInfo); |
| 2978 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2979 | |
| 2980 | Resources r = Resources.getSystem(); |
| 2981 | NotificationManager notificationManager = (NotificationManager) mContext |
| 2982 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 2983 | |
| 2984 | if (visible) { |
| 2985 | CharSequence title; |
| 2986 | CharSequence details; |
| 2987 | int icon; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2988 | Notification notification = new Notification(); |
| 2989 | switch (networkType) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2990 | case ConnectivityManager.TYPE_WIFI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2991 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 2992 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2993 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2994 | icon = R.drawable.stat_notify_wifi_in_range; |
| 2995 | break; |
| 2996 | case ConnectivityManager.TYPE_MOBILE: |
| 2997 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2998 | title = r.getString(R.string.network_available_sign_in, 0); |
| 2999 | // TODO: Change this to pull from NetworkInfo once a printable |
| 3000 | // name has been added to it |
| 3001 | details = mTelephonyManager.getNetworkOperatorName(); |
| 3002 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3003 | break; |
| 3004 | default: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3005 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3006 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3007 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3008 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3009 | break; |
| 3010 | } |
| 3011 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3012 | notification.when = 0; |
| 3013 | notification.icon = icon; |
| 3014 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3015 | notification.tickerText = title; |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 3016 | notification.color = mContext.getResources().getColor( |
| 3017 | com.android.internal.R.color.system_notification_accent_color); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3018 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3019 | notification.contentIntent = intent; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3020 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3021 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3022 | notificationManager.notify(NOTIFICATION_ID, id, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3023 | } catch (NullPointerException npe) { |
| 3024 | loge("setNotificaitionVisible: visible notificationManager npe=" + npe); |
| 3025 | npe.printStackTrace(); |
| 3026 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3027 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3028 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3029 | notificationManager.cancel(NOTIFICATION_ID, id); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3030 | } catch (NullPointerException npe) { |
| 3031 | loge("setNotificaitionVisible: cancel notificationManager npe=" + npe); |
| 3032 | npe.printStackTrace(); |
| 3033 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3034 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3035 | mIsNotificationVisible = visible; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3038 | /** Location to an updatable file listing carrier provisioning urls. |
| 3039 | * An example: |
| 3040 | * |
| 3041 | * <?xml version="1.0" encoding="utf-8"?> |
| 3042 | * <provisioningUrls> |
| 3043 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
| 3044 | * <redirectedUrl mcc="310" mnc="4">http://www.google.com</redirectedUrl> |
| 3045 | * </provisioningUrls> |
| 3046 | */ |
| 3047 | private static final String PROVISIONING_URL_PATH = |
| 3048 | "/data/misc/radio/provisioning_urls.xml"; |
| 3049 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3050 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3051 | /** XML tag for root element. */ |
| 3052 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 3053 | /** XML tag for individual url */ |
| 3054 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
| 3055 | /** XML tag for redirected url */ |
| 3056 | private static final String TAG_REDIRECTED_URL = "redirectedUrl"; |
| 3057 | /** XML attribute for mcc */ |
| 3058 | private static final String ATTR_MCC = "mcc"; |
| 3059 | /** XML attribute for mnc */ |
| 3060 | private static final String ATTR_MNC = "mnc"; |
| 3061 | |
| 3062 | private static final int REDIRECTED_PROVISIONING = 1; |
| 3063 | private static final int PROVISIONING = 2; |
| 3064 | |
| 3065 | private String getProvisioningUrlBaseFromFile(int type) { |
| 3066 | FileReader fileReader = null; |
| 3067 | XmlPullParser parser = null; |
| 3068 | Configuration config = mContext.getResources().getConfiguration(); |
| 3069 | String tagType; |
| 3070 | |
| 3071 | switch (type) { |
| 3072 | case PROVISIONING: |
| 3073 | tagType = TAG_PROVISIONING_URL; |
| 3074 | break; |
| 3075 | case REDIRECTED_PROVISIONING: |
| 3076 | tagType = TAG_REDIRECTED_URL; |
| 3077 | break; |
| 3078 | default: |
| 3079 | throw new RuntimeException("getProvisioningUrlBaseFromFile: Unexpected parameter " + |
| 3080 | type); |
| 3081 | } |
| 3082 | |
| 3083 | try { |
| 3084 | fileReader = new FileReader(mProvisioningUrlFile); |
| 3085 | parser = Xml.newPullParser(); |
| 3086 | parser.setInput(fileReader); |
| 3087 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 3088 | |
| 3089 | while (true) { |
| 3090 | XmlUtils.nextElement(parser); |
| 3091 | |
| 3092 | String element = parser.getName(); |
| 3093 | if (element == null) break; |
| 3094 | |
| 3095 | if (element.equals(tagType)) { |
| 3096 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 3097 | try { |
| 3098 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 3099 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 3100 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 3101 | parser.next(); |
| 3102 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 3103 | return parser.getText(); |
| 3104 | } |
| 3105 | } |
| 3106 | } |
| 3107 | } catch (NumberFormatException e) { |
| 3108 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 3109 | } |
| 3110 | } |
| 3111 | } |
| 3112 | return null; |
| 3113 | } catch (FileNotFoundException e) { |
| 3114 | loge("Carrier Provisioning Urls file not found"); |
| 3115 | } catch (XmlPullParserException e) { |
| 3116 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 3117 | } catch (IOException e) { |
| 3118 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 3119 | } finally { |
| 3120 | if (fileReader != null) { |
| 3121 | try { |
| 3122 | fileReader.close(); |
| 3123 | } catch (IOException e) {} |
| 3124 | } |
| 3125 | } |
| 3126 | return null; |
| 3127 | } |
| 3128 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3129 | @Override |
| 3130 | public String getMobileRedirectedProvisioningUrl() { |
| 3131 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3132 | String url = getProvisioningUrlBaseFromFile(REDIRECTED_PROVISIONING); |
| 3133 | if (TextUtils.isEmpty(url)) { |
| 3134 | url = mContext.getResources().getString(R.string.mobile_redirected_provisioning_url); |
| 3135 | } |
| 3136 | return url; |
| 3137 | } |
| 3138 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3139 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3140 | public String getMobileProvisioningUrl() { |
| 3141 | enforceConnectivityInternalPermission(); |
| 3142 | String url = getProvisioningUrlBaseFromFile(PROVISIONING); |
| 3143 | if (TextUtils.isEmpty(url)) { |
| 3144 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3145 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3146 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3147 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3148 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3149 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3150 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3151 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 3152 | if (TextUtils.isEmpty(phoneNumber)) { |
| 3153 | phoneNumber = "0000000000"; |
| 3154 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3155 | url = String.format(url, |
| 3156 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 3157 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3158 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3161 | return url; |
| 3162 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3163 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3164 | @Override |
| 3165 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3166 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3167 | enforceConnectivityInternalPermission(); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3168 | final long ident = Binder.clearCallingIdentity(); |
| 3169 | try { |
| 3170 | setProvNotificationVisible(visible, networkType, action); |
| 3171 | } finally { |
| 3172 | Binder.restoreCallingIdentity(ident); |
| 3173 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3174 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 3175 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3176 | @Override |
| 3177 | public void setAirplaneMode(boolean enable) { |
| 3178 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3179 | final long ident = Binder.clearCallingIdentity(); |
| 3180 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 3181 | final ContentResolver cr = mContext.getContentResolver(); |
| 3182 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 3183 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 3184 | intent.putExtra("state", enable); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3185 | mContext.sendBroadcast(intent); |
| 3186 | } finally { |
| 3187 | Binder.restoreCallingIdentity(ident); |
| 3188 | } |
| 3189 | } |
| 3190 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3191 | private void onUserStart(int userId) { |
| 3192 | synchronized(mVpns) { |
| 3193 | Vpn userVpn = mVpns.get(userId); |
| 3194 | if (userVpn != null) { |
| 3195 | loge("Starting user already has a VPN"); |
| 3196 | return; |
| 3197 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 3198 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, this, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3199 | mVpns.put(userId, userVpn); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | private void onUserStop(int userId) { |
| 3204 | synchronized(mVpns) { |
| 3205 | Vpn userVpn = mVpns.get(userId); |
| 3206 | if (userVpn == null) { |
| 3207 | loge("Stopping user has no VPN"); |
| 3208 | return; |
| 3209 | } |
| 3210 | mVpns.delete(userId); |
| 3211 | } |
| 3212 | } |
| 3213 | |
| 3214 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 3215 | @Override |
| 3216 | public void onReceive(Context context, Intent intent) { |
| 3217 | final String action = intent.getAction(); |
| 3218 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 3219 | if (userId == UserHandle.USER_NULL) return; |
| 3220 | |
| 3221 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 3222 | onUserStart(userId); |
| 3223 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 3224 | onUserStop(userId); |
| 3225 | } |
| 3226 | } |
| 3227 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3228 | |
| 3229 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3230 | public LinkQualityInfo getLinkQualityInfo(int networkType) { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3231 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3232 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3233 | return mNetTrackers[networkType].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3234 | } else { |
| 3235 | return null; |
| 3236 | } |
| 3237 | } |
| 3238 | |
| 3239 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3240 | public LinkQualityInfo getActiveLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3241 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3242 | if (isNetworkTypeValid(mActiveDefaultNetwork) && |
| 3243 | mNetTrackers[mActiveDefaultNetwork] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3244 | return mNetTrackers[mActiveDefaultNetwork].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3245 | } else { |
| 3246 | return null; |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3251 | public LinkQualityInfo[] getAllLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3252 | enforceAccessPermission(); |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3253 | final ArrayList<LinkQualityInfo> result = Lists.newArrayList(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3254 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3255 | if (tracker != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3256 | LinkQualityInfo li = tracker.getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3257 | if (li != null) { |
| 3258 | result.add(li); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 3263 | return result.toArray(new LinkQualityInfo[result.size()]); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /* Infrastructure for network sampling */ |
| 3267 | |
| 3268 | private void handleNetworkSamplingTimeout() { |
| 3269 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3270 | if (SAMPLE_DBG) log("Sampling interval elapsed, updating statistics .."); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3271 | |
| 3272 | // initialize list of interfaces .. |
| 3273 | Map<String, SamplingDataTracker.SamplingSnapshot> mapIfaceToSample = |
| 3274 | new HashMap<String, SamplingDataTracker.SamplingSnapshot>(); |
| 3275 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3276 | if (tracker != null) { |
| 3277 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 3278 | if (ifaceName != null) { |
| 3279 | mapIfaceToSample.put(ifaceName, null); |
| 3280 | } |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | // Read samples for all interfaces |
| 3285 | SamplingDataTracker.getSamplingSnapshots(mapIfaceToSample); |
| 3286 | |
| 3287 | // process samples for all networks |
| 3288 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 3289 | if (tracker != null) { |
| 3290 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 3291 | SamplingDataTracker.SamplingSnapshot ss = mapIfaceToSample.get(ifaceName); |
| 3292 | if (ss != null) { |
| 3293 | // end the previous sampling cycle |
| 3294 | tracker.stopSampling(ss); |
| 3295 | // start a new sampling cycle .. |
| 3296 | tracker.startSampling(ss); |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3301 | if (SAMPLE_DBG) log("Done."); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3302 | |
| 3303 | int samplingIntervalInSeconds = Settings.Global.getInt(mContext.getContentResolver(), |
| 3304 | Settings.Global.CONNECTIVITY_SAMPLING_INTERVAL_IN_SECONDS, |
| 3305 | DEFAULT_SAMPLING_INTERVAL_IN_SECONDS); |
| 3306 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3307 | if (SAMPLE_DBG) { |
| 3308 | log("Setting timer for " + String.valueOf(samplingIntervalInSeconds) + "seconds"); |
| 3309 | } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3310 | |
| 3311 | setAlarm(samplingIntervalInSeconds * 1000, mSampleIntervalElapsedIntent); |
| 3312 | } |
| 3313 | |
Aaron Whyte | f5e6747 | 2014-07-10 14:05:35 -0700 | [diff] [blame] | 3314 | /** |
| 3315 | * Sets a network sampling alarm. |
| 3316 | */ |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3317 | void setAlarm(int timeoutInMilliseconds, PendingIntent intent) { |
| 3318 | long wakeupTime = SystemClock.elapsedRealtime() + timeoutInMilliseconds; |
Aaron Whyte | f5e6747 | 2014-07-10 14:05:35 -0700 | [diff] [blame] | 3319 | int alarmType; |
| 3320 | if (Resources.getSystem().getBoolean( |
| 3321 | R.bool.config_networkSamplingWakesDevice)) { |
| 3322 | alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; |
| 3323 | } else { |
| 3324 | alarmType = AlarmManager.ELAPSED_REALTIME; |
| 3325 | } |
| 3326 | mAlarmManager.set(alarmType, wakeupTime, intent); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3327 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3328 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3329 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 3330 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3331 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 3332 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3333 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3334 | private static class NetworkFactoryInfo { |
| 3335 | public final String name; |
| 3336 | public final Messenger messenger; |
| 3337 | public final AsyncChannel asyncChannel; |
| 3338 | |
| 3339 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 3340 | this.name = name; |
| 3341 | this.messenger = messenger; |
| 3342 | this.asyncChannel = asyncChannel; |
| 3343 | } |
| 3344 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3345 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3346 | /** |
| 3347 | * Tracks info about the requester. |
| 3348 | * Also used to notice when the calling process dies so we can self-expire |
| 3349 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3350 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
| 3351 | static final boolean REQUEST = true; |
| 3352 | static final boolean LISTEN = false; |
| 3353 | |
| 3354 | final NetworkRequest request; |
| 3355 | IBinder mBinder; |
| 3356 | final int mPid; |
| 3357 | final int mUid; |
| 3358 | final Messenger messenger; |
| 3359 | final boolean isRequest; |
| 3360 | |
| 3361 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, boolean isRequest) { |
| 3362 | super(); |
| 3363 | messenger = m; |
| 3364 | request = r; |
| 3365 | mBinder = binder; |
| 3366 | mPid = getCallingPid(); |
| 3367 | mUid = getCallingUid(); |
| 3368 | this.isRequest = isRequest; |
| 3369 | |
| 3370 | try { |
| 3371 | mBinder.linkToDeath(this, 0); |
| 3372 | } catch (RemoteException e) { |
| 3373 | binderDied(); |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | void unlinkDeathRecipient() { |
| 3378 | mBinder.unlinkToDeath(this, 0); |
| 3379 | } |
| 3380 | |
| 3381 | public void binderDied() { |
| 3382 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 3383 | request + ", " + mBinder + ")"); |
| 3384 | releaseNetworkRequest(request); |
| 3385 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3386 | |
| 3387 | public String toString() { |
| 3388 | return (isRequest ? "Request" : "Listen") + " from uid/pid:" + mUid + "/" + |
| 3389 | mPid + " for " + request; |
| 3390 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | @Override |
| 3394 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3395 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3396 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 3397 | == false) { |
| 3398 | enforceConnectivityInternalPermission(); |
| 3399 | } else { |
| 3400 | enforceChangePermission(); |
| 3401 | } |
| 3402 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3403 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 3404 | |
| 3405 | // if UID is restricted, don't allow them to bring up metered APNs |
| 3406 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) |
| 3407 | == false) { |
| 3408 | final int uidRules; |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 3409 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3410 | synchronized(mRulesLock) { |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 3411 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3412 | } |
| 3413 | if ((uidRules & RULE_REJECT_METERED) != 0) { |
| 3414 | // we could silently fail or we can filter the available nets to only give |
| 3415 | // them those they have access to. Chose the more useful |
| 3416 | networkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 3417 | } |
| 3418 | } |
| 3419 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3420 | if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3421 | throw new IllegalArgumentException("Bad timeout specified"); |
| 3422 | } |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3423 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
| 3424 | nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3425 | if (DBG) log("requestNetwork for " + networkRequest); |
| 3426 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 3427 | NetworkRequestInfo.REQUEST); |
| 3428 | |
| 3429 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3430 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3431 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3432 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3433 | } |
| 3434 | return networkRequest; |
| 3435 | } |
| 3436 | |
| 3437 | @Override |
| 3438 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 3439 | PendingIntent operation) { |
| 3440 | // TODO |
| 3441 | return null; |
| 3442 | } |
| 3443 | |
| 3444 | @Override |
| 3445 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 3446 | Messenger messenger, IBinder binder) { |
| 3447 | enforceAccessPermission(); |
| 3448 | |
| 3449 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3450 | networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3451 | if (DBG) log("listenForNetwork for " + networkRequest); |
| 3452 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 3453 | NetworkRequestInfo.LISTEN); |
| 3454 | |
| 3455 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 3456 | return networkRequest; |
| 3457 | } |
| 3458 | |
| 3459 | @Override |
| 3460 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 3461 | PendingIntent operation) { |
| 3462 | } |
| 3463 | |
| 3464 | @Override |
| 3465 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3466 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), |
| 3467 | 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3468 | } |
| 3469 | |
| 3470 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3471 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3472 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3473 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 3474 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3477 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3478 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3479 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 3480 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 3481 | } |
| 3482 | |
| 3483 | @Override |
| 3484 | public void unregisterNetworkFactory(Messenger messenger) { |
| 3485 | enforceConnectivityInternalPermission(); |
| 3486 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 3487 | } |
| 3488 | |
| 3489 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 3490 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 3491 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3492 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3493 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3494 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3495 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3496 | } |
| 3497 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3498 | /** |
| 3499 | * NetworkAgentInfo supporting a request by requestId. |
| 3500 | * These have already been vetted (their Capabilities satisfy the request) |
| 3501 | * and the are the highest scored network available. |
| 3502 | * the are keyed off the Requests requestId. |
| 3503 | */ |
| 3504 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 3505 | new SparseArray<NetworkAgentInfo>(); |
| 3506 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3507 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 3508 | new SparseArray<NetworkAgentInfo>(); |
| 3509 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3510 | // NetworkAgentInfo keyed off its connecting messenger |
| 3511 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
| 3512 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 3513 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 3514 | |
| 3515 | private final NetworkRequest mDefaultRequest; |
| 3516 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 3517 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
| 3518 | return mNetworkForRequestId.get(mDefaultRequest.requestId) == nai; |
| 3519 | } |
| 3520 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3521 | public void registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 3522 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 3523 | int currentScore, NetworkMisc networkMisc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3524 | enforceConnectivityInternalPermission(); |
| 3525 | |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 3526 | NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3527 | new NetworkInfo(networkInfo), new LinkProperties(linkProperties), |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 3528 | new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler, |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 3529 | new NetworkMisc(networkMisc)); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3530 | synchronized (this) { |
| 3531 | nai.networkMonitor.systemReady = mSystemReady; |
| 3532 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3533 | if (DBG) log("registerNetworkAgent " + nai); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3534 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
| 3535 | } |
| 3536 | |
| 3537 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 3538 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 3539 | mNetworkAgentInfos.put(na.messenger, na); |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 3540 | assignNextNetId(na); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3541 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 3542 | NetworkInfo networkInfo = na.networkInfo; |
| 3543 | na.networkInfo = null; |
| 3544 | updateNetworkInfo(na, networkInfo); |
| 3545 | } |
| 3546 | |
| 3547 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 3548 | LinkProperties newLp = networkAgent.linkProperties; |
| 3549 | int netId = networkAgent.network.netId; |
| 3550 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3551 | // The NetworkAgentInfo does not know whether clatd is running on its network or not. Before |
| 3552 | // we do anything else, make sure its LinkProperties are accurate. |
| 3553 | mClat.fixupLinkProperties(networkAgent, oldLp); |
| 3554 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3555 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3556 | updateMtu(newLp, oldLp); |
| 3557 | // TODO - figure out what to do for clat |
| 3558 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 3559 | // updateMtu(lp, null); |
| 3560 | // } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3561 | updateTcpBufferSizes(networkAgent); |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3562 | final boolean flushDns = updateRoutes(newLp, oldLp, netId); |
| 3563 | updateDnses(newLp, oldLp, netId, flushDns); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3564 | updateClat(newLp, oldLp, networkAgent); |
Robert Greenwalt | b98c598 | 2014-08-23 09:51:35 -0700 | [diff] [blame] | 3565 | if (isDefaultNetwork(networkAgent)) handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3566 | // TODO - move this check to cover the whole function |
| 3567 | if (!Objects.equals(newLp, oldLp)) { |
| 3568 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 3569 | } |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo na) { |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3573 | final boolean wasRunningClat = mClat.isRunningClat(na); |
| 3574 | final boolean shouldRunClat = Nat464Xlat.requiresClat(na); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3575 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 3576 | if (!wasRunningClat && shouldRunClat) { |
| 3577 | // Start clatd. If it's already been started but is not running yet, this is a no-op. |
| 3578 | mClat.startClat(na); |
| 3579 | } else if (wasRunningClat && !shouldRunClat) { |
| 3580 | mClat.stopClat(); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3581 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3582 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3583 | |
| 3584 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 3585 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 3586 | if (oldLp != null) { |
| 3587 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 3588 | } else if (newLp != null) { |
| 3589 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 3590 | } |
| 3591 | for (String iface : interfaceDiff.added) { |
| 3592 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3593 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3594 | mNetd.addInterfaceToNetwork(iface, netId); |
| 3595 | } catch (Exception e) { |
| 3596 | loge("Exception adding interface: " + e); |
| 3597 | } |
| 3598 | } |
| 3599 | for (String iface : interfaceDiff.removed) { |
| 3600 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3601 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 3602 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 3603 | } catch (Exception e) { |
| 3604 | loge("Exception removing interface: " + e); |
| 3605 | } |
| 3606 | } |
| 3607 | } |
| 3608 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3609 | /** |
| 3610 | * Have netd update routes from oldLp to newLp. |
| 3611 | * @return true if routes changed between oldLp and newLp |
| 3612 | */ |
| 3613 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3614 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 3615 | if (oldLp != null) { |
| 3616 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 3617 | } else if (newLp != null) { |
| 3618 | routeDiff.added = newLp.getAllRoutes(); |
| 3619 | } |
| 3620 | |
| 3621 | // add routes before removing old in case it helps with continuous connectivity |
| 3622 | |
| 3623 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 3624 | for (RouteInfo route : routeDiff.added) { |
| 3625 | if (route.hasGateway()) continue; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3626 | if (DBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3627 | try { |
| 3628 | mNetd.addRoute(netId, route); |
| 3629 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3630 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 3631 | loge("Exception in addRoute for non-gateway: " + e); |
| 3632 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3633 | } |
| 3634 | } |
| 3635 | for (RouteInfo route : routeDiff.added) { |
| 3636 | if (route.hasGateway() == false) continue; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3637 | if (DBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3638 | try { |
| 3639 | mNetd.addRoute(netId, route); |
| 3640 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3641 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 3642 | loge("Exception in addRoute for gateway: " + e); |
| 3643 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3644 | } |
| 3645 | } |
| 3646 | |
| 3647 | for (RouteInfo route : routeDiff.removed) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3648 | if (DBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3649 | try { |
| 3650 | mNetd.removeRoute(netId, route); |
| 3651 | } catch (Exception e) { |
| 3652 | loge("Exception in removeRoute: " + e); |
| 3653 | } |
| 3654 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3655 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3656 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3657 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId, boolean flush) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3658 | if (oldLp == null || (newLp.isIdenticalDnses(oldLp) == false)) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 3659 | Collection<InetAddress> dnses = newLp.getDnsServers(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3660 | if (dnses.size() == 0 && mDefaultDns != null) { |
| 3661 | dnses = new ArrayList(); |
| 3662 | dnses.add(mDefaultDns); |
| 3663 | if (DBG) { |
| 3664 | loge("no dns provided for netId " + netId + ", so using defaults"); |
| 3665 | } |
| 3666 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3667 | if (DBG) log("Setting Dns servers for network " + netId + " to " + dnses); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3668 | try { |
| 3669 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), |
| 3670 | newLp.getDomains()); |
| 3671 | } catch (Exception e) { |
| 3672 | loge("Exception in setDnsServersForNetwork: " + e); |
| 3673 | } |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3674 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 3675 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 3676 | setDefaultDnsSystemProperties(dnses); |
| 3677 | } |
Robert Greenwalt | 5c1c832a8 | 2014-07-28 16:32:19 -0700 | [diff] [blame] | 3678 | flushVmDnsCache(); |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 3679 | } else if (flush) { |
| 3680 | try { |
| 3681 | mNetd.flushNetworkDnsCache(netId); |
| 3682 | } catch (Exception e) { |
| 3683 | loge("Exception in flushNetworkDnsCache: " + e); |
| 3684 | } |
| 3685 | flushVmDnsCache(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3689 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 3690 | int last = 0; |
| 3691 | for (InetAddress dns : dnses) { |
| 3692 | ++last; |
| 3693 | String key = "net.dns" + last; |
| 3694 | String value = dns.getHostAddress(); |
| 3695 | SystemProperties.set(key, value); |
| 3696 | } |
| 3697 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 3698 | String key = "net.dns" + i; |
| 3699 | SystemProperties.set(key, ""); |
| 3700 | } |
| 3701 | mNumDnsEntries = last; |
| 3702 | } |
| 3703 | |
| 3704 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3705 | private void updateCapabilities(NetworkAgentInfo networkAgent, |
| 3706 | NetworkCapabilities networkCapabilities) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3707 | // TODO - turn this on in MR1 when we have more dogfooding time. |
| 3708 | // rematchAllNetworksAndRequests(); |
| 3709 | if (!Objects.equals(networkAgent.networkCapabilities, networkCapabilities)) { |
| 3710 | synchronized (networkAgent) { |
| 3711 | networkAgent.networkCapabilities = networkCapabilities; |
| 3712 | } |
| 3713 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 3714 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3715 | } |
| 3716 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3717 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
| 3718 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3719 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 3720 | // Don't send listening requests to factories. b/17393458 |
| 3721 | if (!isRequest(nr)) continue; |
| 3722 | sendUpdatedScoreToFactories(nr, nai.getCurrentScore()); |
| 3723 | } |
| 3724 | } |
| 3725 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3726 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 3727 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3728 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3729 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 3730 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3731 | } |
| 3732 | } |
| 3733 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3734 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3735 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3736 | if (nri.messenger == null) return; // Default request has no msgr |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3737 | Bundle bundle = new Bundle(); |
| 3738 | bundle.putParcelable(NetworkRequest.class.getSimpleName(), |
| 3739 | new NetworkRequest(nri.request)); |
| 3740 | Message msg = Message.obtain(); |
| 3741 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL && |
| 3742 | notificationType != ConnectivityManager.CALLBACK_RELEASED) { |
| 3743 | bundle.putParcelable(Network.class.getSimpleName(), networkAgent.network); |
| 3744 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3745 | switch (notificationType) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3746 | case ConnectivityManager.CALLBACK_LOSING: { |
| 3747 | msg.arg1 = 30 * 1000; // TODO - read this from NetworkMonitor |
| 3748 | break; |
| 3749 | } |
| 3750 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
| 3751 | bundle.putParcelable(NetworkCapabilities.class.getSimpleName(), |
| 3752 | new NetworkCapabilities(networkAgent.networkCapabilities)); |
| 3753 | break; |
| 3754 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3755 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3756 | bundle.putParcelable(LinkProperties.class.getSimpleName(), |
| 3757 | new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3758 | break; |
| 3759 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3760 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3761 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 3762 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3763 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3764 | if (VDBG) { |
| 3765 | log("sending notification " + notifyTypeToName(notificationType) + |
| 3766 | " for " + nri.request); |
| 3767 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3768 | nri.messenger.send(msg); |
| 3769 | } catch (RemoteException e) { |
| 3770 | // may occur naturally in the race of binder death. |
| 3771 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 3772 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3775 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
| 3776 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3777 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 3778 | // Ignore listening requests. |
| 3779 | if (!isRequest(nr)) continue; |
| 3780 | loge("Dead network still had at least " + nr); |
| 3781 | break; |
| 3782 | } |
| 3783 | nai.asyncChannel.disconnect(); |
| 3784 | } |
| 3785 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3786 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 3787 | if (oldNetwork == null) { |
| 3788 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 3789 | return; |
| 3790 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3791 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 3792 | teardownUnneededNetwork(oldNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3793 | } |
| 3794 | |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3795 | private void makeDefault(NetworkAgentInfo newNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3796 | if (DBG) log("Switching to new default network: " + newNetwork); |
Robert Greenwalt | 5c1c832a8 | 2014-07-28 16:32:19 -0700 | [diff] [blame] | 3797 | mActiveDefaultNetwork = newNetwork.networkInfo.getType(); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3798 | setupDataActivityTracking(newNetwork); |
| 3799 | try { |
| 3800 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 3801 | } catch (Exception e) { |
| 3802 | loge("Exception setting default network :" + e); |
| 3803 | } |
| 3804 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 3805 | updateTcpBufferSizes(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3806 | } |
| 3807 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3808 | // Handles a network appearing or improving its score. |
| 3809 | // |
| 3810 | // - Evaluates all current NetworkRequests that can be |
| 3811 | // satisfied by newNetwork, and reassigns to newNetwork |
| 3812 | // any such requests for which newNetwork is the best. |
| 3813 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3814 | // - Lingers any Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3815 | // needed. A network is needed if it is the best network for |
| 3816 | // one or more NetworkRequests, or if it is a VPN. |
| 3817 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3818 | // - Tears down newNetwork if it just became validated |
| 3819 | // (i.e. nascent==true) but turns out to be unneeded. |
| 3820 | // Does not tear down newNetwork if it is unvalidated, |
| 3821 | // because future validation may improve newNetwork's |
| 3822 | // score enough that it is needed. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3823 | // |
| 3824 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 3825 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 3826 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 3827 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 3828 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3829 | // |
| 3830 | // @param nascent indicates if newNetwork just became validated, in which case it should be |
| 3831 | // torn down if unneeded. If nascent is false, no action is taken if newNetwork |
| 3832 | // is found to be unneeded by this call. Presumably, in this case, either: |
| 3833 | // - newNetwork is unvalidated (and left alive), or |
| 3834 | // - the NetworkRequests keeping newNetwork alive have been transitioned to |
| 3835 | // another higher scoring network by another call to rematchNetworkAndRequests() |
| 3836 | // and this other call also lingered newNetwork. |
| 3837 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, boolean nascent) { |
| 3838 | if (!newNetwork.created) loge("ERROR: uncreated network being rematched."); |
| 3839 | if (nascent && !newNetwork.validated) loge("ERROR: nascent network not validated."); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 3840 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3841 | boolean isNewDefault = false; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3842 | if (DBG) log("rematching " + newNetwork.name()); |
| 3843 | // Find and migrate to this Network any NetworkRequests for |
| 3844 | // which this network is now the best. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3845 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3846 | if (VDBG) log(" network has: " + newNetwork.networkCapabilities); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3847 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 3848 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 3849 | if (newNetwork == currentNetwork) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 3850 | if (DBG) { |
| 3851 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 3852 | " request " + nri.request.requestId + ". No change."); |
| 3853 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 3854 | keep = true; |
| 3855 | continue; |
| 3856 | } |
| 3857 | |
| 3858 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3859 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
| 3860 | if (nri.request.networkCapabilities.satisfiedByNetworkCapabilities( |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3861 | newNetwork.networkCapabilities)) { |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 3862 | if (!nri.isRequest) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3863 | // This is not a request, it's a callback listener. |
| 3864 | // Add it to newNetwork regardless of score. |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 3865 | newNetwork.addRequest(nri.request); |
| 3866 | continue; |
| 3867 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3868 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3869 | // next check if it's better than any current network we're using for |
| 3870 | // this request |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3871 | if (VDBG) { |
| 3872 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3873 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
| 3874 | ", newScore = " + newNetwork.getCurrentScore()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3875 | } |
| 3876 | if (currentNetwork == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3877 | currentNetwork.getCurrentScore() < newNetwork.getCurrentScore()) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3878 | if (currentNetwork != null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3879 | if (DBG) log(" accepting network in place of " + currentNetwork.name()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3880 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 3881 | currentNetwork.networkLingered.add(nri.request); |
| 3882 | affectedNetworks.add(currentNetwork); |
| 3883 | } else { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3884 | if (DBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3885 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3886 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3887 | newNetwork.addRequest(nri.request); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 3888 | if (nri.isRequest && nri.request.legacyType != TYPE_NONE) { |
| 3889 | mLegacyTypeTracker.add(nri.request.legacyType, newNetwork); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3890 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3891 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3892 | // Tell NetworkFactories about the new score, so they can stop |
| 3893 | // trying to connect if they know they cannot match it. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3894 | // TODO - this could get expensive if we have alot of requests for this |
| 3895 | // network. Think about if there is a way to reduce this. Push |
| 3896 | // netid->request mapping to each factory? |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3897 | sendUpdatedScoreToFactories(nri.request, newNetwork.getCurrentScore()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3898 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3899 | isNewDefault = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3900 | // TODO: Remove following line. It's redundant with makeDefault call. |
| 3901 | mActiveDefaultNetwork = newNetwork.networkInfo.getType(); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3902 | if (newNetwork.linkProperties != null) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 3903 | updateTcpBufferSizes(newNetwork); |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 3904 | setDefaultDnsSystemProperties( |
| 3905 | newNetwork.linkProperties.getDnsServers()); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 3906 | } else { |
| 3907 | setDefaultDnsSystemProperties(new ArrayList<InetAddress>()); |
| 3908 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 3909 | // Maintain the illusion: since the legacy API only |
| 3910 | // understands one network at a time, we must pretend |
| 3911 | // that the current default network disconnected before |
| 3912 | // the new one connected. |
| 3913 | if (currentNetwork != null) { |
| 3914 | mLegacyTypeTracker.remove(currentNetwork.networkInfo.getType(), |
| 3915 | currentNetwork); |
| 3916 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3917 | mDefaultInetConditionPublished = newNetwork.validated ? 100 : 0; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3918 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3923 | // Linger any networks that are no longer needed. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3924 | for (NetworkAgentInfo nai : affectedNetworks) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3925 | boolean teardown = !nai.isVPN() && nai.validated; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 3926 | for (int i = 0; i < nai.networkRequests.size() && teardown; i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3927 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 3928 | try { |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3929 | if (isRequest(nr)) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3930 | teardown = false; |
| 3931 | } |
| 3932 | } catch (Exception e) { |
| 3933 | loge("Request " + nr + " not found in mNetworkRequests."); |
| 3934 | loge(" it came from request list of " + nai.name()); |
| 3935 | } |
| 3936 | } |
| 3937 | if (teardown) { |
| 3938 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 3939 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 3940 | } else { |
| 3941 | // not going to linger, so kill the list of linger networks.. only |
| 3942 | // notify them of linger if it happens as the result of gaining another, |
| 3943 | // but if they transition and old network stays up, don't tell them of linger |
| 3944 | // or very delayed loss |
| 3945 | nai.networkLingered.clear(); |
| 3946 | if (VDBG) log("Lingered for " + nai.name() + " cleared"); |
| 3947 | } |
| 3948 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3949 | if (keep) { |
| 3950 | if (isNewDefault) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3951 | // Notify system services that this network is up. |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 3952 | makeDefault(newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3953 | synchronized (ConnectivityService.this) { |
| 3954 | // have a new default network, release the transition wakelock in |
| 3955 | // a second if it's held. The second pause is to allow apps |
| 3956 | // to reconnect over the new network |
| 3957 | if (mNetTransitionWakeLock.isHeld()) { |
| 3958 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 3959 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 3960 | mNetTransitionWakeLockSerialNumber, 0), |
| 3961 | 1000); |
| 3962 | } |
| 3963 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3964 | } |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 3965 | |
| 3966 | // Notify battery stats service about this network, both the normal |
| 3967 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3968 | // TODO: Avoid redoing this; this must only be done once when a network comes online. |
Dianne Hackborn | 2932513 | 2014-05-21 15:01:03 -0700 | [diff] [blame] | 3969 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 3970 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 3971 | final int type = newNetwork.networkInfo.getType(); |
| 3972 | |
| 3973 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 3974 | bs.noteNetworkInterfaceType(baseIface, type); |
| 3975 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 3976 | final String stackedIface = stacked.getInterfaceName(); |
| 3977 | bs.noteNetworkInterfaceType(stackedIface, type); |
| 3978 | NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); |
| 3979 | } |
| 3980 | } catch (RemoteException ignored) { |
| 3981 | } |
| 3982 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3983 | notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3984 | } else if (nascent) { |
| 3985 | // Only tear down newly validated networks here. Leave unvalidated to either become |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3986 | // validated (and get evaluated against peers, one losing here) or |
| 3987 | // NetworkMonitor reports a bad network and we tear it down then. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 3988 | // Networks that have been up for a while and are validated should be torn down via |
| 3989 | // the lingering process so communication on that network is given time to wrap up. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3990 | // TODO: Could teardown unvalidated networks when their NetworkCapabilities |
| 3991 | // satisfy no NetworkRequests. |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 3992 | if (DBG) log("Validated network turns out to be unwanted. Tear it down."); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 3993 | teardownUnneededNetwork(newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3994 | } |
| 3995 | } |
| 3996 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 3997 | // Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 3998 | // being disconnected. |
| 3999 | // If only one Network's score or capabilities have been modified since the last time |
| 4000 | // this function was called, pass this Network in via the "changed" arugment, otherwise |
| 4001 | // pass null. |
| 4002 | // If only one Network has been changed but its NetworkCapabilities have not changed, |
| 4003 | // pass in the Network's score (from getCurrentScore()) prior to the change via |
| 4004 | // "oldScore", otherwise pass changed.getCurrentScore() or 0 if "changed" is null. |
| 4005 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
| 4006 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 4007 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 4008 | // example in the case where "changed"'s score decreases and another network should begin |
| 4009 | // satifying a NetworkRequest that "changed" currently satisfies. |
| 4010 | |
| 4011 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 4012 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 4013 | // rematchNetworkAndRequests() handles. |
| 4014 | if (changed != null && oldScore < changed.getCurrentScore()) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4015 | rematchNetworkAndRequests(changed, false); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4016 | } else { |
| 4017 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4018 | rematchNetworkAndRequests(nai, false); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4019 | } |
| 4020 | } |
| 4021 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4022 | |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4023 | private void updateInetCondition(NetworkAgentInfo nai, boolean valid) { |
| 4024 | // Don't bother updating until we've graduated to validated at least once. |
| 4025 | if (!nai.validated) return; |
| 4026 | // For now only update icons for default connection. |
| 4027 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 4028 | if (!isDefaultNetwork(nai)) return; |
| 4029 | |
| 4030 | int newInetCondition = valid ? 100 : 0; |
| 4031 | // Don't repeat publish. |
| 4032 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 4033 | |
| 4034 | mDefaultInetConditionPublished = newInetCondition; |
| 4035 | sendInetConditionBroadcast(nai.networkInfo); |
| 4036 | } |
| 4037 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4038 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 4039 | NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4040 | NetworkInfo oldInfo = null; |
| 4041 | synchronized (networkAgent) { |
| 4042 | oldInfo = networkAgent.networkInfo; |
| 4043 | networkAgent.networkInfo = newInfo; |
| 4044 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4045 | if (networkAgent.isVPN() && mLockdownTracker != null) { |
| 4046 | mLockdownTracker.onVpnStateChanged(newInfo); |
| 4047 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4048 | |
| 4049 | if (oldInfo != null && oldInfo.getState() == state) { |
| 4050 | if (VDBG) log("ignoring duplicate network state non-change"); |
| 4051 | return; |
| 4052 | } |
| 4053 | if (DBG) { |
| 4054 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 4055 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 4056 | " to " + state); |
| 4057 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4058 | |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4059 | if (state == NetworkInfo.State.CONNECTED && !networkAgent.created) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4060 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4061 | // This should never fail. Specifying an already in use NetID will cause failure. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4062 | if (networkAgent.isVPN()) { |
| 4063 | mNetd.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4064 | !networkAgent.linkProperties.getDnsServers().isEmpty(), |
| 4065 | (networkAgent.networkMisc == null || |
| 4066 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4067 | } else { |
| 4068 | mNetd.createPhysicalNetwork(networkAgent.network.netId); |
| 4069 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4070 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4071 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 4072 | + e.getMessage()); |
| 4073 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4074 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4075 | networkAgent.created = true; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4076 | updateLinkProperties(networkAgent, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4077 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 4078 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4079 | if (networkAgent.isVPN()) { |
| 4080 | // Temporarily disable the default proxy (not global). |
| 4081 | synchronized (mProxyLock) { |
| 4082 | if (!mDefaultProxyDisabled) { |
| 4083 | mDefaultProxyDisabled = true; |
| 4084 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4085 | sendProxyBroadcast(null); |
| 4086 | } |
| 4087 | } |
| 4088 | } |
| 4089 | // TODO: support proxy per network. |
| 4090 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4091 | // Consider network even though it is not yet validated. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4092 | rematchNetworkAndRequests(networkAgent, false); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4093 | } else if (state == NetworkInfo.State.DISCONNECTED || |
| 4094 | state == NetworkInfo.State.SUSPENDED) { |
| 4095 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4096 | if (networkAgent.isVPN()) { |
| 4097 | synchronized (mProxyLock) { |
| 4098 | if (mDefaultProxyDisabled) { |
| 4099 | mDefaultProxyDisabled = false; |
| 4100 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4101 | sendProxyBroadcast(mDefaultProxy); |
| 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4106 | } |
| 4107 | } |
| 4108 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4109 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
| 4110 | if (DBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 4111 | if (score < 0) { |
| 4112 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 4113 | "). Bumping score to min of 0"); |
| 4114 | score = 0; |
| 4115 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4116 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4117 | final int oldScore = nai.getCurrentScore(); |
| 4118 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4119 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4120 | if (nai.created) rematchAllNetworksAndRequests(nai, oldScore); |
| 4121 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4122 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4125 | // notify only this one new request of the current state |
| 4126 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 4127 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 4128 | // TODO - read state from monitor to decide what to send. |
| 4129 | // if (nai.networkMonitor.isLingering()) { |
| 4130 | // notifyType = NetworkCallbacks.LOSING; |
| 4131 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 4132 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 4133 | // } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4134 | callCallbackForRequest(nri, nai, notifyType); |
| 4135 | } |
| 4136 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4137 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, boolean connected, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4138 | // The NetworkInfo we actually send out has no bearing on the real |
| 4139 | // state of affairs. For example, if the default connection is mobile, |
| 4140 | // and a request for HIPRI has just gone away, we need to pretend that |
| 4141 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 4142 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 4143 | // and is still connected. |
| 4144 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 4145 | info.setType(type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4146 | if (connected) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4147 | info.setDetailedState(DetailedState.CONNECTED, null, info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4148 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
| 4149 | } else { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4150 | info.setDetailedState(DetailedState.DISCONNECTED, null, info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4151 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 4152 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 4153 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 4154 | if (info.isFailover()) { |
| 4155 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 4156 | nai.networkInfo.setFailover(false); |
| 4157 | } |
| 4158 | if (info.getReason() != null) { |
| 4159 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 4160 | } |
| 4161 | if (info.getExtraInfo() != null) { |
| 4162 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 4163 | } |
| 4164 | NetworkAgentInfo newDefaultAgent = null; |
| 4165 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 4166 | newDefaultAgent = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 4167 | if (newDefaultAgent != null) { |
| 4168 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 4169 | newDefaultAgent.networkInfo); |
| 4170 | } else { |
| 4171 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 4172 | } |
| 4173 | } |
| 4174 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 4175 | mDefaultInetConditionPublished); |
| 4176 | final Intent immediateIntent = new Intent(intent); |
| 4177 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 4178 | sendStickyBroadcast(immediateIntent); |
| 4179 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
| 4180 | if (newDefaultAgent != null) { |
| 4181 | sendConnectedBroadcastDelayed(newDefaultAgent.networkInfo, |
| 4182 | getConnectivityChangeDelay()); |
| 4183 | } |
| 4184 | } |
| 4185 | } |
| 4186 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4187 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4188 | if (DBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4189 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4190 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 4191 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 4192 | if (VDBG) log(" sending notification for " + nr); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4193 | callCallbackForRequest(nri, networkAgent, notifyType); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4194 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4195 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4196 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4197 | private String notifyTypeToName(int notifyType) { |
| 4198 | switch (notifyType) { |
| 4199 | case ConnectivityManager.CALLBACK_PRECHECK: return "PRECHECK"; |
| 4200 | case ConnectivityManager.CALLBACK_AVAILABLE: return "AVAILABLE"; |
| 4201 | case ConnectivityManager.CALLBACK_LOSING: return "LOSING"; |
| 4202 | case ConnectivityManager.CALLBACK_LOST: return "LOST"; |
| 4203 | case ConnectivityManager.CALLBACK_UNAVAIL: return "UNAVAILABLE"; |
| 4204 | case ConnectivityManager.CALLBACK_CAP_CHANGED: return "CAP_CHANGED"; |
| 4205 | case ConnectivityManager.CALLBACK_IP_CHANGED: return "IP_CHANGED"; |
| 4206 | case ConnectivityManager.CALLBACK_RELEASED: return "RELEASED"; |
| 4207 | } |
| 4208 | return "UNKNOWN"; |
| 4209 | } |
| 4210 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4211 | private LinkProperties getLinkPropertiesForTypeInternal(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4212 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4213 | if (nai != null) { |
| 4214 | synchronized (nai) { |
| 4215 | return new LinkProperties(nai.linkProperties); |
| 4216 | } |
| 4217 | } |
| 4218 | return new LinkProperties(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | private NetworkInfo getNetworkInfoForType(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4222 | if (!mLegacyTypeTracker.isTypeSupported(networkType)) |
| 4223 | return null; |
| 4224 | |
| 4225 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 4226 | if (nai != null) { |
| 4227 | NetworkInfo result = new NetworkInfo(nai.networkInfo); |
| 4228 | result.setType(networkType); |
| 4229 | return result; |
| 4230 | } else { |
Lorenzo Colitti | 17e7e97 | 2014-08-29 18:34:49 -0700 | [diff] [blame] | 4231 | NetworkInfo result = new NetworkInfo( |
| 4232 | networkType, 0, ConnectivityManager.getNetworkTypeName(networkType), ""); |
Robert Greenwalt | bdb1bdf | 2014-08-21 09:19:46 -0700 | [diff] [blame] | 4233 | result.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 4234 | return result; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4235 | } |
| 4236 | } |
| 4237 | |
| 4238 | private NetworkCapabilities getNetworkCapabilitiesForType(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4239 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4240 | if (nai != null) { |
| 4241 | synchronized (nai) { |
| 4242 | return new NetworkCapabilities(nai.networkCapabilities); |
| 4243 | } |
| 4244 | } |
| 4245 | return new NetworkCapabilities(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4246 | } |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 4247 | |
| 4248 | @Override |
| 4249 | public boolean addVpnAddress(String address, int prefixLength) { |
| 4250 | throwIfLockdownEnabled(); |
| 4251 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4252 | synchronized (mVpns) { |
| 4253 | return mVpns.get(user).addAddress(address, prefixLength); |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | @Override |
| 4258 | public boolean removeVpnAddress(String address, int prefixLength) { |
| 4259 | throwIfLockdownEnabled(); |
| 4260 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4261 | synchronized (mVpns) { |
| 4262 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 4263 | } |
| 4264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4265 | } |