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