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