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; |
Robert Greenwalt | 5d2e896 | 2014-05-19 11:00:12 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.NetworkCallbackListener; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 24 | import static android.net.ConnectivityManager.TYPE_BLUETOOTH; |
| 25 | import static android.net.ConnectivityManager.TYPE_DUMMY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.TYPE_MOBILE_MMS; |
| 28 | import static android.net.ConnectivityManager.TYPE_MOBILE_SUPL; |
| 29 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 30 | import static android.net.ConnectivityManager.TYPE_MOBILE_FOTA; |
| 31 | import static android.net.ConnectivityManager.TYPE_MOBILE_IMS; |
| 32 | import static android.net.ConnectivityManager.TYPE_MOBILE_CBS; |
| 33 | import static android.net.ConnectivityManager.TYPE_MOBILE_IA; |
| 34 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 35 | import static android.net.ConnectivityManager.TYPE_NONE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 36 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 37 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 38 | import static android.net.ConnectivityManager.TYPE_PROXY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 39 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 40 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 41 | import static android.net.ConnectivityServiceProtocol.NetworkFactoryProtocol; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 42 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 43 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 44 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 45 | import android.app.AlarmManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 46 | import android.app.AppOpsManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 47 | import android.app.Notification; |
| 48 | import android.app.NotificationManager; |
| 49 | import android.app.PendingIntent; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 50 | import android.bluetooth.BluetoothTetheringDataTracker; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 51 | import android.content.ActivityNotFoundException; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 52 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.content.ContentResolver; |
| 54 | import android.content.Context; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 55 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 57 | import android.content.IntentFilter; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 58 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.content.pm.PackageManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 60 | import android.content.pm.PackageManager.NameNotFoundException; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 61 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 62 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 63 | import android.database.ContentObserver; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 64 | import android.net.CaptivePortalTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.net.ConnectivityManager; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 66 | import android.net.DummyDataStateTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 68 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 69 | import android.net.INetworkPolicyListener; |
| 70 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 71 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 72 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 73 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 74 | import android.net.LinkProperties.CompareResult; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 75 | import android.net.LinkQualityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 77 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 78 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 79 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 80 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 82 | import android.net.NetworkInfo.DetailedState; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 83 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 84 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 85 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 87 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 88 | import android.net.Proxy; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 89 | import android.net.ProxyDataTracker; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 90 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 91 | import android.net.RouteInfo; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 92 | import android.net.SamplingDataTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 93 | import android.net.Uri; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 94 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 95 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | import android.os.Binder; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 97 | import android.os.Build; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 98 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 100 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 101 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 102 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | import android.os.Looper; |
| 104 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 105 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 106 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 107 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 108 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 109 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 111 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 113 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 115 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 116 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 117 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 118 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 119 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 120 | import android.util.SparseArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 121 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 122 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 124 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 125 | import com.android.internal.annotations.GuardedBy; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 126 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 127 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 128 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 129 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 130 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 131 | import com.android.internal.telephony.PhoneConstants; |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 132 | import com.android.internal.telephony.TelephonyIntents; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 133 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 134 | import com.android.internal.util.IndentingPrintWriter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 135 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 136 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 137 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 138 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 139 | import com.android.server.connectivity.NetworkAgentInfo; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 140 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 141 | import com.android.server.connectivity.PacManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 142 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 143 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 144 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 145 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 146 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 147 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 148 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 149 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 150 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 151 | import org.xmlpull.v1.XmlPullParser; |
| 152 | import org.xmlpull.v1.XmlPullParserException; |
| 153 | |
| 154 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 156 | import java.io.FileNotFoundException; |
| 157 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 158 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 160 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 161 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 162 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 163 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 164 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 165 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 166 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 167 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 168 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 169 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 170 | import java.util.GregorianCalendar; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 171 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 172 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 173 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 174 | import java.util.Map; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 175 | import java.util.Random; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 176 | import java.util.concurrent.atomic.AtomicBoolean; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 177 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 179 | import javax.net.ssl.HostnameVerifier; |
| 180 | import javax.net.ssl.HttpsURLConnection; |
| 181 | import javax.net.ssl.SSLSession; |
| 182 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 183 | import static android.net.ConnectivityManager.INVALID_NET_ID; |
| 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | /** |
| 186 | * @hide |
| 187 | */ |
| 188 | public class ConnectivityService extends IConnectivityManager.Stub { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 189 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
Robert Greenwalt | ba175a5 | 2010-10-05 19:12:26 -0700 | [diff] [blame] | 191 | private static final boolean DBG = true; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 192 | private static final boolean VDBG = true; // STOPSHIP |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 194 | // network sampling debugging |
| 195 | private static final boolean SAMPLE_DBG = false; |
| 196 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 197 | private static final boolean LOGD_RULES = false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 198 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 199 | // TODO: create better separation between radio types and network types |
| 200 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 201 | // how long to wait before switching back to a radio's default network |
| 202 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 203 | // system property that can override the above value |
| 204 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 205 | "android.telephony.apn-restore"; |
| 206 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 207 | // Default value if FAIL_FAST_TIME_MS is not set |
| 208 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 209 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 210 | private static final String FAIL_FAST_TIME_MS = |
| 211 | "persist.radio.fail_fast_time_ms"; |
| 212 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 213 | private static final String ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED = |
| 214 | "android.net.ConnectivityService.action.PKT_CNT_SAMPLE_INTERVAL_ELAPSED"; |
| 215 | |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 216 | private static final int SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE = 0; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 217 | |
| 218 | private PendingIntent mSampleIntervalElapsedIntent; |
| 219 | |
| 220 | // Set network sampling interval at 12 minutes, this way, even if the timers get |
| 221 | // aggregated, it will fire at around 15 minutes, which should allow us to |
| 222 | // aggregate this timer with other timers (specially the socket keep alive timers) |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 223 | 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] | 224 | |
| 225 | // start network sampling a minute after booting ... |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 226 | 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] | 227 | |
| 228 | AlarmManager mAlarmManager; |
| 229 | |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 230 | // used in recursive route setting to add gateways for the host for which |
| 231 | // a host route was requested. |
| 232 | private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10; |
| 233 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 234 | private Tethering mTethering; |
| 235 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 236 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 237 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 238 | @GuardedBy("mVpns") |
| 239 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 240 | private VpnCallback mVpnCallback = new VpnCallback(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 242 | private boolean mLockdownEnabled; |
| 243 | private LockdownVpnTracker mLockdownTracker; |
| 244 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 245 | private Nat464Xlat mClat; |
| 246 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 247 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 248 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 249 | /** Currently active network rules by UID. */ |
| 250 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 251 | /** Set of ifaces that are costly. */ |
| 252 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 253 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | /** |
| 255 | * Sometimes we want to refer to the individual network state |
| 256 | * trackers separately, and sometimes we just want to treat them |
| 257 | * abstractly. |
| 258 | */ |
| 259 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 260 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Holds references to all NetworkAgentInfos claiming to support the legacy |
| 263 | * NetworkType. We used to have a static set of of NetworkStateTrackers |
| 264 | * for each network type. This is the new model. |
| 265 | * Supports synchronous inspection of state. |
| 266 | * These are built out at startup such that an unsupported network |
| 267 | * doesn't get an ArrayList instance, making this a tristate: |
| 268 | * unsupported, supported but not active and active. |
| 269 | */ |
| 270 | private ArrayList<NetworkAgentInfo> mNetworkAgentInfoForType[]; |
| 271 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 272 | /* Handles captive portal check on a network */ |
| 273 | private CaptivePortalTracker mCaptivePortalTracker; |
| 274 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 275 | /** |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 276 | * The link properties that define the current links |
| 277 | */ |
| 278 | private LinkProperties mCurrentLinkProperties[]; |
| 279 | |
| 280 | /** |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 281 | * A per Net list of the PID's that requested access to the net |
| 282 | * used both as a refcount and for per-PID DNS selection |
| 283 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 284 | private List<Integer> mNetRequestersPids[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 285 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 286 | // priority order of the nettrackers |
| 287 | // (excluding dynamically set mNetworkPreference) |
| 288 | // TODO - move mNetworkTypePreference into this |
| 289 | private int[] mPriorityList; |
| 290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 291 | private Context mContext; |
| 292 | private int mNetworkPreference; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 293 | private int mActiveDefaultNetwork = -1; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 294 | // 0 is full bad, 100 is full good |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 295 | private int mDefaultInetCondition = 0; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 296 | private int mDefaultInetConditionPublished = 0; |
| 297 | private boolean mInetConditionChangeInFlight = false; |
| 298 | private int mDefaultConnectionSequence = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 300 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 301 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
| 303 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 304 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 306 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 307 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 308 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 309 | private static final int ENABLED = 1; |
| 310 | private static final int DISABLED = 0; |
| 311 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 312 | private static final boolean ADD = true; |
| 313 | private static final boolean REMOVE = false; |
| 314 | |
| 315 | private static final boolean TO_DEFAULT_TABLE = true; |
| 316 | private static final boolean TO_SECONDARY_TABLE = false; |
| 317 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 318 | private static final boolean EXEMPT = true; |
| 319 | private static final boolean UNEXEMPT = false; |
| 320 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 321 | /** |
| 322 | * used internally as a delayed event to make us switch back to the |
| 323 | * default network |
| 324 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 325 | private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 326 | |
| 327 | /** |
| 328 | * used internally to change our mobile data enabled flag |
| 329 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 330 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 331 | |
| 332 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 333 | * used internally to synchronize inet condition reports |
| 334 | * arg1 = networkType |
| 335 | * arg2 = condition (0 bad, 100 good) |
| 336 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 337 | private static final int EVENT_INET_CONDITION_CHANGE = 4; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * used internally to mark the end of inet condition hold periods |
| 341 | * arg1 = networkType |
| 342 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 343 | private static final int EVENT_INET_CONDITION_HOLD_END = 5; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 346 | * used internally to set enable/disable cellular data |
| 347 | * arg1 = ENBALED or DISABLED |
| 348 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 349 | private static final int EVENT_SET_MOBILE_DATA = 7; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 350 | |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 351 | /** |
| 352 | * used internally to clear a wakelock when transitioning |
| 353 | * from one net to another |
| 354 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 355 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 356 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 357 | /** |
| 358 | * used internally to reload global proxy settings |
| 359 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 360 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 361 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 362 | /** |
| 363 | * used internally to set external dependency met/unmet |
| 364 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 365 | * arg2 = NetworkType |
| 366 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 367 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 368 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 369 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 370 | * used internally to send a sticky broadcast delayed. |
| 371 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 372 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 11; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 373 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 374 | /** |
| 375 | * Used internally to |
| 376 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 377 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 378 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 12; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 379 | |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 380 | private static final int EVENT_VPN_STATE_CHANGED = 13; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 381 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Used internally to disable fail fast of mobile data |
| 384 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 385 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 14; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 386 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 387 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 388 | * used internally to indicate that data sampling interval is up |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 389 | */ |
| 390 | private static final int EVENT_SAMPLE_INTERVAL_ELAPSED = 15; |
| 391 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 392 | /** |
| 393 | * PAC manager has received new port. |
| 394 | */ |
| 395 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 396 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 397 | /** |
| 398 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 399 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 400 | */ |
| 401 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 402 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 403 | /** |
| 404 | * used internally when registering NetworkAgents |
| 405 | * obj = Messenger |
| 406 | */ |
| 407 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 408 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 409 | /** |
| 410 | * used to add a network request |
| 411 | * includes a NetworkRequestInfo |
| 412 | */ |
| 413 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 414 | |
| 415 | /** |
| 416 | * indicates a timeout period is over - check if we had a network yet or not |
| 417 | * and if not, call the timeout calback (but leave the request live until they |
| 418 | * cancel it. |
| 419 | * includes a NetworkRequestInfo |
| 420 | */ |
| 421 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 422 | |
| 423 | /** |
| 424 | * used to add a network listener - no request |
| 425 | * includes a NetworkRequestInfo |
| 426 | */ |
| 427 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 428 | |
| 429 | /** |
| 430 | * used to remove a network request, either a listener or a real request |
| 431 | * includes a NetworkRequest |
| 432 | */ |
| 433 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 434 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 435 | /** |
| 436 | * used internally when registering NetworkFactories |
| 437 | * obj = Messenger |
| 438 | */ |
| 439 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 440 | |
| 441 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 442 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 443 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 444 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 445 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 446 | |
| 447 | // list of DeathRecipients used to make sure features are turned off when |
| 448 | // a process dies |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 449 | private List<FeatureUser> mFeatureUsers; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 450 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 451 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 452 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 453 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 454 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 455 | private String mNetTransitionWakeLockCausedBy = ""; |
| 456 | private int mNetTransitionWakeLockSerialNumber; |
| 457 | private int mNetTransitionWakeLockTimeout; |
| 458 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 459 | private InetAddress mDefaultDns; |
| 460 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 461 | // Lock for protecting access to mAddedRoutes and mExemptAddresses |
| 462 | private final Object mRoutesLock = new Object(); |
| 463 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 464 | // this collection is used to refcount the added routes - if there are none left |
| 465 | // it's time to remove the route from the route table |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 466 | @GuardedBy("mRoutesLock") |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 467 | private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>(); |
| 468 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 469 | // this collection corresponds to the entries of mAddedRoutes that have routing exemptions |
| 470 | // used to handle cleanup of exempt rules |
| 471 | @GuardedBy("mRoutesLock") |
| 472 | private Collection<LinkAddress> mExemptAddresses = new ArrayList<LinkAddress>(); |
| 473 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 474 | // used in DBG mode to track inet condition reports |
| 475 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 476 | private ArrayList mInetLog; |
| 477 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 478 | // 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] | 479 | private ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 480 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 481 | private boolean mDefaultProxyDisabled = false; |
| 482 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 483 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 484 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 485 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 486 | private PacManager mPacManager = null; |
| 487 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 488 | private SettingsObserver mSettingsObserver; |
| 489 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 490 | private AppOpsManager mAppOpsManager; |
| 491 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 492 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 493 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 494 | |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 495 | private static class RadioAttributes { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 496 | public int mSimultaneity; |
| 497 | public int mType; |
| 498 | public RadioAttributes(String init) { |
| 499 | String fragments[] = init.split(","); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 500 | mType = Integer.parseInt(fragments[0]); |
| 501 | mSimultaneity = Integer.parseInt(fragments[1]); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | RadioAttributes[] mRadioAttributes; |
| 505 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 506 | // the set of network types that can only be enabled by system/sig apps |
| 507 | List mProtectedNetworks; |
| 508 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 509 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 510 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 511 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 512 | |
| 513 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 514 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 515 | // sequence number for Networks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 516 | private final static int MIN_NET_ID = 10; // some reserved marks |
| 517 | private final static int MAX_NET_ID = 65535; |
| 518 | private int mNextNetId = MIN_NET_ID; |
| 519 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 520 | // sequence number of NetworkRequests |
| 521 | private int mNextNetworkRequestId = 1; |
| 522 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 523 | public ConnectivityService(Context context, INetworkManagementService netd, |
| 524 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 525 | // Currently, omitting a NetworkFactory will create one internally |
| 526 | // TODO: create here when we have cleaner WiMAX support |
| 527 | this(context, netd, statsService, policyManager, null); |
| 528 | } |
| 529 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 530 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 531 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 532 | NetworkFactory netFactory) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 533 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 534 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 535 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 536 | netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 537 | netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 538 | mDefaultRequest = new NetworkRequest(netCap, true, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 539 | NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), |
| 540 | NetworkRequestInfo.REQUEST); |
| 541 | mNetworkRequests.put(mDefaultRequest, nri); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 542 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 543 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 544 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 545 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 546 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 547 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 548 | if (netFactory == null) { |
| 549 | netFactory = new DefaultNetworkFactory(context, mTrackerHandler); |
| 550 | } |
| 551 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 552 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 553 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 554 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 555 | Settings.Secure.ANDROID_ID); |
| 556 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 557 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 558 | SystemProperties.set("net.hostname", name); |
| 559 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 562 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 563 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 564 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 565 | if (dns == null || dns.length() == 0) { |
| 566 | dns = context.getResources().getString( |
| 567 | com.android.internal.R.string.config_default_dns_server); |
| 568 | } |
| 569 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 570 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 571 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 572 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 575 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 576 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 577 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 578 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 579 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 580 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 581 | try { |
| 582 | mPolicyManager.registerListener(mPolicyListener); |
| 583 | } catch (RemoteException e) { |
| 584 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 585 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 589 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 590 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 591 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 592 | com.android.internal.R.integer.config_networkTransitionTimeout); |
| 593 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 594 | mNetworkAgentInfoForType = (ArrayList<NetworkAgentInfo>[]) |
| 595 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 596 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 597 | mNetTrackers = new NetworkStateTracker[ |
| 598 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 599 | mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 600 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 601 | mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1]; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 602 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 603 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 604 | // Load device network attributes from resources |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 605 | String[] raStrings = context.getResources().getStringArray( |
| 606 | com.android.internal.R.array.radioAttributes); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 607 | for (String raString : raStrings) { |
| 608 | RadioAttributes r = new RadioAttributes(raString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 609 | if (VDBG) log("raString=" + raString + " r=" + r); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 610 | if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 611 | loge("Error in radioAttributes - ignoring attempt to define type " + r.mType); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 612 | continue; |
| 613 | } |
| 614 | if (mRadioAttributes[r.mType] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 615 | loge("Error in radioAttributes - ignoring attempt to redefine type " + |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 616 | r.mType); |
| 617 | continue; |
| 618 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 619 | mRadioAttributes[r.mType] = r; |
| 620 | } |
| 621 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 622 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 623 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 624 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 625 | String[] naStrings = context.getResources().getStringArray( |
| 626 | com.android.internal.R.array.networkAttributes); |
| 627 | for (String naString : naStrings) { |
| 628 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 629 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 630 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 631 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 632 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 633 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 634 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 635 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 636 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 637 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 638 | n.type); |
| 639 | continue; |
| 640 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 641 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 642 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 643 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 644 | continue; |
| 645 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 646 | if (mRadioAttributes[n.radio] == null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 647 | loge("Error in networkAttributes - ignoring attempt to use undefined " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 648 | "radio " + n.radio + " in network type " + n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 649 | continue; |
| 650 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 651 | mNetworkAgentInfoForType[n.type] = new ArrayList<NetworkAgentInfo>(); |
| 652 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 653 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 654 | mNetworksDefined++; |
| 655 | } catch(Exception e) { |
| 656 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 659 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 660 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 661 | mProtectedNetworks = new ArrayList<Integer>(); |
| 662 | int[] protectedNetworks = context.getResources().getIntArray( |
| 663 | com.android.internal.R.array.config_protectedNetworks); |
| 664 | for (int p : protectedNetworks) { |
| 665 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 666 | mProtectedNetworks.add(p); |
| 667 | } else { |
| 668 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 669 | } |
| 670 | } |
| 671 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 672 | // high priority first |
| 673 | mPriorityList = new int[mNetworksDefined]; |
| 674 | { |
| 675 | int insertionPoint = mNetworksDefined-1; |
| 676 | int currentLowest = 0; |
| 677 | int nextLowest = 0; |
| 678 | while (insertionPoint > -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 679 | for (NetworkConfig na : mNetConfigs) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 680 | if (na == null) continue; |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 681 | if (na.priority < currentLowest) continue; |
| 682 | if (na.priority > currentLowest) { |
| 683 | if (na.priority < nextLowest || nextLowest == 0) { |
| 684 | nextLowest = na.priority; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 685 | } |
| 686 | continue; |
| 687 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 688 | mPriorityList[insertionPoint--] = na.type; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 689 | } |
| 690 | currentLowest = nextLowest; |
| 691 | nextLowest = 0; |
| 692 | } |
| 693 | } |
| 694 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 695 | mNetRequestersPids = |
| 696 | (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 697 | for (int i : mPriorityList) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 698 | mNetRequestersPids[i] = new ArrayList<Integer>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 701 | mFeatureUsers = new ArrayList<FeatureUser>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 702 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 703 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 704 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 705 | |
| 706 | // Create and start trackers for hard-coded networks |
| 707 | for (int targetNetworkType : mPriorityList) { |
| 708 | final NetworkConfig config = mNetConfigs[targetNetworkType]; |
| 709 | final NetworkStateTracker tracker; |
| 710 | try { |
| 711 | tracker = netFactory.createTracker(targetNetworkType, config); |
| 712 | mNetTrackers[targetNetworkType] = tracker; |
| 713 | } catch (IllegalArgumentException e) { |
| 714 | Slog.e(TAG, "Problem creating " + getNetworkTypeName(targetNetworkType) |
| 715 | + " tracker: " + e); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 716 | continue; |
| 717 | } |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 718 | |
| 719 | tracker.startMonitoring(context, mTrackerHandler); |
| 720 | if (config.isDefault()) { |
| 721 | tracker.reconnect(); |
Robert Greenwalt | 6537b02 | 2011-11-10 16:55:20 -0800 | [diff] [blame] | 722 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 723 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 724 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 725 | mTethering = new Tethering(mContext, mNetd, statsService, this, mHandler.getLooper()); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 726 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 727 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 728 | IntentFilter intentFilter = new IntentFilter(); |
| 729 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 730 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 731 | mContext.registerReceiverAsUser( |
| 732 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 733 | mClat = new Nat464Xlat(mContext, mNetd, this, mTrackerHandler); |
| 734 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 735 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 736 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 737 | mNetd.registerObserver(mDataActivityObserver); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 738 | mNetd.registerObserver(mClat); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 739 | } catch (RemoteException e) { |
| 740 | loge("Error registering observer :" + e); |
| 741 | } |
| 742 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 743 | if (DBG) { |
| 744 | mInetLog = new ArrayList(); |
| 745 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 746 | |
| 747 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 748 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 749 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 750 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 751 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 752 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 753 | // start network sampling .. |
| 754 | Intent intent = new Intent(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED, null); |
| 755 | mSampleIntervalElapsedIntent = PendingIntent.getBroadcast(mContext, |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 756 | SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE, intent, 0); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 757 | |
| 758 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 759 | setAlarm(DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS * 1000, mSampleIntervalElapsedIntent); |
| 760 | |
| 761 | IntentFilter filter = new IntentFilter(); |
| 762 | filter.addAction(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 763 | mContext.registerReceiver( |
| 764 | new BroadcastReceiver() { |
| 765 | @Override |
| 766 | public void onReceive(Context context, Intent intent) { |
| 767 | String action = intent.getAction(); |
| 768 | if (action.equals(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED)) { |
| 769 | mHandler.sendMessage(mHandler.obtainMessage |
| 770 | (EVENT_SAMPLE_INTERVAL_ELAPSED)); |
| 771 | } |
| 772 | } |
| 773 | }, |
| 774 | new IntentFilter(filter)); |
| 775 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 776 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 777 | |
| 778 | filter = new IntentFilter(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 779 | filter.addAction(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 780 | mContext.registerReceiver(mProvisioningReceiver, filter); |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 781 | |
| 782 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 784 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 785 | private synchronized int nextNetworkRequestId() { |
| 786 | return mNextNetworkRequestId++; |
| 787 | } |
| 788 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 789 | private synchronized int nextNetId() { |
| 790 | int netId = mNextNetId; |
| 791 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 792 | return netId; |
| 793 | } |
| 794 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 795 | /** |
| 796 | * Factory that creates {@link NetworkStateTracker} instances using given |
| 797 | * {@link NetworkConfig}. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 798 | * |
| 799 | * TODO - this is obsolete and will be deleted. It's replaced by the |
| 800 | * registerNetworkFactory call and protocol. |
| 801 | * @Deprecated in favor of registerNetworkFactory dynamic bindings |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 802 | */ |
| 803 | public interface NetworkFactory { |
| 804 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config); |
| 805 | } |
| 806 | |
| 807 | private static class DefaultNetworkFactory implements NetworkFactory { |
| 808 | private final Context mContext; |
| 809 | private final Handler mTrackerHandler; |
| 810 | |
| 811 | public DefaultNetworkFactory(Context context, Handler trackerHandler) { |
| 812 | mContext = context; |
| 813 | mTrackerHandler = trackerHandler; |
| 814 | } |
| 815 | |
| 816 | @Override |
| 817 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config) { |
| 818 | switch (config.radio) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 819 | case TYPE_DUMMY: |
| 820 | return new DummyDataStateTracker(targetNetworkType, config.name); |
| 821 | case TYPE_BLUETOOTH: |
| 822 | return BluetoothTetheringDataTracker.getInstance(); |
| 823 | case TYPE_WIMAX: |
| 824 | return makeWimaxStateTracker(mContext, mTrackerHandler); |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 825 | case TYPE_PROXY: |
| 826 | return new ProxyDataTracker(); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 827 | default: |
| 828 | throw new IllegalArgumentException( |
| 829 | "Trying to create a NetworkStateTracker for an unknown radio type: " |
| 830 | + config.radio); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * Loads external WiMAX library and registers as system service, returning a |
| 837 | * {@link NetworkStateTracker} for WiMAX. Caller is still responsible for |
| 838 | * invoking {@link NetworkStateTracker#startMonitoring(Context, Handler)}. |
| 839 | */ |
| 840 | private static NetworkStateTracker makeWimaxStateTracker( |
| 841 | Context context, Handler trackerHandler) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 842 | // Initialize Wimax |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 843 | DexClassLoader wimaxClassLoader; |
| 844 | Class wimaxStateTrackerClass = null; |
| 845 | Class wimaxServiceClass = null; |
| 846 | Class wimaxManagerClass; |
| 847 | String wimaxJarLocation; |
| 848 | String wimaxLibLocation; |
| 849 | String wimaxManagerClassName; |
| 850 | String wimaxServiceClassName; |
| 851 | String wimaxStateTrackerClassName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 852 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 853 | NetworkStateTracker wimaxStateTracker = null; |
| 854 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 855 | boolean isWimaxEnabled = context.getResources().getBoolean( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 856 | com.android.internal.R.bool.config_wimaxEnabled); |
| 857 | |
| 858 | if (isWimaxEnabled) { |
| 859 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 860 | wimaxJarLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 861 | com.android.internal.R.string.config_wimaxServiceJarLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 862 | wimaxLibLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 863 | com.android.internal.R.string.config_wimaxNativeLibLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 864 | wimaxManagerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 865 | com.android.internal.R.string.config_wimaxManagerClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 866 | wimaxServiceClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 867 | com.android.internal.R.string.config_wimaxServiceClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 868 | wimaxStateTrackerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 869 | com.android.internal.R.string.config_wimaxStateTrackerClassname); |
| 870 | |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 871 | if (DBG) log("wimaxJarLocation: " + wimaxJarLocation); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 872 | wimaxClassLoader = new DexClassLoader(wimaxJarLocation, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 873 | new ContextWrapper(context).getCacheDir().getAbsolutePath(), |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 874 | wimaxLibLocation, ClassLoader.getSystemClassLoader()); |
| 875 | |
| 876 | try { |
| 877 | wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName); |
| 878 | wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName); |
| 879 | wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName); |
| 880 | } catch (ClassNotFoundException ex) { |
| 881 | loge("Exception finding Wimax classes: " + ex.toString()); |
| 882 | return null; |
| 883 | } |
| 884 | } catch(Resources.NotFoundException ex) { |
| 885 | loge("Wimax Resources does not exist!!! "); |
| 886 | return null; |
| 887 | } |
| 888 | |
| 889 | try { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 890 | if (DBG) log("Starting Wimax Service... "); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 891 | |
| 892 | Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor |
| 893 | (new Class[] {Context.class, Handler.class}); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 894 | wimaxStateTracker = (NetworkStateTracker) wmxStTrkrConst.newInstance( |
| 895 | context, trackerHandler); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 896 | |
| 897 | Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor |
| 898 | (new Class[] {Context.class, wimaxStateTrackerClass}); |
| 899 | wmxSrvConst.setAccessible(true); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 900 | IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(context, wimaxStateTracker); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 901 | wmxSrvConst.setAccessible(false); |
| 902 | |
| 903 | ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker); |
| 904 | |
| 905 | } catch(Exception ex) { |
| 906 | loge("Exception creating Wimax classes: " + ex.toString()); |
| 907 | return null; |
| 908 | } |
| 909 | } else { |
| 910 | loge("Wimax is not enabled or not added to the network attributes!!! "); |
| 911 | return null; |
| 912 | } |
| 913 | |
| 914 | return wimaxStateTracker; |
| 915 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 916 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 917 | private int getConnectivityChangeDelay() { |
| 918 | final ContentResolver cr = mContext.getContentResolver(); |
| 919 | |
| 920 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 921 | int defaultDelay = SystemProperties.getInt( |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 922 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 923 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 924 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 925 | defaultDelay); |
| 926 | } |
| 927 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 928 | private boolean teardown(NetworkStateTracker netTracker) { |
| 929 | if (netTracker.teardown()) { |
| 930 | netTracker.setTeardownRequested(true); |
| 931 | return true; |
| 932 | } else { |
| 933 | return false; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 938 | * Check if UID should be blocked from using the network represented by the |
| 939 | * given {@link NetworkStateTracker}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 940 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 941 | private boolean isNetworkBlocked(int networkType, int uid) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 942 | final boolean networkCostly; |
| 943 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 944 | |
| 945 | LinkProperties lp = getLinkPropertiesForType(networkType); |
| 946 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 947 | synchronized (mRulesLock) { |
| 948 | networkCostly = mMeteredIfaces.contains(iface); |
| 949 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 950 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 951 | |
| 952 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 953 | return true; |
| 954 | } |
| 955 | |
| 956 | // no restrictive rules; network is visible |
| 957 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 961 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 962 | * {@link DetailedState#BLOCKED} based on |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 963 | * {@link #isNetworkBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 964 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 965 | private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) { |
| 966 | NetworkInfo info = getNetworkInfoForType(networkType); |
| 967 | if (isNetworkBlocked(networkType, uid)) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 968 | // network is blocked; clone and override state |
| 969 | info = new NetworkInfo(info); |
| 970 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 971 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 972 | if (mLockdownTracker != null) { |
| 973 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 974 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 975 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 980 | * It is assumed that at most one network is active at a time. If more |
| 981 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 982 | * @return the info for the active network, or {@code null} if none is |
| 983 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 985 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 987 | enforceAccessPermission(); |
| 988 | final int uid = Binder.getCallingUid(); |
| 989 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 992 | // only called when the default request is satisfied |
| 993 | private void updateActiveDefaultNetwork(NetworkAgentInfo nai) { |
| 994 | if (nai != null) { |
| 995 | mActiveDefaultNetwork = nai.networkInfo.getType(); |
| 996 | } else { |
| 997 | mActiveDefaultNetwork = TYPE_NONE; |
| 998 | } |
| 999 | } |
| 1000 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1001 | /** |
| 1002 | * Find the first Provisioning network. |
| 1003 | * |
| 1004 | * @return NetworkInfo or null if none. |
| 1005 | */ |
| 1006 | private NetworkInfo getProvisioningNetworkInfo() { |
| 1007 | enforceAccessPermission(); |
| 1008 | |
| 1009 | // Find the first Provisioning Network |
| 1010 | NetworkInfo provNi = null; |
| 1011 | for (NetworkInfo ni : getAllNetworkInfo()) { |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 1012 | if (ni.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1013 | provNi = ni; |
| 1014 | break; |
| 1015 | } |
| 1016 | } |
| 1017 | if (DBG) log("getProvisioningNetworkInfo: X provNi=" + provNi); |
| 1018 | return provNi; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * Find the first Provisioning network or the ActiveDefaultNetwork |
| 1023 | * if there is no Provisioning network |
| 1024 | * |
| 1025 | * @return NetworkInfo or null if none. |
| 1026 | */ |
| 1027 | @Override |
| 1028 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 1029 | enforceAccessPermission(); |
| 1030 | |
| 1031 | NetworkInfo provNi = getProvisioningNetworkInfo(); |
| 1032 | if (provNi == null) { |
| 1033 | final int uid = Binder.getCallingUid(); |
| 1034 | provNi = getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1035 | } |
| 1036 | if (DBG) log("getProvisioningOrActiveNetworkInfo: X provNi=" + provNi); |
| 1037 | return provNi; |
| 1038 | } |
| 1039 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1040 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1041 | enforceAccessPermission(); |
| 1042 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1043 | return getNetworkInfoForType(mActiveDefaultNetwork); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1044 | } |
| 1045 | return null; |
| 1046 | } |
| 1047 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1048 | @Override |
| 1049 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1050 | enforceConnectivityInternalPermission(); |
| 1051 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1052 | } |
| 1053 | |
| 1054 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1056 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1057 | final int uid = Binder.getCallingUid(); |
| 1058 | return getNetworkInfo(networkType, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1061 | private NetworkInfo getNetworkInfo(int networkType, int uid) { |
| 1062 | NetworkInfo info = null; |
| 1063 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1064 | if (getNetworkInfoForType(networkType) != null) { |
| 1065 | info = getFilteredNetworkInfo(networkType, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | return info; |
| 1069 | } |
| 1070 | |
| 1071 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | public NetworkInfo[] getAllNetworkInfo() { |
| 1073 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1074 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1075 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1076 | synchronized (mRulesLock) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1077 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1078 | networkType++) { |
| 1079 | if (getNetworkInfoForType(networkType) != null) { |
| 1080 | result.add(getFilteredNetworkInfo(networkType, uid)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1084 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1085 | } |
| 1086 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1087 | @Override |
| 1088 | public boolean isNetworkSupported(int networkType) { |
| 1089 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1090 | return (isNetworkTypeValid(networkType) && (getNetworkInfoForType(networkType) != null)); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1091 | } |
| 1092 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1093 | /** |
| 1094 | * Return LinkProperties for the active (i.e., connected) default |
| 1095 | * network interface. It is assumed that at most one default network |
| 1096 | * is active at a time. If more than one is active, it is indeterminate |
| 1097 | * which will be returned. |
| 1098 | * @return the ip properties for the active network, or {@code null} if |
| 1099 | * none is active |
| 1100 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1101 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1102 | public LinkProperties getActiveLinkProperties() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1103 | return getLinkPropertiesForType(mActiveDefaultNetwork); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1106 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1107 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1108 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1109 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1110 | return getLinkPropertiesForTypeInternal(networkType); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1111 | } |
| 1112 | return null; |
| 1113 | } |
| 1114 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1115 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1116 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1117 | public LinkProperties getLinkProperties(Network network) { |
| 1118 | enforceAccessPermission(); |
| 1119 | NetworkAgentInfo nai = mNetworkForNetId.get(network.netId); |
| 1120 | if (nai != null) return new LinkProperties(nai.linkProperties); |
| 1121 | return null; |
| 1122 | } |
| 1123 | |
| 1124 | @Override |
| 1125 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1126 | enforceAccessPermission(); |
| 1127 | NetworkAgentInfo nai = mNetworkForNetId.get(network.netId); |
| 1128 | if (nai != null) return new NetworkCapabilities(nai.networkCapabilities); |
| 1129 | return null; |
| 1130 | } |
| 1131 | |
| 1132 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1133 | public NetworkState[] getAllNetworkState() { |
| 1134 | enforceAccessPermission(); |
| 1135 | final int uid = Binder.getCallingUid(); |
| 1136 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1137 | synchronized (mRulesLock) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1138 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1139 | networkType++) { |
| 1140 | if (getNetworkInfoForType(networkType) != null) { |
| 1141 | final NetworkInfo info = getFilteredNetworkInfo(networkType, uid); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1142 | final LinkProperties lp = getLinkPropertiesForTypeInternal(networkType); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1143 | final NetworkCapabilities netcap = getNetworkCapabilitiesForType(networkType); |
| 1144 | result.add(new NetworkState(info, lp, netcap)); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | return result.toArray(new NetworkState[result.size()]); |
| 1149 | } |
| 1150 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1151 | private NetworkState getNetworkStateUnchecked(int networkType) { |
| 1152 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1153 | NetworkInfo info = getNetworkInfoForType(networkType); |
| 1154 | if (info != null) { |
| 1155 | return new NetworkState(info, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1156 | getLinkPropertiesForTypeInternal(networkType), |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1157 | getNetworkCapabilitiesForType(networkType)); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | return null; |
| 1161 | } |
| 1162 | |
| 1163 | @Override |
| 1164 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1165 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1166 | |
| 1167 | final long token = Binder.clearCallingIdentity(); |
| 1168 | try { |
| 1169 | final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork); |
| 1170 | if (state != null) { |
| 1171 | try { |
| 1172 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1173 | } catch (RemoteException e) { |
| 1174 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1175 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1176 | return null; |
| 1177 | } finally { |
| 1178 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1179 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1182 | @Override |
| 1183 | public boolean isActiveNetworkMetered() { |
| 1184 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1185 | final long token = Binder.clearCallingIdentity(); |
| 1186 | try { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1187 | return isNetworkMeteredUnchecked(mActiveDefaultNetwork); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1188 | } finally { |
| 1189 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1190 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1193 | private boolean isNetworkMeteredUnchecked(int networkType) { |
| 1194 | final NetworkState state = getNetworkStateUnchecked(networkType); |
| 1195 | if (state != null) { |
| 1196 | try { |
| 1197 | return mPolicyManager.isNetworkMetered(state); |
| 1198 | } catch (RemoteException e) { |
| 1199 | } |
| 1200 | } |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1204 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1205 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1206 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1207 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1208 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1209 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1210 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1211 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1212 | /** |
| 1213 | * Used to notice when the calling process dies so we can self-expire |
| 1214 | * |
| 1215 | * Also used to know if the process has cleaned up after itself when |
| 1216 | * our auto-expire timer goes off. The timer has a link to an object. |
| 1217 | * |
| 1218 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1219 | private class FeatureUser implements IBinder.DeathRecipient { |
| 1220 | int mNetworkType; |
| 1221 | String mFeature; |
| 1222 | IBinder mBinder; |
| 1223 | int mPid; |
| 1224 | int mUid; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1225 | long mCreateTime; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1226 | |
| 1227 | FeatureUser(int type, String feature, IBinder binder) { |
| 1228 | super(); |
| 1229 | mNetworkType = type; |
| 1230 | mFeature = feature; |
| 1231 | mBinder = binder; |
| 1232 | mPid = getCallingPid(); |
| 1233 | mUid = getCallingUid(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1234 | mCreateTime = System.currentTimeMillis(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1235 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1236 | try { |
| 1237 | mBinder.linkToDeath(this, 0); |
| 1238 | } catch (RemoteException e) { |
| 1239 | binderDied(); |
| 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | void unlinkDeathRecipient() { |
| 1244 | mBinder.unlinkToDeath(this, 0); |
| 1245 | } |
| 1246 | |
| 1247 | public void binderDied() { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1248 | log("ConnectivityService FeatureUser binderDied(" + |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1249 | mNetworkType + ", " + mFeature + ", " + mBinder + "), created " + |
| 1250 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1251 | stopUsingNetworkFeature(this, false); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1254 | public void expire() { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1255 | if (VDBG) { |
| 1256 | log("ConnectivityService FeatureUser expire(" + |
| 1257 | mNetworkType + ", " + mFeature + ", " + mBinder +"), created " + |
| 1258 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
| 1259 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1260 | stopUsingNetworkFeature(this, false); |
| 1261 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1262 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1263 | public boolean isSameUser(FeatureUser u) { |
| 1264 | if (u == null) return false; |
| 1265 | |
| 1266 | return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature); |
| 1267 | } |
| 1268 | |
| 1269 | public boolean isSameUser(int pid, int uid, int networkType, String feature) { |
| 1270 | if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) && |
| 1271 | TextUtils.equals(mFeature, feature)) { |
| 1272 | return true; |
| 1273 | } |
| 1274 | return false; |
| 1275 | } |
| 1276 | |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1277 | public String toString() { |
| 1278 | return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " + |
| 1279 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"; |
| 1280 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1283 | // javadoc from interface |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1284 | public int startUsingNetworkFeature(int networkType, String feature, |
| 1285 | IBinder binder) { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1286 | long startTime = 0; |
| 1287 | if (DBG) { |
| 1288 | startTime = SystemClock.elapsedRealtime(); |
| 1289 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1290 | if (VDBG) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1291 | log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid=" |
| 1292 | + Binder.getCallingUid()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1294 | enforceChangePermission(); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1295 | try { |
| 1296 | if (!ConnectivityManager.isNetworkTypeValid(networkType) || |
| 1297 | mNetConfigs[networkType] == null) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1298 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1299 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1300 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1301 | FeatureUser f = new FeatureUser(networkType, feature, binder); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1302 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1303 | // TODO - move this into individual networktrackers |
| 1304 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1305 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1306 | if (mLockdownEnabled) { |
| 1307 | // Since carrier APNs usually aren't available from VPN |
| 1308 | // endpoint, mark them as unavailable. |
| 1309 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
| 1310 | } |
| 1311 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1312 | if (mProtectedNetworks.contains(usedNetworkType)) { |
| 1313 | enforceConnectivityInternalPermission(); |
| 1314 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1315 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1316 | // if UID is restricted, don't allow them to bring up metered APNs |
| 1317 | final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType); |
| 1318 | final int uidRules; |
| 1319 | synchronized (mRulesLock) { |
| 1320 | uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL); |
| 1321 | } |
| 1322 | if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1323 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1324 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1325 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1326 | NetworkStateTracker network = mNetTrackers[usedNetworkType]; |
| 1327 | if (network != null) { |
| 1328 | Integer currentPid = new Integer(getCallingPid()); |
| 1329 | if (usedNetworkType != networkType) { |
| 1330 | NetworkInfo ni = network.getNetworkInfo(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1331 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1332 | if (ni.isAvailable() == false) { |
| 1333 | if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 1334 | if (DBG) log("special network not available ni=" + ni.getTypeName()); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1335 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1336 | } else { |
| 1337 | // else make the attempt anyway - probably giving REQUEST_STARTED below |
| 1338 | if (DBG) { |
| 1339 | log("special network not available, but try anyway ni=" + |
| 1340 | ni.getTypeName()); |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1345 | int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1346 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1347 | synchronized(this) { |
| 1348 | boolean addToList = true; |
| 1349 | if (restoreTimer < 0) { |
| 1350 | // In case there is no timer is specified for the feature, |
| 1351 | // make sure we don't add duplicate entry with the same request. |
| 1352 | for (FeatureUser u : mFeatureUsers) { |
| 1353 | if (u.isSameUser(f)) { |
| 1354 | // Duplicate user is found. Do not add. |
| 1355 | addToList = false; |
| 1356 | break; |
| 1357 | } |
| 1358 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1359 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1360 | |
| 1361 | if (addToList) mFeatureUsers.add(f); |
| 1362 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1363 | // this gets used for per-pid dns when connected |
| 1364 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1365 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1366 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1367 | |
| 1368 | if (restoreTimer >= 0) { |
| 1369 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1370 | EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer); |
| 1371 | } |
| 1372 | |
| 1373 | if ((ni.isConnectedOrConnecting() == true) && |
| 1374 | !network.isTeardownRequested()) { |
| 1375 | if (ni.isConnected() == true) { |
| 1376 | final long token = Binder.clearCallingIdentity(); |
| 1377 | try { |
| 1378 | // add the pid-specific dns |
| 1379 | handleDnsConfigurationChange(usedNetworkType); |
| 1380 | if (VDBG) log("special network already active"); |
| 1381 | } finally { |
| 1382 | Binder.restoreCallingIdentity(token); |
| 1383 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1384 | return PhoneConstants.APN_ALREADY_ACTIVE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1385 | } |
| 1386 | if (VDBG) log("special network already connecting"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1387 | return PhoneConstants.APN_REQUEST_STARTED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | // check if the radio in play can make another contact |
| 1391 | // assume if cannot for now |
| 1392 | |
| 1393 | if (DBG) { |
| 1394 | log("startUsingNetworkFeature reconnecting to " + networkType + ": " + |
| 1395 | feature); |
| 1396 | } |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1397 | if (network.reconnect()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1398 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_STARTED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1399 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1400 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1401 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_FAILED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1402 | return PhoneConstants.APN_REQUEST_FAILED; |
| 1403 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1404 | } else { |
| 1405 | // need to remember this unsupported request so we respond appropriately on stop |
| 1406 | synchronized(this) { |
| 1407 | mFeatureUsers.add(f); |
| 1408 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1409 | // this gets used for per-pid dns when connected |
| 1410 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1411 | } |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1412 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1413 | if (DBG) log("startUsingNetworkFeature X: return -1 unsupported feature."); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1414 | return -1; |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1415 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1416 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1417 | if (DBG) log("startUsingNetworkFeature X: return APN_TYPE_NOT_AVAILABLE"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1418 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1419 | } finally { |
| 1420 | if (DBG) { |
| 1421 | final long execTime = SystemClock.elapsedRealtime() - startTime; |
| 1422 | if (execTime > 250) { |
| 1423 | loge("startUsingNetworkFeature took too long: " + execTime + "ms"); |
| 1424 | } else { |
| 1425 | if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms"); |
| 1426 | } |
| 1427 | } |
| 1428 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | } |
| 1430 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1431 | // javadoc from interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1432 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1433 | enforceChangePermission(); |
| 1434 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1435 | int pid = getCallingPid(); |
| 1436 | int uid = getCallingUid(); |
| 1437 | |
| 1438 | FeatureUser u = null; |
| 1439 | boolean found = false; |
| 1440 | |
| 1441 | synchronized(this) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1442 | for (FeatureUser x : mFeatureUsers) { |
| 1443 | if (x.isSameUser(pid, uid, networkType, feature)) { |
| 1444 | u = x; |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1445 | found = true; |
| 1446 | break; |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | if (found && u != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1451 | if (VDBG) log("stopUsingNetworkFeature: X"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1452 | // stop regardless of how many other time this proc had called start |
| 1453 | return stopUsingNetworkFeature(u, true); |
| 1454 | } else { |
| 1455 | // none found! |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1456 | if (VDBG) log("stopUsingNetworkFeature: X not a live request, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1457 | return 1; |
| 1458 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1461 | private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) { |
| 1462 | int networkType = u.mNetworkType; |
| 1463 | String feature = u.mFeature; |
| 1464 | int pid = u.mPid; |
| 1465 | int uid = u.mUid; |
| 1466 | |
| 1467 | NetworkStateTracker tracker = null; |
| 1468 | boolean callTeardown = false; // used to carry our decision outside of sync block |
| 1469 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1470 | if (VDBG) { |
| 1471 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1472 | } |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1473 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1474 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1475 | if (DBG) { |
| 1476 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1477 | ", net is invalid"); |
| 1478 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1479 | return -1; |
| 1480 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1481 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1482 | // need to link the mFeatureUsers list with the mNetRequestersPids state in this |
| 1483 | // sync block |
| 1484 | synchronized(this) { |
| 1485 | // check if this process still has an outstanding start request |
| 1486 | if (!mFeatureUsers.contains(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1487 | if (VDBG) { |
| 1488 | log("stopUsingNetworkFeature: this process has no outstanding requests" + |
| 1489 | ", ignoring"); |
| 1490 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1491 | return 1; |
| 1492 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1493 | u.unlinkDeathRecipient(); |
| 1494 | mFeatureUsers.remove(mFeatureUsers.indexOf(u)); |
| 1495 | // If we care about duplicate requests, check for that here. |
| 1496 | // |
| 1497 | // This is done to support the extension of a request - the app |
| 1498 | // can request we start the network feature again and renew the |
| 1499 | // auto-shutoff delay. Normal "stop" calls from the app though |
| 1500 | // do not pay attention to duplicate requests - in effect the |
| 1501 | // API does not refcount and a single stop will counter multiple starts. |
| 1502 | if (ignoreDups == false) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1503 | for (FeatureUser x : mFeatureUsers) { |
| 1504 | if (x.isSameUser(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1505 | if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1506 | return 1; |
| 1507 | } |
| 1508 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1509 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1510 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 1511 | // TODO - move to individual network trackers |
| 1512 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
| 1513 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1514 | tracker = mNetTrackers[usedNetworkType]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1515 | if (tracker == null) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1516 | if (DBG) { |
| 1517 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1518 | " no known tracker for used net type " + usedNetworkType); |
| 1519 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1520 | return -1; |
| 1521 | } |
| 1522 | if (usedNetworkType != networkType) { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1523 | Integer currentPid = new Integer(pid); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1524 | mNetRequestersPids[usedNetworkType].remove(currentPid); |
Robert Greenwalt | 9055ae3 | 2013-02-12 17:18:25 -0800 | [diff] [blame] | 1525 | |
| 1526 | final long token = Binder.clearCallingIdentity(); |
| 1527 | try { |
| 1528 | reassessPidDns(pid, true); |
| 1529 | } finally { |
| 1530 | Binder.restoreCallingIdentity(token); |
| 1531 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1532 | flushVmDnsCache(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1533 | if (mNetRequestersPids[usedNetworkType].size() != 0) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1534 | if (VDBG) { |
| 1535 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1536 | " others still using it"); |
| 1537 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1538 | return 1; |
| 1539 | } |
| 1540 | callTeardown = true; |
Robert Greenwalt | 19b9ab4 | 2011-01-10 11:58:31 -0800 | [diff] [blame] | 1541 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1542 | if (DBG) { |
| 1543 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1544 | " not a known feature - dropping"); |
| 1545 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1546 | } |
| 1547 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1548 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1549 | if (callTeardown) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1550 | if (DBG) { |
| 1551 | log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature); |
| 1552 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1553 | tracker.teardown(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1554 | return 1; |
| 1555 | } else { |
Robert Greenwalt | 02648a4 | 2010-05-18 10:52:51 -0700 | [diff] [blame] | 1556 | return -1; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1557 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /** |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1561 | * Check if the address falls into any of currently running VPN's route's. |
| 1562 | */ |
| 1563 | private boolean isAddressUnderVpn(InetAddress address) { |
| 1564 | synchronized (mVpns) { |
| 1565 | synchronized (mRoutesLock) { |
| 1566 | int uid = UserHandle.getCallingUserId(); |
| 1567 | Vpn vpn = mVpns.get(uid); |
| 1568 | if (vpn == null) { |
| 1569 | return false; |
| 1570 | } |
| 1571 | |
| 1572 | // Check if an exemption exists for this address. |
| 1573 | for (LinkAddress destination : mExemptAddresses) { |
| 1574 | if (!NetworkUtils.addressTypeMatches(address, destination.getAddress())) { |
| 1575 | continue; |
| 1576 | } |
| 1577 | |
| 1578 | int prefix = destination.getNetworkPrefixLength(); |
| 1579 | InetAddress addrMasked = NetworkUtils.getNetworkPart(address, prefix); |
| 1580 | InetAddress destMasked = NetworkUtils.getNetworkPart(destination.getAddress(), |
| 1581 | prefix); |
| 1582 | |
| 1583 | if (addrMasked.equals(destMasked)) { |
| 1584 | return false; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | // Finally check if the address is covered by the VPN. |
| 1589 | return vpn.isAddressCovered(address); |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1595 | * @deprecated use requestRouteToHostAddress instead |
| 1596 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1597 | * Ensure that a network route exists to deliver traffic to the specified |
| 1598 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1599 | * @param networkType the type of the network over which traffic to the |
| 1600 | * specified host is to be routed |
| 1601 | * @param hostAddress the IP address of the host to which the route is |
| 1602 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1603 | * @return {@code true} on success, {@code false} on failure |
| 1604 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1605 | public boolean requestRouteToHost(int networkType, int hostAddress, String packageName) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1606 | InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); |
| 1607 | |
| 1608 | if (inetAddress == null) { |
| 1609 | return false; |
| 1610 | } |
| 1611 | |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1612 | return requestRouteToHostAddress(networkType, inetAddress.getAddress(), packageName); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * Ensure that a network route exists to deliver traffic to the specified |
| 1617 | * host via the specified network interface. |
| 1618 | * @param networkType the type of the network over which traffic to the |
| 1619 | * specified host is to be routed |
| 1620 | * @param hostAddress the IP address of the host to which the route is |
| 1621 | * desired |
| 1622 | * @return {@code true} on success, {@code false} on failure |
| 1623 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1624 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress, |
| 1625 | String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1627 | if (mProtectedNetworks.contains(networkType)) { |
| 1628 | enforceConnectivityInternalPermission(); |
| 1629 | } |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1630 | boolean exempt; |
| 1631 | InetAddress addr; |
| 1632 | try { |
| 1633 | addr = InetAddress.getByAddress(hostAddress); |
| 1634 | } catch (UnknownHostException e) { |
| 1635 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1636 | return false; |
| 1637 | } |
| 1638 | // System apps may request routes bypassing the VPN to keep other networks working. |
| 1639 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1640 | exempt = true; |
| 1641 | } else { |
| 1642 | mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); |
| 1643 | try { |
| 1644 | ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(packageName, |
| 1645 | 0); |
| 1646 | exempt = (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1647 | } catch (NameNotFoundException e) { |
| 1648 | throw new IllegalArgumentException("Failed to find calling package details", e); |
| 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | // Non-exempt routeToHost's can only be added if the host is not covered by the VPN. |
| 1653 | // This can be either because the VPN's routes do not cover the destination or a |
| 1654 | // system application added an exemption that covers this destination. |
| 1655 | if (!exempt && isAddressUnderVpn(addr)) { |
| 1656 | return false; |
| 1657 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1658 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1660 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1661 | return false; |
| 1662 | } |
| 1663 | NetworkStateTracker tracker = mNetTrackers[networkType]; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1664 | DetailedState netState = DetailedState.DISCONNECTED; |
| 1665 | if (tracker != null) { |
| 1666 | netState = tracker.getNetworkInfo().getDetailedState(); |
| 1667 | } |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1668 | |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1669 | if ((netState != DetailedState.CONNECTED && |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 1670 | netState != DetailedState.CAPTIVE_PORTAL_CHECK) || |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1671 | tracker.isTeardownRequested()) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1672 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1673 | log("requestRouteToHostAddress on down network " |
| 1674 | + "(" + networkType + ") - dropped" |
| 1675 | + " tracker=" + tracker |
| 1676 | + " netState=" + netState |
| 1677 | + " isTeardownRequested=" |
| 1678 | + ((tracker != null) ? tracker.isTeardownRequested() : "tracker:null")); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1679 | } |
| 1680 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1681 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1682 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1683 | try { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1684 | LinkProperties lp = tracker.getLinkProperties(); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1685 | boolean ok = addRouteToAddress(lp, addr, exempt, tracker.getNetwork().netId); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1686 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1687 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1688 | } finally { |
| 1689 | Binder.restoreCallingIdentity(token); |
| 1690 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1691 | } |
| 1692 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1693 | private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1694 | boolean exempt, int netId) { |
| 1695 | return modifyRoute(p, r, 0, ADD, toDefaultTable, exempt, netId); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1698 | private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, int netId) { |
| 1699 | return modifyRoute(p, r, 0, REMOVE, toDefaultTable, UNEXEMPT, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1702 | private boolean addRouteToAddress(LinkProperties lp, InetAddress addr, boolean exempt, |
| 1703 | int netId) { |
| 1704 | return modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE, exempt, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1707 | private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr, int netId) { |
| 1708 | return modifyRouteToAddress(lp, addr, REMOVE, TO_DEFAULT_TABLE, UNEXEMPT, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1711 | private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1712 | boolean toDefaultTable, boolean exempt, int netId) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1713 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1714 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1715 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1716 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1717 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1718 | if (bestRoute.getGateway().equals(addr)) { |
| 1719 | // 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] | 1720 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1721 | } else { |
| 1722 | // if we will connect to this through another route, add a direct route |
| 1723 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1724 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1725 | } |
| 1726 | } |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1727 | return modifyRoute(lp, bestRoute, 0, doAdd, toDefaultTable, exempt, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1730 | private boolean modifyRoute(LinkProperties lp, RouteInfo r, int cycleCount, boolean doAdd, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1731 | boolean toDefaultTable, boolean exempt, int netId) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1732 | if ((lp == null) || (r == null)) { |
| 1733 | if (DBG) log("modifyRoute got unexpected null: " + lp + ", " + r); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1734 | return false; |
| 1735 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1736 | |
| 1737 | if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1738 | loge("Error modifying route - too much recursion"); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1739 | return false; |
| 1740 | } |
| 1741 | |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1742 | String ifaceName = r.getInterface(); |
| 1743 | if(ifaceName == null) { |
| 1744 | loge("Error modifying route - no interface name"); |
| 1745 | return false; |
| 1746 | } |
Robert Greenwalt | ca441ee | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 1747 | if (r.hasGateway()) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1748 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), r.getGateway()); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1749 | if (bestRoute != null) { |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1750 | if (bestRoute.getGateway().equals(r.getGateway())) { |
| 1751 | // 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] | 1752 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), ifaceName); |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1753 | } else { |
| 1754 | // if we will connect to our gateway through another route, add a direct |
| 1755 | // route to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1756 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), |
| 1757 | bestRoute.getGateway(), |
| 1758 | ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1759 | } |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1760 | modifyRoute(lp, bestRoute, cycleCount+1, doAdd, toDefaultTable, exempt, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | if (doAdd) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1764 | if (VDBG) log("Adding " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1765 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1766 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1767 | synchronized (mRoutesLock) { |
| 1768 | // only track default table - only one apps can effect |
| 1769 | mAddedRoutes.add(r); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1770 | mNetd.addRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1771 | if (exempt) { |
| 1772 | LinkAddress dest = r.getDestination(); |
| 1773 | if (!mExemptAddresses.contains(dest)) { |
| 1774 | mNetd.setHostExemption(dest); |
| 1775 | mExemptAddresses.add(dest); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1779 | } else { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1780 | mNetd.addRoute(netId, r); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1781 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1782 | } catch (Exception e) { |
| 1783 | // never crash - catch them all |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1784 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1785 | return false; |
| 1786 | } |
| 1787 | } else { |
| 1788 | // if we remove this one and there are no more like it, then refcount==0 and |
| 1789 | // we can remove it from the table |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1790 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1791 | synchronized (mRoutesLock) { |
| 1792 | mAddedRoutes.remove(r); |
| 1793 | if (mAddedRoutes.contains(r) == false) { |
| 1794 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 1795 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1796 | mNetd.removeRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1797 | LinkAddress dest = r.getDestination(); |
| 1798 | if (mExemptAddresses.contains(dest)) { |
| 1799 | mNetd.clearHostExemption(dest); |
| 1800 | mExemptAddresses.remove(dest); |
| 1801 | } |
| 1802 | } catch (Exception e) { |
| 1803 | // never crash - catch them all |
| 1804 | if (VDBG) loge("Exception trying to remove a route: " + e); |
| 1805 | return false; |
| 1806 | } |
| 1807 | } else { |
| 1808 | if (VDBG) log("not removing " + r + " as it's still in use"); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1809 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1810 | } |
| 1811 | } else { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1812 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1813 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1814 | mNetd.removeRoute(netId, r); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1815 | } catch (Exception e) { |
| 1816 | // never crash - catch them all |
Robert Greenwalt | 2473a4ba | 2012-04-23 18:00:37 -0700 | [diff] [blame] | 1817 | if (VDBG) loge("Exception trying to remove a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1818 | return false; |
| 1819 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1820 | } |
| 1821 | } |
| 1822 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1826 | * @see ConnectivityManager#getMobileDataEnabled() |
| 1827 | */ |
| 1828 | public boolean getMobileDataEnabled() { |
Wink Saville | e798268 | 2010-12-07 10:31:02 -0800 | [diff] [blame] | 1829 | // TODO: This detail should probably be in DataConnectionTracker's |
| 1830 | // which is where we store the value and maybe make this |
| 1831 | // asynchronous. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1832 | enforceAccessPermission(); |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 1833 | boolean retVal = Settings.Global.getInt(mContext.getContentResolver(), |
| 1834 | Settings.Global.MOBILE_DATA, 1) == 1; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1835 | if (VDBG) log("getMobileDataEnabled returning " + retVal); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1836 | return retVal; |
| 1837 | } |
| 1838 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1839 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1840 | enforceConnectivityInternalPermission(); |
| 1841 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1842 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1843 | (met ? ENABLED : DISABLED), networkType)); |
| 1844 | } |
| 1845 | |
| 1846 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1847 | if (mNetTrackers[networkType] != null) { |
| 1848 | if (DBG) { |
| 1849 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1850 | } |
| 1851 | mNetTrackers[networkType].setDependencyMet(met); |
| 1852 | } |
| 1853 | } |
| 1854 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1855 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1856 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1857 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1858 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1859 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1860 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1861 | } |
| 1862 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1863 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1864 | // skip update when we've already applied rules |
| 1865 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1866 | if (oldRules == uidRules) return; |
| 1867 | |
| 1868 | mUidRules.put(uid, uidRules); |
| 1869 | } |
| 1870 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1871 | // TODO: notify UID when it has requested targeted updates |
| 1872 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1873 | |
| 1874 | @Override |
| 1875 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1876 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1877 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1878 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | synchronized (mRulesLock) { |
| 1882 | mMeteredIfaces.clear(); |
| 1883 | for (String iface : meteredIfaces) { |
| 1884 | mMeteredIfaces.add(iface); |
| 1885 | } |
| 1886 | } |
| 1887 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1888 | |
| 1889 | @Override |
| 1890 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1891 | // caller is NPMS, since we only register with them |
| 1892 | if (LOGD_RULES) { |
| 1893 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1894 | } |
| 1895 | |
| 1896 | // kick off connectivity change broadcast for active network, since |
| 1897 | // global background policy change is radical. |
| 1898 | final int networkType = mActiveDefaultNetwork; |
| 1899 | if (isNetworkTypeValid(networkType)) { |
| 1900 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1901 | if (tracker != null) { |
| 1902 | final NetworkInfo info = tracker.getNetworkInfo(); |
| 1903 | if (info != null && info.isConnected()) { |
| 1904 | sendConnectedBroadcast(info); |
| 1905 | } |
| 1906 | } |
| 1907 | } |
| 1908 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1909 | }; |
| 1910 | |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1911 | /** |
| 1912 | * @see ConnectivityManager#setMobileDataEnabled(boolean) |
| 1913 | */ |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1914 | public void setMobileDataEnabled(boolean enabled) { |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1915 | enforceChangePermission(); |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1916 | if (DBG) log("setMobileDataEnabled(" + enabled + ")"); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1917 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1918 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA, |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1919 | (enabled ? ENABLED : DISABLED), 0)); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | private void handleSetMobileData(boolean enabled) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1923 | // TODO - handle this - probably generalize passing in a transport type and send to the |
| 1924 | // factories? |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | @Override |
| 1928 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1929 | // only someone like NPMS should only be calling us |
| 1930 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1931 | |
| 1932 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1933 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 1934 | } |
| 1935 | |
| 1936 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1937 | // TODO - handle this passing to factories |
| 1938 | // if (isNetworkTypeValid(networkType)) { |
| 1939 | // final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1940 | // if (tracker != null) { |
| 1941 | // tracker.setPolicyDataEnable(enabled); |
| 1942 | // } |
| 1943 | // } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1944 | } |
| 1945 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1946 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1947 | mContext.enforceCallingOrSelfPermission( |
| 1948 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1949 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1953 | mContext.enforceCallingOrSelfPermission( |
| 1954 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1955 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1956 | } |
| 1957 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1958 | // TODO Make this a special check when it goes public |
| 1959 | private void enforceTetherChangePermission() { |
| 1960 | mContext.enforceCallingOrSelfPermission( |
| 1961 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1962 | "ConnectivityService"); |
| 1963 | } |
| 1964 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1965 | private void enforceTetherAccessPermission() { |
| 1966 | mContext.enforceCallingOrSelfPermission( |
| 1967 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1968 | "ConnectivityService"); |
| 1969 | } |
| 1970 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1971 | private void enforceConnectivityInternalPermission() { |
| 1972 | mContext.enforceCallingOrSelfPermission( |
| 1973 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1974 | "ConnectivityService"); |
| 1975 | } |
| 1976 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 1977 | private void enforceMarkNetworkSocketPermission() { |
| 1978 | //Media server special case |
| 1979 | if (Binder.getCallingUid() == Process.MEDIA_UID) { |
| 1980 | return; |
| 1981 | } |
| 1982 | mContext.enforceCallingOrSelfPermission( |
| 1983 | android.Manifest.permission.MARK_NETWORK_SOCKET, |
| 1984 | "ConnectivityService"); |
| 1985 | } |
| 1986 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1987 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1988 | * Handle a {@code DISCONNECTED} event. If this pertains to the non-active |
| 1989 | * network, we ignore it. If it is for the active network, we send out a |
| 1990 | * broadcast. But first, we check whether it might be possible to connect |
| 1991 | * to a different network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | * @param info the {@code NetworkInfo} for the network |
| 1993 | */ |
| 1994 | private void handleDisconnect(NetworkInfo info) { |
| 1995 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1996 | int prevNetType = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1997 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1998 | mNetTrackers[prevNetType].setTeardownRequested(false); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 1999 | int thisNetId = mNetTrackers[prevNetType].getNetwork().netId; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2000 | |
| 2001 | // Remove idletimer previously setup in {@code handleConnect} |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2002 | // Already in place in new function. This is dead code. |
| 2003 | // if (mNetConfigs[prevNetType].isDefault()) { |
| 2004 | // removeDataActivityTracking(prevNetType); |
| 2005 | // } |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | /* |
| 2008 | * If the disconnected network is not the active one, then don't report |
| 2009 | * this as a loss of connectivity. What probably happened is that we're |
| 2010 | * getting the disconnect for a network that we explicitly disabled |
| 2011 | * in accordance with network preference policies. |
| 2012 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2013 | if (!mNetConfigs[prevNetType].isDefault()) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2014 | List<Integer> pids = mNetRequestersPids[prevNetType]; |
| 2015 | for (Integer pid : pids) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2016 | // will remove them because the net's no longer connected |
| 2017 | // need to do this now as only now do we know the pids and |
| 2018 | // can properly null things that are no longer referenced. |
| 2019 | reassessPidDns(pid.intValue(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2021 | } |
| 2022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2023 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2024 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2025 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2026 | if (info.isFailover()) { |
| 2027 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2028 | info.setFailover(false); |
| 2029 | } |
| 2030 | if (info.getReason() != null) { |
| 2031 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2032 | } |
| 2033 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2034 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2035 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2036 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2037 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2038 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2039 | tryFailover(prevNetType); |
| 2040 | if (mActiveDefaultNetwork != -1) { |
| 2041 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2042 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 2043 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2044 | mDefaultInetConditionPublished = 0; // we're not connected anymore |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2045 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2046 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2047 | } |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2048 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2049 | |
| 2050 | // Reset interface if no other connections are using the same interface |
| 2051 | boolean doReset = true; |
| 2052 | LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties(); |
| 2053 | if (linkProperties != null) { |
| 2054 | String oldIface = linkProperties.getInterfaceName(); |
| 2055 | if (TextUtils.isEmpty(oldIface) == false) { |
| 2056 | for (NetworkStateTracker networkStateTracker : mNetTrackers) { |
| 2057 | if (networkStateTracker == null) continue; |
| 2058 | NetworkInfo networkInfo = networkStateTracker.getNetworkInfo(); |
| 2059 | if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) { |
| 2060 | LinkProperties l = networkStateTracker.getLinkProperties(); |
| 2061 | if (l == null) continue; |
| 2062 | if (oldIface.equals(l.getInterfaceName())) { |
| 2063 | doReset = false; |
| 2064 | break; |
| 2065 | } |
| 2066 | } |
| 2067 | } |
| 2068 | } |
| 2069 | } |
| 2070 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2071 | // do this before we broadcast the change |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2072 | // Already done in new function. This is dead code. |
| 2073 | // handleConnectivityChange(prevNetType, doReset); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2074 | |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2075 | final Intent immediateIntent = new Intent(intent); |
| 2076 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 2077 | sendStickyBroadcast(immediateIntent); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2078 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2079 | /* |
| 2080 | * If the failover network is already connected, then immediately send |
| 2081 | * out a followup broadcast indicating successful failover |
| 2082 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2083 | if (mActiveDefaultNetwork != -1) { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2084 | sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), |
| 2085 | getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2086 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2087 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2088 | // mNetd.removeNetwork(thisNetId); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2089 | } catch (Exception e) { |
| 2090 | loge("Exception removing network: " + e); |
| 2091 | } finally { |
| 2092 | mNetTrackers[prevNetType].setNetId(INVALID_NET_ID); |
| 2093 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2096 | private void tryFailover(int prevNetType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2097 | /* |
Robert Greenwalt | bff9018 | 2011-01-06 15:41:07 -0800 | [diff] [blame] | 2098 | * If this is a default network, check if other defaults are available. |
| 2099 | * Try to reconnect on all available and let them hash it out when |
| 2100 | * more than one connects. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2101 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2102 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2103 | if (mActiveDefaultNetwork == prevNetType) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2104 | if (DBG) { |
| 2105 | log("tryFailover: set mActiveDefaultNetwork=-1, prevNetType=" + prevNetType); |
| 2106 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2107 | mActiveDefaultNetwork = -1; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2108 | try { |
| 2109 | mNetd.clearDefaultNetId(); |
| 2110 | } catch (Exception e) { |
| 2111 | loge("Exception clearing default network :" + e); |
| 2112 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2115 | // don't signal a reconnect for anything lower or equal priority than our |
| 2116 | // current connected default |
| 2117 | // TODO - don't filter by priority now - nice optimization but risky |
| 2118 | // int currentPriority = -1; |
| 2119 | // if (mActiveDefaultNetwork != -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2120 | // currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority; |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2121 | // } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2122 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2123 | for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 2124 | if (checkType == prevNetType) continue; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2125 | if (mNetConfigs[checkType] == null) continue; |
| 2126 | if (!mNetConfigs[checkType].isDefault()) continue; |
Robert Greenwalt | 424781e | 2011-11-11 09:56:03 -0800 | [diff] [blame] | 2127 | if (mNetTrackers[checkType] == null) continue; |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2128 | |
| 2129 | // Enabling the isAvailable() optimization caused mobile to not get |
| 2130 | // selected if it was in the middle of error handling. Specifically |
| 2131 | // a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL |
| 2132 | // would not be available and we wouldn't get connected to anything. |
| 2133 | // So removing the isAvailable() optimization below for now. TODO: This |
| 2134 | // optimization should work and we need to investigate why it doesn't work. |
| 2135 | // This could be related to how DEACTIVATE_DATA_CALL is reporting its |
| 2136 | // complete before it is really complete. |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2137 | |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2138 | // if (!mNetTrackers[checkType].isAvailable()) continue; |
| 2139 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2140 | // if (currentPriority >= mNetConfigs[checkType].mPriority) continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2141 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2142 | NetworkStateTracker checkTracker = mNetTrackers[checkType]; |
| 2143 | NetworkInfo checkInfo = checkTracker.getNetworkInfo(); |
| 2144 | if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) { |
| 2145 | checkInfo.setFailover(true); |
| 2146 | checkTracker.reconnect(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2147 | } |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2148 | if (DBG) log("Attempting to switch to " + checkInfo.getTypeName()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2149 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2150 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2151 | } |
| 2152 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2153 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2154 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2155 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2156 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2159 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2160 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2161 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2164 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 2165 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 2166 | } |
| 2167 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2168 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2169 | if (mLockdownTracker != null) { |
| 2170 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 2171 | } |
| 2172 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2173 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2174 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2175 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2176 | if (info.isFailover()) { |
| 2177 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2178 | info.setFailover(false); |
| 2179 | } |
| 2180 | if (info.getReason() != null) { |
| 2181 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2182 | } |
| 2183 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2184 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2185 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2186 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2187 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2188 | return intent; |
| 2189 | } |
| 2190 | |
| 2191 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 2192 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 2193 | } |
| 2194 | |
| 2195 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 2196 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2199 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2200 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 2201 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 2202 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2203 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2204 | final long ident = Binder.clearCallingIdentity(); |
| 2205 | try { |
| 2206 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 2207 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2208 | } finally { |
| 2209 | Binder.restoreCallingIdentity(ident); |
| 2210 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2213 | private void sendStickyBroadcast(Intent intent) { |
| 2214 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2215 | if (!mSystemReady) { |
| 2216 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2217 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2218 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2219 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2220 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2221 | } |
| 2222 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2223 | final long ident = Binder.clearCallingIdentity(); |
| 2224 | try { |
| 2225 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2226 | } finally { |
| 2227 | Binder.restoreCallingIdentity(ident); |
| 2228 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2232 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 2233 | if (delayMs <= 0) { |
| 2234 | sendStickyBroadcast(intent); |
| 2235 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2236 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2237 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 2238 | + intent.getAction()); |
| 2239 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2240 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 2241 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 2242 | } |
| 2243 | } |
| 2244 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2245 | void systemReady() { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2246 | mCaptivePortalTracker = CaptivePortalTracker.makeCaptivePortalTracker(mContext, this); |
| 2247 | loadGlobalProxy(); |
| 2248 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2249 | synchronized(this) { |
| 2250 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2251 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2252 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2253 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2254 | } |
| 2255 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2256 | // load the global proxy at startup |
| 2257 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2258 | |
| 2259 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 2260 | // for user to unlock device. |
| 2261 | if (!updateLockdownVpn()) { |
| 2262 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 2263 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 2264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2265 | } |
| 2266 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2267 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 2268 | @Override |
| 2269 | public void onReceive(Context context, Intent intent) { |
| 2270 | // Try creating lockdown tracker, since user present usually means |
| 2271 | // unlocked keystore. |
| 2272 | if (updateLockdownVpn()) { |
| 2273 | mContext.unregisterReceiver(this); |
| 2274 | } |
| 2275 | } |
| 2276 | }; |
| 2277 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2278 | private boolean isNewNetTypePreferredOverCurrentNetType(int type) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2279 | if (((type != mNetworkPreference) |
| 2280 | && (mNetConfigs[mActiveDefaultNetwork].priority > mNetConfigs[type].priority)) |
| 2281 | || (mNetworkPreference == mActiveDefaultNetwork)) { |
| 2282 | return false; |
| 2283 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2284 | return true; |
| 2285 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2286 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2287 | private void handleConnect(NetworkInfo info) { |
| 2288 | final int newNetType = info.getType(); |
| 2289 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2290 | // snapshot isFailover, because sendConnectedBroadcast() resets it |
| 2291 | boolean isFailover = info.isFailover(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2292 | final NetworkStateTracker thisNet = mNetTrackers[newNetType]; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2293 | final String thisIface = thisNet.getLinkProperties().getInterfaceName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2295 | if (VDBG) { |
| 2296 | log("handleConnect: E newNetType=" + newNetType + " thisIface=" + thisIface |
| 2297 | + " isFailover" + isFailover); |
| 2298 | } |
| 2299 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2300 | // if this is a default net and other default is running |
| 2301 | // kill the one not preferred |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2302 | if (mNetConfigs[newNetType].isDefault()) { |
| 2303 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { |
| 2304 | if (isNewNetTypePreferredOverCurrentNetType(newNetType)) { |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2305 | String teardownPolicy = SystemProperties.get("net.teardownPolicy"); |
| 2306 | if (TextUtils.equals(teardownPolicy, "keep") == false) { |
| 2307 | // tear down the other |
| 2308 | NetworkStateTracker otherNet = |
| 2309 | mNetTrackers[mActiveDefaultNetwork]; |
| 2310 | if (DBG) { |
| 2311 | log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + |
| 2312 | " teardown"); |
| 2313 | } |
| 2314 | if (!teardown(otherNet)) { |
| 2315 | loge("Network declined teardown request"); |
| 2316 | teardown(thisNet); |
| 2317 | return; |
| 2318 | } |
| 2319 | } else { |
| 2320 | //TODO - remove |
| 2321 | loge("network teardown skipped due to net.teardownPolicy setting"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2322 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2323 | } else { |
| 2324 | // don't accept this one |
| 2325 | if (VDBG) { |
| 2326 | log("Not broadcasting CONNECT_ACTION " + |
| 2327 | "to torn down network " + info.getTypeName()); |
| 2328 | } |
| 2329 | teardown(thisNet); |
| 2330 | return; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2331 | } |
| 2332 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2333 | int thisNetId = nextNetId(); |
| 2334 | thisNet.setNetId(thisNetId); |
| 2335 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2336 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2337 | } catch (Exception e) { |
| 2338 | loge("Exception creating network :" + e); |
| 2339 | teardown(thisNet); |
| 2340 | return; |
| 2341 | } |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2342 | // Already in place in new function. This is dead code. |
| 2343 | // setupDataActivityTracking(newNetType); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2344 | synchronized (ConnectivityService.this) { |
| 2345 | // have a new default network, release the transition wakelock in a second |
| 2346 | // if it's held. The second pause is to allow apps to reconnect over the |
| 2347 | // new network |
| 2348 | if (mNetTransitionWakeLock.isHeld()) { |
| 2349 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 2350 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2351 | mNetTransitionWakeLockSerialNumber, 0), |
| 2352 | 1000); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2355 | mActiveDefaultNetwork = newNetType; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2356 | try { |
| 2357 | mNetd.setDefaultNetId(thisNetId); |
| 2358 | } catch (Exception e) { |
| 2359 | loge("Exception setting default network :" + e); |
| 2360 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2361 | // this will cause us to come up initially as unconnected and switching |
| 2362 | // to connected after our normal pause unless somebody reports us as reall |
| 2363 | // disconnected |
| 2364 | mDefaultInetConditionPublished = 0; |
| 2365 | mDefaultConnectionSequence++; |
| 2366 | mInetConditionChangeInFlight = false; |
| 2367 | // Don't do this - if we never sign in stay, grey |
| 2368 | //reportNetworkCondition(mActiveDefaultNetwork, 100); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 2369 | updateNetworkSettings(thisNet); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2370 | } else { |
| 2371 | int thisNetId = nextNetId(); |
| 2372 | thisNet.setNetId(thisNetId); |
| 2373 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2374 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2375 | } catch (Exception e) { |
| 2376 | loge("Exception creating network :" + e); |
| 2377 | teardown(thisNet); |
| 2378 | return; |
| 2379 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2380 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2381 | thisNet.setTeardownRequested(false); |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2382 | // Already in place in new function. This is dead code. |
| 2383 | // updateMtuSizeSettings(thisNet); |
| 2384 | // handleConnectivityChange(newNetType, false); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2385 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2386 | |
| 2387 | // notify battery stats service about this network |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2388 | if (thisIface != null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2389 | try { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2390 | BatteryStatsService.getService().noteNetworkInterfaceType(thisIface, newNetType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2391 | } catch (RemoteException e) { |
| 2392 | // ignored; service lives in system_server |
| 2393 | } |
| 2394 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2395 | } |
| 2396 | |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2397 | /** @hide */ |
| 2398 | @Override |
| 2399 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 2400 | enforceConnectivityInternalPermission(); |
| 2401 | if (DBG) log("captivePortalCheckCompleted: ni=" + info + " captive=" + isCaptivePortal); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2402 | // mNetTrackers[info.getType()].captivePortalCheckCompleted(isCaptivePortal); |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2405 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2406 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2407 | * |
| 2408 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2409 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2410 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2411 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2412 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2413 | |
| 2414 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2415 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2416 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2417 | if (networkAgent.networkCapabilities.hasTransport( |
| 2418 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2419 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2420 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 2421 | 5); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2422 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2423 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 2424 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2425 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2426 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2427 | 0); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2428 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2429 | } else { |
| 2430 | // do not track any other networks |
| 2431 | timeout = 0; |
| 2432 | } |
| 2433 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2434 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2435 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2436 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2437 | } catch (Exception e) { |
| 2438 | // You shall not crash! |
| 2439 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * Remove data activity tracking when network disconnects. |
| 2446 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2447 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2448 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 2449 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2450 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2451 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 2452 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2453 | try { |
| 2454 | // the call fails silently if no idletimer setup for this interface |
| 2455 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2456 | } catch (Exception e) { |
| 2457 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2458 | } |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | /** |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2463 | * After a change in the connectivity state of a network. We're mainly |
| 2464 | * concerned with making sure that the list of DNS servers is set up |
| 2465 | * according to which networks are connected, and ensuring that the |
| 2466 | * right routing table entries exist. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2467 | * |
| 2468 | * TODO - delete when we're sure all this functionallity is captured. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2469 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2470 | private void handleConnectivityChange(int netType, LinkProperties curLp, boolean doReset) { |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2471 | int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0; |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2472 | boolean exempt = ConnectivityManager.isNetworkTypeExempt(netType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2473 | if (VDBG) { |
| 2474 | log("handleConnectivityChange: netType=" + netType + " doReset=" + doReset |
| 2475 | + " resetMask=" + resetMask); |
| 2476 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2478 | /* |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2479 | * If a non-default network is enabled, add the host routes that |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2480 | * will allow it's DNS servers to be accessed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2481 | */ |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2482 | handleDnsConfigurationChange(netType); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2483 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2484 | LinkProperties newLp = null; |
| 2485 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2486 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2487 | newLp = mNetTrackers[netType].getLinkProperties(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2488 | if (VDBG) { |
| 2489 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2490 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2491 | "\n curLp=" + curLp + |
| 2492 | "\n newLp=" + newLp); |
| 2493 | } |
| 2494 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2495 | if (curLp != null) { |
| 2496 | if (curLp.isIdenticalInterfaceName(newLp)) { |
| 2497 | CompareResult<LinkAddress> car = curLp.compareAddresses(newLp); |
| 2498 | if ((car.removed.size() != 0) || (car.added.size() != 0)) { |
| 2499 | for (LinkAddress linkAddr : car.removed) { |
| 2500 | if (linkAddr.getAddress() instanceof Inet4Address) { |
| 2501 | resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES; |
| 2502 | } |
| 2503 | if (linkAddr.getAddress() instanceof Inet6Address) { |
| 2504 | resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES; |
| 2505 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2506 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2507 | if (DBG) { |
| 2508 | log("handleConnectivityChange: addresses changed" + |
| 2509 | " linkProperty[" + netType + "]:" + " resetMask=" + resetMask + |
| 2510 | "\n car=" + car); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2511 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2512 | } else { |
Robert Greenwalt | d01f842 | 2014-01-23 16:03:04 -0800 | [diff] [blame] | 2513 | if (VDBG) { |
| 2514 | log("handleConnectivityChange: addresses are the same reset per" + |
| 2515 | " doReset linkProperty[" + netType + "]:" + |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2516 | " resetMask=" + resetMask); |
| 2517 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2518 | } |
| 2519 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2520 | resetMask = NetworkUtils.RESET_ALL_ADDRESSES; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2521 | if (DBG) { |
| 2522 | log("handleConnectivityChange: interface not not equivalent reset both" + |
| 2523 | " linkProperty[" + netType + "]:" + |
| 2524 | " resetMask=" + resetMask); |
| 2525 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2526 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2527 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2528 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2529 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2530 | } |
| 2531 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2532 | if (VDBG) { |
| 2533 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2534 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2535 | "\n curLp=" + curLp + |
| 2536 | "\n newLp= null"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2537 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2538 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2539 | mCurrentLinkProperties[netType] = newLp; |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2540 | boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault(), exempt, |
| 2541 | mNetTrackers[netType].getNetwork().netId); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2542 | |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2543 | if (resetMask != 0 || resetDns) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2544 | if (VDBG) log("handleConnectivityChange: resetting"); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2545 | if (curLp != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2546 | if (VDBG) log("handleConnectivityChange: resetting curLp=" + curLp); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2547 | for (String iface : curLp.getAllInterfaceNames()) { |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2548 | if (TextUtils.isEmpty(iface) == false) { |
| 2549 | if (resetMask != 0) { |
| 2550 | if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")"); |
| 2551 | NetworkUtils.resetConnections(iface, resetMask); |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2552 | |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2553 | // Tell VPN the interface is down. It is a temporary |
| 2554 | // but effective fix to make VPN aware of the change. |
| 2555 | if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2556 | synchronized(mVpns) { |
| 2557 | for (int i = 0; i < mVpns.size(); i++) { |
| 2558 | mVpns.valueAt(i).interfaceStatusChanged(iface, false); |
| 2559 | } |
| 2560 | } |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2561 | } |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2562 | } |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2563 | } else { |
| 2564 | loge("Can't reset connection for type "+netType); |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2565 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2566 | } |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2567 | if (resetDns) { |
| 2568 | flushVmDnsCache(); |
| 2569 | if (VDBG) log("resetting DNS cache for type " + netType); |
| 2570 | try { |
| 2571 | mNetd.flushNetworkDnsCache(mNetTrackers[netType].getNetwork().netId); |
| 2572 | } catch (Exception e) { |
| 2573 | // never crash - catch them all |
| 2574 | if (DBG) loge("Exception resetting dns cache: " + e); |
| 2575 | } |
| 2576 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2577 | } |
| 2578 | } |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2579 | |
| 2580 | // TODO: Temporary notifying upstread change to Tethering. |
| 2581 | // @see bug/4455071 |
| 2582 | /** Notify TetheringService if interface name has been changed. */ |
| 2583 | if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(), |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 2584 | PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) { |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2585 | if (isTetheringSupported()) { |
| 2586 | mTethering.handleTetherIfaceChange(); |
| 2587 | } |
| 2588 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | } |
| 2590 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2591 | /** |
| 2592 | * Add and remove routes using the old properties (null if not previously connected), |
| 2593 | * new properties (null if becoming disconnected). May even be double null, which |
| 2594 | * is a noop. |
| 2595 | * Uses isLinkDefault to determine if default routes should be set or conversely if |
| 2596 | * host routes should be set to the dns servers |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2597 | * returns a boolean indicating the routes changed |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2598 | */ |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2599 | private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2600 | boolean isLinkDefault, boolean exempt, int netId) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2601 | Collection<RouteInfo> routesToAdd = null; |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2602 | CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>(); |
| 2603 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2604 | if (curLp != null) { |
| 2605 | // check for the delta between the current set and the new |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 2606 | routeDiff = curLp.compareAllRoutes(newLp); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2607 | dnsDiff = curLp.compareDnses(newLp); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2608 | } else if (newLp != null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 2609 | routeDiff.added = newLp.getAllRoutes(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2610 | dnsDiff.added = newLp.getDnses(); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2611 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2612 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2613 | boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0); |
| 2614 | |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2615 | for (RouteInfo r : routeDiff.removed) { |
| 2616 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2617 | if (VDBG) log("updateRoutes: default remove route r=" + r); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2618 | removeRoute(curLp, r, TO_DEFAULT_TABLE, netId); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 2619 | } |
| 2620 | if (isLinkDefault == false) { |
| 2621 | // remove from a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2622 | removeRoute(curLp, r, TO_SECONDARY_TABLE, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2623 | } |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2624 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2625 | |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2626 | for (RouteInfo r : routeDiff.added) { |
| 2627 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2628 | addRoute(newLp, r, TO_DEFAULT_TABLE, exempt, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2629 | } else { |
| 2630 | // add to a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2631 | addRoute(newLp, r, TO_SECONDARY_TABLE, UNEXEMPT, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2632 | |
| 2633 | // many radios add a default route even when we don't want one. |
| 2634 | // remove the default route unless somebody else has asked for it |
| 2635 | String ifaceName = newLp.getInterfaceName(); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2636 | synchronized (mRoutesLock) { |
| 2637 | if (!TextUtils.isEmpty(ifaceName) && !mAddedRoutes.contains(r)) { |
| 2638 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 2639 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2640 | mNetd.removeRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2641 | } catch (Exception e) { |
| 2642 | // never crash - catch them all |
| 2643 | if (DBG) loge("Exception trying to remove a route: " + e); |
| 2644 | } |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | } |
| 2649 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2650 | return routesChanged; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2651 | } |
| 2652 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2653 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2654 | * Reads the network specific MTU size from reources. |
| 2655 | * and set it on it's iface. |
| 2656 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2657 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 2658 | final String iface = newLp.getInterfaceName(); |
| 2659 | final int mtu = newLp.getMtu(); |
| 2660 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 2661 | if (VDBG) log("identical MTU - not setting"); |
| 2662 | return; |
| 2663 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2664 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2665 | if (mtu < 68 || mtu > 10000) { |
| 2666 | loge("Unexpected mtu value: " + mtu + ", " + iface); |
| 2667 | return; |
| 2668 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2669 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2670 | try { |
| 2671 | if (VDBG) log("Setting MTU size: " + iface + ", " + mtu); |
| 2672 | mNetd.setMtu(iface, mtu); |
| 2673 | } catch (Exception e) { |
| 2674 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2675 | } |
| 2676 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2677 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2678 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2679 | * Reads the network specific TCP buffer sizes from SystemProperties |
| 2680 | * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system |
| 2681 | * wide use |
| 2682 | */ |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2683 | private void updateNetworkSettings(NetworkStateTracker nt) { |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2684 | String key = nt.getTcpBufferSizesPropName(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2685 | String bufferSizes = key == null ? null : SystemProperties.get(key); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2686 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2687 | if (TextUtils.isEmpty(bufferSizes)) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2688 | if (VDBG) log(key + " not found in system properties. Using defaults"); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2689 | |
| 2690 | // Setting to default values so we won't be stuck to previous values |
| 2691 | key = "net.tcp.buffersize.default"; |
| 2692 | bufferSizes = SystemProperties.get(key); |
| 2693 | } |
| 2694 | |
| 2695 | // Set values in kernel |
| 2696 | if (bufferSizes.length() != 0) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2697 | if (VDBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2698 | log("Setting TCP values: [" + bufferSizes |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2699 | + "] which comes from [" + key + "]"); |
| 2700 | } |
| 2701 | setBufferSize(bufferSizes); |
| 2702 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2703 | |
| 2704 | final String defaultRwndKey = "net.tcp.default_init_rwnd"; |
| 2705 | int defaultRwndValue = SystemProperties.getInt(defaultRwndKey, 0); |
| 2706 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 2707 | Settings.Global.TCP_DEFAULT_INIT_RWND, defaultRwndValue); |
| 2708 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2709 | if (rwndValue != 0) { |
| 2710 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 2711 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2714 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2715 | * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max] |
| 2716 | * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem |
| 2717 | * |
| 2718 | * @param bufferSizes in the format of "readMin, readInitial, readMax, |
| 2719 | * writeMin, writeInitial, writeMax" |
| 2720 | */ |
| 2721 | private void setBufferSize(String bufferSizes) { |
| 2722 | try { |
| 2723 | String[] values = bufferSizes.split(","); |
| 2724 | |
| 2725 | if (values.length == 6) { |
| 2726 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 2727 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 2728 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 2729 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 2730 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 2731 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 2732 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2733 | } else { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2734 | loge("Invalid buffersize string: " + bufferSizes); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } catch (IOException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2737 | loge("Can't set tcp buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2738 | } |
| 2739 | } |
| 2740 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2741 | /** |
| 2742 | * Adjust the per-process dns entries (net.dns<x>.<pid>) based |
| 2743 | * on the highest priority active net which this process requested. |
| 2744 | * If there aren't any, clear it out |
| 2745 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2746 | private void reassessPidDns(int pid, boolean doBump) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2747 | { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2748 | if (VDBG) log("reassessPidDns for pid " + pid); |
| 2749 | Integer myPid = new Integer(pid); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2750 | for(int i : mPriorityList) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2751 | if (mNetConfigs[i].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2752 | continue; |
| 2753 | } |
| 2754 | NetworkStateTracker nt = mNetTrackers[i]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2755 | if (nt.getNetworkInfo().isConnected() && |
| 2756 | !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2757 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 2758 | if (p == null) continue; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2759 | if (mNetRequestersPids[i].contains(myPid)) { |
| 2760 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2761 | // TODO: Reimplement this via local variable in bionic. |
| 2762 | // mNetd.setDnsNetworkForPid(nt.getNetwork().netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2763 | } catch (Exception e) { |
| 2764 | Slog.e(TAG, "exception reasseses pid dns: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2765 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2766 | return; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | // nothing found - delete |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2771 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2772 | // TODO: Reimplement this via local variable in bionic. |
| 2773 | // mNetd.clearDnsNetworkForPid(pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2774 | } catch (Exception e) { |
| 2775 | Slog.e(TAG, "exception clear interface from pid: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2779 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 2780 | /* |
| 2781 | * Tell the VMs to toss their DNS caches |
| 2782 | */ |
| 2783 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 2784 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 2785 | /* |
| 2786 | * Connectivity events can happen before boot has completed ... |
| 2787 | */ |
| 2788 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2789 | final long ident = Binder.clearCallingIdentity(); |
| 2790 | try { |
| 2791 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2792 | } finally { |
| 2793 | Binder.restoreCallingIdentity(ident); |
| 2794 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2797 | // Caller must grab mDnsLock. |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2798 | private void updateDnsLocked(String network, int netId, |
| 2799 | Collection<InetAddress> dnses, String domains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2800 | int last = 0; |
| 2801 | if (dnses.size() == 0 && mDefaultDns != null) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2802 | dnses = new ArrayList(); |
| 2803 | dnses.add(mDefaultDns); |
| 2804 | if (DBG) { |
| 2805 | loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2806 | } |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2809 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2810 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), domains); |
Chad Brubaker | 20a7e88 | 2013-07-23 17:13:36 -0700 | [diff] [blame] | 2811 | |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 2812 | for (InetAddress dns : dnses) { |
| 2813 | ++last; |
| 2814 | String key = "net.dns" + last; |
| 2815 | String value = dns.getHostAddress(); |
| 2816 | SystemProperties.set(key, value); |
| 2817 | } |
| 2818 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 2819 | String key = "net.dns" + i; |
| 2820 | SystemProperties.set(key, ""); |
| 2821 | } |
| 2822 | mNumDnsEntries = last; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2823 | } catch (Exception e) { |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 2824 | loge("exception setting default dns interface: " + e); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2825 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2828 | private void handleDnsConfigurationChange(int netType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2829 | // add default net's dns entries |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2830 | NetworkStateTracker nt = mNetTrackers[netType]; |
| 2831 | if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2832 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2833 | if (p == null) return; |
| 2834 | Collection<InetAddress> dnses = p.getDnses(); |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2835 | int netId = nt.getNetwork().netId; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2836 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2837 | String network = nt.getNetworkInfo().getTypeName(); |
| 2838 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2839 | updateDnsLocked(network, netId, dnses, p.getDomains()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2840 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2841 | } else { |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2842 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2843 | mNetd.setDnsServersForNetwork(netId, |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 2844 | NetworkUtils.makeStrings(dnses), p.getDomains()); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2845 | } catch (Exception e) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 2846 | if (DBG) loge("exception setting dns servers: " + e); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2847 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2848 | // set per-pid dns for attached secondary nets |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2849 | List<Integer> pids = mNetRequestersPids[netType]; |
| 2850 | for (Integer pid : pids) { |
| 2851 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2852 | // TODO: Reimplement this via local variable in bionic. |
| 2853 | // mNetd.setDnsNetworkForPid(netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2854 | } catch (Exception e) { |
| 2855 | Slog.e(TAG, "exception setting interface for pid: " + e); |
| 2856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2857 | } |
| 2858 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2859 | flushVmDnsCache(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2861 | } |
| 2862 | |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2863 | private int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2864 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 2865 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2866 | if(restoreDefaultNetworkDelayStr != null && |
| 2867 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2868 | try { |
| 2869 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 2870 | } catch (NumberFormatException e) { |
| 2871 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2872 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2873 | // if the system property isn't set, use the value for the apn type |
| 2874 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2875 | |
| 2876 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2877 | (mNetConfigs[networkType] != null)) { |
| 2878 | ret = mNetConfigs[networkType].restoreTime; |
| 2879 | } |
| 2880 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2884 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2885 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2886 | if (mContext.checkCallingOrSelfPermission( |
| 2887 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2888 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2889 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 2890 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 2891 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2892 | return; |
| 2893 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2894 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2895 | pw.println("NetworkFactories for:"); |
| 2896 | pw.increaseIndent(); |
| 2897 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 2898 | pw.println(nfi.name); |
| 2899 | } |
| 2900 | pw.decreaseIndent(); |
| 2901 | pw.println(); |
| 2902 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2903 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 2904 | pw.print("Active default network: "); |
| 2905 | if (defaultNai == null) { |
| 2906 | pw.println("none"); |
| 2907 | } else { |
| 2908 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2909 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2910 | pw.println(); |
| 2911 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2912 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2913 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2914 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2915 | pw.println(nai.toString()); |
| 2916 | pw.increaseIndent(); |
| 2917 | pw.println("Requests:"); |
| 2918 | pw.increaseIndent(); |
| 2919 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2920 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2921 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2922 | pw.decreaseIndent(); |
| 2923 | pw.println("Lingered:"); |
| 2924 | pw.increaseIndent(); |
| 2925 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 2926 | pw.decreaseIndent(); |
| 2927 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2928 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2929 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2930 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2931 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2932 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2933 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2934 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 2935 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2936 | } |
| 2937 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2938 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2939 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2940 | synchronized (this) { |
| 2941 | pw.println("NetworkTranstionWakeLock is currently " + |
| 2942 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 2943 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 2944 | } |
| 2945 | pw.println(); |
| 2946 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2947 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2948 | |
| 2949 | if (mInetLog != null) { |
| 2950 | pw.println(); |
| 2951 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2952 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2953 | for(int i = 0; i < mInetLog.size(); i++) { |
| 2954 | pw.println(mInetLog.get(i)); |
| 2955 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2956 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2957 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2958 | } |
| 2959 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2960 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2961 | private class NetworkStateTrackerHandler extends Handler { |
| 2962 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 2963 | super(looper); |
| 2964 | } |
| 2965 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2966 | @Override |
| 2967 | public void handleMessage(Message msg) { |
| 2968 | NetworkInfo info; |
| 2969 | switch (msg.what) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2970 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2971 | handleAsyncChannelHalfConnect(msg); |
| 2972 | break; |
| 2973 | } |
| 2974 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 2975 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2976 | if (nai != null) nai.asyncChannel.disconnect(); |
| 2977 | break; |
| 2978 | } |
| 2979 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 2980 | handleAsyncChannelDisconnected(msg); |
| 2981 | break; |
| 2982 | } |
| 2983 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
| 2984 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2985 | if (nai == null) { |
| 2986 | loge("EVENT_NETWORK_CAPABILITIES_CHANGED from unknown NetworkAgent"); |
| 2987 | } else { |
| 2988 | updateCapabilities(nai, (NetworkCapabilities)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2989 | } |
| 2990 | break; |
| 2991 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2992 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
| 2993 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2994 | if (nai == null) { |
| 2995 | loge("NetworkAgent not found for EVENT_NETWORK_PROPERTIES_CHANGED"); |
| 2996 | } else { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2997 | if (VDBG) log("Update of Linkproperties for " + nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2998 | LinkProperties oldLp = nai.linkProperties; |
| 2999 | nai.linkProperties = (LinkProperties)msg.obj; |
| 3000 | updateLinkProperties(nai, oldLp); |
| 3001 | } |
| 3002 | break; |
| 3003 | } |
| 3004 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
| 3005 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3006 | if (nai == null) { |
| 3007 | loge("EVENT_NETWORK_INFO_CHANGED from unknown NetworkAgent"); |
| 3008 | break; |
| 3009 | } |
| 3010 | info = (NetworkInfo) msg.obj; |
| 3011 | updateNetworkInfo(nai, info); |
| 3012 | break; |
| 3013 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3014 | case NetworkMonitor.EVENT_NETWORK_VALIDATED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3015 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
| 3016 | handleConnectionValidated(nai); |
| 3017 | break; |
| 3018 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3019 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3020 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
| 3021 | handleLingerComplete(nai); |
| 3022 | break; |
| 3023 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3024 | case NetworkStateTracker.EVENT_STATE_CHANGED: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3025 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3026 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3027 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3028 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 3029 | (state == NetworkInfo.State.DISCONNECTED) || |
| 3030 | (state == NetworkInfo.State.SUSPENDED)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3031 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3032 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3033 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3034 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3035 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3036 | // Since mobile has the notion of a network/apn that can be used for |
| 3037 | // provisioning we need to check every time we're connected as |
| 3038 | // CaptiveProtalTracker won't detected it because DCT doesn't report it |
| 3039 | // as connected as ACTION_ANY_DATA_CONNECTION_STATE_CHANGED instead its |
| 3040 | // reported as ACTION_DATA_CONNECTION_CONNECTED_TO_PROVISIONING_APN. Which |
| 3041 | // is received by MDST and sent here as EVENT_STATE_CHANGED. |
| 3042 | if (ConnectivityManager.isNetworkTypeMobile(info.getType()) |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3043 | && (0 != Settings.Global.getInt(mContext.getContentResolver(), |
| 3044 | Settings.Global.DEVICE_PROVISIONED, 0)) |
Wink Saville | 127beff | 2013-09-14 09:04:53 -0700 | [diff] [blame] | 3045 | && (((state == NetworkInfo.State.CONNECTED) |
| 3046 | && (info.getType() == ConnectivityManager.TYPE_MOBILE)) |
| 3047 | || info.isConnectedToProvisioningNetwork())) { |
| 3048 | log("ConnectivityChange checkMobileProvisioning for" |
| 3049 | + " TYPE_MOBILE or ProvisioningNetwork"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3050 | checkMobileProvisioning(CheckMp.MAX_TIMEOUT_MS); |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 3053 | EventLogTags.writeConnectivityStateChanged( |
| 3054 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3055 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3056 | if (info.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3057 | /** |
| 3058 | * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING |
| 3059 | * for now its an in between network, its a network that |
| 3060 | * is actually a default network but we don't want it to be |
| 3061 | * announced as such to keep background applications from |
| 3062 | * trying to use it. It turns out that some still try so we |
| 3063 | * take the additional step of clearing any default routes |
| 3064 | * to the link that may have incorrectly setup by the lower |
| 3065 | * levels. |
| 3066 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3067 | LinkProperties lp = getLinkPropertiesForTypeInternal(info.getType()); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3068 | if (DBG) { |
| 3069 | log("EVENT_STATE_CHANGED: connected to provisioning network, lp=" + lp); |
| 3070 | } |
| 3071 | |
| 3072 | // Clear any default routes setup by the radio so |
| 3073 | // any activity by applications trying to use this |
| 3074 | // connection will fail until the provisioning network |
| 3075 | // is enabled. |
| 3076 | for (RouteInfo r : lp.getRoutes()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 3077 | removeRoute(lp, r, TO_DEFAULT_TABLE, |
| 3078 | mNetTrackers[info.getType()].getNetwork().netId); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3079 | } |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3080 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3081 | } else if (state == NetworkInfo.State.SUSPENDED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3082 | } else if (state == NetworkInfo.State.CONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3083 | // handleConnect(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3084 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3085 | if (mLockdownTracker != null) { |
| 3086 | mLockdownTracker.onNetworkInfoChanged(info); |
| 3087 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3089 | } |
| 3090 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: { |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 3091 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 3092 | // TODO: Temporary allowing network configuration |
| 3093 | // change not resetting sockets. |
| 3094 | // @see bug/4455071 |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3095 | handleConnectivityChange(info.getType(), mCurrentLinkProperties[info.getType()], |
| 3096 | false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3097 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3098 | } |
| 3099 | case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: { |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3100 | info = (NetworkInfo) msg.obj; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3101 | int type = info.getType(); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 3102 | if (mNetConfigs[type].isDefault()) updateNetworkSettings(mNetTrackers[type]); |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3103 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3104 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3105 | } |
| 3106 | } |
| 3107 | } |
| 3108 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3109 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 3110 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3111 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3112 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 3113 | if (VDBG) log("NetworkFactory connected"); |
| 3114 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3115 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3116 | if (nri.isRequest == false) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3117 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3118 | ac.sendMessage(NetworkFactoryProtocol.CMD_REQUEST_NETWORK, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3119 | (nai != null ? nai.currentScore : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3120 | } |
| 3121 | } else { |
| 3122 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3123 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3124 | } |
| 3125 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 3126 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 3127 | if (VDBG) log("NetworkAgent connected"); |
| 3128 | // A network agent has requested a connection. Establish the connection. |
| 3129 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 3130 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 3131 | } else { |
| 3132 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3133 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
| 3134 | try { |
| 3135 | mNetworkAgentInfoForType[nai.networkInfo.getType()].remove(nai); |
| 3136 | } catch (NullPointerException e) {} |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3137 | if (nai != null) { |
| 3138 | mNetworkForNetId.remove(nai.network.netId); |
| 3139 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | } |
| 3143 | private void handleAsyncChannelDisconnected(Message msg) { |
| 3144 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3145 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3146 | if (DBG) { |
| 3147 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 3148 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3149 | // A network agent has disconnected. |
| 3150 | // Tell netd to clean up the configuration for this network |
| 3151 | // (routing rules, DNS, etc). |
| 3152 | try { |
| 3153 | mNetd.removeNetwork(nai.network.netId); |
| 3154 | } catch (Exception e) { |
| 3155 | loge("Exception removing network: " + e); |
| 3156 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3157 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3158 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3159 | mNetworkAgentInfos.remove(msg.replyTo); |
| 3160 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3161 | try { |
| 3162 | mNetworkAgentInfoForType[nai.networkInfo.getType()].remove(nai); |
| 3163 | } catch (NullPointerException e) {} |
| 3164 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3165 | mNetworkForNetId.remove(nai.network.netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3166 | // Since we've lost the network, go through all the requests that |
| 3167 | // it was satisfying and see if any other factory can satisfy them. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3168 | final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3169 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3170 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 3171 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3172 | if (VDBG) { |
| 3173 | log(" checking request " + request + ", currentNetwork = " + |
| 3174 | currentNetwork != null ? currentNetwork.name() : "null"); |
| 3175 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3176 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 3177 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3178 | sendUpdatedScoreToFactories(request, 0); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3179 | NetworkAgentInfo alternative = null; |
| 3180 | for (Map.Entry entry : mNetworkAgentInfos.entrySet()) { |
| 3181 | NetworkAgentInfo existing = (NetworkAgentInfo)entry.getValue(); |
| 3182 | if (existing.networkInfo.isConnected() && |
| 3183 | request.networkCapabilities.satisfiedByNetworkCapabilities( |
| 3184 | existing.networkCapabilities) && |
| 3185 | (alternative == null || |
| 3186 | alternative.currentScore < existing.currentScore)) { |
| 3187 | alternative = existing; |
| 3188 | } |
| 3189 | } |
| 3190 | if (alternative != null && !toActivate.contains(alternative)) { |
| 3191 | toActivate.add(alternative); |
| 3192 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 3196 | removeDataActivityTracking(nai); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3197 | mActiveDefaultNetwork = ConnectivityManager.TYPE_NONE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3198 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3199 | for (NetworkAgentInfo networkToActivate : toActivate) { |
| 3200 | networkToActivate.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 3201 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3205 | private void handleRegisterNetworkRequest(Message msg) { |
| 3206 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 3207 | final NetworkCapabilities newCap = nri.request.networkCapabilities; |
| 3208 | int score = 0; |
| 3209 | |
| 3210 | // Check for the best currently alive network that satisfies this request |
| 3211 | NetworkAgentInfo bestNetwork = null; |
| 3212 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
| 3213 | if (VDBG) log("handleRegisterNetworkRequest checking " + network.name()); |
| 3214 | if (newCap.satisfiedByNetworkCapabilities(network.networkCapabilities)) { |
| 3215 | if (VDBG) log("apparently satisfied. currentScore=" + network.currentScore); |
| 3216 | if ((bestNetwork == null) || bestNetwork.currentScore < network.currentScore) { |
| 3217 | bestNetwork = network; |
| 3218 | } |
| 3219 | } |
| 3220 | } |
| 3221 | if (bestNetwork != null) { |
| 3222 | if (VDBG) log("using " + bestNetwork.name()); |
| 3223 | bestNetwork.networkRequests.put(nri.request.requestId, nri.request); |
| 3224 | notifyNetworkCallback(bestNetwork, nri); |
| 3225 | score = bestNetwork.currentScore; |
| 3226 | } |
| 3227 | mNetworkRequests.put(nri.request, nri); |
| 3228 | if (msg.what == EVENT_REGISTER_NETWORK_REQUEST) { |
| 3229 | if (DBG) log("sending new NetworkRequest to factories"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3230 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 3231 | nfi.asyncChannel.sendMessage(NetworkFactoryProtocol.CMD_REQUEST_NETWORK, score, 0, nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | private void handleReleaseNetworkRequest(NetworkRequest request) { |
| 3237 | if (DBG) log("releasing NetworkRequest " + request); |
| 3238 | NetworkRequestInfo nri = mNetworkRequests.remove(request); |
| 3239 | if (nri != null) { |
| 3240 | // tell the network currently servicing this that it's no longer interested |
| 3241 | NetworkAgentInfo affectedNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 3242 | if (affectedNetwork != null) { |
Robert Greenwalt | 4064af8 | 2014-05-16 08:54:07 -0700 | [diff] [blame] | 3243 | mNetworkForRequestId.remove(nri.request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3244 | affectedNetwork.networkRequests.remove(nri.request.requestId); |
| 3245 | if (VDBG) { |
| 3246 | log(" Removing from current network " + affectedNetwork.name() + ", leaving " + |
| 3247 | affectedNetwork.networkRequests.size() + " requests."); |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | if (nri.isRequest) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3252 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 3253 | nfi.asyncChannel.sendMessage(NetworkFactoryProtocol.CMD_CANCEL_REQUEST, nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | if (affectedNetwork != null) { |
| 3257 | // check if this network still has live requests - otherwise, tear down |
| 3258 | // TODO - probably push this to the NF/NA |
| 3259 | boolean keep = false; |
| 3260 | for (int i = 0; i < affectedNetwork.networkRequests.size(); i++) { |
| 3261 | NetworkRequest r = affectedNetwork.networkRequests.valueAt(i); |
| 3262 | if (mNetworkRequests.get(r).isRequest) { |
| 3263 | keep = true; |
| 3264 | break; |
| 3265 | } |
| 3266 | } |
| 3267 | if (keep == false) { |
| 3268 | if (DBG) log("no live requests for " + affectedNetwork.name() + |
| 3269 | "; disconnecting"); |
| 3270 | affectedNetwork.asyncChannel.disconnect(); |
| 3271 | } |
| 3272 | } |
| 3273 | } |
| 3274 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 3275 | } |
| 3276 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3277 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3278 | private class InternalHandler extends Handler { |
| 3279 | public InternalHandler(Looper looper) { |
| 3280 | super(looper); |
| 3281 | } |
| 3282 | |
| 3283 | @Override |
| 3284 | public void handleMessage(Message msg) { |
| 3285 | NetworkInfo info; |
| 3286 | switch (msg.what) { |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3287 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3288 | String causedBy = null; |
| 3289 | synchronized (ConnectivityService.this) { |
| 3290 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 3291 | mNetTransitionWakeLock.isHeld()) { |
| 3292 | mNetTransitionWakeLock.release(); |
| 3293 | causedBy = mNetTransitionWakeLockCausedBy; |
| 3294 | } |
| 3295 | } |
| 3296 | if (causedBy != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3297 | log("NetTransition Wakelock for " + causedBy + " released by timeout"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3298 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3299 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3300 | } |
| 3301 | case EVENT_RESTORE_DEFAULT_NETWORK: { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 3302 | FeatureUser u = (FeatureUser)msg.obj; |
| 3303 | u.expire(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3304 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3305 | } |
| 3306 | case EVENT_INET_CONDITION_CHANGE: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3307 | int netType = msg.arg1; |
| 3308 | int condition = msg.arg2; |
| 3309 | handleInetConditionChange(netType, condition); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3310 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3311 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3312 | case EVENT_INET_CONDITION_HOLD_END: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3313 | int netType = msg.arg1; |
| 3314 | int sequence = msg.arg2; |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3315 | handleInetConditionHoldEnd(netType, sequence); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3316 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3317 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3318 | case EVENT_SET_MOBILE_DATA: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3319 | boolean enabled = (msg.arg1 == ENABLED); |
| 3320 | handleSetMobileData(enabled); |
| 3321 | break; |
| 3322 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3323 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3324 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3325 | break; |
| 3326 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3327 | case EVENT_SET_DEPENDENCY_MET: { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3328 | boolean met = (msg.arg1 == ENABLED); |
| 3329 | handleSetDependencyMet(msg.arg2, met); |
| 3330 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3331 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3332 | case EVENT_SEND_STICKY_BROADCAST_INTENT: { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3333 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3334 | sendStickyBroadcast(intent); |
| 3335 | break; |
| 3336 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3337 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 3338 | final int networkType = msg.arg1; |
| 3339 | final boolean enabled = msg.arg2 == ENABLED; |
| 3340 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3341 | break; |
| 3342 | } |
| 3343 | case EVENT_VPN_STATE_CHANGED: { |
| 3344 | if (mLockdownTracker != null) { |
| 3345 | mLockdownTracker.onVpnStateChanged((NetworkInfo) msg.obj); |
| 3346 | } |
| 3347 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3348 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3349 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 3350 | int tag = mEnableFailFastMobileDataTag.get(); |
| 3351 | if (msg.arg1 == tag) { |
| 3352 | MobileDataStateTracker mobileDst = |
| 3353 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 3354 | if (mobileDst != null) { |
| 3355 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 3356 | } |
| 3357 | } else { |
| 3358 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 3359 | + " != tag:" + tag); |
| 3360 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3361 | break; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3362 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3363 | case EVENT_SAMPLE_INTERVAL_ELAPSED: { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3364 | handleNetworkSamplingTimeout(); |
| 3365 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3366 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3367 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3368 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3369 | break; |
| 3370 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3371 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3372 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 3373 | break; |
| 3374 | } |
| 3375 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 3376 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3377 | break; |
| 3378 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3379 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 3380 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 3381 | break; |
| 3382 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3383 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 3384 | case EVENT_REGISTER_NETWORK_LISTENER: { |
| 3385 | handleRegisterNetworkRequest(msg); |
| 3386 | break; |
| 3387 | } |
| 3388 | case EVENT_RELEASE_NETWORK_REQUEST: { |
| 3389 | handleReleaseNetworkRequest((NetworkRequest) msg.obj); |
| 3390 | break; |
| 3391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3395 | |
| 3396 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3397 | public int tether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3398 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3399 | |
| 3400 | if (isTetheringSupported()) { |
| 3401 | return mTethering.tether(iface); |
| 3402 | } else { |
| 3403 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3404 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3408 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3409 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3410 | |
| 3411 | if (isTetheringSupported()) { |
| 3412 | return mTethering.untether(iface); |
| 3413 | } else { |
| 3414 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3415 | } |
| 3416 | } |
| 3417 | |
| 3418 | // javadoc from interface |
| 3419 | public int getLastTetherError(String iface) { |
| 3420 | enforceTetherAccessPermission(); |
| 3421 | |
| 3422 | if (isTetheringSupported()) { |
| 3423 | return mTethering.getLastTetherError(iface); |
| 3424 | } else { |
| 3425 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3426 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | // TODO - proper iface API for selection by property, inspection, etc |
| 3430 | public String[] getTetherableUsbRegexs() { |
| 3431 | enforceTetherAccessPermission(); |
| 3432 | if (isTetheringSupported()) { |
| 3433 | return mTethering.getTetherableUsbRegexs(); |
| 3434 | } else { |
| 3435 | return new String[0]; |
| 3436 | } |
| 3437 | } |
| 3438 | |
| 3439 | public String[] getTetherableWifiRegexs() { |
| 3440 | enforceTetherAccessPermission(); |
| 3441 | if (isTetheringSupported()) { |
| 3442 | return mTethering.getTetherableWifiRegexs(); |
| 3443 | } else { |
| 3444 | return new String[0]; |
| 3445 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3446 | } |
| 3447 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3448 | public String[] getTetherableBluetoothRegexs() { |
| 3449 | enforceTetherAccessPermission(); |
| 3450 | if (isTetheringSupported()) { |
| 3451 | return mTethering.getTetherableBluetoothRegexs(); |
| 3452 | } else { |
| 3453 | return new String[0]; |
| 3454 | } |
| 3455 | } |
| 3456 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3457 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3458 | enforceTetherChangePermission(); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3459 | if (isTetheringSupported()) { |
| 3460 | return mTethering.setUsbTethering(enable); |
| 3461 | } else { |
| 3462 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3463 | } |
| 3464 | } |
| 3465 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3466 | // TODO - move iface listing, queries, etc to new module |
| 3467 | // javadoc from interface |
| 3468 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3469 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3470 | return mTethering.getTetherableIfaces(); |
| 3471 | } |
| 3472 | |
| 3473 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3474 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3475 | return mTethering.getTetheredIfaces(); |
| 3476 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3477 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3478 | public String[] getTetheringErroredIfaces() { |
| 3479 | enforceTetherAccessPermission(); |
| 3480 | return mTethering.getErroredIfaces(); |
| 3481 | } |
| 3482 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3483 | // if ro.tether.denied = true we default to no tethering |
| 3484 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3485 | // had previously been turned off. |
| 3486 | public boolean isTetheringSupported() { |
| 3487 | enforceTetherAccessPermission(); |
| 3488 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 3489 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
| 3490 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 3491 | return tetherEnabledInSettings && ((mTethering.getTetherableUsbRegexs().length != 0 || |
| 3492 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 3493 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 3494 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3495 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3496 | |
| 3497 | // An API NetworkStateTrackers can call when they lose their network. |
| 3498 | // This will automatically be cleared after X seconds or a network becomes CONNECTED, |
| 3499 | // whichever happens first. The timer is started by the first caller and not |
| 3500 | // restarted by subsequent callers. |
| 3501 | public void requestNetworkTransitionWakelock(String forWhom) { |
| 3502 | enforceConnectivityInternalPermission(); |
| 3503 | synchronized (this) { |
| 3504 | if (mNetTransitionWakeLock.isHeld()) return; |
| 3505 | mNetTransitionWakeLockSerialNumber++; |
| 3506 | mNetTransitionWakeLock.acquire(); |
| 3507 | mNetTransitionWakeLockCausedBy = forWhom; |
| 3508 | } |
| 3509 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 3510 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3511 | mNetTransitionWakeLockSerialNumber, 0), |
| 3512 | mNetTransitionWakeLockTimeout); |
| 3513 | return; |
| 3514 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3515 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3516 | // 100 percent is full good, 0 is full bad. |
| 3517 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3518 | if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")"); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3519 | mContext.enforceCallingOrSelfPermission( |
| 3520 | android.Manifest.permission.STATUS_BAR, |
| 3521 | "ConnectivityService"); |
| 3522 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3523 | if (DBG) { |
| 3524 | int pid = getCallingPid(); |
| 3525 | int uid = getCallingUid(); |
| 3526 | String s = pid + "(" + uid + ") reports inet is " + |
| 3527 | (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " + |
| 3528 | "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime(); |
| 3529 | mInetLog.add(s); |
| 3530 | while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) { |
| 3531 | mInetLog.remove(0); |
| 3532 | } |
| 3533 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3534 | mHandler.sendMessage(mHandler.obtainMessage( |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3535 | EVENT_INET_CONDITION_CHANGE, networkType, percentage)); |
| 3536 | } |
| 3537 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3538 | public void reportBadNetwork(Network network) { |
| 3539 | //TODO |
| 3540 | } |
| 3541 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3542 | private void handleInetConditionChange(int netType, int condition) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3543 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3544 | if (DBG) log("handleInetConditionChange: no active default network - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3545 | return; |
| 3546 | } |
| 3547 | if (mActiveDefaultNetwork != netType) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3548 | if (DBG) log("handleInetConditionChange: net=" + netType + |
| 3549 | " != default=" + mActiveDefaultNetwork + " - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3550 | return; |
| 3551 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3552 | if (VDBG) { |
| 3553 | log("handleInetConditionChange: net=" + |
| 3554 | netType + ", condition=" + condition + |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3555 | ",mActiveDefaultNetwork=" + mActiveDefaultNetwork); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3556 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3557 | mDefaultInetCondition = condition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3558 | int delay; |
| 3559 | if (mInetConditionChangeInFlight == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3560 | if (VDBG) log("handleInetConditionChange: starting a change hold"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3561 | // setup a new hold to debounce this |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3562 | if (mDefaultInetCondition > 50) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3563 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3564 | Settings.Global.INET_CONDITION_DEBOUNCE_UP_DELAY, 500); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3565 | } else { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3566 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3567 | Settings.Global.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3568 | } |
| 3569 | mInetConditionChangeInFlight = true; |
| 3570 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END, |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3571 | mActiveDefaultNetwork, mDefaultConnectionSequence), delay); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3572 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3573 | // we've set the new condition, when this hold ends that will get picked up |
| 3574 | if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3575 | } |
| 3576 | } |
| 3577 | |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3578 | private void handleInetConditionHoldEnd(int netType, int sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3579 | if (DBG) { |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3580 | log("handleInetConditionHoldEnd: net=" + netType + |
| 3581 | ", condition=" + mDefaultInetCondition + |
| 3582 | ", published condition=" + mDefaultInetConditionPublished); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3583 | } |
| 3584 | mInetConditionChangeInFlight = false; |
| 3585 | |
| 3586 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3587 | if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3588 | return; |
| 3589 | } |
| 3590 | if (mDefaultConnectionSequence != sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3591 | if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3592 | return; |
| 3593 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3594 | // TODO: Figure out why this optimization sometimes causes a |
| 3595 | // change in mDefaultInetCondition to be missed and the |
| 3596 | // UI to not be updated. |
| 3597 | //if (mDefaultInetConditionPublished == mDefaultInetCondition) { |
| 3598 | // if (DBG) log("no change in condition - aborting"); |
| 3599 | // return; |
| 3600 | //} |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3601 | NetworkInfo networkInfo = getNetworkInfoForType(mActiveDefaultNetwork); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3602 | if (networkInfo.isConnected() == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3603 | if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3604 | return; |
| 3605 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3606 | mDefaultInetConditionPublished = mDefaultInetCondition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3607 | sendInetConditionBroadcast(networkInfo); |
| 3608 | return; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3609 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3610 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3611 | public ProxyInfo getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3612 | // this information is already available as a world read/writable jvm property |
| 3613 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3614 | // of proxy info to all the JVMs. |
| 3615 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3616 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3617 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3618 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 3619 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3620 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3621 | } |
| 3622 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3623 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3624 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3625 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3626 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3627 | if (proxyProperties == mGlobalProxy) return; |
| 3628 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 3629 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 3630 | |
| 3631 | String host = ""; |
| 3632 | int port = 0; |
| 3633 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3634 | String pacFileUrl = ""; |
| 3635 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3636 | (proxyProperties.getPacFileUrl() != null))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3637 | if (!proxyProperties.isValid()) { |
| 3638 | if (DBG) |
| 3639 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3640 | return; |
| 3641 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3642 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3643 | host = mGlobalProxy.getHost(); |
| 3644 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3645 | exclList = mGlobalProxy.getExclusionListAsString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3646 | if (proxyProperties.getPacFileUrl() != null) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3647 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3648 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3649 | } else { |
| 3650 | mGlobalProxy = null; |
| 3651 | } |
| 3652 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3653 | final long token = Binder.clearCallingIdentity(); |
| 3654 | try { |
| 3655 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 3656 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 3657 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 3658 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3659 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3660 | } finally { |
| 3661 | Binder.restoreCallingIdentity(token); |
| 3662 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | if (mGlobalProxy == null) { |
| 3666 | proxyProperties = mDefaultProxy; |
| 3667 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3668 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3669 | } |
| 3670 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3671 | private void loadGlobalProxy() { |
| 3672 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3673 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 3674 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 3675 | String exclList = Settings.Global.getString(res, |
| 3676 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3677 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 3678 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3679 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3680 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3681 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3682 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3683 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3684 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3685 | if (!proxyProperties.isValid()) { |
| 3686 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3687 | return; |
| 3688 | } |
| 3689 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3690 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3691 | mGlobalProxy = proxyProperties; |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3696 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3697 | // this information is already available as a world read/writable jvm property |
| 3698 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3699 | // of proxy info to all the JVMs. |
| 3700 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3701 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3702 | return mGlobalProxy; |
| 3703 | } |
| 3704 | } |
| 3705 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3706 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3707 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3708 | && (proxy.getPacFileUrl() == null)) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3709 | proxy = null; |
| 3710 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3711 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3712 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3713 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 3714 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3715 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 3716 | return; |
| 3717 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 3718 | |
| 3719 | // This call could be coming from the PacManager, containing the port of the local |
| 3720 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 3721 | // global (to get the correct local port), and send a broadcast. |
| 3722 | // TODO: Switch PacManager to have its own message to send back rather than |
| 3723 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
| 3724 | if ((mGlobalProxy != null) && (proxy != null) && (proxy.getPacFileUrl() != null) |
| 3725 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 3726 | mGlobalProxy = proxy; |
| 3727 | sendProxyBroadcast(mGlobalProxy); |
| 3728 | return; |
| 3729 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3730 | mDefaultProxy = proxy; |
| 3731 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3732 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3733 | if (!mDefaultProxyDisabled) { |
| 3734 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3735 | } |
| 3736 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3740 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 3741 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3742 | if (!TextUtils.isEmpty(proxy)) { |
| 3743 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3744 | if (data.length == 0) { |
| 3745 | return; |
| 3746 | } |
| 3747 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3748 | String proxyHost = data[0]; |
| 3749 | int proxyPort = 8080; |
| 3750 | if (data.length > 1) { |
| 3751 | try { |
| 3752 | proxyPort = Integer.parseInt(data[1]); |
| 3753 | } catch (NumberFormatException e) { |
| 3754 | return; |
| 3755 | } |
| 3756 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3757 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3758 | setGlobalProxy(p); |
| 3759 | } |
| 3760 | } |
| 3761 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3762 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 3763 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 3764 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3765 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3766 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3767 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3768 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3769 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3770 | final long ident = Binder.clearCallingIdentity(); |
| 3771 | try { |
| 3772 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3773 | } finally { |
| 3774 | Binder.restoreCallingIdentity(ident); |
| 3775 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | private static class SettingsObserver extends ContentObserver { |
| 3779 | private int mWhat; |
| 3780 | private Handler mHandler; |
| 3781 | SettingsObserver(Handler handler, int what) { |
| 3782 | super(handler); |
| 3783 | mHandler = handler; |
| 3784 | mWhat = what; |
| 3785 | } |
| 3786 | |
| 3787 | void observe(Context context) { |
| 3788 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3789 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 3790 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | @Override |
| 3794 | public void onChange(boolean selfChange) { |
| 3795 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 3796 | } |
| 3797 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3798 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3799 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3800 | Slog.d(TAG, s); |
| 3801 | } |
| 3802 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3803 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3804 | Slog.e(TAG, s); |
| 3805 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3806 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3807 | int convertFeatureToNetworkType(int networkType, String feature) { |
| 3808 | int usedNetworkType = networkType; |
| 3809 | |
| 3810 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 3811 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 3812 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 3813 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 3814 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 3815 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) || |
| 3816 | TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 3817 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 3818 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 3819 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 3820 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) { |
| 3821 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA; |
| 3822 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) { |
| 3823 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS; |
| 3824 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) { |
| 3825 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS; |
| 3826 | } else { |
| 3827 | Slog.e(TAG, "Can't match any mobile netTracker!"); |
| 3828 | } |
| 3829 | } else if (networkType == ConnectivityManager.TYPE_WIFI) { |
| 3830 | if (TextUtils.equals(feature, "p2p")) { |
| 3831 | usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P; |
| 3832 | } else { |
| 3833 | Slog.e(TAG, "Can't match any wifi netTracker!"); |
| 3834 | } |
| 3835 | } else { |
| 3836 | Slog.e(TAG, "Unexpected network type"); |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3837 | } |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3838 | return usedNetworkType; |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3839 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3840 | |
| 3841 | private static <T> T checkNotNull(T value, String message) { |
| 3842 | if (value == null) { |
| 3843 | throw new NullPointerException(message); |
| 3844 | } |
| 3845 | return value; |
| 3846 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3847 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3848 | /** |
| 3849 | * Protect a socket from VPN routing rules. This method is used by |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3850 | * VpnBuilder and not available in ConnectivityManager. Permissions |
| 3851 | * are checked in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3852 | * @hide |
| 3853 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3854 | @Override |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3855 | public boolean protectVpn(ParcelFileDescriptor socket) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3856 | throwIfLockdownEnabled(); |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3857 | try { |
| 3858 | int type = mActiveDefaultNetwork; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3859 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3860 | if (ConnectivityManager.isNetworkTypeValid(type) && mNetTrackers[type] != null) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3861 | synchronized(mVpns) { |
Chad Brubaker | bcf12b3 | 2014-02-11 14:18:56 -0800 | [diff] [blame] | 3862 | mVpns.get(user).protect(socket); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3863 | } |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3864 | return true; |
| 3865 | } |
| 3866 | } catch (Exception e) { |
| 3867 | // ignore |
| 3868 | } finally { |
| 3869 | try { |
| 3870 | socket.close(); |
| 3871 | } catch (Exception e) { |
| 3872 | // ignore |
| 3873 | } |
| 3874 | } |
| 3875 | return false; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3878 | /** |
| 3879 | * Prepare for a VPN application. This method is used by VpnDialogs |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3880 | * and not available in ConnectivityManager. Permissions are checked |
| 3881 | * in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3882 | * @hide |
| 3883 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3884 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 3885 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3886 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3887 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3888 | synchronized(mVpns) { |
| 3889 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 3890 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3891 | } |
| 3892 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 3893 | @Override |
| 3894 | public void markSocketAsUser(ParcelFileDescriptor socket, int uid) { |
| 3895 | enforceMarkNetworkSocketPermission(); |
| 3896 | final long token = Binder.clearCallingIdentity(); |
| 3897 | try { |
| 3898 | int mark = mNetd.getMarkForUid(uid); |
| 3899 | // Clear the mark on the socket if no mark is needed to prevent socket reuse issues |
| 3900 | if (mark == -1) { |
| 3901 | mark = 0; |
| 3902 | } |
| 3903 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 3904 | } catch (RemoteException e) { |
| 3905 | } finally { |
| 3906 | Binder.restoreCallingIdentity(token); |
| 3907 | } |
| 3908 | } |
| 3909 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3910 | /** |
| 3911 | * Configure a TUN interface and return its file descriptor. Parameters |
| 3912 | * 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] | 3913 | * and not available in ConnectivityManager. Permissions are checked in |
| 3914 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3915 | * @hide |
| 3916 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3917 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3918 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3919 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3920 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3921 | synchronized(mVpns) { |
| 3922 | return mVpns.get(user).establish(config); |
| 3923 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3926 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3927 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 3928 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3929 | */ |
| 3930 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3931 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3932 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3933 | final LinkProperties egress = getActiveLinkProperties(); |
| 3934 | if (egress == null) { |
| 3935 | throw new IllegalStateException("Missing active network connection"); |
| 3936 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3937 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3938 | synchronized(mVpns) { |
| 3939 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 3940 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
| 3943 | /** |
| 3944 | * Return the information of the ongoing legacy VPN. This method is used |
| 3945 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 3946 | * are checked in Vpn class. |
| 3947 | * @hide |
| 3948 | */ |
| 3949 | @Override |
| 3950 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3951 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3952 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3953 | synchronized(mVpns) { |
| 3954 | return mVpns.get(user).getLegacyVpnInfo(); |
| 3955 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3956 | } |
| 3957 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3958 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3959 | * Returns the information of the ongoing VPN. This method is used by VpnDialogs and |
| 3960 | * not available in ConnectivityManager. |
| 3961 | * Permissions are checked in Vpn class. |
| 3962 | * @hide |
| 3963 | */ |
| 3964 | @Override |
| 3965 | public VpnConfig getVpnConfig() { |
| 3966 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3967 | synchronized(mVpns) { |
| 3968 | return mVpns.get(user).getVpnConfig(); |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | /** |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3973 | * Callback for VPN subsystem. Currently VPN is not adapted to the service |
| 3974 | * through NetworkStateTracker since it works differently. For example, it |
| 3975 | * needs to override DNS servers but never takes the default routes. It |
| 3976 | * relies on another data network, and it could keep existing connections |
| 3977 | * alive after reconnecting, switching between networks, or even resuming |
| 3978 | * from deep sleep. Calls from applications should be done synchronously |
| 3979 | * to avoid race conditions. As these are all hidden APIs, refactoring can |
| 3980 | * be done whenever a better abstraction is developed. |
| 3981 | */ |
| 3982 | public class VpnCallback { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3983 | private VpnCallback() { |
| 3984 | } |
| 3985 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3986 | public void onStateChanged(NetworkInfo info) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3987 | mHandler.obtainMessage(EVENT_VPN_STATE_CHANGED, info).sendToTarget(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3990 | public void override(String iface, List<String> dnsServers, List<String> searchDomains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3991 | if (dnsServers == null) { |
| 3992 | restore(); |
| 3993 | return; |
| 3994 | } |
| 3995 | |
| 3996 | // Convert DNS servers into addresses. |
| 3997 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 3998 | for (String address : dnsServers) { |
| 3999 | // Double check the addresses and remove invalid ones. |
| 4000 | try { |
| 4001 | addresses.add(InetAddress.parseNumericAddress(address)); |
| 4002 | } catch (Exception e) { |
| 4003 | // ignore |
| 4004 | } |
| 4005 | } |
| 4006 | if (addresses.isEmpty()) { |
| 4007 | restore(); |
| 4008 | return; |
| 4009 | } |
| 4010 | |
| 4011 | // Concatenate search domains into a string. |
| 4012 | StringBuilder buffer = new StringBuilder(); |
| 4013 | if (searchDomains != null) { |
| 4014 | for (String domain : searchDomains) { |
| 4015 | buffer.append(domain).append(' '); |
| 4016 | } |
| 4017 | } |
| 4018 | String domains = buffer.toString().trim(); |
| 4019 | |
| 4020 | // Apply DNS changes. |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4021 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4022 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4023 | // updateDnsLocked("VPN", netId, addresses, domains); |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4024 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4025 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4026 | // Temporarily disable the default proxy (not global). |
| 4027 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4028 | mDefaultProxyDisabled = true; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4029 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4030 | sendProxyBroadcast(null); |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | // TODO: support proxy per network. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4035 | } |
| 4036 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4037 | public void restore() { |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4038 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4039 | mDefaultProxyDisabled = false; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4040 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4041 | sendProxyBroadcast(mDefaultProxy); |
| 4042 | } |
| 4043 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4044 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4045 | |
| 4046 | public void protect(ParcelFileDescriptor socket) { |
| 4047 | try { |
| 4048 | final int mark = mNetd.getMarkForProtect(); |
| 4049 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 4050 | } catch (RemoteException e) { |
| 4051 | } |
| 4052 | } |
| 4053 | |
| 4054 | public void setRoutes(String interfaze, List<RouteInfo> routes) { |
| 4055 | for (RouteInfo route : routes) { |
| 4056 | try { |
| 4057 | mNetd.setMarkedForwardingRoute(interfaze, route); |
| 4058 | } catch (RemoteException e) { |
| 4059 | } |
| 4060 | } |
| 4061 | } |
| 4062 | |
| 4063 | public void setMarkedForwarding(String interfaze) { |
| 4064 | try { |
| 4065 | mNetd.setMarkedForwarding(interfaze); |
| 4066 | } catch (RemoteException e) { |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | public void clearMarkedForwarding(String interfaze) { |
| 4071 | try { |
| 4072 | mNetd.clearMarkedForwarding(interfaze); |
| 4073 | } catch (RemoteException e) { |
| 4074 | } |
| 4075 | } |
| 4076 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4077 | public void addUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4078 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 4079 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4080 | addUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4083 | public void clearUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4084 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 4085 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4086 | clearUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4087 | } |
| 4088 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4089 | public void addUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 4090 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4091 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4092 | // try { |
| 4093 | // mNetd.setUidRangeRoute(netId, uidStart, uidEnd, forwardDns); |
| 4094 | // } catch (RemoteException e) { |
| 4095 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4096 | |
| 4097 | } |
| 4098 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4099 | public void clearUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 4100 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4101 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4102 | // try { |
| 4103 | // mNetd.clearUidRangeRoute(interfaze, uidStart, uidEnd); |
| 4104 | // } catch (RemoteException e) { |
| 4105 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4106 | |
| 4107 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4108 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4109 | |
| 4110 | @Override |
| 4111 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 4112 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4113 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 4114 | return false; |
| 4115 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4116 | |
| 4117 | // Tear down existing lockdown if profile was removed |
| 4118 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 4119 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 4120 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4121 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 4122 | return false; |
| 4123 | } |
| 4124 | |
| 4125 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 4126 | final VpnProfile profile = VpnProfile.decode( |
| 4127 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4128 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4129 | synchronized(mVpns) { |
| 4130 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 4131 | profile)); |
| 4132 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4133 | } else { |
| 4134 | setLockdownTracker(null); |
| 4135 | } |
| 4136 | |
| 4137 | return true; |
| 4138 | } |
| 4139 | |
| 4140 | /** |
| 4141 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4142 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4143 | */ |
| 4144 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4145 | // Shutdown any existing tracker |
| 4146 | final LockdownVpnTracker existing = mLockdownTracker; |
| 4147 | mLockdownTracker = null; |
| 4148 | if (existing != null) { |
| 4149 | existing.shutdown(); |
| 4150 | } |
| 4151 | |
| 4152 | try { |
| 4153 | if (tracker != null) { |
| 4154 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 4155 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4156 | mLockdownTracker = tracker; |
| 4157 | mLockdownTracker.init(); |
| 4158 | } else { |
| 4159 | mNetd.setFirewallEnabled(false); |
| 4160 | } |
| 4161 | } catch (RemoteException e) { |
| 4162 | // ignored; NMS lives inside system_server |
| 4163 | } |
| 4164 | } |
| 4165 | |
| 4166 | private void throwIfLockdownEnabled() { |
| 4167 | if (mLockdownEnabled) { |
| 4168 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4169 | } |
| 4170 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4171 | |
| 4172 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 4173 | enforceConnectivityInternalPermission(); |
| 4174 | |
| 4175 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 4176 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 4177 | } |
| 4178 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 4179 | |
| 4180 | public int findConnectionTypeForIface(String iface) { |
| 4181 | enforceConnectivityInternalPermission(); |
| 4182 | |
| 4183 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
| 4184 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 4185 | if (tracker != null) { |
| 4186 | LinkProperties lp = tracker.getLinkProperties(); |
| 4187 | if (lp != null && iface.equals(lp.getInterfaceName())) { |
| 4188 | return tracker.getNetworkInfo().getType(); |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | return ConnectivityManager.TYPE_NONE; |
| 4193 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4194 | |
| 4195 | /** |
| 4196 | * Have mobile data fail fast if enabled. |
| 4197 | * |
| 4198 | * @param enabled DctConstants.ENABLED/DISABLED |
| 4199 | */ |
| 4200 | private void setEnableFailFastMobileData(int enabled) { |
| 4201 | int tag; |
| 4202 | |
| 4203 | if (enabled == DctConstants.ENABLED) { |
| 4204 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 4205 | } else { |
| 4206 | tag = mEnableFailFastMobileDataTag.get(); |
| 4207 | } |
| 4208 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 4209 | enabled)); |
| 4210 | } |
| 4211 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4212 | private boolean isMobileDataStateTrackerReady() { |
| 4213 | MobileDataStateTracker mdst = |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4214 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4215 | return (mdst != null) && (mdst.isReady()); |
| 4216 | } |
| 4217 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4218 | /** |
| 4219 | * The ResultReceiver resultCode for checkMobileProvisioning (CMP_RESULT_CODE) |
| 4220 | */ |
| 4221 | |
| 4222 | /** |
| 4223 | * No connection was possible to the network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4224 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4225 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4226 | private static final int CMP_RESULT_CODE_NO_CONNECTION = 0; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4227 | |
| 4228 | /** |
| 4229 | * A connection was made to the internet, all is well. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4230 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4231 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4232 | private static final int CMP_RESULT_CODE_CONNECTABLE = 1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4233 | |
| 4234 | /** |
| 4235 | * 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] | 4236 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4237 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4238 | private static final int CMP_RESULT_CODE_NO_DNS = 2; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4239 | |
| 4240 | /** |
| 4241 | * A connection was made but could not open a TCP connection. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4242 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4243 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4244 | private static final int CMP_RESULT_CODE_NO_TCP_CONNECTION = 3; |
| 4245 | |
| 4246 | /** |
| 4247 | * A connection was made but there was a redirection, we appear to be in walled garden. |
| 4248 | * This is an indication of a warm sim on a mobile network such as T-Mobile. |
| 4249 | */ |
| 4250 | private static final int CMP_RESULT_CODE_REDIRECTED = 4; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4251 | |
| 4252 | /** |
| 4253 | * The mobile network is a provisioning network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4254 | * 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] | 4255 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4256 | private static final int CMP_RESULT_CODE_PROVISIONING_NETWORK = 5; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4257 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4258 | /** |
| 4259 | * The mobile network is provisioning |
| 4260 | */ |
| 4261 | private static final int CMP_RESULT_CODE_IS_PROVISIONING = 6; |
| 4262 | |
| 4263 | private AtomicBoolean mIsProvisioningNetwork = new AtomicBoolean(false); |
| 4264 | private AtomicBoolean mIsStartingProvisioning = new AtomicBoolean(false); |
| 4265 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4266 | private AtomicBoolean mIsCheckingMobileProvisioning = new AtomicBoolean(false); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4267 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4268 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4269 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
| 4270 | int timeOutMs = -1; |
| 4271 | if (DBG) log("checkMobileProvisioning: E suggestedTimeOutMs=" + suggestedTimeOutMs); |
| 4272 | enforceConnectivityInternalPermission(); |
Wink Saville | 68e6c64 | 2013-07-02 10:55:14 -0700 | [diff] [blame] | 4273 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4274 | final long token = Binder.clearCallingIdentity(); |
| 4275 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4276 | timeOutMs = suggestedTimeOutMs; |
| 4277 | if (suggestedTimeOutMs > CheckMp.MAX_TIMEOUT_MS) { |
| 4278 | timeOutMs = CheckMp.MAX_TIMEOUT_MS; |
| 4279 | } |
| 4280 | |
| 4281 | // Check that mobile networks are supported |
| 4282 | if (!isNetworkSupported(ConnectivityManager.TYPE_MOBILE) |
| 4283 | || !isNetworkSupported(ConnectivityManager.TYPE_MOBILE_HIPRI)) { |
| 4284 | if (DBG) log("checkMobileProvisioning: X no mobile network"); |
| 4285 | return timeOutMs; |
| 4286 | } |
| 4287 | |
| 4288 | // If we're already checking don't do it again |
| 4289 | // TODO: Add a queue of results... |
| 4290 | if (mIsCheckingMobileProvisioning.getAndSet(true)) { |
| 4291 | if (DBG) log("checkMobileProvisioning: X already checking ignore for the moment"); |
| 4292 | return timeOutMs; |
| 4293 | } |
| 4294 | |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4295 | // Start off with mobile notification off |
| 4296 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4297 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4298 | CheckMp checkMp = new CheckMp(mContext, this); |
| 4299 | CheckMp.CallBack cb = new CheckMp.CallBack() { |
| 4300 | @Override |
| 4301 | void onComplete(Integer result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4302 | if (DBG) log("CheckMp.onComplete: result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4303 | NetworkInfo ni = |
| 4304 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI].getNetworkInfo(); |
| 4305 | switch(result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4306 | case CMP_RESULT_CODE_CONNECTABLE: |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4307 | case CMP_RESULT_CODE_NO_CONNECTION: |
| 4308 | case CMP_RESULT_CODE_NO_DNS: |
| 4309 | case CMP_RESULT_CODE_NO_TCP_CONNECTION: { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4310 | if (DBG) log("CheckMp.onComplete: ignore, connected or no connection"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4311 | break; |
| 4312 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4313 | case CMP_RESULT_CODE_REDIRECTED: { |
| 4314 | if (DBG) log("CheckMp.onComplete: warm sim"); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4315 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4316 | if (TextUtils.isEmpty(url)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4317 | url = getMobileRedirectedProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4318 | } |
| 4319 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4320 | if (DBG) log("CheckMp.onComplete: warm (redirected), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4321 | setProvNotificationVisible(true, |
| 4322 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4323 | url); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4324 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4325 | if (DBG) log("CheckMp.onComplete: warm (redirected), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4326 | } |
| 4327 | break; |
| 4328 | } |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4329 | case CMP_RESULT_CODE_PROVISIONING_NETWORK: { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4330 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4331 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4332 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4333 | setProvNotificationVisible(true, |
| 4334 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4335 | url); |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4336 | // Mark that we've got a provisioning network and |
| 4337 | // Disable Mobile Data until user actually starts provisioning. |
| 4338 | mIsProvisioningNetwork.set(true); |
| 4339 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4340 | mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4341 | mdst.setInternalDataEnable(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4342 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4343 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4344 | } |
| 4345 | break; |
| 4346 | } |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4347 | case CMP_RESULT_CODE_IS_PROVISIONING: { |
| 4348 | // FIXME: Need to know when provisioning is done. Probably we can |
| 4349 | // check the completion status if successful we're done if we |
| 4350 | // "timedout" or still connected to provisioning APN turn off data? |
| 4351 | if (DBG) log("CheckMp.onComplete: provisioning started"); |
| 4352 | mIsStartingProvisioning.set(false); |
| 4353 | break; |
| 4354 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4355 | default: { |
| 4356 | loge("CheckMp.onComplete: ignore unexpected result=" + result); |
| 4357 | break; |
| 4358 | } |
| 4359 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4360 | mIsCheckingMobileProvisioning.set(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4361 | } |
| 4362 | }; |
| 4363 | CheckMp.Params params = |
| 4364 | new CheckMp.Params(checkMp.getDefaultUrl(), timeOutMs, cb); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4365 | if (DBG) log("checkMobileProvisioning: params=" + params); |
Paul Jensen | b6bb6c6 | 2014-05-12 17:39:09 -0400 | [diff] [blame] | 4366 | // TODO: Reenable when calls to the now defunct |
| 4367 | // MobileDataStateTracker.isProvisioningNetwork() are removed. |
| 4368 | // This code should be moved to the Telephony code. |
| 4369 | // checkMp.execute(params); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4370 | } finally { |
| 4371 | Binder.restoreCallingIdentity(token); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4372 | if (DBG) log("checkMobileProvisioning: X"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4373 | } |
| 4374 | return timeOutMs; |
| 4375 | } |
| 4376 | |
| 4377 | static class CheckMp extends |
| 4378 | AsyncTask<CheckMp.Params, Void, Integer> { |
| 4379 | private static final String CHECKMP_TAG = "CheckMp"; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4380 | |
| 4381 | // adb shell setprop persist.checkmp.testfailures 1 to enable testing failures |
| 4382 | private static boolean mTestingFailures; |
| 4383 | |
| 4384 | // Choosing 4 loops as half of them will use HTTPS and the other half HTTP |
| 4385 | private static final int MAX_LOOPS = 4; |
| 4386 | |
| 4387 | // Number of milli-seconds to complete all of the retires |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4388 | public static final int MAX_TIMEOUT_MS = 60000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4389 | |
| 4390 | // The socket should retry only 5 seconds, the default is longer |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4391 | private static final int SOCKET_TIMEOUT_MS = 5000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4392 | |
| 4393 | // Sleep time for network errors |
| 4394 | private static final int NET_ERROR_SLEEP_SEC = 3; |
| 4395 | |
| 4396 | // Sleep time for network route establishment |
| 4397 | private static final int NET_ROUTE_ESTABLISHMENT_SLEEP_SEC = 3; |
| 4398 | |
| 4399 | // Short sleep time for polling :( |
| 4400 | private static final int POLLING_SLEEP_SEC = 1; |
| 4401 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4402 | private Context mContext; |
| 4403 | private ConnectivityService mCs; |
| 4404 | private TelephonyManager mTm; |
| 4405 | private Params mParams; |
| 4406 | |
| 4407 | /** |
| 4408 | * Parameters for AsyncTask.execute |
| 4409 | */ |
| 4410 | static class Params { |
| 4411 | private String mUrl; |
| 4412 | private long mTimeOutMs; |
| 4413 | private CallBack mCb; |
| 4414 | |
| 4415 | Params(String url, long timeOutMs, CallBack cb) { |
| 4416 | mUrl = url; |
| 4417 | mTimeOutMs = timeOutMs; |
| 4418 | mCb = cb; |
| 4419 | } |
| 4420 | |
| 4421 | @Override |
| 4422 | public String toString() { |
| 4423 | return "{" + " url=" + mUrl + " mTimeOutMs=" + mTimeOutMs + " mCb=" + mCb + "}"; |
| 4424 | } |
| 4425 | } |
| 4426 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4427 | // As explained to me by Brian Carlstrom and Kenny Root, Certificates can be |
| 4428 | // issued by name or ip address, for Google its by name so when we construct |
| 4429 | // this HostnameVerifier we'll pass the original Uri and use it to verify |
| 4430 | // the host. If the host name in the original uril fails we'll test the |
| 4431 | // hostname parameter just incase things change. |
| 4432 | static class CheckMpHostnameVerifier implements HostnameVerifier { |
| 4433 | Uri mOrgUri; |
| 4434 | |
| 4435 | CheckMpHostnameVerifier(Uri orgUri) { |
| 4436 | mOrgUri = orgUri; |
| 4437 | } |
| 4438 | |
| 4439 | @Override |
| 4440 | public boolean verify(String hostname, SSLSession session) { |
| 4441 | HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); |
| 4442 | String orgUriHost = mOrgUri.getHost(); |
| 4443 | boolean retVal = hv.verify(orgUriHost, session) || hv.verify(hostname, session); |
| 4444 | if (DBG) { |
| 4445 | log("isMobileOk: hostnameVerify retVal=" + retVal + " hostname=" + hostname |
| 4446 | + " orgUriHost=" + orgUriHost); |
| 4447 | } |
| 4448 | return retVal; |
| 4449 | } |
| 4450 | } |
| 4451 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4452 | /** |
| 4453 | * The call back object passed in Params. onComplete will be called |
| 4454 | * on the main thread. |
| 4455 | */ |
| 4456 | abstract static class CallBack { |
| 4457 | // Called on the main thread. |
| 4458 | abstract void onComplete(Integer result); |
| 4459 | } |
| 4460 | |
| 4461 | public CheckMp(Context context, ConnectivityService cs) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4462 | if (Build.IS_DEBUGGABLE) { |
| 4463 | mTestingFailures = |
| 4464 | SystemProperties.getInt("persist.checkmp.testfailures", 0) == 1; |
| 4465 | } else { |
| 4466 | mTestingFailures = false; |
| 4467 | } |
| 4468 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4469 | mContext = context; |
| 4470 | mCs = cs; |
| 4471 | |
| 4472 | // Setup access to TelephonyService we'll be using. |
| 4473 | mTm = (TelephonyManager) mContext.getSystemService( |
| 4474 | Context.TELEPHONY_SERVICE); |
| 4475 | } |
| 4476 | |
| 4477 | /** |
| 4478 | * Get the default url to use for the test. |
| 4479 | */ |
| 4480 | public String getDefaultUrl() { |
| 4481 | // See http://go/clientsdns for usage approval |
| 4482 | String server = Settings.Global.getString(mContext.getContentResolver(), |
| 4483 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 4484 | if (server == null) { |
| 4485 | server = "clients3.google.com"; |
| 4486 | } |
| 4487 | return "http://" + server + "/generate_204"; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * Detect if its possible to connect to the http url. DNS based detection techniques |
| 4492 | * do not work at all hotspots. The best way to check is to perform a request to |
| 4493 | * a known address that fetches the data we expect. |
| 4494 | */ |
| 4495 | private synchronized Integer isMobileOk(Params params) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4496 | Integer result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4497 | Uri orgUri = Uri.parse(params.mUrl); |
| 4498 | Random rand = new Random(); |
| 4499 | mParams = params; |
| 4500 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4501 | if (mCs.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4502 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4503 | log("isMobileOk: X not mobile capable result=" + result); |
| 4504 | return result; |
| 4505 | } |
| 4506 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4507 | if (mCs.mIsStartingProvisioning.get()) { |
| 4508 | result = CMP_RESULT_CODE_IS_PROVISIONING; |
| 4509 | log("isMobileOk: X is provisioning result=" + result); |
| 4510 | return result; |
| 4511 | } |
| 4512 | |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4513 | // See if we've already determined we've got a provisioning connection, |
| 4514 | // if so we don't need to do anything active. |
| 4515 | MobileDataStateTracker mdstDefault = (MobileDataStateTracker) |
| 4516 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4517 | boolean isDefaultProvisioning = mdstDefault.isProvisioningNetwork(); |
| 4518 | log("isMobileOk: isDefaultProvisioning=" + isDefaultProvisioning); |
| 4519 | |
| 4520 | MobileDataStateTracker mdstHipri = (MobileDataStateTracker) |
| 4521 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4522 | boolean isHipriProvisioning = mdstHipri.isProvisioningNetwork(); |
| 4523 | log("isMobileOk: isHipriProvisioning=" + isHipriProvisioning); |
| 4524 | |
| 4525 | if (isDefaultProvisioning || isHipriProvisioning) { |
| 4526 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
| 4527 | log("isMobileOk: X default || hipri is provisioning result=" + result); |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4528 | return result; |
| 4529 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4530 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4531 | try { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4532 | // Continue trying to connect until time has run out |
| 4533 | long endTime = SystemClock.elapsedRealtime() + params.mTimeOutMs; |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4534 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4535 | if (!mCs.isMobileDataStateTrackerReady()) { |
| 4536 | // Wait for MobileDataStateTracker to be ready. |
| 4537 | if (DBG) log("isMobileOk: mdst is not ready"); |
| 4538 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4539 | if (mCs.isMobileDataStateTrackerReady()) { |
| 4540 | // Enable fail fast as we'll do retries here and use a |
| 4541 | // hipri connection so the default connection stays active. |
| 4542 | if (DBG) log("isMobileOk: mdst ready, enable fail fast of mobile data"); |
| 4543 | mCs.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 4544 | break; |
| 4545 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4546 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | |
| 4550 | log("isMobileOk: start hipri url=" + params.mUrl); |
| 4551 | |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4552 | // First wait until we can start using hipri |
| 4553 | Binder binder = new Binder(); |
| 4554 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4555 | int ret = mCs.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4556 | Phone.FEATURE_ENABLE_HIPRI, binder); |
| 4557 | if ((ret == PhoneConstants.APN_ALREADY_ACTIVE) |
| 4558 | || (ret == PhoneConstants.APN_REQUEST_STARTED)) { |
| 4559 | log("isMobileOk: hipri started"); |
| 4560 | break; |
| 4561 | } |
| 4562 | if (VDBG) log("isMobileOk: hipri not started yet"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4563 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4564 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | // Continue trying to connect until time has run out |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4568 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4569 | try { |
| 4570 | // Wait for hipri to connect. |
| 4571 | // TODO: Don't poll and handle situation where hipri fails |
| 4572 | // because default is retrying. See b/9569540 |
| 4573 | NetworkInfo.State state = mCs |
| 4574 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4575 | if (state != NetworkInfo.State.CONNECTED) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4576 | if (true/*VDBG*/) { |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4577 | log("isMobileOk: not connected ni=" + |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4578 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4579 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4580 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4581 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4582 | continue; |
| 4583 | } |
| 4584 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4585 | // Hipri has started check if this is a provisioning url |
| 4586 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4587 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4588 | if (mdst.isProvisioningNetwork()) { |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4589 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4590 | if (DBG) log("isMobileOk: X isProvisioningNetwork result=" + result); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4591 | return result; |
| 4592 | } else { |
| 4593 | if (DBG) log("isMobileOk: isProvisioningNetwork is false, continue"); |
| 4594 | } |
| 4595 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4596 | // Get of the addresses associated with the url host. We need to use the |
| 4597 | // address otherwise HttpURLConnection object will use the name to get |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4598 | // the addresses and will try every address but that will bypass the |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4599 | // route to host we setup and the connection could succeed as the default |
| 4600 | // interface might be connected to the internet via wifi or other interface. |
| 4601 | InetAddress[] addresses; |
| 4602 | try { |
| 4603 | addresses = InetAddress.getAllByName(orgUri.getHost()); |
| 4604 | } catch (UnknownHostException e) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4605 | result = CMP_RESULT_CODE_NO_DNS; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4606 | log("isMobileOk: X UnknownHostException result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4607 | return result; |
| 4608 | } |
| 4609 | log("isMobileOk: addresses=" + inetAddressesToString(addresses)); |
| 4610 | |
| 4611 | // Get the type of addresses supported by this link |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4612 | LinkProperties lp = mCs.getLinkPropertiesForTypeInternal( |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4613 | ConnectivityManager.TYPE_MOBILE_HIPRI); |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 4614 | boolean linkHasIpv4 = lp.hasIPv4Address(); |
| 4615 | boolean linkHasIpv6 = lp.hasIPv6Address(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4616 | log("isMobileOk: linkHasIpv4=" + linkHasIpv4 |
| 4617 | + " linkHasIpv6=" + linkHasIpv6); |
| 4618 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4619 | final ArrayList<InetAddress> validAddresses = |
| 4620 | new ArrayList<InetAddress>(addresses.length); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4621 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4622 | for (InetAddress addr : addresses) { |
| 4623 | if (((addr instanceof Inet4Address) && linkHasIpv4) || |
| 4624 | ((addr instanceof Inet6Address) && linkHasIpv6)) { |
| 4625 | validAddresses.add(addr); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4626 | } |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | if (validAddresses.size() == 0) { |
| 4630 | return CMP_RESULT_CODE_NO_CONNECTION; |
| 4631 | } |
| 4632 | |
| 4633 | int addrTried = 0; |
| 4634 | while (true) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4635 | // Loop through at most MAX_LOOPS valid addresses or until |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4636 | // we run out of time |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4637 | if (addrTried++ >= MAX_LOOPS) { |
| 4638 | log("isMobileOk: too many loops tried - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4639 | break; |
| 4640 | } |
| 4641 | if (SystemClock.elapsedRealtime() >= endTime) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4642 | log("isMobileOk: spend too much time - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4643 | break; |
| 4644 | } |
| 4645 | |
| 4646 | InetAddress hostAddr = validAddresses.get(rand.nextInt( |
| 4647 | validAddresses.size())); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4648 | |
| 4649 | // Make a route to host so we check the specific interface. |
| 4650 | if (mCs.requestRouteToHostAddress(ConnectivityManager.TYPE_MOBILE_HIPRI, |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 4651 | hostAddr.getAddress(), null)) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4652 | // Wait a short time to be sure the route is established ?? |
| 4653 | log("isMobileOk:" |
| 4654 | + " wait to establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4655 | sleep(NET_ROUTE_ESTABLISHMENT_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4656 | } else { |
| 4657 | log("isMobileOk:" |
| 4658 | + " could not establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4659 | // Wait a short time before the next attempt |
| 4660 | sleep(NET_ERROR_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4661 | continue; |
| 4662 | } |
| 4663 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4664 | // Rewrite the url to have numeric address to use the specific route |
| 4665 | // using http for half the attempts and https for the other half. |
| 4666 | // Doing https first and http second as on a redirected walled garden |
| 4667 | // such as t-mobile uses we get a SocketTimeoutException: "SSL |
| 4668 | // handshake timed out" which we declare as |
| 4669 | // CMP_RESULT_CODE_NO_TCP_CONNECTION. We could change this, but by |
| 4670 | // having http second we will be using logic used for some time. |
| 4671 | URL newUrl; |
| 4672 | String scheme = (addrTried <= (MAX_LOOPS/2)) ? "https" : "http"; |
| 4673 | newUrl = new URL(scheme, hostAddr.getHostAddress(), |
| 4674 | orgUri.getPath()); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4675 | log("isMobileOk: newUrl=" + newUrl); |
| 4676 | |
| 4677 | HttpURLConnection urlConn = null; |
| 4678 | try { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4679 | // Open the connection set the request headers and get the response |
| 4680 | urlConn = (HttpURLConnection)newUrl.openConnection( |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4681 | java.net.Proxy.NO_PROXY); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4682 | if (scheme.equals("https")) { |
| 4683 | ((HttpsURLConnection)urlConn).setHostnameVerifier( |
| 4684 | new CheckMpHostnameVerifier(orgUri)); |
| 4685 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4686 | urlConn.setInstanceFollowRedirects(false); |
| 4687 | urlConn.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 4688 | urlConn.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 4689 | urlConn.setUseCaches(false); |
| 4690 | urlConn.setAllowUserInteraction(false); |
Lorenzo Colitti | 143a2cf | 2013-10-08 10:41:25 +0900 | [diff] [blame] | 4691 | // Set the "Connection" to "Close" as by default "Keep-Alive" |
| 4692 | // is used which is useless in this case. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4693 | urlConn.setRequestProperty("Connection", "close"); |
| 4694 | int responseCode = urlConn.getResponseCode(); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4695 | |
| 4696 | // For debug display the headers |
| 4697 | Map<String, List<String>> headers = urlConn.getHeaderFields(); |
| 4698 | log("isMobileOk: headers=" + headers); |
| 4699 | |
| 4700 | // Close the connection |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4701 | urlConn.disconnect(); |
| 4702 | urlConn = null; |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4703 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4704 | if (mTestingFailures) { |
| 4705 | // Pretend no connection, this tests using http and https |
| 4706 | result = CMP_RESULT_CODE_NO_CONNECTION; |
| 4707 | log("isMobileOk: TESTING_FAILURES, pretend no connction"); |
| 4708 | continue; |
| 4709 | } |
| 4710 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4711 | if (responseCode == 204) { |
| 4712 | // Return |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4713 | result = CMP_RESULT_CODE_CONNECTABLE; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4714 | log("isMobileOk: X got expected responseCode=" + responseCode |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4715 | + " result=" + result); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4716 | return result; |
| 4717 | } else { |
| 4718 | // Retry to be sure this was redirected, we've gotten |
| 4719 | // occasions where a server returned 200 even though |
| 4720 | // the device didn't have a "warm" sim. |
| 4721 | log("isMobileOk: not expected responseCode=" + responseCode); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4722 | // TODO - it would be nice in the single-address case to do |
| 4723 | // another DNS resolve here, but flushing the cache is a bit |
| 4724 | // heavy-handed. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4725 | result = CMP_RESULT_CODE_REDIRECTED; |
| 4726 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4727 | } catch (Exception e) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4728 | log("isMobileOk: HttpURLConnection Exception" + e); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4729 | result = CMP_RESULT_CODE_NO_TCP_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4730 | if (urlConn != null) { |
| 4731 | urlConn.disconnect(); |
| 4732 | urlConn = null; |
| 4733 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4734 | sleep(NET_ERROR_SLEEP_SEC); |
| 4735 | continue; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4736 | } |
| 4737 | } |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4738 | log("isMobileOk: X loops|timed out result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4739 | return result; |
| 4740 | } catch (Exception e) { |
| 4741 | log("isMobileOk: Exception e=" + e); |
| 4742 | continue; |
| 4743 | } |
| 4744 | } |
| 4745 | log("isMobileOk: timed out"); |
| 4746 | } finally { |
| 4747 | log("isMobileOk: F stop hipri"); |
| 4748 | mCs.setEnableFailFastMobileData(DctConstants.DISABLED); |
| 4749 | mCs.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4750 | Phone.FEATURE_ENABLE_HIPRI); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4751 | |
| 4752 | // Wait for hipri to disconnect. |
| 4753 | long endTime = SystemClock.elapsedRealtime() + 5000; |
| 4754 | |
| 4755 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4756 | NetworkInfo.State state = mCs |
| 4757 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4758 | if (state != NetworkInfo.State.DISCONNECTED) { |
| 4759 | if (VDBG) { |
| 4760 | log("isMobileOk: connected ni=" + |
| 4761 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
| 4762 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4763 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4764 | continue; |
| 4765 | } |
| 4766 | } |
| 4767 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4768 | log("isMobileOk: X result=" + result); |
| 4769 | } |
| 4770 | return result; |
| 4771 | } |
| 4772 | |
| 4773 | @Override |
| 4774 | protected Integer doInBackground(Params... params) { |
| 4775 | return isMobileOk(params[0]); |
| 4776 | } |
| 4777 | |
| 4778 | @Override |
| 4779 | protected void onPostExecute(Integer result) { |
| 4780 | log("onPostExecute: result=" + result); |
| 4781 | if ((mParams != null) && (mParams.mCb != null)) { |
| 4782 | mParams.mCb.onComplete(result); |
| 4783 | } |
| 4784 | } |
| 4785 | |
| 4786 | private String inetAddressesToString(InetAddress[] addresses) { |
| 4787 | StringBuffer sb = new StringBuffer(); |
| 4788 | boolean firstTime = true; |
| 4789 | for(InetAddress addr : addresses) { |
| 4790 | if (firstTime) { |
| 4791 | firstTime = false; |
| 4792 | } else { |
| 4793 | sb.append(","); |
| 4794 | } |
| 4795 | sb.append(addr); |
| 4796 | } |
| 4797 | return sb.toString(); |
| 4798 | } |
| 4799 | |
| 4800 | private void printNetworkInfo() { |
| 4801 | boolean hasIccCard = mTm.hasIccCard(); |
| 4802 | int simState = mTm.getSimState(); |
| 4803 | log("hasIccCard=" + hasIccCard |
| 4804 | + " simState=" + simState); |
| 4805 | NetworkInfo[] ni = mCs.getAllNetworkInfo(); |
| 4806 | if (ni != null) { |
| 4807 | log("ni.length=" + ni.length); |
| 4808 | for (NetworkInfo netInfo: ni) { |
| 4809 | log("netInfo=" + netInfo.toString()); |
| 4810 | } |
| 4811 | } else { |
| 4812 | log("no network info ni=null"); |
| 4813 | } |
| 4814 | } |
| 4815 | |
| 4816 | /** |
| 4817 | * Sleep for a few seconds then return. |
| 4818 | * @param seconds |
| 4819 | */ |
| 4820 | private static void sleep(int seconds) { |
Jake Hamby | 8f9b33e | 2014-01-15 13:08:03 -0800 | [diff] [blame] | 4821 | long stopTime = System.nanoTime() + (seconds * 1000000000); |
| 4822 | long sleepTime; |
| 4823 | while ((sleepTime = stopTime - System.nanoTime()) > 0) { |
| 4824 | try { |
| 4825 | Thread.sleep(sleepTime / 1000000); |
| 4826 | } catch (InterruptedException ignored) { |
| 4827 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4828 | } |
| 4829 | } |
| 4830 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4831 | private static void log(String s) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4832 | Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s); |
| 4833 | } |
| 4834 | } |
| 4835 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4836 | // TODO: Move to ConnectivityManager and make public? |
| 4837 | private static final String CONNECTED_TO_PROVISIONING_NETWORK_ACTION = |
| 4838 | "com.android.server.connectivityservice.CONNECTED_TO_PROVISIONING_NETWORK_ACTION"; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4839 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4840 | private BroadcastReceiver mProvisioningReceiver = new BroadcastReceiver() { |
| 4841 | @Override |
| 4842 | public void onReceive(Context context, Intent intent) { |
| 4843 | if (intent.getAction().equals(CONNECTED_TO_PROVISIONING_NETWORK_ACTION)) { |
| 4844 | handleMobileProvisioningAction(intent.getStringExtra("EXTRA_URL")); |
| 4845 | } |
| 4846 | } |
| 4847 | }; |
| 4848 | |
| 4849 | private void handleMobileProvisioningAction(String url) { |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4850 | // Mark notification as not visible |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4851 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4852 | |
| 4853 | // If provisioning network handle as a special case, |
| 4854 | // otherwise launch browser with the intent directly. |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4855 | if (mIsProvisioningNetwork.get()) { |
| 4856 | if (DBG) log("handleMobileProvisioningAction: on prov network enable then launch"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4857 | // mIsStartingProvisioning.set(true); |
| 4858 | // MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4859 | // mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4860 | // mdst.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 4861 | // mdst.enableMobileProvisioning(url); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4862 | } else { |
Robert Greenwalt | 1b98e90 | 2014-03-19 12:41:54 -0700 | [diff] [blame] | 4863 | if (DBG) log("handleMobileProvisioningAction: not prov network"); |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 4864 | // Check for apps that can handle provisioning first |
| 4865 | Intent provisioningIntent = new Intent(TelephonyIntents.ACTION_CARRIER_SETUP); |
| 4866 | provisioningIntent.addCategory(TelephonyIntents.CATEGORY_MCCMNC_PREFIX |
| 4867 | + mTelephonyManager.getSimOperator()); |
| 4868 | if (mContext.getPackageManager().resolveActivity(provisioningIntent, 0 /* flags */) |
| 4869 | != null) { |
| 4870 | provisioningIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4871 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4872 | mContext.startActivity(provisioningIntent); |
| 4873 | } else { |
| 4874 | // If no apps exist, use standard URL ACTION_VIEW method |
| 4875 | Intent newIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, |
| 4876 | Intent.CATEGORY_APP_BROWSER); |
| 4877 | newIntent.setData(Uri.parse(url)); |
| 4878 | newIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4879 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4880 | try { |
| 4881 | mContext.startActivity(newIntent); |
| 4882 | } catch (ActivityNotFoundException e) { |
| 4883 | loge("handleMobileProvisioningAction: startActivity failed" + e); |
| 4884 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4885 | } |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 4890 | private volatile boolean mIsNotificationVisible = false; |
| 4891 | |
| 4892 | private void setProvNotificationVisible(boolean visible, int networkType, String extraInfo, |
| 4893 | String url) { |
| 4894 | if (DBG) { |
| 4895 | log("setProvNotificationVisible: E visible=" + visible + " networkType=" + networkType |
| 4896 | + " extraInfo=" + extraInfo + " url=" + url); |
| 4897 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4898 | |
| 4899 | Resources r = Resources.getSystem(); |
| 4900 | NotificationManager notificationManager = (NotificationManager) mContext |
| 4901 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 4902 | |
| 4903 | if (visible) { |
| 4904 | CharSequence title; |
| 4905 | CharSequence details; |
| 4906 | int icon; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4907 | Intent intent; |
| 4908 | Notification notification = new Notification(); |
| 4909 | switch (networkType) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4910 | case ConnectivityManager.TYPE_WIFI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4911 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 4912 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4913 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4914 | icon = R.drawable.stat_notify_wifi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4915 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 4916 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4917 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4918 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4919 | break; |
| 4920 | case ConnectivityManager.TYPE_MOBILE: |
| 4921 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4922 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4923 | // TODO: Change this to pull from NetworkInfo once a printable |
| 4924 | // name has been added to it |
| 4925 | details = mTelephonyManager.getNetworkOperatorName(); |
| 4926 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4927 | intent = new Intent(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 4928 | intent.putExtra("EXTRA_URL", url); |
| 4929 | intent.setFlags(0); |
| 4930 | notification.contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4931 | break; |
| 4932 | default: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4933 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4934 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4935 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4936 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4937 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 4938 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4939 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4940 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4941 | break; |
| 4942 | } |
| 4943 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4944 | notification.when = 0; |
| 4945 | notification.icon = icon; |
| 4946 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4947 | notification.tickerText = title; |
| 4948 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
| 4949 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4950 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4951 | notificationManager.notify(NOTIFICATION_ID, networkType, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4952 | } catch (NullPointerException npe) { |
| 4953 | loge("setNotificaitionVisible: visible notificationManager npe=" + npe); |
| 4954 | npe.printStackTrace(); |
| 4955 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4956 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4957 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4958 | notificationManager.cancel(NOTIFICATION_ID, networkType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4959 | } catch (NullPointerException npe) { |
| 4960 | loge("setNotificaitionVisible: cancel notificationManager npe=" + npe); |
| 4961 | npe.printStackTrace(); |
| 4962 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4963 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4964 | mIsNotificationVisible = visible; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4967 | /** Location to an updatable file listing carrier provisioning urls. |
| 4968 | * An example: |
| 4969 | * |
| 4970 | * <?xml version="1.0" encoding="utf-8"?> |
| 4971 | * <provisioningUrls> |
| 4972 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
| 4973 | * <redirectedUrl mcc="310" mnc="4">http://www.google.com</redirectedUrl> |
| 4974 | * </provisioningUrls> |
| 4975 | */ |
| 4976 | private static final String PROVISIONING_URL_PATH = |
| 4977 | "/data/misc/radio/provisioning_urls.xml"; |
| 4978 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4979 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4980 | /** XML tag for root element. */ |
| 4981 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 4982 | /** XML tag for individual url */ |
| 4983 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
| 4984 | /** XML tag for redirected url */ |
| 4985 | private static final String TAG_REDIRECTED_URL = "redirectedUrl"; |
| 4986 | /** XML attribute for mcc */ |
| 4987 | private static final String ATTR_MCC = "mcc"; |
| 4988 | /** XML attribute for mnc */ |
| 4989 | private static final String ATTR_MNC = "mnc"; |
| 4990 | |
| 4991 | private static final int REDIRECTED_PROVISIONING = 1; |
| 4992 | private static final int PROVISIONING = 2; |
| 4993 | |
| 4994 | private String getProvisioningUrlBaseFromFile(int type) { |
| 4995 | FileReader fileReader = null; |
| 4996 | XmlPullParser parser = null; |
| 4997 | Configuration config = mContext.getResources().getConfiguration(); |
| 4998 | String tagType; |
| 4999 | |
| 5000 | switch (type) { |
| 5001 | case PROVISIONING: |
| 5002 | tagType = TAG_PROVISIONING_URL; |
| 5003 | break; |
| 5004 | case REDIRECTED_PROVISIONING: |
| 5005 | tagType = TAG_REDIRECTED_URL; |
| 5006 | break; |
| 5007 | default: |
| 5008 | throw new RuntimeException("getProvisioningUrlBaseFromFile: Unexpected parameter " + |
| 5009 | type); |
| 5010 | } |
| 5011 | |
| 5012 | try { |
| 5013 | fileReader = new FileReader(mProvisioningUrlFile); |
| 5014 | parser = Xml.newPullParser(); |
| 5015 | parser.setInput(fileReader); |
| 5016 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 5017 | |
| 5018 | while (true) { |
| 5019 | XmlUtils.nextElement(parser); |
| 5020 | |
| 5021 | String element = parser.getName(); |
| 5022 | if (element == null) break; |
| 5023 | |
| 5024 | if (element.equals(tagType)) { |
| 5025 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 5026 | try { |
| 5027 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 5028 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 5029 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 5030 | parser.next(); |
| 5031 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 5032 | return parser.getText(); |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | } catch (NumberFormatException e) { |
| 5037 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 5038 | } |
| 5039 | } |
| 5040 | } |
| 5041 | return null; |
| 5042 | } catch (FileNotFoundException e) { |
| 5043 | loge("Carrier Provisioning Urls file not found"); |
| 5044 | } catch (XmlPullParserException e) { |
| 5045 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 5046 | } catch (IOException e) { |
| 5047 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 5048 | } finally { |
| 5049 | if (fileReader != null) { |
| 5050 | try { |
| 5051 | fileReader.close(); |
| 5052 | } catch (IOException e) {} |
| 5053 | } |
| 5054 | } |
| 5055 | return null; |
| 5056 | } |
| 5057 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5058 | @Override |
| 5059 | public String getMobileRedirectedProvisioningUrl() { |
| 5060 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5061 | String url = getProvisioningUrlBaseFromFile(REDIRECTED_PROVISIONING); |
| 5062 | if (TextUtils.isEmpty(url)) { |
| 5063 | url = mContext.getResources().getString(R.string.mobile_redirected_provisioning_url); |
| 5064 | } |
| 5065 | return url; |
| 5066 | } |
| 5067 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5068 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5069 | public String getMobileProvisioningUrl() { |
| 5070 | enforceConnectivityInternalPermission(); |
| 5071 | String url = getProvisioningUrlBaseFromFile(PROVISIONING); |
| 5072 | if (TextUtils.isEmpty(url)) { |
| 5073 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5074 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5075 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5076 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5077 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5078 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5079 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5080 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 5081 | if (TextUtils.isEmpty(phoneNumber)) { |
| 5082 | phoneNumber = "0000000000"; |
| 5083 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5084 | url = String.format(url, |
| 5085 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 5086 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5087 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5088 | } |
| 5089 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5090 | return url; |
| 5091 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 5092 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5093 | @Override |
| 5094 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
| 5095 | String extraInfo, String url) { |
| 5096 | enforceConnectivityInternalPermission(); |
| 5097 | setProvNotificationVisible(visible, networkType, extraInfo, url); |
| 5098 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 5099 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5100 | @Override |
| 5101 | public void setAirplaneMode(boolean enable) { |
| 5102 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5103 | final long ident = Binder.clearCallingIdentity(); |
| 5104 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 5105 | final ContentResolver cr = mContext.getContentResolver(); |
| 5106 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 5107 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 5108 | intent.putExtra("state", enable); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5109 | mContext.sendBroadcast(intent); |
| 5110 | } finally { |
| 5111 | Binder.restoreCallingIdentity(ident); |
| 5112 | } |
| 5113 | } |
| 5114 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 5115 | private void onUserStart(int userId) { |
| 5116 | synchronized(mVpns) { |
| 5117 | Vpn userVpn = mVpns.get(userId); |
| 5118 | if (userVpn != null) { |
| 5119 | loge("Starting user already has a VPN"); |
| 5120 | return; |
| 5121 | } |
| 5122 | userVpn = new Vpn(mContext, mVpnCallback, mNetd, this, userId); |
| 5123 | mVpns.put(userId, userVpn); |
| 5124 | userVpn.startMonitoring(mContext, mTrackerHandler); |
| 5125 | } |
| 5126 | } |
| 5127 | |
| 5128 | private void onUserStop(int userId) { |
| 5129 | synchronized(mVpns) { |
| 5130 | Vpn userVpn = mVpns.get(userId); |
| 5131 | if (userVpn == null) { |
| 5132 | loge("Stopping user has no VPN"); |
| 5133 | return; |
| 5134 | } |
| 5135 | mVpns.delete(userId); |
| 5136 | } |
| 5137 | } |
| 5138 | |
| 5139 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 5140 | @Override |
| 5141 | public void onReceive(Context context, Intent intent) { |
| 5142 | final String action = intent.getAction(); |
| 5143 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 5144 | if (userId == UserHandle.USER_NULL) return; |
| 5145 | |
| 5146 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 5147 | onUserStart(userId); |
| 5148 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 5149 | onUserStop(userId); |
| 5150 | } |
| 5151 | } |
| 5152 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5153 | |
| 5154 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5155 | public LinkQualityInfo getLinkQualityInfo(int networkType) { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5156 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5157 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5158 | return mNetTrackers[networkType].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5159 | } else { |
| 5160 | return null; |
| 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5165 | public LinkQualityInfo getActiveLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5166 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5167 | if (isNetworkTypeValid(mActiveDefaultNetwork) && |
| 5168 | mNetTrackers[mActiveDefaultNetwork] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5169 | return mNetTrackers[mActiveDefaultNetwork].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5170 | } else { |
| 5171 | return null; |
| 5172 | } |
| 5173 | } |
| 5174 | |
| 5175 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5176 | public LinkQualityInfo[] getAllLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5177 | enforceAccessPermission(); |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5178 | final ArrayList<LinkQualityInfo> result = Lists.newArrayList(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5179 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5180 | if (tracker != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5181 | LinkQualityInfo li = tracker.getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5182 | if (li != null) { |
| 5183 | result.add(li); |
| 5184 | } |
| 5185 | } |
| 5186 | } |
| 5187 | |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5188 | return result.toArray(new LinkQualityInfo[result.size()]); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | /* Infrastructure for network sampling */ |
| 5192 | |
| 5193 | private void handleNetworkSamplingTimeout() { |
| 5194 | |
| 5195 | log("Sampling interval elapsed, updating statistics .."); |
| 5196 | |
| 5197 | // initialize list of interfaces .. |
| 5198 | Map<String, SamplingDataTracker.SamplingSnapshot> mapIfaceToSample = |
| 5199 | new HashMap<String, SamplingDataTracker.SamplingSnapshot>(); |
| 5200 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5201 | if (tracker != null) { |
| 5202 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5203 | if (ifaceName != null) { |
| 5204 | mapIfaceToSample.put(ifaceName, null); |
| 5205 | } |
| 5206 | } |
| 5207 | } |
| 5208 | |
| 5209 | // Read samples for all interfaces |
| 5210 | SamplingDataTracker.getSamplingSnapshots(mapIfaceToSample); |
| 5211 | |
| 5212 | // process samples for all networks |
| 5213 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5214 | if (tracker != null) { |
| 5215 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5216 | SamplingDataTracker.SamplingSnapshot ss = mapIfaceToSample.get(ifaceName); |
| 5217 | if (ss != null) { |
| 5218 | // end the previous sampling cycle |
| 5219 | tracker.stopSampling(ss); |
| 5220 | // start a new sampling cycle .. |
| 5221 | tracker.startSampling(ss); |
| 5222 | } |
| 5223 | } |
| 5224 | } |
| 5225 | |
| 5226 | log("Done."); |
| 5227 | |
| 5228 | int samplingIntervalInSeconds = Settings.Global.getInt(mContext.getContentResolver(), |
| 5229 | Settings.Global.CONNECTIVITY_SAMPLING_INTERVAL_IN_SECONDS, |
| 5230 | DEFAULT_SAMPLING_INTERVAL_IN_SECONDS); |
| 5231 | |
| 5232 | if (DBG) log("Setting timer for " + String.valueOf(samplingIntervalInSeconds) + "seconds"); |
| 5233 | |
| 5234 | setAlarm(samplingIntervalInSeconds * 1000, mSampleIntervalElapsedIntent); |
| 5235 | } |
| 5236 | |
| 5237 | void setAlarm(int timeoutInMilliseconds, PendingIntent intent) { |
| 5238 | long wakeupTime = SystemClock.elapsedRealtime() + timeoutInMilliseconds; |
| 5239 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, intent); |
| 5240 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5241 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5242 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 5243 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5244 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 5245 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5246 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5247 | private static class NetworkFactoryInfo { |
| 5248 | public final String name; |
| 5249 | public final Messenger messenger; |
| 5250 | public final AsyncChannel asyncChannel; |
| 5251 | |
| 5252 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 5253 | this.name = name; |
| 5254 | this.messenger = messenger; |
| 5255 | this.asyncChannel = asyncChannel; |
| 5256 | } |
| 5257 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5258 | |
| 5259 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
| 5260 | static final boolean REQUEST = true; |
| 5261 | static final boolean LISTEN = false; |
| 5262 | |
| 5263 | final NetworkRequest request; |
| 5264 | IBinder mBinder; |
| 5265 | final int mPid; |
| 5266 | final int mUid; |
| 5267 | final Messenger messenger; |
| 5268 | final boolean isRequest; |
| 5269 | |
| 5270 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, boolean isRequest) { |
| 5271 | super(); |
| 5272 | messenger = m; |
| 5273 | request = r; |
| 5274 | mBinder = binder; |
| 5275 | mPid = getCallingPid(); |
| 5276 | mUid = getCallingUid(); |
| 5277 | this.isRequest = isRequest; |
| 5278 | |
| 5279 | try { |
| 5280 | mBinder.linkToDeath(this, 0); |
| 5281 | } catch (RemoteException e) { |
| 5282 | binderDied(); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | void unlinkDeathRecipient() { |
| 5287 | mBinder.unlinkToDeath(this, 0); |
| 5288 | } |
| 5289 | |
| 5290 | public void binderDied() { |
| 5291 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 5292 | request + ", " + mBinder + ")"); |
| 5293 | releaseNetworkRequest(request); |
| 5294 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5295 | |
| 5296 | public String toString() { |
| 5297 | return (isRequest ? "Request" : "Listen") + " from uid/pid:" + mUid + "/" + |
| 5298 | mPid + " for " + request; |
| 5299 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
| 5302 | @Override |
| 5303 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
| 5304 | Messenger messenger, int timeoutSec, IBinder binder) { |
| 5305 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 5306 | == false) { |
| 5307 | enforceConnectivityInternalPermission(); |
| 5308 | } else { |
| 5309 | enforceChangePermission(); |
| 5310 | } |
| 5311 | |
| 5312 | if (timeoutSec < 0 || timeoutSec > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_SEC) { |
| 5313 | throw new IllegalArgumentException("Bad timeout specified"); |
| 5314 | } |
| 5315 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 5316 | networkCapabilities), false, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5317 | if (DBG) log("requestNetwork for " + networkRequest); |
| 5318 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 5319 | NetworkRequestInfo.REQUEST); |
| 5320 | |
| 5321 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
| 5322 | if (timeoutSec > 0) { |
| 5323 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
| 5324 | nri), timeoutSec * 1000); |
| 5325 | } |
| 5326 | return networkRequest; |
| 5327 | } |
| 5328 | |
| 5329 | @Override |
| 5330 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 5331 | PendingIntent operation) { |
| 5332 | // TODO |
| 5333 | return null; |
| 5334 | } |
| 5335 | |
| 5336 | @Override |
| 5337 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 5338 | Messenger messenger, IBinder binder) { |
| 5339 | enforceAccessPermission(); |
| 5340 | |
| 5341 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 5342 | networkCapabilities), false, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5343 | if (DBG) log("listenForNetwork for " + networkRequest); |
| 5344 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 5345 | NetworkRequestInfo.LISTEN); |
| 5346 | |
| 5347 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 5348 | return networkRequest; |
| 5349 | } |
| 5350 | |
| 5351 | @Override |
| 5352 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 5353 | PendingIntent operation) { |
| 5354 | } |
| 5355 | |
| 5356 | @Override |
| 5357 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
| 5358 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, |
| 5359 | networkRequest)); |
| 5360 | } |
| 5361 | |
| 5362 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5363 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5364 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5365 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 5366 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5367 | } |
| 5368 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5369 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
| 5370 | if (VDBG) log("Got NetworkFactory Messenger for " + nfi.name); |
| 5371 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 5372 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 5373 | } |
| 5374 | |
| 5375 | @Override |
| 5376 | public void unregisterNetworkFactory(Messenger messenger) { |
| 5377 | enforceConnectivityInternalPermission(); |
| 5378 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 5379 | } |
| 5380 | |
| 5381 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 5382 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 5383 | if (nfi == null) { |
| 5384 | if (VDBG) log("Failed to find Messenger in unregisterNetworkFactory"); |
| 5385 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5386 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5387 | if (VDBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5390 | /** |
| 5391 | * NetworkAgentInfo supporting a request by requestId. |
| 5392 | * These have already been vetted (their Capabilities satisfy the request) |
| 5393 | * and the are the highest scored network available. |
| 5394 | * the are keyed off the Requests requestId. |
| 5395 | */ |
| 5396 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 5397 | new SparseArray<NetworkAgentInfo>(); |
| 5398 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5399 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 5400 | new SparseArray<NetworkAgentInfo>(); |
| 5401 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5402 | // NetworkAgentInfo keyed off its connecting messenger |
| 5403 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
| 5404 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 5405 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 5406 | |
| 5407 | private final NetworkRequest mDefaultRequest; |
| 5408 | |
| 5409 | public void registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 5410 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
| 5411 | int currentScore) { |
| 5412 | enforceConnectivityInternalPermission(); |
| 5413 | |
| 5414 | NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), nextNetId(), |
| 5415 | new NetworkInfo(networkInfo), new LinkProperties(linkProperties), |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 5416 | new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5417 | |
| 5418 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
| 5419 | } |
| 5420 | |
| 5421 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 5422 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 5423 | mNetworkAgentInfos.put(na.messenger, na); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5424 | try { |
| 5425 | mNetworkAgentInfoForType[na.networkInfo.getType()].add(na); |
| 5426 | } catch (NullPointerException e) { |
| 5427 | loge("registered NetworkAgent for unsupported type: " + na); |
| 5428 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5429 | mNetworkForNetId.put(na.network.netId, na); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5430 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 5431 | NetworkInfo networkInfo = na.networkInfo; |
| 5432 | na.networkInfo = null; |
| 5433 | updateNetworkInfo(na, networkInfo); |
| 5434 | } |
| 5435 | |
| 5436 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 5437 | LinkProperties newLp = networkAgent.linkProperties; |
| 5438 | int netId = networkAgent.network.netId; |
| 5439 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5440 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5441 | updateMtu(newLp, oldLp); |
| 5442 | // TODO - figure out what to do for clat |
| 5443 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 5444 | // updateMtu(lp, null); |
| 5445 | // } |
| 5446 | updateRoutes(newLp, oldLp, netId); |
| 5447 | updateDnses(newLp, oldLp, netId); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5448 | updateClat(newLp, oldLp, networkAgent); |
| 5449 | } |
| 5450 | |
| 5451 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo na) { |
| 5452 | // Update 464xlat state. |
| 5453 | if (mClat.requiresClat(na)) { |
| 5454 | |
| 5455 | // If the connection was previously using clat, but is not using it now, stop the clat |
| 5456 | // daemon. Normally, this happens automatically when the connection disconnects, but if |
| 5457 | // the disconnect is not reported, or if the connection's LinkProperties changed for |
| 5458 | // some other reason (e.g., handoff changes the IP addresses on the link), it would |
| 5459 | // still be running. If it's not running, then stopping it is a no-op. |
| 5460 | if (Nat464Xlat.isRunningClat(oldLp) && !Nat464Xlat.isRunningClat(newLp)) { |
| 5461 | mClat.stopClat(); |
| 5462 | } |
| 5463 | // If the link requires clat to be running, then start the daemon now. |
| 5464 | if (newLp != null && na.networkInfo.isConnected()) { |
| 5465 | mClat.startClat(na); |
| 5466 | } else { |
| 5467 | mClat.stopClat(); |
| 5468 | } |
| 5469 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5470 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5471 | |
| 5472 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5473 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 5474 | if (oldLp != null) { |
| 5475 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 5476 | } else if (newLp != null) { |
| 5477 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 5478 | } |
| 5479 | for (String iface : interfaceDiff.added) { |
| 5480 | try { |
| 5481 | mNetd.addInterfaceToNetwork(iface, netId); |
| 5482 | } catch (Exception e) { |
| 5483 | loge("Exception adding interface: " + e); |
| 5484 | } |
| 5485 | } |
| 5486 | for (String iface : interfaceDiff.removed) { |
| 5487 | try { |
| 5488 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 5489 | } catch (Exception e) { |
| 5490 | loge("Exception removing interface: " + e); |
| 5491 | } |
| 5492 | } |
| 5493 | } |
| 5494 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5495 | private void updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5496 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 5497 | if (oldLp != null) { |
| 5498 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 5499 | } else if (newLp != null) { |
| 5500 | routeDiff.added = newLp.getAllRoutes(); |
| 5501 | } |
| 5502 | |
| 5503 | // add routes before removing old in case it helps with continuous connectivity |
| 5504 | |
| 5505 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 5506 | for (RouteInfo route : routeDiff.added) { |
| 5507 | if (route.hasGateway()) continue; |
| 5508 | try { |
| 5509 | mNetd.addRoute(netId, route); |
| 5510 | } catch (Exception e) { |
| 5511 | loge("Exception in addRoute for non-gateway: " + e); |
| 5512 | } |
| 5513 | } |
| 5514 | for (RouteInfo route : routeDiff.added) { |
| 5515 | if (route.hasGateway() == false) continue; |
| 5516 | try { |
| 5517 | mNetd.addRoute(netId, route); |
| 5518 | } catch (Exception e) { |
| 5519 | loge("Exception in addRoute for gateway: " + e); |
| 5520 | } |
| 5521 | } |
| 5522 | |
| 5523 | for (RouteInfo route : routeDiff.removed) { |
| 5524 | try { |
| 5525 | mNetd.removeRoute(netId, route); |
| 5526 | } catch (Exception e) { |
| 5527 | loge("Exception in removeRoute: " + e); |
| 5528 | } |
| 5529 | } |
| 5530 | } |
| 5531 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5532 | if (oldLp == null || (newLp.isIdenticalDnses(oldLp) == false)) { |
| 5533 | Collection<InetAddress> dnses = newLp.getDnses(); |
| 5534 | if (dnses.size() == 0 && mDefaultDns != null) { |
| 5535 | dnses = new ArrayList(); |
| 5536 | dnses.add(mDefaultDns); |
| 5537 | if (DBG) { |
| 5538 | loge("no dns provided for netId " + netId + ", so using defaults"); |
| 5539 | } |
| 5540 | } |
| 5541 | try { |
| 5542 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), |
| 5543 | newLp.getDomains()); |
| 5544 | } catch (Exception e) { |
| 5545 | loge("Exception in setDnsServersForNetwork: " + e); |
| 5546 | } |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5547 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 5548 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 5549 | setDefaultDnsSystemProperties(dnses); |
| 5550 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5551 | } |
| 5552 | } |
| 5553 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5554 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 5555 | int last = 0; |
| 5556 | for (InetAddress dns : dnses) { |
| 5557 | ++last; |
| 5558 | String key = "net.dns" + last; |
| 5559 | String value = dns.getHostAddress(); |
| 5560 | SystemProperties.set(key, value); |
| 5561 | } |
| 5562 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 5563 | String key = "net.dns" + i; |
| 5564 | SystemProperties.set(key, ""); |
| 5565 | } |
| 5566 | mNumDnsEntries = last; |
| 5567 | } |
| 5568 | |
| 5569 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5570 | private void updateCapabilities(NetworkAgentInfo networkAgent, |
| 5571 | NetworkCapabilities networkCapabilities) { |
| 5572 | // TODO - what else here? Verify still satisfies everybody? |
| 5573 | // Check if satisfies somebody new? call callbacks? |
| 5574 | networkAgent.networkCapabilities = networkCapabilities; |
| 5575 | } |
| 5576 | |
| 5577 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 5578 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5579 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 5580 | nfi.asyncChannel.sendMessage(NetworkFactoryProtocol.CMD_REQUEST_NETWORK, score, 0, networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5581 | } |
| 5582 | } |
| 5583 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5584 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5585 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5586 | if (nri.messenger == null) return; // Default request has no msgr |
| 5587 | Object o; |
| 5588 | int a1 = 0; |
| 5589 | int a2 = 0; |
| 5590 | switch (notificationType) { |
| 5591 | case ConnectivityManager.CALLBACK_LOSING: |
| 5592 | a1 = 30; // TODO - read this from NetworkMonitor |
| 5593 | // fall through |
| 5594 | case ConnectivityManager.CALLBACK_PRECHECK: |
| 5595 | case ConnectivityManager.CALLBACK_AVAILABLE: |
| 5596 | case ConnectivityManager.CALLBACK_LOST: |
| 5597 | case ConnectivityManager.CALLBACK_CAP_CHANGED: |
| 5598 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
| 5599 | o = new NetworkRequest(nri.request); |
| 5600 | a2 = networkAgent.network.netId; |
| 5601 | break; |
| 5602 | } |
| 5603 | case ConnectivityManager.CALLBACK_UNAVAIL: |
| 5604 | case ConnectivityManager.CALLBACK_RELEASED: { |
| 5605 | o = new NetworkRequest(nri.request); |
| 5606 | break; |
| 5607 | } |
| 5608 | default: { |
| 5609 | loge("Unknown notificationType " + notificationType); |
| 5610 | return; |
| 5611 | } |
| 5612 | } |
| 5613 | Message msg = Message.obtain(); |
| 5614 | msg.arg1 = a1; |
| 5615 | msg.arg2 = a2; |
| 5616 | msg.obj = o; |
| 5617 | msg.what = notificationType; |
| 5618 | try { |
| 5619 | if (VDBG) log("sending notification " + notificationType + " for " + nri.request); |
| 5620 | nri.messenger.send(msg); |
| 5621 | } catch (RemoteException e) { |
| 5622 | // may occur naturally in the race of binder death. |
| 5623 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 5624 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5625 | } |
| 5626 | |
| 5627 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 5628 | if (oldNetwork == null) { |
| 5629 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 5630 | return; |
| 5631 | } |
| 5632 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 5633 | if (DBG) { |
| 5634 | if (oldNetwork.networkRequests.size() != 0) { |
| 5635 | loge("Dead network still had " + oldNetwork.networkRequests.size() + " requests"); |
| 5636 | } |
| 5637 | } |
| 5638 | oldNetwork.asyncChannel.disconnect(); |
| 5639 | } |
| 5640 | |
| 5641 | private void handleConnectionValidated(NetworkAgentInfo newNetwork) { |
| 5642 | if (newNetwork == null) { |
| 5643 | loge("Unknown NetworkAgentInfo in handleConnectionValidated"); |
| 5644 | return; |
| 5645 | } |
| 5646 | boolean keep = false; |
| 5647 | boolean isNewDefault = false; |
| 5648 | if (DBG) log("handleConnectionValidated for "+newNetwork.name()); |
| 5649 | // check if any NetworkRequest wants this NetworkAgent |
| 5650 | // first check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5651 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
| 5652 | if (VDBG) log(" new Network has: " + newNetwork.networkCapabilities); |
| 5653 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5654 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
| 5655 | if (nri.request.networkCapabilities.satisfiedByNetworkCapabilities( |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5656 | newNetwork.networkCapabilities)) { |
| 5657 | // next check if it's better than any current network we're using for |
| 5658 | // this request |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5659 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5660 | if (VDBG) { |
| 5661 | log("currentScore = " + |
| 5662 | (currentNetwork != null ? currentNetwork.currentScore : 0) + |
| 5663 | ", newScore = " + newNetwork.currentScore); |
| 5664 | } |
| 5665 | if (currentNetwork == null || |
| 5666 | currentNetwork.currentScore < newNetwork.currentScore) { |
| 5667 | if (currentNetwork != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5668 | if (VDBG) log(" accepting network in place of " + currentNetwork.name()); |
| 5669 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 5670 | currentNetwork.networkLingered.add(nri.request); |
| 5671 | affectedNetworks.add(currentNetwork); |
| 5672 | } else { |
| 5673 | if (VDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5674 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5675 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
| 5676 | newNetwork.networkRequests.put(nri.request.requestId, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5677 | keep = true; |
| 5678 | // TODO - this could get expensive if we have alot of requests for this |
| 5679 | // network. Think about if there is a way to reduce this. Push |
| 5680 | // netid->request mapping to each factory? |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5681 | sendUpdatedScoreToFactories(nri.request, newNetwork.currentScore); |
| 5682 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5683 | isNewDefault = true; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5684 | updateActiveDefaultNetwork(newNetwork); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5685 | if (newNetwork.linkProperties != null) { |
| 5686 | setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnses()); |
| 5687 | } else { |
| 5688 | setDefaultDnsSystemProperties(new ArrayList<InetAddress>()); |
| 5689 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5694 | for (NetworkAgentInfo nai : affectedNetworks) { |
| 5695 | boolean teardown = true; |
| 5696 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 5697 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 5698 | try { |
| 5699 | if (mNetworkRequests.get(nr).isRequest) { |
| 5700 | teardown = false; |
| 5701 | } |
| 5702 | } catch (Exception e) { |
| 5703 | loge("Request " + nr + " not found in mNetworkRequests."); |
| 5704 | loge(" it came from request list of " + nai.name()); |
| 5705 | } |
| 5706 | } |
| 5707 | if (teardown) { |
| 5708 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 5709 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 5710 | } else { |
| 5711 | // not going to linger, so kill the list of linger networks.. only |
| 5712 | // notify them of linger if it happens as the result of gaining another, |
| 5713 | // but if they transition and old network stays up, don't tell them of linger |
| 5714 | // or very delayed loss |
| 5715 | nai.networkLingered.clear(); |
| 5716 | if (VDBG) log("Lingered for " + nai.name() + " cleared"); |
| 5717 | } |
| 5718 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5719 | if (keep) { |
| 5720 | if (isNewDefault) { |
| 5721 | if (VDBG) log("Switching to new default network: " + newNetwork); |
| 5722 | setupDataActivityTracking(newNetwork); |
| 5723 | try { |
| 5724 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 5725 | } catch (Exception e) { |
| 5726 | loge("Exception setting default network :" + e); |
| 5727 | } |
| 5728 | if (newNetwork.equals(mNetworkForRequestId.get(mDefaultRequest.requestId))) { |
| 5729 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
| 5730 | } |
| 5731 | synchronized (ConnectivityService.this) { |
| 5732 | // have a new default network, release the transition wakelock in |
| 5733 | // a second if it's held. The second pause is to allow apps |
| 5734 | // to reconnect over the new network |
| 5735 | if (mNetTransitionWakeLock.isHeld()) { |
| 5736 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 5737 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 5738 | mNetTransitionWakeLockSerialNumber, 0), |
| 5739 | 1000); |
| 5740 | } |
| 5741 | } |
| 5742 | |
| 5743 | // this will cause us to come up initially as unconnected and switching |
| 5744 | // to connected after our normal pause unless somebody reports us as |
| 5745 | // really disconnected |
| 5746 | mDefaultInetConditionPublished = 0; |
| 5747 | mDefaultConnectionSequence++; |
| 5748 | mInetConditionChangeInFlight = false; |
| 5749 | // TODO - read the tcp buffer size config string from somewhere |
| 5750 | // updateNetworkSettings(); |
| 5751 | } |
| 5752 | // notify battery stats service about this network |
| 5753 | // try { |
| 5754 | // TODO |
| 5755 | //BatteryStatsService.getService().noteNetworkInterfaceType(iface, netType); |
| 5756 | // } catch (RemoteException e) { } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5757 | notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE); |
| 5758 | } else { |
| 5759 | if (DBG && newNetwork.networkRequests.size() != 0) { |
| 5760 | loge("tearing down network with live requests:"); |
| 5761 | for (int i=0; i < newNetwork.networkRequests.size(); i++) { |
| 5762 | loge(" " + newNetwork.networkRequests.valueAt(i)); |
| 5763 | } |
| 5764 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5765 | if (VDBG) log("Validated network turns out to be unwanted. Tear it down."); |
| 5766 | newNetwork.asyncChannel.disconnect(); |
| 5767 | } |
| 5768 | } |
| 5769 | |
| 5770 | |
| 5771 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 5772 | NetworkInfo.State state = newInfo.getState(); |
| 5773 | NetworkInfo oldInfo = networkAgent.networkInfo; |
| 5774 | networkAgent.networkInfo = newInfo; |
| 5775 | |
| 5776 | if (oldInfo != null && oldInfo.getState() == state) { |
| 5777 | if (VDBG) log("ignoring duplicate network state non-change"); |
| 5778 | return; |
| 5779 | } |
| 5780 | if (DBG) { |
| 5781 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 5782 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 5783 | " to " + state); |
| 5784 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5785 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5786 | if (state == NetworkInfo.State.CONNECTED) { |
| 5787 | // TODO - check if we want it (optimization) |
| 5788 | try { |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5789 | mNetd.createNetwork(networkAgent.network.netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5790 | } catch (Exception e) { |
| 5791 | loge("Error creating Network " + networkAgent.network.netId); |
| 5792 | } |
| 5793 | updateLinkProperties(networkAgent, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5794 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 5795 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5796 | } else if (state == NetworkInfo.State.DISCONNECTED || |
| 5797 | state == NetworkInfo.State.SUSPENDED) { |
| 5798 | networkAgent.asyncChannel.disconnect(); |
| 5799 | } |
| 5800 | } |
| 5801 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5802 | // notify only this one new request of the current state |
| 5803 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 5804 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 5805 | // TODO - read state from monitor to decide what to send. |
| 5806 | // if (nai.networkMonitor.isLingering()) { |
| 5807 | // notifyType = NetworkCallbacks.LOSING; |
| 5808 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 5809 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 5810 | // } |
| 5811 | if (nri.request.needsBroadcasts) { |
| 5812 | // TODO |
| 5813 | // sendNetworkBroadcast(nai, notifyType); |
| 5814 | } |
| 5815 | callCallbackForRequest(nri, nai, notifyType); |
| 5816 | } |
| 5817 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5818 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
| 5819 | if (VDBG) log("notifyType " + notifyType + " for " + networkAgent.name()); |
| 5820 | boolean needsBroadcasts = false; |
| 5821 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5822 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 5823 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 5824 | if (VDBG) log(" sending notification for " + nr); |
| 5825 | if (nr.needsBroadcasts) needsBroadcasts = true; |
| 5826 | callCallbackForRequest(nri, networkAgent, notifyType); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5827 | } |
| 5828 | if (needsBroadcasts) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5829 | if (notifyType == ConnectivityManager.CALLBACK_AVAILABLE) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5830 | sendConnectedBroadcastDelayed(networkAgent.networkInfo, |
| 5831 | getConnectivityChangeDelay()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5832 | } else if (notifyType == ConnectivityManager.CALLBACK_LOST) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5833 | NetworkInfo info = new NetworkInfo(networkAgent.networkInfo); |
| 5834 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 5835 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 5836 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 5837 | if (info.isFailover()) { |
| 5838 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 5839 | networkAgent.networkInfo.setFailover(false); |
| 5840 | } |
| 5841 | if (info.getReason() != null) { |
| 5842 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 5843 | } |
| 5844 | if (info.getExtraInfo() != null) { |
| 5845 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 5846 | } |
| 5847 | NetworkAgentInfo newDefaultAgent = null; |
| 5848 | if (networkAgent.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 5849 | newDefaultAgent = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 5850 | if (newDefaultAgent != null) { |
| 5851 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 5852 | newDefaultAgent.networkInfo); |
| 5853 | } else { |
| 5854 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 5855 | } |
| 5856 | } |
| 5857 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 5858 | mDefaultInetConditionPublished); |
| 5859 | final Intent immediateIntent = new Intent(intent); |
| 5860 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 5861 | sendStickyBroadcast(immediateIntent); |
| 5862 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
| 5863 | if (newDefaultAgent != null) { |
| 5864 | sendConnectedBroadcastDelayed(newDefaultAgent.networkInfo, |
| 5865 | getConnectivityChangeDelay()); |
| 5866 | } |
| 5867 | } |
| 5868 | } |
| 5869 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5870 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5871 | private LinkProperties getLinkPropertiesForTypeInternal(int networkType) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5872 | ArrayList<NetworkAgentInfo> list = mNetworkAgentInfoForType[networkType]; |
| 5873 | if (list == null) return null; |
| 5874 | try { |
| 5875 | return new LinkProperties(list.get(0).linkProperties); |
| 5876 | } catch (IndexOutOfBoundsException e) { |
| 5877 | return new LinkProperties(); |
| 5878 | } |
| 5879 | } |
| 5880 | |
| 5881 | private NetworkInfo getNetworkInfoForType(int networkType) { |
| 5882 | ArrayList<NetworkAgentInfo> list = mNetworkAgentInfoForType[networkType]; |
| 5883 | if (list == null) return null; |
| 5884 | try { |
| 5885 | return new NetworkInfo(list.get(0).networkInfo); |
| 5886 | } catch (IndexOutOfBoundsException e) { |
| 5887 | return new NetworkInfo(networkType, 0, "Unknown", ""); |
| 5888 | } |
| 5889 | } |
| 5890 | |
| 5891 | private NetworkCapabilities getNetworkCapabilitiesForType(int networkType) { |
| 5892 | ArrayList<NetworkAgentInfo> list = mNetworkAgentInfoForType[networkType]; |
| 5893 | if (list == null) return null; |
| 5894 | try { |
| 5895 | return new NetworkCapabilities(list.get(0).networkCapabilities); |
| 5896 | } catch (IndexOutOfBoundsException e) { |
| 5897 | return new NetworkCapabilities(); |
| 5898 | } |
| 5899 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5900 | } |