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