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