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