The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | 1a30395 | 2011-06-16 13:04:20 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.MANAGE_NETWORK_POLICY; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 20 | import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
| 22 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_BLUETOOTH; |
| 24 | import static android.net.ConnectivityManager.TYPE_DUMMY; |
| 25 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
| 26 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 27 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 28 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 29 | import static android.net.ConnectivityManager.TYPE_PROXY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 30 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 31 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 32 | import static android.net.ConnectivityServiceProtocol.NetworkFactoryProtocol; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 33 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 34 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 35 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 36 | import android.app.AlarmManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 37 | import android.app.AppOpsManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 38 | import android.app.Notification; |
| 39 | import android.app.NotificationManager; |
| 40 | import android.app.PendingIntent; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 41 | import android.bluetooth.BluetoothTetheringDataTracker; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 42 | import android.content.ActivityNotFoundException; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 43 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.content.ContentResolver; |
| 45 | import android.content.Context; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 46 | import android.content.ContextWrapper; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 48 | import android.content.IntentFilter; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 49 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | import android.content.pm.PackageManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 51 | import android.content.pm.PackageManager.NameNotFoundException; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 52 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 53 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 54 | import android.database.ContentObserver; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 55 | import android.net.CaptivePortalTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.net.ConnectivityManager; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 57 | import android.net.DummyDataStateTracker; |
Benoit Goby | 1997069 | 2010-12-22 14:29:40 -0800 | [diff] [blame] | 58 | import android.net.EthernetDataTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 60 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 61 | import android.net.INetworkPolicyListener; |
| 62 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 63 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 64 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 65 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 66 | import android.net.LinkProperties.CompareResult; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 67 | import android.net.LinkQualityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 69 | import android.net.Network; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 70 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 71 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 73 | import android.net.NetworkInfo.DetailedState; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 74 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 75 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 76 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 78 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 79 | import android.net.Proxy; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 80 | import android.net.ProxyDataTracker; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 81 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 82 | import android.net.RouteInfo; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 83 | import android.net.SamplingDataTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 84 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | import android.net.wifi.WifiStateTracker; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 86 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 87 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.os.Binder; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 89 | import android.os.Build; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 90 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 92 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 93 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 94 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.os.Looper; |
| 96 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 97 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 98 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 99 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 100 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 101 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 103 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 105 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 107 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 108 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 109 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 110 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 111 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 112 | import android.util.SparseArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 113 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 114 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 116 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 117 | import com.android.internal.annotations.GuardedBy; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 118 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 119 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 120 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 121 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 122 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 123 | import com.android.internal.telephony.PhoneConstants; |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 124 | import com.android.internal.telephony.TelephonyIntents; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 125 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 126 | import com.android.internal.util.IndentingPrintWriter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 127 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 128 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 129 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 130 | import com.android.server.connectivity.Nat464Xlat; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 131 | import com.android.server.connectivity.PacManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 132 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 133 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 134 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 135 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 136 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 137 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 138 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 139 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 140 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 141 | import org.xmlpull.v1.XmlPullParser; |
| 142 | import org.xmlpull.v1.XmlPullParserException; |
| 143 | |
| 144 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 146 | import java.io.FileNotFoundException; |
| 147 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 148 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 150 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 151 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 152 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 153 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 154 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 155 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 156 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 157 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 158 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 159 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 160 | import java.util.GregorianCalendar; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 161 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 162 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 163 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 164 | import java.util.Map; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 165 | import java.util.Random; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 166 | import java.util.concurrent.atomic.AtomicBoolean; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 167 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 169 | import javax.net.ssl.HostnameVerifier; |
| 170 | import javax.net.ssl.HttpsURLConnection; |
| 171 | import javax.net.ssl.SSLSession; |
| 172 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 173 | import static android.net.ConnectivityManager.INVALID_NET_ID; |
| 174 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | /** |
| 176 | * @hide |
| 177 | */ |
| 178 | public class ConnectivityService extends IConnectivityManager.Stub { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 179 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | |
Robert Greenwalt | ba175a5 | 2010-10-05 19:12:26 -0700 | [diff] [blame] | 181 | private static final boolean DBG = true; |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 182 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 184 | private static final boolean LOGD_RULES = false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 185 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 186 | // TODO: create better separation between radio types and network types |
| 187 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 188 | // how long to wait before switching back to a radio's default network |
| 189 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 190 | // system property that can override the above value |
| 191 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 192 | "android.telephony.apn-restore"; |
| 193 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 194 | // Default value if FAIL_FAST_TIME_MS is not set |
| 195 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 196 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 197 | private static final String FAIL_FAST_TIME_MS = |
| 198 | "persist.radio.fail_fast_time_ms"; |
| 199 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 200 | private static final String ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED = |
| 201 | "android.net.ConnectivityService.action.PKT_CNT_SAMPLE_INTERVAL_ELAPSED"; |
| 202 | |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 203 | private static final int SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE = 0; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 204 | |
| 205 | private PendingIntent mSampleIntervalElapsedIntent; |
| 206 | |
| 207 | // Set network sampling interval at 12 minutes, this way, even if the timers get |
| 208 | // aggregated, it will fire at around 15 minutes, which should allow us to |
| 209 | // aggregate this timer with other timers (specially the socket keep alive timers) |
| 210 | private static final int DEFAULT_SAMPLING_INTERVAL_IN_SECONDS = (VDBG ? 30 : 12 * 60); |
| 211 | |
| 212 | // start network sampling a minute after booting ... |
| 213 | private static final int DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS = (VDBG ? 30 : 60); |
| 214 | |
| 215 | AlarmManager mAlarmManager; |
| 216 | |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 217 | // used in recursive route setting to add gateways for the host for which |
| 218 | // a host route was requested. |
| 219 | private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10; |
| 220 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 221 | private Tethering mTethering; |
| 222 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 223 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 224 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 225 | @GuardedBy("mVpns") |
| 226 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 227 | private VpnCallback mVpnCallback = new VpnCallback(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 228 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 229 | private boolean mLockdownEnabled; |
| 230 | private LockdownVpnTracker mLockdownTracker; |
| 231 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 232 | private Nat464Xlat mClat; |
| 233 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 234 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 235 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 236 | /** Currently active network rules by UID. */ |
| 237 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 238 | /** Set of ifaces that are costly. */ |
| 239 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 240 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | /** |
| 242 | * Sometimes we want to refer to the individual network state |
| 243 | * trackers separately, and sometimes we just want to treat them |
| 244 | * abstractly. |
| 245 | */ |
| 246 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 247 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 248 | /* Handles captive portal check on a network */ |
| 249 | private CaptivePortalTracker mCaptivePortalTracker; |
| 250 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 251 | /** |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 252 | * The link properties that define the current links |
| 253 | */ |
| 254 | private LinkProperties mCurrentLinkProperties[]; |
| 255 | |
| 256 | /** |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 257 | * A per Net list of the PID's that requested access to the net |
| 258 | * used both as a refcount and for per-PID DNS selection |
| 259 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 260 | private List<Integer> mNetRequestersPids[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 261 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 262 | // priority order of the nettrackers |
| 263 | // (excluding dynamically set mNetworkPreference) |
| 264 | // TODO - move mNetworkTypePreference into this |
| 265 | private int[] mPriorityList; |
| 266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 267 | private Context mContext; |
| 268 | private int mNetworkPreference; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 269 | private int mActiveDefaultNetwork = -1; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 270 | // 0 is full bad, 100 is full good |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 271 | private int mDefaultInetCondition = 0; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 272 | private int mDefaultInetConditionPublished = 0; |
| 273 | private boolean mInetConditionChangeInFlight = false; |
| 274 | private int mDefaultConnectionSequence = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 276 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 277 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | |
| 279 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 280 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 282 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 283 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 284 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 285 | private static final int ENABLED = 1; |
| 286 | private static final int DISABLED = 0; |
| 287 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 288 | private static final boolean ADD = true; |
| 289 | private static final boolean REMOVE = false; |
| 290 | |
| 291 | private static final boolean TO_DEFAULT_TABLE = true; |
| 292 | private static final boolean TO_SECONDARY_TABLE = false; |
| 293 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 294 | private static final boolean EXEMPT = true; |
| 295 | private static final boolean UNEXEMPT = false; |
| 296 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 297 | /** |
| 298 | * used internally as a delayed event to make us switch back to the |
| 299 | * default network |
| 300 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 301 | private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * used internally to change our mobile data enabled flag |
| 305 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 306 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 307 | |
| 308 | /** |
| 309 | * used internally to change our network preference setting |
| 310 | * arg1 = networkType to prefer |
| 311 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 312 | private static final int EVENT_SET_NETWORK_PREFERENCE = 3; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * used internally to synchronize inet condition reports |
| 316 | * arg1 = networkType |
| 317 | * arg2 = condition (0 bad, 100 good) |
| 318 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 319 | private static final int EVENT_INET_CONDITION_CHANGE = 4; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 320 | |
| 321 | /** |
| 322 | * used internally to mark the end of inet condition hold periods |
| 323 | * arg1 = networkType |
| 324 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 325 | private static final int EVENT_INET_CONDITION_HOLD_END = 5; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 326 | |
| 327 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 328 | * used internally to set enable/disable cellular data |
| 329 | * arg1 = ENBALED or DISABLED |
| 330 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 331 | private static final int EVENT_SET_MOBILE_DATA = 7; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 332 | |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 333 | /** |
| 334 | * used internally to clear a wakelock when transitioning |
| 335 | * from one net to another |
| 336 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 337 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 338 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 339 | /** |
| 340 | * used internally to reload global proxy settings |
| 341 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 342 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 343 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 344 | /** |
| 345 | * used internally to set external dependency met/unmet |
| 346 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 347 | * arg2 = NetworkType |
| 348 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 349 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 350 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 351 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 352 | * used internally to send a sticky broadcast delayed. |
| 353 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 354 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 11; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 355 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 356 | /** |
| 357 | * Used internally to |
| 358 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 359 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 360 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 12; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 361 | |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 362 | private static final int EVENT_VPN_STATE_CHANGED = 13; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 363 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 364 | /** |
| 365 | * Used internally to disable fail fast of mobile data |
| 366 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 367 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 14; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 368 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 369 | /** |
| 370 | * user internally to indicate that data sampling interval is up |
| 371 | */ |
| 372 | private static final int EVENT_SAMPLE_INTERVAL_ELAPSED = 15; |
| 373 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 374 | /** |
| 375 | * PAC manager has received new port. |
| 376 | */ |
| 377 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 378 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 379 | /** |
| 380 | * used internally when registering NetworkFactories |
| 381 | * obj = Messenger |
| 382 | */ |
| 383 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 384 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 385 | /** Handler used for internal events. */ |
| 386 | private InternalHandler mHandler; |
| 387 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
| 388 | private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 389 | |
| 390 | // list of DeathRecipients used to make sure features are turned off when |
| 391 | // a process dies |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 392 | private List<FeatureUser> mFeatureUsers; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 393 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 394 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 395 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 396 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 397 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 398 | private String mNetTransitionWakeLockCausedBy = ""; |
| 399 | private int mNetTransitionWakeLockSerialNumber; |
| 400 | private int mNetTransitionWakeLockTimeout; |
| 401 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 402 | private InetAddress mDefaultDns; |
| 403 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 404 | // Lock for protecting access to mAddedRoutes and mExemptAddresses |
| 405 | private final Object mRoutesLock = new Object(); |
| 406 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 407 | // this collection is used to refcount the added routes - if there are none left |
| 408 | // it's time to remove the route from the route table |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 409 | @GuardedBy("mRoutesLock") |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 410 | private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>(); |
| 411 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 412 | // this collection corresponds to the entries of mAddedRoutes that have routing exemptions |
| 413 | // used to handle cleanup of exempt rules |
| 414 | @GuardedBy("mRoutesLock") |
| 415 | private Collection<LinkAddress> mExemptAddresses = new ArrayList<LinkAddress>(); |
| 416 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 417 | // used in DBG mode to track inet condition reports |
| 418 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 419 | private ArrayList mInetLog; |
| 420 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 421 | // 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] | 422 | private ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 423 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 424 | private boolean mDefaultProxyDisabled = false; |
| 425 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 426 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 427 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 428 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 429 | private PacManager mPacManager = null; |
| 430 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 431 | private SettingsObserver mSettingsObserver; |
| 432 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 433 | private AppOpsManager mAppOpsManager; |
| 434 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 435 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 436 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 437 | |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 438 | private static class RadioAttributes { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 439 | public int mSimultaneity; |
| 440 | public int mType; |
| 441 | public RadioAttributes(String init) { |
| 442 | String fragments[] = init.split(","); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 443 | mType = Integer.parseInt(fragments[0]); |
| 444 | mSimultaneity = Integer.parseInt(fragments[1]); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | RadioAttributes[] mRadioAttributes; |
| 448 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 449 | // the set of network types that can only be enabled by system/sig apps |
| 450 | List mProtectedNetworks; |
| 451 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 452 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 453 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 454 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 455 | |
| 456 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 457 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 458 | private final static int MIN_NET_ID = 10; // some reserved marks |
| 459 | private final static int MAX_NET_ID = 65535; |
| 460 | private int mNextNetId = MIN_NET_ID; |
| 461 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 462 | public ConnectivityService(Context context, INetworkManagementService netd, |
| 463 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 464 | // Currently, omitting a NetworkFactory will create one internally |
| 465 | // TODO: create here when we have cleaner WiMAX support |
| 466 | this(context, netd, statsService, policyManager, null); |
| 467 | } |
| 468 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 469 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 470 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 471 | NetworkFactory netFactory) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 472 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 473 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 474 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 475 | netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 476 | netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
| 477 | NetworkRequest netRequest = new NetworkRequest(netCap); |
| 478 | mNetworkRequests.append(netRequest.requestId, netRequest); |
| 479 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 480 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 481 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 482 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 483 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 484 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 485 | if (netFactory == null) { |
| 486 | netFactory = new DefaultNetworkFactory(context, mTrackerHandler); |
| 487 | } |
| 488 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 489 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 490 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 491 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 492 | Settings.Secure.ANDROID_ID); |
| 493 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 494 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 495 | SystemProperties.set("net.hostname", name); |
| 496 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 497 | } |
| 498 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 499 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 500 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 501 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 502 | if (dns == null || dns.length() == 0) { |
| 503 | dns = context.getResources().getString( |
| 504 | com.android.internal.R.string.config_default_dns_server); |
| 505 | } |
| 506 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 507 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 508 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 509 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 512 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 513 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 514 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 515 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 516 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 517 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 518 | try { |
| 519 | mPolicyManager.registerListener(mPolicyListener); |
| 520 | } catch (RemoteException e) { |
| 521 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 522 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 526 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 527 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 528 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 529 | com.android.internal.R.integer.config_networkTransitionTimeout); |
| 530 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 531 | mNetTrackers = new NetworkStateTracker[ |
| 532 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 533 | mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 534 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 535 | mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1]; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 536 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 537 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 538 | // Load device network attributes from resources |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 539 | String[] raStrings = context.getResources().getStringArray( |
| 540 | com.android.internal.R.array.radioAttributes); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 541 | for (String raString : raStrings) { |
| 542 | RadioAttributes r = new RadioAttributes(raString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 543 | if (VDBG) log("raString=" + raString + " r=" + r); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 544 | if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 545 | loge("Error in radioAttributes - ignoring attempt to define type " + r.mType); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 546 | continue; |
| 547 | } |
| 548 | if (mRadioAttributes[r.mType] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 549 | loge("Error in radioAttributes - ignoring attempt to redefine type " + |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 550 | r.mType); |
| 551 | continue; |
| 552 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 553 | mRadioAttributes[r.mType] = r; |
| 554 | } |
| 555 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 556 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 557 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 558 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 559 | String[] naStrings = context.getResources().getStringArray( |
| 560 | com.android.internal.R.array.networkAttributes); |
| 561 | for (String naString : naStrings) { |
| 562 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 563 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 564 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 565 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 566 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 567 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 568 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 569 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 570 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 571 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 572 | n.type); |
| 573 | continue; |
| 574 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 575 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 576 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 577 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 578 | continue; |
| 579 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 580 | if (mRadioAttributes[n.radio] == null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 581 | loge("Error in networkAttributes - ignoring attempt to use undefined " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 582 | "radio " + n.radio + " in network type " + n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 583 | continue; |
| 584 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 585 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 586 | mNetworksDefined++; |
| 587 | } catch(Exception e) { |
| 588 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 591 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 592 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 593 | mProtectedNetworks = new ArrayList<Integer>(); |
| 594 | int[] protectedNetworks = context.getResources().getIntArray( |
| 595 | com.android.internal.R.array.config_protectedNetworks); |
| 596 | for (int p : protectedNetworks) { |
| 597 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 598 | mProtectedNetworks.add(p); |
| 599 | } else { |
| 600 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 601 | } |
| 602 | } |
| 603 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 604 | // high priority first |
| 605 | mPriorityList = new int[mNetworksDefined]; |
| 606 | { |
| 607 | int insertionPoint = mNetworksDefined-1; |
| 608 | int currentLowest = 0; |
| 609 | int nextLowest = 0; |
| 610 | while (insertionPoint > -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 611 | for (NetworkConfig na : mNetConfigs) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 612 | if (na == null) continue; |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 613 | if (na.priority < currentLowest) continue; |
| 614 | if (na.priority > currentLowest) { |
| 615 | if (na.priority < nextLowest || nextLowest == 0) { |
| 616 | nextLowest = na.priority; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 617 | } |
| 618 | continue; |
| 619 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 620 | mPriorityList[insertionPoint--] = na.type; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 621 | } |
| 622 | currentLowest = nextLowest; |
| 623 | nextLowest = 0; |
| 624 | } |
| 625 | } |
| 626 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 627 | // Update mNetworkPreference according to user mannually first then overlay config.xml |
| 628 | mNetworkPreference = getPersistedNetworkPreference(); |
| 629 | if (mNetworkPreference == -1) { |
| 630 | for (int n : mPriorityList) { |
| 631 | if (mNetConfigs[n].isDefault() && ConnectivityManager.isNetworkTypeValid(n)) { |
| 632 | mNetworkPreference = n; |
| 633 | break; |
| 634 | } |
| 635 | } |
| 636 | if (mNetworkPreference == -1) { |
| 637 | throw new IllegalStateException( |
| 638 | "You should set at least one default Network in config.xml!"); |
| 639 | } |
| 640 | } |
| 641 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 642 | mNetRequestersPids = |
| 643 | (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 644 | for (int i : mPriorityList) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 645 | mNetRequestersPids[i] = new ArrayList<Integer>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 648 | mFeatureUsers = new ArrayList<FeatureUser>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 649 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 650 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 651 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 652 | |
| 653 | // Create and start trackers for hard-coded networks |
| 654 | for (int targetNetworkType : mPriorityList) { |
| 655 | final NetworkConfig config = mNetConfigs[targetNetworkType]; |
| 656 | final NetworkStateTracker tracker; |
| 657 | try { |
| 658 | tracker = netFactory.createTracker(targetNetworkType, config); |
| 659 | mNetTrackers[targetNetworkType] = tracker; |
| 660 | } catch (IllegalArgumentException e) { |
| 661 | Slog.e(TAG, "Problem creating " + getNetworkTypeName(targetNetworkType) |
| 662 | + " tracker: " + e); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 663 | continue; |
| 664 | } |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 665 | |
| 666 | tracker.startMonitoring(context, mTrackerHandler); |
| 667 | if (config.isDefault()) { |
| 668 | tracker.reconnect(); |
Robert Greenwalt | 6537b02 | 2011-11-10 16:55:20 -0800 | [diff] [blame] | 669 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 670 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 671 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 672 | mTethering = new Tethering(mContext, mNetd, statsService, this, mHandler.getLooper()); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 673 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 674 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 675 | IntentFilter intentFilter = new IntentFilter(); |
| 676 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 677 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 678 | mContext.registerReceiverAsUser( |
| 679 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 680 | mClat = new Nat464Xlat(mContext, mNetd, this, mTrackerHandler); |
| 681 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 682 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 683 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 684 | mNetd.registerObserver(mDataActivityObserver); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 685 | mNetd.registerObserver(mClat); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 686 | } catch (RemoteException e) { |
| 687 | loge("Error registering observer :" + e); |
| 688 | } |
| 689 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 690 | if (DBG) { |
| 691 | mInetLog = new ArrayList(); |
| 692 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 693 | |
| 694 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 695 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 696 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 697 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 698 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 699 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 700 | // start network sampling .. |
| 701 | Intent intent = new Intent(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED, null); |
| 702 | mSampleIntervalElapsedIntent = PendingIntent.getBroadcast(mContext, |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 703 | SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE, intent, 0); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 704 | |
| 705 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 706 | setAlarm(DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS * 1000, mSampleIntervalElapsedIntent); |
| 707 | |
| 708 | IntentFilter filter = new IntentFilter(); |
| 709 | filter.addAction(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 710 | mContext.registerReceiver( |
| 711 | new BroadcastReceiver() { |
| 712 | @Override |
| 713 | public void onReceive(Context context, Intent intent) { |
| 714 | String action = intent.getAction(); |
| 715 | if (action.equals(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED)) { |
| 716 | mHandler.sendMessage(mHandler.obtainMessage |
| 717 | (EVENT_SAMPLE_INTERVAL_ELAPSED)); |
| 718 | } |
| 719 | } |
| 720 | }, |
| 721 | new IntentFilter(filter)); |
| 722 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 723 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 724 | |
| 725 | filter = new IntentFilter(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 726 | filter.addAction(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 727 | mContext.registerReceiver(mProvisioningReceiver, filter); |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 728 | |
| 729 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 731 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 732 | private synchronized int nextNetId() { |
| 733 | int netId = mNextNetId; |
| 734 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 735 | return netId; |
| 736 | } |
| 737 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 738 | /** |
| 739 | * Factory that creates {@link NetworkStateTracker} instances using given |
| 740 | * {@link NetworkConfig}. |
| 741 | */ |
| 742 | public interface NetworkFactory { |
| 743 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config); |
| 744 | } |
| 745 | |
| 746 | private static class DefaultNetworkFactory implements NetworkFactory { |
| 747 | private final Context mContext; |
| 748 | private final Handler mTrackerHandler; |
| 749 | |
| 750 | public DefaultNetworkFactory(Context context, Handler trackerHandler) { |
| 751 | mContext = context; |
| 752 | mTrackerHandler = trackerHandler; |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config) { |
| 757 | switch (config.radio) { |
| 758 | case TYPE_WIFI: |
| 759 | return new WifiStateTracker(targetNetworkType, config.name); |
| 760 | case TYPE_MOBILE: |
| 761 | return new MobileDataStateTracker(targetNetworkType, config.name); |
| 762 | case TYPE_DUMMY: |
| 763 | return new DummyDataStateTracker(targetNetworkType, config.name); |
| 764 | case TYPE_BLUETOOTH: |
| 765 | return BluetoothTetheringDataTracker.getInstance(); |
| 766 | case TYPE_WIMAX: |
| 767 | return makeWimaxStateTracker(mContext, mTrackerHandler); |
| 768 | case TYPE_ETHERNET: |
| 769 | return EthernetDataTracker.getInstance(); |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 770 | case TYPE_PROXY: |
| 771 | return new ProxyDataTracker(); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 772 | default: |
| 773 | throw new IllegalArgumentException( |
| 774 | "Trying to create a NetworkStateTracker for an unknown radio type: " |
| 775 | + config.radio); |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Loads external WiMAX library and registers as system service, returning a |
| 782 | * {@link NetworkStateTracker} for WiMAX. Caller is still responsible for |
| 783 | * invoking {@link NetworkStateTracker#startMonitoring(Context, Handler)}. |
| 784 | */ |
| 785 | private static NetworkStateTracker makeWimaxStateTracker( |
| 786 | Context context, Handler trackerHandler) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 787 | // Initialize Wimax |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 788 | DexClassLoader wimaxClassLoader; |
| 789 | Class wimaxStateTrackerClass = null; |
| 790 | Class wimaxServiceClass = null; |
| 791 | Class wimaxManagerClass; |
| 792 | String wimaxJarLocation; |
| 793 | String wimaxLibLocation; |
| 794 | String wimaxManagerClassName; |
| 795 | String wimaxServiceClassName; |
| 796 | String wimaxStateTrackerClassName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 798 | NetworkStateTracker wimaxStateTracker = null; |
| 799 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 800 | boolean isWimaxEnabled = context.getResources().getBoolean( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 801 | com.android.internal.R.bool.config_wimaxEnabled); |
| 802 | |
| 803 | if (isWimaxEnabled) { |
| 804 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 805 | wimaxJarLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 806 | com.android.internal.R.string.config_wimaxServiceJarLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 807 | wimaxLibLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 808 | com.android.internal.R.string.config_wimaxNativeLibLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 809 | wimaxManagerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 810 | com.android.internal.R.string.config_wimaxManagerClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 811 | wimaxServiceClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 812 | com.android.internal.R.string.config_wimaxServiceClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 813 | wimaxStateTrackerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 814 | com.android.internal.R.string.config_wimaxStateTrackerClassname); |
| 815 | |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 816 | if (DBG) log("wimaxJarLocation: " + wimaxJarLocation); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 817 | wimaxClassLoader = new DexClassLoader(wimaxJarLocation, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 818 | new ContextWrapper(context).getCacheDir().getAbsolutePath(), |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 819 | wimaxLibLocation, ClassLoader.getSystemClassLoader()); |
| 820 | |
| 821 | try { |
| 822 | wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName); |
| 823 | wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName); |
| 824 | wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName); |
| 825 | } catch (ClassNotFoundException ex) { |
| 826 | loge("Exception finding Wimax classes: " + ex.toString()); |
| 827 | return null; |
| 828 | } |
| 829 | } catch(Resources.NotFoundException ex) { |
| 830 | loge("Wimax Resources does not exist!!! "); |
| 831 | return null; |
| 832 | } |
| 833 | |
| 834 | try { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 835 | if (DBG) log("Starting Wimax Service... "); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 836 | |
| 837 | Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor |
| 838 | (new Class[] {Context.class, Handler.class}); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 839 | wimaxStateTracker = (NetworkStateTracker) wmxStTrkrConst.newInstance( |
| 840 | context, trackerHandler); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 841 | |
| 842 | Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor |
| 843 | (new Class[] {Context.class, wimaxStateTrackerClass}); |
| 844 | wmxSrvConst.setAccessible(true); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 845 | IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(context, wimaxStateTracker); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 846 | wmxSrvConst.setAccessible(false); |
| 847 | |
| 848 | ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker); |
| 849 | |
| 850 | } catch(Exception ex) { |
| 851 | loge("Exception creating Wimax classes: " + ex.toString()); |
| 852 | return null; |
| 853 | } |
| 854 | } else { |
| 855 | loge("Wimax is not enabled or not added to the network attributes!!! "); |
| 856 | return null; |
| 857 | } |
| 858 | |
| 859 | return wimaxStateTracker; |
| 860 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 861 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 862 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 863 | * Sets the preferred network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | * @param preference the new preference |
| 865 | */ |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 866 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | enforceChangePermission(); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 868 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 869 | mHandler.sendMessage( |
| 870 | mHandler.obtainMessage(EVENT_SET_NETWORK_PREFERENCE, preference, 0)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | public int getNetworkPreference() { |
| 874 | enforceAccessPermission(); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 875 | int preference; |
| 876 | synchronized(this) { |
| 877 | preference = mNetworkPreference; |
| 878 | } |
| 879 | return preference; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 880 | } |
| 881 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 882 | private void handleSetNetworkPreference(int preference) { |
| 883 | if (ConnectivityManager.isNetworkTypeValid(preference) && |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 884 | mNetConfigs[preference] != null && |
| 885 | mNetConfigs[preference].isDefault()) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 886 | if (mNetworkPreference != preference) { |
| 887 | final ContentResolver cr = mContext.getContentResolver(); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 888 | Settings.Global.putInt(cr, Settings.Global.NETWORK_PREFERENCE, preference); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 889 | synchronized(this) { |
| 890 | mNetworkPreference = preference; |
| 891 | } |
| 892 | enforcePreference(); |
| 893 | } |
| 894 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 896 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 897 | private int getConnectivityChangeDelay() { |
| 898 | final ContentResolver cr = mContext.getContentResolver(); |
| 899 | |
| 900 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 901 | int defaultDelay = SystemProperties.getInt( |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 902 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 903 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 904 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 905 | defaultDelay); |
| 906 | } |
| 907 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 908 | private int getPersistedNetworkPreference() { |
| 909 | final ContentResolver cr = mContext.getContentResolver(); |
| 910 | |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 911 | final int networkPrefSetting = Settings.Global |
| 912 | .getInt(cr, Settings.Global.NETWORK_PREFERENCE, -1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 914 | return networkPrefSetting; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | } |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 916 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 918 | * Make the state of network connectivity conform to the preference settings |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | * In this method, we only tear down a non-preferred network. Establishing |
| 920 | * a connection to the preferred network is taken care of when we handle |
| 921 | * the disconnect event from the non-preferred network |
| 922 | * (see {@link #handleDisconnect(NetworkInfo)}). |
| 923 | */ |
| 924 | private void enforcePreference() { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 925 | if (mNetTrackers[mNetworkPreference].getNetworkInfo().isConnected()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | return; |
| 927 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 928 | if (!mNetTrackers[mNetworkPreference].isAvailable()) |
| 929 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 931 | for (int t=0; t <= ConnectivityManager.MAX_RADIO_TYPE; t++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 932 | if (t != mNetworkPreference && mNetTrackers[t] != null && |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 933 | mNetTrackers[t].getNetworkInfo().isConnected()) { |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 934 | if (DBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 935 | log("tearing down " + mNetTrackers[t].getNetworkInfo() + |
Robert Greenwalt | ec9fe46 | 2009-08-20 15:25:14 -0700 | [diff] [blame] | 936 | " in enforcePreference"); |
| 937 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 938 | teardown(mNetTrackers[t]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | private boolean teardown(NetworkStateTracker netTracker) { |
| 944 | if (netTracker.teardown()) { |
| 945 | netTracker.setTeardownRequested(true); |
| 946 | return true; |
| 947 | } else { |
| 948 | return false; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 953 | * Check if UID should be blocked from using the network represented by the |
| 954 | * given {@link NetworkStateTracker}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 955 | */ |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 956 | private boolean isNetworkBlocked(NetworkStateTracker tracker, int uid) { |
| 957 | final String iface = tracker.getLinkProperties().getInterfaceName(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 958 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 959 | final boolean networkCostly; |
| 960 | final int uidRules; |
| 961 | synchronized (mRulesLock) { |
| 962 | networkCostly = mMeteredIfaces.contains(iface); |
| 963 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 964 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 965 | |
| 966 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 967 | return true; |
| 968 | } |
| 969 | |
| 970 | // no restrictive rules; network is visible |
| 971 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 975 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 976 | * {@link DetailedState#BLOCKED} based on |
| 977 | * {@link #isNetworkBlocked(NetworkStateTracker, int)}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 978 | */ |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 979 | private NetworkInfo getFilteredNetworkInfo(NetworkStateTracker tracker, int uid) { |
| 980 | NetworkInfo info = tracker.getNetworkInfo(); |
| 981 | if (isNetworkBlocked(tracker, uid)) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 982 | // network is blocked; clone and override state |
| 983 | info = new NetworkInfo(info); |
| 984 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 985 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 986 | if (mLockdownTracker != null) { |
| 987 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 988 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 989 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 994 | * It is assumed that at most one network is active at a time. If more |
| 995 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 996 | * @return the info for the active network, or {@code null} if none is |
| 997 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 998 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 999 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1001 | enforceAccessPermission(); |
| 1002 | final int uid = Binder.getCallingUid(); |
| 1003 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1006 | /** |
| 1007 | * Find the first Provisioning network. |
| 1008 | * |
| 1009 | * @return NetworkInfo or null if none. |
| 1010 | */ |
| 1011 | private NetworkInfo getProvisioningNetworkInfo() { |
| 1012 | enforceAccessPermission(); |
| 1013 | |
| 1014 | // Find the first Provisioning Network |
| 1015 | NetworkInfo provNi = null; |
| 1016 | for (NetworkInfo ni : getAllNetworkInfo()) { |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 1017 | if (ni.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1018 | provNi = ni; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | if (DBG) log("getProvisioningNetworkInfo: X provNi=" + provNi); |
| 1023 | return provNi; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Find the first Provisioning network or the ActiveDefaultNetwork |
| 1028 | * if there is no Provisioning network |
| 1029 | * |
| 1030 | * @return NetworkInfo or null if none. |
| 1031 | */ |
| 1032 | @Override |
| 1033 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 1034 | enforceAccessPermission(); |
| 1035 | |
| 1036 | NetworkInfo provNi = getProvisioningNetworkInfo(); |
| 1037 | if (provNi == null) { |
| 1038 | final int uid = Binder.getCallingUid(); |
| 1039 | provNi = getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1040 | } |
| 1041 | if (DBG) log("getProvisioningOrActiveNetworkInfo: X provNi=" + provNi); |
| 1042 | return provNi; |
| 1043 | } |
| 1044 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1045 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1046 | enforceAccessPermission(); |
| 1047 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
| 1048 | final NetworkStateTracker tracker = mNetTrackers[mActiveDefaultNetwork]; |
| 1049 | if (tracker != null) { |
| 1050 | return tracker.getNetworkInfo(); |
| 1051 | } |
| 1052 | } |
| 1053 | return null; |
| 1054 | } |
| 1055 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1056 | @Override |
| 1057 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1058 | enforceConnectivityInternalPermission(); |
| 1059 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1060 | } |
| 1061 | |
| 1062 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1064 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1065 | final int uid = Binder.getCallingUid(); |
| 1066 | return getNetworkInfo(networkType, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | } |
| 1068 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1069 | private NetworkInfo getNetworkInfo(int networkType, int uid) { |
| 1070 | NetworkInfo info = null; |
| 1071 | if (isNetworkTypeValid(networkType)) { |
| 1072 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1073 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1074 | info = getFilteredNetworkInfo(tracker, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | return info; |
| 1078 | } |
| 1079 | |
| 1080 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | public NetworkInfo[] getAllNetworkInfo() { |
| 1082 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1083 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1084 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1085 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1086 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 1087 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1088 | result.add(getFilteredNetworkInfo(tracker, uid)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1089 | } |
| 1090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1092 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1095 | @Override |
| 1096 | public boolean isNetworkSupported(int networkType) { |
| 1097 | enforceAccessPermission(); |
| 1098 | return (isNetworkTypeValid(networkType) && (mNetTrackers[networkType] != null)); |
| 1099 | } |
| 1100 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1101 | /** |
| 1102 | * Return LinkProperties for the active (i.e., connected) default |
| 1103 | * network interface. It is assumed that at most one default network |
| 1104 | * is active at a time. If more than one is active, it is indeterminate |
| 1105 | * which will be returned. |
| 1106 | * @return the ip properties for the active network, or {@code null} if |
| 1107 | * none is active |
| 1108 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1109 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1110 | public LinkProperties getActiveLinkProperties() { |
Robert Greenwalt | 5991158 | 2011-05-20 12:23:41 -0700 | [diff] [blame] | 1111 | return getLinkProperties(mActiveDefaultNetwork); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1114 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1115 | public LinkProperties getLinkProperties(int networkType) { |
| 1116 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1117 | if (isNetworkTypeValid(networkType)) { |
| 1118 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1119 | if (tracker != null) { |
| 1120 | return tracker.getLinkProperties(); |
| 1121 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1122 | } |
| 1123 | return null; |
| 1124 | } |
| 1125 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1126 | @Override |
| 1127 | public NetworkState[] getAllNetworkState() { |
| 1128 | enforceAccessPermission(); |
| 1129 | final int uid = Binder.getCallingUid(); |
| 1130 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1131 | synchronized (mRulesLock) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1132 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 1133 | if (tracker != null) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1134 | final NetworkInfo info = getFilteredNetworkInfo(tracker, uid); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1135 | result.add(new NetworkState( |
Robert Greenwalt | f9cb86a | 2014-04-08 17:34:00 -0700 | [diff] [blame] | 1136 | info, tracker.getLinkProperties(), tracker.getNetworkCapabilities())); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | return result.toArray(new NetworkState[result.size()]); |
| 1141 | } |
| 1142 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1143 | private NetworkState getNetworkStateUnchecked(int networkType) { |
| 1144 | if (isNetworkTypeValid(networkType)) { |
| 1145 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1146 | if (tracker != null) { |
| 1147 | return new NetworkState(tracker.getNetworkInfo(), tracker.getLinkProperties(), |
Robert Greenwalt | f9cb86a | 2014-04-08 17:34:00 -0700 | [diff] [blame] | 1148 | tracker.getNetworkCapabilities()); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | return null; |
| 1152 | } |
| 1153 | |
| 1154 | @Override |
| 1155 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1156 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1157 | |
| 1158 | final long token = Binder.clearCallingIdentity(); |
| 1159 | try { |
| 1160 | final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork); |
| 1161 | if (state != null) { |
| 1162 | try { |
| 1163 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1164 | } catch (RemoteException e) { |
| 1165 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1166 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1167 | return null; |
| 1168 | } finally { |
| 1169 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1170 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1173 | @Override |
| 1174 | public boolean isActiveNetworkMetered() { |
| 1175 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1176 | final long token = Binder.clearCallingIdentity(); |
| 1177 | try { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1178 | return isNetworkMeteredUnchecked(mActiveDefaultNetwork); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1179 | } finally { |
| 1180 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1181 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1184 | private boolean isNetworkMeteredUnchecked(int networkType) { |
| 1185 | final NetworkState state = getNetworkStateUnchecked(networkType); |
| 1186 | if (state != null) { |
| 1187 | try { |
| 1188 | return mPolicyManager.isNetworkMetered(state); |
| 1189 | } catch (RemoteException e) { |
| 1190 | } |
| 1191 | } |
| 1192 | return false; |
| 1193 | } |
| 1194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | public boolean setRadios(boolean turnOn) { |
| 1196 | boolean result = true; |
| 1197 | enforceChangePermission(); |
| 1198 | for (NetworkStateTracker t : mNetTrackers) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1199 | if (t != null) result = t.setRadio(turnOn) && result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | } |
| 1201 | return result; |
| 1202 | } |
| 1203 | |
| 1204 | public boolean setRadio(int netType, boolean turnOn) { |
| 1205 | enforceChangePermission(); |
| 1206 | if (!ConnectivityManager.isNetworkTypeValid(netType)) { |
| 1207 | return false; |
| 1208 | } |
| 1209 | NetworkStateTracker tracker = mNetTrackers[netType]; |
| 1210 | return tracker != null && tracker.setRadio(turnOn); |
| 1211 | } |
| 1212 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1213 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1214 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1215 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1216 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1217 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1218 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1219 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1220 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1221 | /** |
| 1222 | * Used to notice when the calling process dies so we can self-expire |
| 1223 | * |
| 1224 | * Also used to know if the process has cleaned up after itself when |
| 1225 | * our auto-expire timer goes off. The timer has a link to an object. |
| 1226 | * |
| 1227 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1228 | private class FeatureUser implements IBinder.DeathRecipient { |
| 1229 | int mNetworkType; |
| 1230 | String mFeature; |
| 1231 | IBinder mBinder; |
| 1232 | int mPid; |
| 1233 | int mUid; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1234 | long mCreateTime; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1235 | |
| 1236 | FeatureUser(int type, String feature, IBinder binder) { |
| 1237 | super(); |
| 1238 | mNetworkType = type; |
| 1239 | mFeature = feature; |
| 1240 | mBinder = binder; |
| 1241 | mPid = getCallingPid(); |
| 1242 | mUid = getCallingUid(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1243 | mCreateTime = System.currentTimeMillis(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1244 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1245 | try { |
| 1246 | mBinder.linkToDeath(this, 0); |
| 1247 | } catch (RemoteException e) { |
| 1248 | binderDied(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | void unlinkDeathRecipient() { |
| 1253 | mBinder.unlinkToDeath(this, 0); |
| 1254 | } |
| 1255 | |
| 1256 | public void binderDied() { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1257 | log("ConnectivityService FeatureUser binderDied(" + |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1258 | mNetworkType + ", " + mFeature + ", " + mBinder + "), created " + |
| 1259 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1260 | stopUsingNetworkFeature(this, false); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1263 | public void expire() { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1264 | if (VDBG) { |
| 1265 | log("ConnectivityService FeatureUser expire(" + |
| 1266 | mNetworkType + ", " + mFeature + ", " + mBinder +"), created " + |
| 1267 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
| 1268 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1269 | stopUsingNetworkFeature(this, false); |
| 1270 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1271 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1272 | public boolean isSameUser(FeatureUser u) { |
| 1273 | if (u == null) return false; |
| 1274 | |
| 1275 | return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature); |
| 1276 | } |
| 1277 | |
| 1278 | public boolean isSameUser(int pid, int uid, int networkType, String feature) { |
| 1279 | if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) && |
| 1280 | TextUtils.equals(mFeature, feature)) { |
| 1281 | return true; |
| 1282 | } |
| 1283 | return false; |
| 1284 | } |
| 1285 | |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1286 | public String toString() { |
| 1287 | return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " + |
| 1288 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"; |
| 1289 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1290 | } |
| 1291 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1292 | // javadoc from interface |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1293 | public int startUsingNetworkFeature(int networkType, String feature, |
| 1294 | IBinder binder) { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1295 | long startTime = 0; |
| 1296 | if (DBG) { |
| 1297 | startTime = SystemClock.elapsedRealtime(); |
| 1298 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1299 | if (VDBG) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1300 | log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid=" |
| 1301 | + Binder.getCallingUid()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1302 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | enforceChangePermission(); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1304 | try { |
| 1305 | if (!ConnectivityManager.isNetworkTypeValid(networkType) || |
| 1306 | mNetConfigs[networkType] == null) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1307 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1308 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1309 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1310 | FeatureUser f = new FeatureUser(networkType, feature, binder); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1311 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1312 | // TODO - move this into individual networktrackers |
| 1313 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1314 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1315 | if (mLockdownEnabled) { |
| 1316 | // Since carrier APNs usually aren't available from VPN |
| 1317 | // endpoint, mark them as unavailable. |
| 1318 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
| 1319 | } |
| 1320 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1321 | if (mProtectedNetworks.contains(usedNetworkType)) { |
| 1322 | enforceConnectivityInternalPermission(); |
| 1323 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1324 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1325 | // if UID is restricted, don't allow them to bring up metered APNs |
| 1326 | final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType); |
| 1327 | final int uidRules; |
| 1328 | synchronized (mRulesLock) { |
| 1329 | uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL); |
| 1330 | } |
| 1331 | if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1332 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1333 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1334 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1335 | NetworkStateTracker network = mNetTrackers[usedNetworkType]; |
| 1336 | if (network != null) { |
| 1337 | Integer currentPid = new Integer(getCallingPid()); |
| 1338 | if (usedNetworkType != networkType) { |
| 1339 | NetworkInfo ni = network.getNetworkInfo(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1340 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1341 | if (ni.isAvailable() == false) { |
| 1342 | if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 1343 | if (DBG) log("special network not available ni=" + ni.getTypeName()); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1344 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1345 | } else { |
| 1346 | // else make the attempt anyway - probably giving REQUEST_STARTED below |
| 1347 | if (DBG) { |
| 1348 | log("special network not available, but try anyway ni=" + |
| 1349 | ni.getTypeName()); |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1354 | int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1355 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1356 | synchronized(this) { |
| 1357 | boolean addToList = true; |
| 1358 | if (restoreTimer < 0) { |
| 1359 | // In case there is no timer is specified for the feature, |
| 1360 | // make sure we don't add duplicate entry with the same request. |
| 1361 | for (FeatureUser u : mFeatureUsers) { |
| 1362 | if (u.isSameUser(f)) { |
| 1363 | // Duplicate user is found. Do not add. |
| 1364 | addToList = false; |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1368 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1369 | |
| 1370 | if (addToList) mFeatureUsers.add(f); |
| 1371 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1372 | // this gets used for per-pid dns when connected |
| 1373 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1374 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1375 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1376 | |
| 1377 | if (restoreTimer >= 0) { |
| 1378 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1379 | EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer); |
| 1380 | } |
| 1381 | |
| 1382 | if ((ni.isConnectedOrConnecting() == true) && |
| 1383 | !network.isTeardownRequested()) { |
| 1384 | if (ni.isConnected() == true) { |
| 1385 | final long token = Binder.clearCallingIdentity(); |
| 1386 | try { |
| 1387 | // add the pid-specific dns |
| 1388 | handleDnsConfigurationChange(usedNetworkType); |
| 1389 | if (VDBG) log("special network already active"); |
| 1390 | } finally { |
| 1391 | Binder.restoreCallingIdentity(token); |
| 1392 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1393 | return PhoneConstants.APN_ALREADY_ACTIVE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1394 | } |
| 1395 | if (VDBG) log("special network already connecting"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1396 | return PhoneConstants.APN_REQUEST_STARTED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | // check if the radio in play can make another contact |
| 1400 | // assume if cannot for now |
| 1401 | |
| 1402 | if (DBG) { |
| 1403 | log("startUsingNetworkFeature reconnecting to " + networkType + ": " + |
| 1404 | feature); |
| 1405 | } |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1406 | if (network.reconnect()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1407 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_STARTED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1408 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1409 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1410 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_FAILED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1411 | return PhoneConstants.APN_REQUEST_FAILED; |
| 1412 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1413 | } else { |
| 1414 | // need to remember this unsupported request so we respond appropriately on stop |
| 1415 | synchronized(this) { |
| 1416 | mFeatureUsers.add(f); |
| 1417 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1418 | // this gets used for per-pid dns when connected |
| 1419 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1420 | } |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1421 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1422 | if (DBG) log("startUsingNetworkFeature X: return -1 unsupported feature."); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1423 | return -1; |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1424 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1425 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1426 | if (DBG) log("startUsingNetworkFeature X: return APN_TYPE_NOT_AVAILABLE"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1427 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1428 | } finally { |
| 1429 | if (DBG) { |
| 1430 | final long execTime = SystemClock.elapsedRealtime() - startTime; |
| 1431 | if (execTime > 250) { |
| 1432 | loge("startUsingNetworkFeature took too long: " + execTime + "ms"); |
| 1433 | } else { |
| 1434 | if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms"); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1438 | } |
| 1439 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1440 | // javadoc from interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1442 | enforceChangePermission(); |
| 1443 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1444 | int pid = getCallingPid(); |
| 1445 | int uid = getCallingUid(); |
| 1446 | |
| 1447 | FeatureUser u = null; |
| 1448 | boolean found = false; |
| 1449 | |
| 1450 | synchronized(this) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1451 | for (FeatureUser x : mFeatureUsers) { |
| 1452 | if (x.isSameUser(pid, uid, networkType, feature)) { |
| 1453 | u = x; |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1454 | found = true; |
| 1455 | break; |
| 1456 | } |
| 1457 | } |
| 1458 | } |
| 1459 | if (found && u != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1460 | if (VDBG) log("stopUsingNetworkFeature: X"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1461 | // stop regardless of how many other time this proc had called start |
| 1462 | return stopUsingNetworkFeature(u, true); |
| 1463 | } else { |
| 1464 | // none found! |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1465 | if (VDBG) log("stopUsingNetworkFeature: X not a live request, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1466 | return 1; |
| 1467 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1470 | private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) { |
| 1471 | int networkType = u.mNetworkType; |
| 1472 | String feature = u.mFeature; |
| 1473 | int pid = u.mPid; |
| 1474 | int uid = u.mUid; |
| 1475 | |
| 1476 | NetworkStateTracker tracker = null; |
| 1477 | boolean callTeardown = false; // used to carry our decision outside of sync block |
| 1478 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1479 | if (VDBG) { |
| 1480 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1481 | } |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1482 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1483 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1484 | if (DBG) { |
| 1485 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1486 | ", net is invalid"); |
| 1487 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1488 | return -1; |
| 1489 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1490 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1491 | // need to link the mFeatureUsers list with the mNetRequestersPids state in this |
| 1492 | // sync block |
| 1493 | synchronized(this) { |
| 1494 | // check if this process still has an outstanding start request |
| 1495 | if (!mFeatureUsers.contains(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1496 | if (VDBG) { |
| 1497 | log("stopUsingNetworkFeature: this process has no outstanding requests" + |
| 1498 | ", ignoring"); |
| 1499 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1500 | return 1; |
| 1501 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1502 | u.unlinkDeathRecipient(); |
| 1503 | mFeatureUsers.remove(mFeatureUsers.indexOf(u)); |
| 1504 | // If we care about duplicate requests, check for that here. |
| 1505 | // |
| 1506 | // This is done to support the extension of a request - the app |
| 1507 | // can request we start the network feature again and renew the |
| 1508 | // auto-shutoff delay. Normal "stop" calls from the app though |
| 1509 | // do not pay attention to duplicate requests - in effect the |
| 1510 | // API does not refcount and a single stop will counter multiple starts. |
| 1511 | if (ignoreDups == false) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1512 | for (FeatureUser x : mFeatureUsers) { |
| 1513 | if (x.isSameUser(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1514 | if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1515 | return 1; |
| 1516 | } |
| 1517 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1518 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1519 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 1520 | // TODO - move to individual network trackers |
| 1521 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
| 1522 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1523 | tracker = mNetTrackers[usedNetworkType]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1524 | if (tracker == null) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1525 | if (DBG) { |
| 1526 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1527 | " no known tracker for used net type " + usedNetworkType); |
| 1528 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1529 | return -1; |
| 1530 | } |
| 1531 | if (usedNetworkType != networkType) { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1532 | Integer currentPid = new Integer(pid); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1533 | mNetRequestersPids[usedNetworkType].remove(currentPid); |
Robert Greenwalt | 9055ae3 | 2013-02-12 17:18:25 -0800 | [diff] [blame] | 1534 | |
| 1535 | final long token = Binder.clearCallingIdentity(); |
| 1536 | try { |
| 1537 | reassessPidDns(pid, true); |
| 1538 | } finally { |
| 1539 | Binder.restoreCallingIdentity(token); |
| 1540 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1541 | flushVmDnsCache(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1542 | if (mNetRequestersPids[usedNetworkType].size() != 0) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1543 | if (VDBG) { |
| 1544 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1545 | " others still using it"); |
| 1546 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1547 | return 1; |
| 1548 | } |
| 1549 | callTeardown = true; |
Robert Greenwalt | 19b9ab4 | 2011-01-10 11:58:31 -0800 | [diff] [blame] | 1550 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1551 | if (DBG) { |
| 1552 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1553 | " not a known feature - dropping"); |
| 1554 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1555 | } |
| 1556 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1557 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1558 | if (callTeardown) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1559 | if (DBG) { |
| 1560 | log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature); |
| 1561 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1562 | tracker.teardown(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1563 | return 1; |
| 1564 | } else { |
Robert Greenwalt | 02648a4 | 2010-05-18 10:52:51 -0700 | [diff] [blame] | 1565 | return -1; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | /** |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1570 | * Check if the address falls into any of currently running VPN's route's. |
| 1571 | */ |
| 1572 | private boolean isAddressUnderVpn(InetAddress address) { |
| 1573 | synchronized (mVpns) { |
| 1574 | synchronized (mRoutesLock) { |
| 1575 | int uid = UserHandle.getCallingUserId(); |
| 1576 | Vpn vpn = mVpns.get(uid); |
| 1577 | if (vpn == null) { |
| 1578 | return false; |
| 1579 | } |
| 1580 | |
| 1581 | // Check if an exemption exists for this address. |
| 1582 | for (LinkAddress destination : mExemptAddresses) { |
| 1583 | if (!NetworkUtils.addressTypeMatches(address, destination.getAddress())) { |
| 1584 | continue; |
| 1585 | } |
| 1586 | |
| 1587 | int prefix = destination.getNetworkPrefixLength(); |
| 1588 | InetAddress addrMasked = NetworkUtils.getNetworkPart(address, prefix); |
| 1589 | InetAddress destMasked = NetworkUtils.getNetworkPart(destination.getAddress(), |
| 1590 | prefix); |
| 1591 | |
| 1592 | if (addrMasked.equals(destMasked)) { |
| 1593 | return false; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | // Finally check if the address is covered by the VPN. |
| 1598 | return vpn.isAddressCovered(address); |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1604 | * @deprecated use requestRouteToHostAddress instead |
| 1605 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1606 | * Ensure that a network route exists to deliver traffic to the specified |
| 1607 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1608 | * @param networkType the type of the network over which traffic to the |
| 1609 | * specified host is to be routed |
| 1610 | * @param hostAddress the IP address of the host to which the route is |
| 1611 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | * @return {@code true} on success, {@code false} on failure |
| 1613 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1614 | public boolean requestRouteToHost(int networkType, int hostAddress, String packageName) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1615 | InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); |
| 1616 | |
| 1617 | if (inetAddress == null) { |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1621 | return requestRouteToHostAddress(networkType, inetAddress.getAddress(), packageName); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * Ensure that a network route exists to deliver traffic to the specified |
| 1626 | * host via the specified network interface. |
| 1627 | * @param networkType the type of the network over which traffic to the |
| 1628 | * specified host is to be routed |
| 1629 | * @param hostAddress the IP address of the host to which the route is |
| 1630 | * desired |
| 1631 | * @return {@code true} on success, {@code false} on failure |
| 1632 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1633 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress, |
| 1634 | String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1636 | if (mProtectedNetworks.contains(networkType)) { |
| 1637 | enforceConnectivityInternalPermission(); |
| 1638 | } |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1639 | boolean exempt; |
| 1640 | InetAddress addr; |
| 1641 | try { |
| 1642 | addr = InetAddress.getByAddress(hostAddress); |
| 1643 | } catch (UnknownHostException e) { |
| 1644 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1645 | return false; |
| 1646 | } |
| 1647 | // System apps may request routes bypassing the VPN to keep other networks working. |
| 1648 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1649 | exempt = true; |
| 1650 | } else { |
| 1651 | mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); |
| 1652 | try { |
| 1653 | ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(packageName, |
| 1654 | 0); |
| 1655 | exempt = (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1656 | } catch (NameNotFoundException e) { |
| 1657 | throw new IllegalArgumentException("Failed to find calling package details", e); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | // Non-exempt routeToHost's can only be added if the host is not covered by the VPN. |
| 1662 | // This can be either because the VPN's routes do not cover the destination or a |
| 1663 | // system application added an exemption that covers this destination. |
| 1664 | if (!exempt && isAddressUnderVpn(addr)) { |
| 1665 | return false; |
| 1666 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1669 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | return false; |
| 1671 | } |
| 1672 | NetworkStateTracker tracker = mNetTrackers[networkType]; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1673 | DetailedState netState = DetailedState.DISCONNECTED; |
| 1674 | if (tracker != null) { |
| 1675 | netState = tracker.getNetworkInfo().getDetailedState(); |
| 1676 | } |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1677 | |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1678 | if ((netState != DetailedState.CONNECTED && |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 1679 | netState != DetailedState.CAPTIVE_PORTAL_CHECK) || |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1680 | tracker.isTeardownRequested()) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1681 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1682 | log("requestRouteToHostAddress on down network " |
| 1683 | + "(" + networkType + ") - dropped" |
| 1684 | + " tracker=" + tracker |
| 1685 | + " netState=" + netState |
| 1686 | + " isTeardownRequested=" |
| 1687 | + ((tracker != null) ? tracker.isTeardownRequested() : "tracker:null")); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1688 | } |
| 1689 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1690 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1691 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1692 | try { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1693 | LinkProperties lp = tracker.getLinkProperties(); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1694 | boolean ok = addRouteToAddress(lp, addr, exempt, tracker.getNetwork().netId); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1695 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1696 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1697 | } finally { |
| 1698 | Binder.restoreCallingIdentity(token); |
| 1699 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1702 | private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1703 | boolean exempt, int netId) { |
| 1704 | return modifyRoute(p, r, 0, ADD, toDefaultTable, exempt, netId); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1707 | private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, int netId) { |
| 1708 | return modifyRoute(p, r, 0, REMOVE, toDefaultTable, UNEXEMPT, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1709 | } |
| 1710 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1711 | private boolean addRouteToAddress(LinkProperties lp, InetAddress addr, boolean exempt, |
| 1712 | int netId) { |
| 1713 | return modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE, exempt, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1714 | } |
| 1715 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1716 | private boolean removeRouteToAddress(LinkProperties lp, InetAddress addr, int netId) { |
| 1717 | return modifyRouteToAddress(lp, addr, REMOVE, TO_DEFAULT_TABLE, UNEXEMPT, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1720 | private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1721 | boolean toDefaultTable, boolean exempt, int netId) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1722 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1723 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1724 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1725 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1726 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1727 | if (bestRoute.getGateway().equals(addr)) { |
| 1728 | // 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] | 1729 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1730 | } else { |
| 1731 | // if we will connect to this through another route, add a direct route |
| 1732 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1733 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1734 | } |
| 1735 | } |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1736 | return modifyRoute(lp, bestRoute, 0, doAdd, toDefaultTable, exempt, netId); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1737 | } |
| 1738 | |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1739 | private boolean modifyRoute(LinkProperties lp, RouteInfo r, int cycleCount, boolean doAdd, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1740 | boolean toDefaultTable, boolean exempt, int netId) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1741 | if ((lp == null) || (r == null)) { |
| 1742 | if (DBG) log("modifyRoute got unexpected null: " + lp + ", " + r); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1743 | return false; |
| 1744 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1745 | |
| 1746 | if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1747 | loge("Error modifying route - too much recursion"); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1748 | return false; |
| 1749 | } |
| 1750 | |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1751 | String ifaceName = r.getInterface(); |
| 1752 | if(ifaceName == null) { |
| 1753 | loge("Error modifying route - no interface name"); |
| 1754 | return false; |
| 1755 | } |
Robert Greenwalt | ca441ee | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 1756 | if (r.hasGateway()) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1757 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), r.getGateway()); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1758 | if (bestRoute != null) { |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1759 | if (bestRoute.getGateway().equals(r.getGateway())) { |
| 1760 | // 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] | 1761 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), ifaceName); |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1762 | } else { |
| 1763 | // if we will connect to our gateway through another route, add a direct |
| 1764 | // route to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1765 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), |
| 1766 | bestRoute.getGateway(), |
| 1767 | ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1768 | } |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1769 | modifyRoute(lp, bestRoute, cycleCount+1, doAdd, toDefaultTable, exempt, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | if (doAdd) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1773 | if (VDBG) log("Adding " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1774 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1775 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1776 | synchronized (mRoutesLock) { |
| 1777 | // only track default table - only one apps can effect |
| 1778 | mAddedRoutes.add(r); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1779 | mNetd.addRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1780 | if (exempt) { |
| 1781 | LinkAddress dest = r.getDestination(); |
| 1782 | if (!mExemptAddresses.contains(dest)) { |
| 1783 | mNetd.setHostExemption(dest); |
| 1784 | mExemptAddresses.add(dest); |
| 1785 | } |
| 1786 | } |
| 1787 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1788 | } else { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1789 | mNetd.addRoute(netId, r); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1790 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1791 | } catch (Exception e) { |
| 1792 | // never crash - catch them all |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1793 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1794 | return false; |
| 1795 | } |
| 1796 | } else { |
| 1797 | // if we remove this one and there are no more like it, then refcount==0 and |
| 1798 | // we can remove it from the table |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1799 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1800 | synchronized (mRoutesLock) { |
| 1801 | mAddedRoutes.remove(r); |
| 1802 | if (mAddedRoutes.contains(r) == false) { |
| 1803 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 1804 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1805 | mNetd.removeRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1806 | LinkAddress dest = r.getDestination(); |
| 1807 | if (mExemptAddresses.contains(dest)) { |
| 1808 | mNetd.clearHostExemption(dest); |
| 1809 | mExemptAddresses.remove(dest); |
| 1810 | } |
| 1811 | } catch (Exception e) { |
| 1812 | // never crash - catch them all |
| 1813 | if (VDBG) loge("Exception trying to remove a route: " + e); |
| 1814 | return false; |
| 1815 | } |
| 1816 | } else { |
| 1817 | if (VDBG) log("not removing " + r + " as it's still in use"); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1818 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1819 | } |
| 1820 | } else { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1821 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1822 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1823 | mNetd.removeRoute(netId, r); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1824 | } catch (Exception e) { |
| 1825 | // never crash - catch them all |
Robert Greenwalt | 2473a4ba | 2012-04-23 18:00:37 -0700 | [diff] [blame] | 1826 | if (VDBG) loge("Exception trying to remove a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1827 | return false; |
| 1828 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1829 | } |
| 1830 | } |
| 1831 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1835 | * @see ConnectivityManager#getMobileDataEnabled() |
| 1836 | */ |
| 1837 | public boolean getMobileDataEnabled() { |
Wink Saville | e798268 | 2010-12-07 10:31:02 -0800 | [diff] [blame] | 1838 | // TODO: This detail should probably be in DataConnectionTracker's |
| 1839 | // which is where we store the value and maybe make this |
| 1840 | // asynchronous. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1841 | enforceAccessPermission(); |
Jeff Sharkey | bdfce2e | 2012-09-26 15:54:06 -0700 | [diff] [blame] | 1842 | boolean retVal = Settings.Global.getInt(mContext.getContentResolver(), |
| 1843 | Settings.Global.MOBILE_DATA, 1) == 1; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1844 | if (VDBG) log("getMobileDataEnabled returning " + retVal); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1845 | return retVal; |
| 1846 | } |
| 1847 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1848 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1849 | enforceConnectivityInternalPermission(); |
| 1850 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1851 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1852 | (met ? ENABLED : DISABLED), networkType)); |
| 1853 | } |
| 1854 | |
| 1855 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1856 | if (mNetTrackers[networkType] != null) { |
| 1857 | if (DBG) { |
| 1858 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1859 | } |
| 1860 | mNetTrackers[networkType].setDependencyMet(met); |
| 1861 | } |
| 1862 | } |
| 1863 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1864 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1865 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1866 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1867 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1868 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1869 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1872 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1873 | // skip update when we've already applied rules |
| 1874 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1875 | if (oldRules == uidRules) return; |
| 1876 | |
| 1877 | mUidRules.put(uid, uidRules); |
| 1878 | } |
| 1879 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1880 | // TODO: notify UID when it has requested targeted updates |
| 1881 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1882 | |
| 1883 | @Override |
| 1884 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1885 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1886 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1887 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | synchronized (mRulesLock) { |
| 1891 | mMeteredIfaces.clear(); |
| 1892 | for (String iface : meteredIfaces) { |
| 1893 | mMeteredIfaces.add(iface); |
| 1894 | } |
| 1895 | } |
| 1896 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1897 | |
| 1898 | @Override |
| 1899 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1900 | // caller is NPMS, since we only register with them |
| 1901 | if (LOGD_RULES) { |
| 1902 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1903 | } |
| 1904 | |
| 1905 | // kick off connectivity change broadcast for active network, since |
| 1906 | // global background policy change is radical. |
| 1907 | final int networkType = mActiveDefaultNetwork; |
| 1908 | if (isNetworkTypeValid(networkType)) { |
| 1909 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1910 | if (tracker != null) { |
| 1911 | final NetworkInfo info = tracker.getNetworkInfo(); |
| 1912 | if (info != null && info.isConnected()) { |
| 1913 | sendConnectedBroadcast(info); |
| 1914 | } |
| 1915 | } |
| 1916 | } |
| 1917 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1918 | }; |
| 1919 | |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1920 | /** |
| 1921 | * @see ConnectivityManager#setMobileDataEnabled(boolean) |
| 1922 | */ |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1923 | public void setMobileDataEnabled(boolean enabled) { |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1924 | enforceChangePermission(); |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1925 | if (DBG) log("setMobileDataEnabled(" + enabled + ")"); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1926 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1927 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA, |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1928 | (enabled ? ENABLED : DISABLED), 0)); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | private void handleSetMobileData(boolean enabled) { |
Wink Saville | e798268 | 2010-12-07 10:31:02 -0800 | [diff] [blame] | 1932 | if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1933 | if (VDBG) { |
| 1934 | log(mNetTrackers[ConnectivityManager.TYPE_MOBILE].toString() + enabled); |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1935 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1936 | mNetTrackers[ConnectivityManager.TYPE_MOBILE].setUserDataEnable(enabled); |
| 1937 | } |
tk.mun | b97d34c | 2012-01-06 10:43:52 +0900 | [diff] [blame] | 1938 | if (mNetTrackers[ConnectivityManager.TYPE_WIMAX] != null) { |
| 1939 | if (VDBG) { |
| 1940 | log(mNetTrackers[ConnectivityManager.TYPE_WIMAX].toString() + enabled); |
| 1941 | } |
| 1942 | mNetTrackers[ConnectivityManager.TYPE_WIMAX].setUserDataEnable(enabled); |
| 1943 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | @Override |
| 1947 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 1948 | // only someone like NPMS should only be calling us |
| 1949 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 1950 | |
| 1951 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1952 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 1953 | } |
| 1954 | |
| 1955 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
| 1956 | if (isNetworkTypeValid(networkType)) { |
| 1957 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 1958 | if (tracker != null) { |
| 1959 | tracker.setPolicyDataEnable(enabled); |
| 1960 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1961 | } |
| 1962 | } |
| 1963 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1964 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1965 | mContext.enforceCallingOrSelfPermission( |
| 1966 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1967 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1971 | mContext.enforceCallingOrSelfPermission( |
| 1972 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1973 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1974 | } |
| 1975 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1976 | // TODO Make this a special check when it goes public |
| 1977 | private void enforceTetherChangePermission() { |
| 1978 | mContext.enforceCallingOrSelfPermission( |
| 1979 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 1980 | "ConnectivityService"); |
| 1981 | } |
| 1982 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1983 | private void enforceTetherAccessPermission() { |
| 1984 | mContext.enforceCallingOrSelfPermission( |
| 1985 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1986 | "ConnectivityService"); |
| 1987 | } |
| 1988 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1989 | private void enforceConnectivityInternalPermission() { |
| 1990 | mContext.enforceCallingOrSelfPermission( |
| 1991 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1992 | "ConnectivityService"); |
| 1993 | } |
| 1994 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 1995 | private void enforceMarkNetworkSocketPermission() { |
| 1996 | //Media server special case |
| 1997 | if (Binder.getCallingUid() == Process.MEDIA_UID) { |
| 1998 | return; |
| 1999 | } |
| 2000 | mContext.enforceCallingOrSelfPermission( |
| 2001 | android.Manifest.permission.MARK_NETWORK_SOCKET, |
| 2002 | "ConnectivityService"); |
| 2003 | } |
| 2004 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2005 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2006 | * Handle a {@code DISCONNECTED} event. If this pertains to the non-active |
| 2007 | * network, we ignore it. If it is for the active network, we send out a |
| 2008 | * broadcast. But first, we check whether it might be possible to connect |
| 2009 | * to a different network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2010 | * @param info the {@code NetworkInfo} for the network |
| 2011 | */ |
| 2012 | private void handleDisconnect(NetworkInfo info) { |
| 2013 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2014 | int prevNetType = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2015 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2016 | mNetTrackers[prevNetType].setTeardownRequested(false); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2017 | int thisNetId = mNetTrackers[prevNetType].getNetwork().netId; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2018 | |
| 2019 | // Remove idletimer previously setup in {@code handleConnect} |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2020 | if (mNetConfigs[prevNetType].isDefault()) { |
| 2021 | removeDataActivityTracking(prevNetType); |
| 2022 | } |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2023 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2024 | /* |
| 2025 | * If the disconnected network is not the active one, then don't report |
| 2026 | * this as a loss of connectivity. What probably happened is that we're |
| 2027 | * getting the disconnect for a network that we explicitly disabled |
| 2028 | * in accordance with network preference policies. |
| 2029 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2030 | if (!mNetConfigs[prevNetType].isDefault()) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2031 | List<Integer> pids = mNetRequestersPids[prevNetType]; |
| 2032 | for (Integer pid : pids) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2033 | // will remove them because the net's no longer connected |
| 2034 | // need to do this now as only now do we know the pids and |
| 2035 | // can properly null things that are no longer referenced. |
| 2036 | reassessPidDns(pid.intValue(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2041 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2042 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | if (info.isFailover()) { |
| 2044 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2045 | info.setFailover(false); |
| 2046 | } |
| 2047 | if (info.getReason() != null) { |
| 2048 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2049 | } |
| 2050 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2051 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2052 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2053 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2054 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2055 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2056 | tryFailover(prevNetType); |
| 2057 | if (mActiveDefaultNetwork != -1) { |
| 2058 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2059 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 2060 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2061 | mDefaultInetConditionPublished = 0; // we're not connected anymore |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2062 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2063 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2064 | } |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2065 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2066 | |
| 2067 | // Reset interface if no other connections are using the same interface |
| 2068 | boolean doReset = true; |
| 2069 | LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties(); |
| 2070 | if (linkProperties != null) { |
| 2071 | String oldIface = linkProperties.getInterfaceName(); |
| 2072 | if (TextUtils.isEmpty(oldIface) == false) { |
| 2073 | for (NetworkStateTracker networkStateTracker : mNetTrackers) { |
| 2074 | if (networkStateTracker == null) continue; |
| 2075 | NetworkInfo networkInfo = networkStateTracker.getNetworkInfo(); |
| 2076 | if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) { |
| 2077 | LinkProperties l = networkStateTracker.getLinkProperties(); |
| 2078 | if (l == null) continue; |
| 2079 | if (oldIface.equals(l.getInterfaceName())) { |
| 2080 | doReset = false; |
| 2081 | break; |
| 2082 | } |
| 2083 | } |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2088 | // do this before we broadcast the change |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2089 | handleConnectivityChange(prevNetType, doReset); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2090 | |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2091 | final Intent immediateIntent = new Intent(intent); |
| 2092 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 2093 | sendStickyBroadcast(immediateIntent); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2094 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2095 | /* |
| 2096 | * If the failover network is already connected, then immediately send |
| 2097 | * out a followup broadcast indicating successful failover |
| 2098 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2099 | if (mActiveDefaultNetwork != -1) { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2100 | sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), |
| 2101 | getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2102 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2103 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2104 | // mNetd.removeNetwork(thisNetId); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2105 | } catch (Exception e) { |
| 2106 | loge("Exception removing network: " + e); |
| 2107 | } finally { |
| 2108 | mNetTrackers[prevNetType].setNetId(INVALID_NET_ID); |
| 2109 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2110 | } |
| 2111 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2112 | private void tryFailover(int prevNetType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2113 | /* |
Robert Greenwalt | bff9018 | 2011-01-06 15:41:07 -0800 | [diff] [blame] | 2114 | * If this is a default network, check if other defaults are available. |
| 2115 | * Try to reconnect on all available and let them hash it out when |
| 2116 | * more than one connects. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2117 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2118 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2119 | if (mActiveDefaultNetwork == prevNetType) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2120 | if (DBG) { |
| 2121 | log("tryFailover: set mActiveDefaultNetwork=-1, prevNetType=" + prevNetType); |
| 2122 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2123 | mActiveDefaultNetwork = -1; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2124 | try { |
| 2125 | mNetd.clearDefaultNetId(); |
| 2126 | } catch (Exception e) { |
| 2127 | loge("Exception clearing default network :" + e); |
| 2128 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2131 | // don't signal a reconnect for anything lower or equal priority than our |
| 2132 | // current connected default |
| 2133 | // TODO - don't filter by priority now - nice optimization but risky |
| 2134 | // int currentPriority = -1; |
| 2135 | // if (mActiveDefaultNetwork != -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2136 | // currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority; |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2137 | // } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2138 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2139 | for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 2140 | if (checkType == prevNetType) continue; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2141 | if (mNetConfigs[checkType] == null) continue; |
| 2142 | if (!mNetConfigs[checkType].isDefault()) continue; |
Robert Greenwalt | 424781e | 2011-11-11 09:56:03 -0800 | [diff] [blame] | 2143 | if (mNetTrackers[checkType] == null) continue; |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2144 | |
| 2145 | // Enabling the isAvailable() optimization caused mobile to not get |
| 2146 | // selected if it was in the middle of error handling. Specifically |
| 2147 | // a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL |
| 2148 | // would not be available and we wouldn't get connected to anything. |
| 2149 | // So removing the isAvailable() optimization below for now. TODO: This |
| 2150 | // optimization should work and we need to investigate why it doesn't work. |
| 2151 | // This could be related to how DEACTIVATE_DATA_CALL is reporting its |
| 2152 | // complete before it is really complete. |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2153 | |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2154 | // if (!mNetTrackers[checkType].isAvailable()) continue; |
| 2155 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2156 | // if (currentPriority >= mNetConfigs[checkType].mPriority) continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2157 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2158 | NetworkStateTracker checkTracker = mNetTrackers[checkType]; |
| 2159 | NetworkInfo checkInfo = checkTracker.getNetworkInfo(); |
| 2160 | if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) { |
| 2161 | checkInfo.setFailover(true); |
| 2162 | checkTracker.reconnect(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2163 | } |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2164 | if (DBG) log("Attempting to switch to " + checkInfo.getTypeName()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2165 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2167 | } |
| 2168 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2169 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2170 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2171 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2172 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2175 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2176 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2177 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2180 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 2181 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 2182 | } |
| 2183 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2184 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2185 | if (mLockdownTracker != null) { |
| 2186 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 2187 | } |
| 2188 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2189 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2190 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2191 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2192 | if (info.isFailover()) { |
| 2193 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2194 | info.setFailover(false); |
| 2195 | } |
| 2196 | if (info.getReason() != null) { |
| 2197 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2198 | } |
| 2199 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2200 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2201 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2202 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2203 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2204 | return intent; |
| 2205 | } |
| 2206 | |
| 2207 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 2208 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 2209 | } |
| 2210 | |
| 2211 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 2212 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2213 | } |
| 2214 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2215 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2216 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 2217 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 2218 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2219 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2220 | final long ident = Binder.clearCallingIdentity(); |
| 2221 | try { |
| 2222 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 2223 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2224 | } finally { |
| 2225 | Binder.restoreCallingIdentity(ident); |
| 2226 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2229 | /** |
| 2230 | * Called when an attempt to fail over to another network has failed. |
| 2231 | * @param info the {@link NetworkInfo} for the failed network |
| 2232 | */ |
| 2233 | private void handleConnectionFailure(NetworkInfo info) { |
| 2234 | mNetTrackers[info.getType()].setTeardownRequested(false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2235 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2236 | String reason = info.getReason(); |
| 2237 | String extraInfo = info.getExtraInfo(); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2238 | |
Robert Greenwalt | 572172b | 2010-10-08 16:35:52 -0700 | [diff] [blame] | 2239 | String reasonText; |
| 2240 | if (reason == null) { |
| 2241 | reasonText = "."; |
| 2242 | } else { |
| 2243 | reasonText = " (" + reason + ")."; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2244 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2245 | loge("Attempt to connect to " + info.getTypeName() + " failed" + reasonText); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2246 | |
| 2247 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2248 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2249 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2250 | if (getActiveNetworkInfo() == null) { |
| 2251 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2252 | } |
| 2253 | if (reason != null) { |
| 2254 | intent.putExtra(ConnectivityManager.EXTRA_REASON, reason); |
| 2255 | } |
| 2256 | if (extraInfo != null) { |
| 2257 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, extraInfo); |
| 2258 | } |
| 2259 | if (info.isFailover()) { |
| 2260 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2261 | info.setFailover(false); |
| 2262 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2263 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2264 | if (mNetConfigs[info.getType()].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2265 | tryFailover(info.getType()); |
| 2266 | if (mActiveDefaultNetwork != -1) { |
| 2267 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2268 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 2269 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2270 | mDefaultInetConditionPublished = 0; |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2271 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2272 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2273 | } |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2274 | |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2275 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2276 | |
| 2277 | final Intent immediateIntent = new Intent(intent); |
| 2278 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 2279 | sendStickyBroadcast(immediateIntent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2280 | sendStickyBroadcast(intent); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2281 | /* |
| 2282 | * If the failover network is already connected, then immediately send |
| 2283 | * out a followup broadcast indicating successful failover |
| 2284 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2285 | if (mActiveDefaultNetwork != -1) { |
| 2286 | sendConnectedBroadcast(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2287 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | private void sendStickyBroadcast(Intent intent) { |
| 2291 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2292 | if (!mSystemReady) { |
| 2293 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2294 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2295 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2296 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2297 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2300 | final long ident = Binder.clearCallingIdentity(); |
| 2301 | try { |
| 2302 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2303 | } finally { |
| 2304 | Binder.restoreCallingIdentity(ident); |
| 2305 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2309 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 2310 | if (delayMs <= 0) { |
| 2311 | sendStickyBroadcast(intent); |
| 2312 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2313 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2314 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 2315 | + intent.getAction()); |
| 2316 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2317 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 2318 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 2319 | } |
| 2320 | } |
| 2321 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2322 | void systemReady() { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2323 | mCaptivePortalTracker = CaptivePortalTracker.makeCaptivePortalTracker(mContext, this); |
| 2324 | loadGlobalProxy(); |
| 2325 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2326 | synchronized(this) { |
| 2327 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2328 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2329 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2330 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2331 | } |
| 2332 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2333 | // load the global proxy at startup |
| 2334 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2335 | |
| 2336 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 2337 | // for user to unlock device. |
| 2338 | if (!updateLockdownVpn()) { |
| 2339 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 2340 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 2341 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2342 | } |
| 2343 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2344 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 2345 | @Override |
| 2346 | public void onReceive(Context context, Intent intent) { |
| 2347 | // Try creating lockdown tracker, since user present usually means |
| 2348 | // unlocked keystore. |
| 2349 | if (updateLockdownVpn()) { |
| 2350 | mContext.unregisterReceiver(this); |
| 2351 | } |
| 2352 | } |
| 2353 | }; |
| 2354 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2355 | private boolean isNewNetTypePreferredOverCurrentNetType(int type) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2356 | if (((type != mNetworkPreference) |
| 2357 | && (mNetConfigs[mActiveDefaultNetwork].priority > mNetConfigs[type].priority)) |
| 2358 | || (mNetworkPreference == mActiveDefaultNetwork)) { |
| 2359 | return false; |
| 2360 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2361 | return true; |
| 2362 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2363 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2364 | private void handleConnect(NetworkInfo info) { |
| 2365 | final int newNetType = info.getType(); |
| 2366 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2367 | // snapshot isFailover, because sendConnectedBroadcast() resets it |
| 2368 | boolean isFailover = info.isFailover(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2369 | final NetworkStateTracker thisNet = mNetTrackers[newNetType]; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2370 | final String thisIface = thisNet.getLinkProperties().getInterfaceName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2372 | if (VDBG) { |
| 2373 | log("handleConnect: E newNetType=" + newNetType + " thisIface=" + thisIface |
| 2374 | + " isFailover" + isFailover); |
| 2375 | } |
| 2376 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2377 | // if this is a default net and other default is running |
| 2378 | // kill the one not preferred |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2379 | if (mNetConfigs[newNetType].isDefault()) { |
| 2380 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { |
| 2381 | if (isNewNetTypePreferredOverCurrentNetType(newNetType)) { |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2382 | String teardownPolicy = SystemProperties.get("net.teardownPolicy"); |
| 2383 | if (TextUtils.equals(teardownPolicy, "keep") == false) { |
| 2384 | // tear down the other |
| 2385 | NetworkStateTracker otherNet = |
| 2386 | mNetTrackers[mActiveDefaultNetwork]; |
| 2387 | if (DBG) { |
| 2388 | log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + |
| 2389 | " teardown"); |
| 2390 | } |
| 2391 | if (!teardown(otherNet)) { |
| 2392 | loge("Network declined teardown request"); |
| 2393 | teardown(thisNet); |
| 2394 | return; |
| 2395 | } |
| 2396 | } else { |
| 2397 | //TODO - remove |
| 2398 | loge("network teardown skipped due to net.teardownPolicy setting"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2399 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2400 | } else { |
| 2401 | // don't accept this one |
| 2402 | if (VDBG) { |
| 2403 | log("Not broadcasting CONNECT_ACTION " + |
| 2404 | "to torn down network " + info.getTypeName()); |
| 2405 | } |
| 2406 | teardown(thisNet); |
| 2407 | return; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2408 | } |
| 2409 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2410 | int thisNetId = nextNetId(); |
| 2411 | thisNet.setNetId(thisNetId); |
| 2412 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2413 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2414 | } catch (Exception e) { |
| 2415 | loge("Exception creating network :" + e); |
| 2416 | teardown(thisNet); |
| 2417 | return; |
| 2418 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2419 | setupDataActivityTracking(newNetType); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2420 | synchronized (ConnectivityService.this) { |
| 2421 | // have a new default network, release the transition wakelock in a second |
| 2422 | // if it's held. The second pause is to allow apps to reconnect over the |
| 2423 | // new network |
| 2424 | if (mNetTransitionWakeLock.isHeld()) { |
| 2425 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 2426 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2427 | mNetTransitionWakeLockSerialNumber, 0), |
| 2428 | 1000); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2429 | } |
| 2430 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2431 | mActiveDefaultNetwork = newNetType; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2432 | try { |
| 2433 | mNetd.setDefaultNetId(thisNetId); |
| 2434 | } catch (Exception e) { |
| 2435 | loge("Exception setting default network :" + e); |
| 2436 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2437 | // this will cause us to come up initially as unconnected and switching |
| 2438 | // to connected after our normal pause unless somebody reports us as reall |
| 2439 | // disconnected |
| 2440 | mDefaultInetConditionPublished = 0; |
| 2441 | mDefaultConnectionSequence++; |
| 2442 | mInetConditionChangeInFlight = false; |
| 2443 | // Don't do this - if we never sign in stay, grey |
| 2444 | //reportNetworkCondition(mActiveDefaultNetwork, 100); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 2445 | updateNetworkSettings(thisNet); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2446 | } else { |
| 2447 | int thisNetId = nextNetId(); |
| 2448 | thisNet.setNetId(thisNetId); |
| 2449 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2450 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2451 | } catch (Exception e) { |
| 2452 | loge("Exception creating network :" + e); |
| 2453 | teardown(thisNet); |
| 2454 | return; |
| 2455 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2456 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2457 | thisNet.setTeardownRequested(false); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2458 | updateMtuSizeSettings(thisNet); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2459 | handleConnectivityChange(newNetType, false); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2460 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2461 | |
| 2462 | // notify battery stats service about this network |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2463 | if (thisIface != null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2464 | try { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2465 | BatteryStatsService.getService().noteNetworkInterfaceType(thisIface, newNetType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2466 | } catch (RemoteException e) { |
| 2467 | // ignored; service lives in system_server |
| 2468 | } |
| 2469 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2472 | /** @hide */ |
| 2473 | @Override |
| 2474 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 2475 | enforceConnectivityInternalPermission(); |
| 2476 | if (DBG) log("captivePortalCheckCompleted: ni=" + info + " captive=" + isCaptivePortal); |
| 2477 | mNetTrackers[info.getType()].captivePortalCheckCompleted(isCaptivePortal); |
| 2478 | } |
| 2479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2480 | /** |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2481 | * Setup data activity tracking for the given network interface. |
| 2482 | * |
| 2483 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2484 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2485 | */ |
| 2486 | private void setupDataActivityTracking(int type) { |
| 2487 | final NetworkStateTracker thisNet = mNetTrackers[type]; |
| 2488 | final String iface = thisNet.getLinkProperties().getInterfaceName(); |
| 2489 | |
| 2490 | final int timeout; |
| 2491 | |
| 2492 | if (ConnectivityManager.isNetworkTypeMobile(type)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2493 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2494 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 2495 | 5); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2496 | // Canonicalize mobile network type |
| 2497 | type = ConnectivityManager.TYPE_MOBILE; |
| 2498 | } else if (ConnectivityManager.TYPE_WIFI == type) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2499 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2500 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2501 | 0); |
| 2502 | } else { |
| 2503 | // do not track any other networks |
| 2504 | timeout = 0; |
| 2505 | } |
| 2506 | |
| 2507 | if (timeout > 0 && iface != null) { |
| 2508 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2509 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2510 | } catch (Exception e) { |
| 2511 | // You shall not crash! |
| 2512 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2513 | } |
| 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /** |
| 2518 | * Remove data activity tracking when network disconnects. |
| 2519 | */ |
| 2520 | private void removeDataActivityTracking(int type) { |
| 2521 | final NetworkStateTracker net = mNetTrackers[type]; |
| 2522 | final String iface = net.getLinkProperties().getInterfaceName(); |
| 2523 | |
| 2524 | if (iface != null && (ConnectivityManager.isNetworkTypeMobile(type) || |
| 2525 | ConnectivityManager.TYPE_WIFI == type)) { |
| 2526 | try { |
| 2527 | // the call fails silently if no idletimer setup for this interface |
| 2528 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2529 | } catch (Exception e) { |
| 2530 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2531 | } |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | /** |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2536 | * After a change in the connectivity state of a network. We're mainly |
| 2537 | * concerned with making sure that the list of DNS servers is set up |
| 2538 | * according to which networks are connected, and ensuring that the |
| 2539 | * right routing table entries exist. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2540 | */ |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2541 | private void handleConnectivityChange(int netType, boolean doReset) { |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2542 | int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0; |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2543 | boolean exempt = ConnectivityManager.isNetworkTypeExempt(netType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2544 | if (VDBG) { |
| 2545 | log("handleConnectivityChange: netType=" + netType + " doReset=" + doReset |
| 2546 | + " resetMask=" + resetMask); |
| 2547 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2549 | /* |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2550 | * If a non-default network is enabled, add the host routes that |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2551 | * will allow it's DNS servers to be accessed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2552 | */ |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2553 | handleDnsConfigurationChange(netType); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2554 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2555 | LinkProperties curLp = mCurrentLinkProperties[netType]; |
| 2556 | LinkProperties newLp = null; |
| 2557 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2558 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2559 | newLp = mNetTrackers[netType].getLinkProperties(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2560 | if (VDBG) { |
| 2561 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2562 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2563 | "\n curLp=" + curLp + |
| 2564 | "\n newLp=" + newLp); |
| 2565 | } |
| 2566 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2567 | if (curLp != null) { |
| 2568 | if (curLp.isIdenticalInterfaceName(newLp)) { |
| 2569 | CompareResult<LinkAddress> car = curLp.compareAddresses(newLp); |
| 2570 | if ((car.removed.size() != 0) || (car.added.size() != 0)) { |
| 2571 | for (LinkAddress linkAddr : car.removed) { |
| 2572 | if (linkAddr.getAddress() instanceof Inet4Address) { |
| 2573 | resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES; |
| 2574 | } |
| 2575 | if (linkAddr.getAddress() instanceof Inet6Address) { |
| 2576 | resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES; |
| 2577 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2578 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2579 | if (DBG) { |
| 2580 | log("handleConnectivityChange: addresses changed" + |
| 2581 | " linkProperty[" + netType + "]:" + " resetMask=" + resetMask + |
| 2582 | "\n car=" + car); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2583 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2584 | } else { |
Robert Greenwalt | d01f842 | 2014-01-23 16:03:04 -0800 | [diff] [blame] | 2585 | if (VDBG) { |
| 2586 | log("handleConnectivityChange: addresses are the same reset per" + |
| 2587 | " doReset linkProperty[" + netType + "]:" + |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2588 | " resetMask=" + resetMask); |
| 2589 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2590 | } |
| 2591 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2592 | resetMask = NetworkUtils.RESET_ALL_ADDRESSES; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2593 | if (DBG) { |
| 2594 | log("handleConnectivityChange: interface not not equivalent reset both" + |
| 2595 | " linkProperty[" + netType + "]:" + |
| 2596 | " resetMask=" + resetMask); |
| 2597 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2598 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2599 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2600 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2601 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2602 | } |
| 2603 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2604 | if (VDBG) { |
| 2605 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2606 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2607 | "\n curLp=" + curLp + |
| 2608 | "\n newLp= null"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2609 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2610 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2611 | mCurrentLinkProperties[netType] = newLp; |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2612 | boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault(), exempt, |
| 2613 | mNetTrackers[netType].getNetwork().netId); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2614 | |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2615 | if (resetMask != 0 || resetDns) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2616 | if (VDBG) log("handleConnectivityChange: resetting"); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2617 | if (curLp != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2618 | if (VDBG) log("handleConnectivityChange: resetting curLp=" + curLp); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2619 | for (String iface : curLp.getAllInterfaceNames()) { |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2620 | if (TextUtils.isEmpty(iface) == false) { |
| 2621 | if (resetMask != 0) { |
| 2622 | if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")"); |
| 2623 | NetworkUtils.resetConnections(iface, resetMask); |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2624 | |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2625 | // Tell VPN the interface is down. It is a temporary |
| 2626 | // but effective fix to make VPN aware of the change. |
| 2627 | if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2628 | synchronized(mVpns) { |
| 2629 | for (int i = 0; i < mVpns.size(); i++) { |
| 2630 | mVpns.valueAt(i).interfaceStatusChanged(iface, false); |
| 2631 | } |
| 2632 | } |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2633 | } |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2634 | } |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2635 | } else { |
| 2636 | loge("Can't reset connection for type "+netType); |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2637 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2638 | } |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2639 | if (resetDns) { |
| 2640 | flushVmDnsCache(); |
| 2641 | if (VDBG) log("resetting DNS cache for type " + netType); |
| 2642 | try { |
| 2643 | mNetd.flushNetworkDnsCache(mNetTrackers[netType].getNetwork().netId); |
| 2644 | } catch (Exception e) { |
| 2645 | // never crash - catch them all |
| 2646 | if (DBG) loge("Exception resetting dns cache: " + e); |
| 2647 | } |
| 2648 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2649 | } |
| 2650 | } |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2651 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2652 | // Update 464xlat state. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2653 | NetworkStateTracker tracker = mNetTrackers[netType]; |
| 2654 | if (mClat.requiresClat(netType, tracker)) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2655 | |
Lorenzo Colitti | d2ef1e5 | 2013-03-28 14:13:43 +0900 | [diff] [blame] | 2656 | // If the connection was previously using clat, but is not using it now, stop the clat |
| 2657 | // daemon. Normally, this happens automatically when the connection disconnects, but if |
| 2658 | // the disconnect is not reported, or if the connection's LinkProperties changed for |
| 2659 | // some other reason (e.g., handoff changes the IP addresses on the link), it would |
| 2660 | // still be running. If it's not running, then stopping it is a no-op. |
| 2661 | if (Nat464Xlat.isRunningClat(curLp) && !Nat464Xlat.isRunningClat(newLp)) { |
| 2662 | mClat.stopClat(); |
| 2663 | } |
| 2664 | // If the link requires clat to be running, then start the daemon now. |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 2665 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
| 2666 | mClat.startClat(tracker); |
| 2667 | } else { |
| 2668 | mClat.stopClat(); |
| 2669 | } |
| 2670 | } |
| 2671 | |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2672 | // TODO: Temporary notifying upstread change to Tethering. |
| 2673 | // @see bug/4455071 |
| 2674 | /** Notify TetheringService if interface name has been changed. */ |
| 2675 | if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(), |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 2676 | PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) { |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2677 | if (isTetheringSupported()) { |
| 2678 | mTethering.handleTetherIfaceChange(); |
| 2679 | } |
| 2680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | } |
| 2682 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2683 | /** |
| 2684 | * Add and remove routes using the old properties (null if not previously connected), |
| 2685 | * new properties (null if becoming disconnected). May even be double null, which |
| 2686 | * is a noop. |
| 2687 | * Uses isLinkDefault to determine if default routes should be set or conversely if |
| 2688 | * host routes should be set to the dns servers |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2689 | * returns a boolean indicating the routes changed |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2690 | */ |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2691 | private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2692 | boolean isLinkDefault, boolean exempt, int netId) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2693 | Collection<RouteInfo> routesToAdd = null; |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2694 | CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>(); |
| 2695 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2696 | if (curLp != null) { |
| 2697 | // check for the delta between the current set and the new |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 2698 | routeDiff = curLp.compareAllRoutes(newLp); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2699 | dnsDiff = curLp.compareDnses(newLp); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2700 | } else if (newLp != null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 2701 | routeDiff.added = newLp.getAllRoutes(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2702 | dnsDiff.added = newLp.getDnses(); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2703 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2704 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2705 | boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0); |
| 2706 | |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2707 | for (RouteInfo r : routeDiff.removed) { |
| 2708 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2709 | if (VDBG) log("updateRoutes: default remove route r=" + r); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2710 | removeRoute(curLp, r, TO_DEFAULT_TABLE, netId); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 2711 | } |
| 2712 | if (isLinkDefault == false) { |
| 2713 | // remove from a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2714 | removeRoute(curLp, r, TO_SECONDARY_TABLE, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2715 | } |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2716 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2717 | |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2718 | for (RouteInfo r : routeDiff.added) { |
| 2719 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2720 | addRoute(newLp, r, TO_DEFAULT_TABLE, exempt, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2721 | } else { |
| 2722 | // add to a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2723 | addRoute(newLp, r, TO_SECONDARY_TABLE, UNEXEMPT, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2724 | |
| 2725 | // many radios add a default route even when we don't want one. |
| 2726 | // remove the default route unless somebody else has asked for it |
| 2727 | String ifaceName = newLp.getInterfaceName(); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2728 | synchronized (mRoutesLock) { |
| 2729 | if (!TextUtils.isEmpty(ifaceName) && !mAddedRoutes.contains(r)) { |
| 2730 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 2731 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2732 | mNetd.removeRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2733 | } catch (Exception e) { |
| 2734 | // never crash - catch them all |
| 2735 | if (DBG) loge("Exception trying to remove a route: " + e); |
| 2736 | } |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2742 | return routesChanged; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2745 | /** |
| 2746 | * Reads the network specific MTU size from reources. |
| 2747 | * and set it on it's iface. |
| 2748 | */ |
| 2749 | private void updateMtuSizeSettings(NetworkStateTracker nt) { |
| 2750 | final String iface = nt.getLinkProperties().getInterfaceName(); |
| 2751 | final int mtu = nt.getLinkProperties().getMtu(); |
| 2752 | |
| 2753 | if (mtu < 68 || mtu > 10000) { |
Patrick Tjin | a45610f | 2014-02-19 11:28:08 -0800 | [diff] [blame] | 2754 | loge("Unexpected mtu value: " + mtu + ", " + nt); |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2755 | return; |
| 2756 | } |
| 2757 | |
| 2758 | try { |
| 2759 | if (VDBG) log("Setting MTU size: " + iface + ", " + mtu); |
| 2760 | mNetd.setMtu(iface, mtu); |
| 2761 | } catch (Exception e) { |
| 2762 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2763 | } |
| 2764 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2765 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2766 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2767 | * Reads the network specific TCP buffer sizes from SystemProperties |
| 2768 | * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system |
| 2769 | * wide use |
| 2770 | */ |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2771 | private void updateNetworkSettings(NetworkStateTracker nt) { |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2772 | String key = nt.getTcpBufferSizesPropName(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2773 | String bufferSizes = key == null ? null : SystemProperties.get(key); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2774 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2775 | if (TextUtils.isEmpty(bufferSizes)) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2776 | if (VDBG) log(key + " not found in system properties. Using defaults"); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2777 | |
| 2778 | // Setting to default values so we won't be stuck to previous values |
| 2779 | key = "net.tcp.buffersize.default"; |
| 2780 | bufferSizes = SystemProperties.get(key); |
| 2781 | } |
| 2782 | |
| 2783 | // Set values in kernel |
| 2784 | if (bufferSizes.length() != 0) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2785 | if (VDBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2786 | log("Setting TCP values: [" + bufferSizes |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2787 | + "] which comes from [" + key + "]"); |
| 2788 | } |
| 2789 | setBufferSize(bufferSizes); |
| 2790 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2791 | |
| 2792 | final String defaultRwndKey = "net.tcp.default_init_rwnd"; |
| 2793 | int defaultRwndValue = SystemProperties.getInt(defaultRwndKey, 0); |
| 2794 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 2795 | Settings.Global.TCP_DEFAULT_INIT_RWND, defaultRwndValue); |
| 2796 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2797 | if (rwndValue != 0) { |
| 2798 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 2799 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2802 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2803 | * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max] |
| 2804 | * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem |
| 2805 | * |
| 2806 | * @param bufferSizes in the format of "readMin, readInitial, readMax, |
| 2807 | * writeMin, writeInitial, writeMax" |
| 2808 | */ |
| 2809 | private void setBufferSize(String bufferSizes) { |
| 2810 | try { |
| 2811 | String[] values = bufferSizes.split(","); |
| 2812 | |
| 2813 | if (values.length == 6) { |
| 2814 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 2815 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 2816 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 2817 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 2818 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 2819 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 2820 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2821 | } else { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2822 | loge("Invalid buffersize string: " + bufferSizes); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2823 | } |
| 2824 | } catch (IOException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2825 | loge("Can't set tcp buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2826 | } |
| 2827 | } |
| 2828 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2829 | /** |
| 2830 | * Adjust the per-process dns entries (net.dns<x>.<pid>) based |
| 2831 | * on the highest priority active net which this process requested. |
| 2832 | * If there aren't any, clear it out |
| 2833 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2834 | private void reassessPidDns(int pid, boolean doBump) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2835 | { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2836 | if (VDBG) log("reassessPidDns for pid " + pid); |
| 2837 | Integer myPid = new Integer(pid); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2838 | for(int i : mPriorityList) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2839 | if (mNetConfigs[i].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2840 | continue; |
| 2841 | } |
| 2842 | NetworkStateTracker nt = mNetTrackers[i]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2843 | if (nt.getNetworkInfo().isConnected() && |
| 2844 | !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2845 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 2846 | if (p == null) continue; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2847 | if (mNetRequestersPids[i].contains(myPid)) { |
| 2848 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2849 | // TODO: Reimplement this via local variable in bionic. |
| 2850 | // mNetd.setDnsNetworkForPid(nt.getNetwork().netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2851 | } catch (Exception e) { |
| 2852 | Slog.e(TAG, "exception reasseses pid dns: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2853 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2854 | return; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | } |
| 2858 | // nothing found - delete |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2859 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2860 | // TODO: Reimplement this via local variable in bionic. |
| 2861 | // mNetd.clearDnsNetworkForPid(pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2862 | } catch (Exception e) { |
| 2863 | Slog.e(TAG, "exception clear interface from pid: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2867 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 2868 | /* |
| 2869 | * Tell the VMs to toss their DNS caches |
| 2870 | */ |
| 2871 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 2872 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 2873 | /* |
| 2874 | * Connectivity events can happen before boot has completed ... |
| 2875 | */ |
| 2876 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2877 | final long ident = Binder.clearCallingIdentity(); |
| 2878 | try { |
| 2879 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2880 | } finally { |
| 2881 | Binder.restoreCallingIdentity(ident); |
| 2882 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2885 | // Caller must grab mDnsLock. |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2886 | private void updateDnsLocked(String network, int netId, |
| 2887 | Collection<InetAddress> dnses, String domains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2888 | int last = 0; |
| 2889 | if (dnses.size() == 0 && mDefaultDns != null) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2890 | dnses = new ArrayList(); |
| 2891 | dnses.add(mDefaultDns); |
| 2892 | if (DBG) { |
| 2893 | loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2894 | } |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2897 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2898 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), domains); |
Chad Brubaker | 20a7e88 | 2013-07-23 17:13:36 -0700 | [diff] [blame] | 2899 | |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 2900 | for (InetAddress dns : dnses) { |
| 2901 | ++last; |
| 2902 | String key = "net.dns" + last; |
| 2903 | String value = dns.getHostAddress(); |
| 2904 | SystemProperties.set(key, value); |
| 2905 | } |
| 2906 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 2907 | String key = "net.dns" + i; |
| 2908 | SystemProperties.set(key, ""); |
| 2909 | } |
| 2910 | mNumDnsEntries = last; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2911 | } catch (Exception e) { |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 2912 | loge("exception setting default dns interface: " + e); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2913 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2914 | } |
| 2915 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2916 | private void handleDnsConfigurationChange(int netType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2917 | // add default net's dns entries |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2918 | NetworkStateTracker nt = mNetTrackers[netType]; |
| 2919 | if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2920 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2921 | if (p == null) return; |
| 2922 | Collection<InetAddress> dnses = p.getDnses(); |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2923 | int netId = nt.getNetwork().netId; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2924 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2925 | String network = nt.getNetworkInfo().getTypeName(); |
| 2926 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2927 | updateDnsLocked(network, netId, dnses, p.getDomains()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2928 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2929 | } else { |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2930 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2931 | mNetd.setDnsServersForNetwork(netId, |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 2932 | NetworkUtils.makeStrings(dnses), p.getDomains()); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2933 | } catch (Exception e) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 2934 | if (DBG) loge("exception setting dns servers: " + e); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2935 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2936 | // set per-pid dns for attached secondary nets |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2937 | List<Integer> pids = mNetRequestersPids[netType]; |
| 2938 | for (Integer pid : pids) { |
| 2939 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2940 | // TODO: Reimplement this via local variable in bionic. |
| 2941 | // mNetd.setDnsNetworkForPid(netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2942 | } catch (Exception e) { |
| 2943 | Slog.e(TAG, "exception setting interface for pid: " + e); |
| 2944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2945 | } |
| 2946 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2947 | flushVmDnsCache(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2948 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2951 | private int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2952 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 2953 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2954 | if(restoreDefaultNetworkDelayStr != null && |
| 2955 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2956 | try { |
| 2957 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 2958 | } catch (NumberFormatException e) { |
| 2959 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2960 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2961 | // if the system property isn't set, use the value for the apn type |
| 2962 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2963 | |
| 2964 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2965 | (mNetConfigs[networkType] != null)) { |
| 2966 | ret = mNetConfigs[networkType].restoreTime; |
| 2967 | } |
| 2968 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2972 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2973 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2974 | if (mContext.checkCallingOrSelfPermission( |
| 2975 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2976 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2977 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 2978 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 2979 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2980 | return; |
| 2981 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2982 | |
| 2983 | // TODO: add locking to get atomic snapshot |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2984 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2985 | for (int i = 0; i < mNetTrackers.length; i++) { |
| 2986 | final NetworkStateTracker nst = mNetTrackers[i]; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2987 | if (nst != null) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2988 | pw.println("NetworkStateTracker for " + getNetworkTypeName(i) + ":"); |
| 2989 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2990 | if (nst.getNetworkInfo().isConnected()) { |
| 2991 | pw.println("Active network: " + nst.getNetworkInfo(). |
| 2992 | getTypeName()); |
| 2993 | } |
| 2994 | pw.println(nst.getNetworkInfo()); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2995 | pw.println(nst.getLinkProperties()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2996 | pw.println(nst); |
| 2997 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2998 | pw.decreaseIndent(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2999 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3000 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3001 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 3002 | pw.print("Active default network: "); pw.println(getNetworkTypeName(mActiveDefaultNetwork)); |
| 3003 | pw.println(); |
| 3004 | |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3005 | pw.println("Network Requester Pids:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3006 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3007 | for (int net : mPriorityList) { |
| 3008 | String pidString = net + ": "; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 3009 | for (Integer pid : mNetRequestersPids[net]) { |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3010 | pidString = pidString + pid.toString() + ", "; |
| 3011 | } |
| 3012 | pw.println(pidString); |
| 3013 | } |
| 3014 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3015 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3016 | |
| 3017 | pw.println("FeatureUsers:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3018 | pw.increaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3019 | for (Object requester : mFeatureUsers) { |
| 3020 | pw.println(requester.toString()); |
| 3021 | } |
| 3022 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3023 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3024 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3025 | synchronized (this) { |
| 3026 | pw.println("NetworkTranstionWakeLock is currently " + |
| 3027 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 3028 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 3029 | } |
| 3030 | pw.println(); |
| 3031 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3032 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3033 | |
| 3034 | if (mInetLog != null) { |
| 3035 | pw.println(); |
| 3036 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3037 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3038 | for(int i = 0; i < mInetLog.size(); i++) { |
| 3039 | pw.println(mInetLog.get(i)); |
| 3040 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3041 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3042 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3043 | } |
| 3044 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 3045 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3046 | private class NetworkStateTrackerHandler extends Handler { |
| 3047 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 3048 | super(looper); |
| 3049 | } |
| 3050 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3051 | @Override |
| 3052 | public void handleMessage(Message msg) { |
| 3053 | NetworkInfo info; |
| 3054 | switch (msg.what) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3055 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
| 3056 | AsyncChannel ac = (AsyncChannel) msg.obj; |
| 3057 | if (mNetworkFactories.contains(ac)) { |
| 3058 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 3059 | if (VDBG) log("NetworkFactory connected"); |
| 3060 | for (int i = 0; i < mNetworkRequests.size(); i++) { |
| 3061 | ac.sendMessage(NetworkFactoryProtocol.CMD_REQUEST_NETWORK, |
| 3062 | mNetworkRequests.valueAt(i)); |
| 3063 | } |
| 3064 | } else { |
| 3065 | loge("Error connecting NetworkFactory"); |
| 3066 | mNetworkFactories.remove((AsyncChannel) msg.obj); |
| 3067 | } |
| 3068 | } |
| 3069 | break; |
| 3070 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3071 | case NetworkStateTracker.EVENT_STATE_CHANGED: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3072 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3073 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3074 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3075 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 3076 | (state == NetworkInfo.State.DISCONNECTED) || |
| 3077 | (state == NetworkInfo.State.SUSPENDED)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3078 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3079 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3080 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3081 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3082 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3083 | // Since mobile has the notion of a network/apn that can be used for |
| 3084 | // provisioning we need to check every time we're connected as |
| 3085 | // CaptiveProtalTracker won't detected it because DCT doesn't report it |
| 3086 | // as connected as ACTION_ANY_DATA_CONNECTION_STATE_CHANGED instead its |
| 3087 | // reported as ACTION_DATA_CONNECTION_CONNECTED_TO_PROVISIONING_APN. Which |
| 3088 | // is received by MDST and sent here as EVENT_STATE_CHANGED. |
| 3089 | if (ConnectivityManager.isNetworkTypeMobile(info.getType()) |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3090 | && (0 != Settings.Global.getInt(mContext.getContentResolver(), |
| 3091 | Settings.Global.DEVICE_PROVISIONED, 0)) |
Wink Saville | 127beff | 2013-09-14 09:04:53 -0700 | [diff] [blame] | 3092 | && (((state == NetworkInfo.State.CONNECTED) |
| 3093 | && (info.getType() == ConnectivityManager.TYPE_MOBILE)) |
| 3094 | || info.isConnectedToProvisioningNetwork())) { |
| 3095 | log("ConnectivityChange checkMobileProvisioning for" |
| 3096 | + " TYPE_MOBILE or ProvisioningNetwork"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3097 | checkMobileProvisioning(CheckMp.MAX_TIMEOUT_MS); |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 3100 | EventLogTags.writeConnectivityStateChanged( |
| 3101 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3102 | |
| 3103 | if (info.getDetailedState() == |
| 3104 | NetworkInfo.DetailedState.FAILED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | handleConnectionFailure(info); |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 3106 | } else if (info.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3107 | /** |
| 3108 | * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING |
| 3109 | * for now its an in between network, its a network that |
| 3110 | * is actually a default network but we don't want it to be |
| 3111 | * announced as such to keep background applications from |
| 3112 | * trying to use it. It turns out that some still try so we |
| 3113 | * take the additional step of clearing any default routes |
| 3114 | * to the link that may have incorrectly setup by the lower |
| 3115 | * levels. |
| 3116 | */ |
| 3117 | LinkProperties lp = getLinkProperties(info.getType()); |
| 3118 | if (DBG) { |
| 3119 | log("EVENT_STATE_CHANGED: connected to provisioning network, lp=" + lp); |
| 3120 | } |
| 3121 | |
| 3122 | // Clear any default routes setup by the radio so |
| 3123 | // any activity by applications trying to use this |
| 3124 | // connection will fail until the provisioning network |
| 3125 | // is enabled. |
| 3126 | for (RouteInfo r : lp.getRoutes()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 3127 | removeRoute(lp, r, TO_DEFAULT_TABLE, |
| 3128 | mNetTrackers[info.getType()].getNetwork().netId); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3129 | } |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3130 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3131 | handleDisconnect(info); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3132 | } else if (state == NetworkInfo.State.SUSPENDED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3133 | // TODO: need to think this over. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3134 | // the logic here is, handle SUSPENDED the same as |
| 3135 | // DISCONNECTED. The only difference being we are |
| 3136 | // broadcasting an intent with NetworkInfo that's |
| 3137 | // suspended. This allows the applications an |
| 3138 | // opportunity to handle DISCONNECTED and SUSPENDED |
| 3139 | // differently, or not. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3140 | handleDisconnect(info); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3141 | } else if (state == NetworkInfo.State.CONNECTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3142 | handleConnect(info); |
| 3143 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3144 | if (mLockdownTracker != null) { |
| 3145 | mLockdownTracker.onNetworkInfoChanged(info); |
| 3146 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3147 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3148 | } |
| 3149 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: { |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 3150 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 3151 | // TODO: Temporary allowing network configuration |
| 3152 | // change not resetting sockets. |
| 3153 | // @see bug/4455071 |
| 3154 | handleConnectivityChange(info.getType(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3155 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3156 | } |
| 3157 | case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: { |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3158 | info = (NetworkInfo) msg.obj; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3159 | int type = info.getType(); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 3160 | if (mNetConfigs[type].isDefault()) updateNetworkSettings(mNetTrackers[type]); |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3161 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3162 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | private class InternalHandler extends Handler { |
| 3168 | public InternalHandler(Looper looper) { |
| 3169 | super(looper); |
| 3170 | } |
| 3171 | |
| 3172 | @Override |
| 3173 | public void handleMessage(Message msg) { |
| 3174 | NetworkInfo info; |
| 3175 | switch (msg.what) { |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3176 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3177 | String causedBy = null; |
| 3178 | synchronized (ConnectivityService.this) { |
| 3179 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 3180 | mNetTransitionWakeLock.isHeld()) { |
| 3181 | mNetTransitionWakeLock.release(); |
| 3182 | causedBy = mNetTransitionWakeLockCausedBy; |
| 3183 | } |
| 3184 | } |
| 3185 | if (causedBy != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3186 | log("NetTransition Wakelock for " + causedBy + " released by timeout"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3187 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3188 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3189 | } |
| 3190 | case EVENT_RESTORE_DEFAULT_NETWORK: { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 3191 | FeatureUser u = (FeatureUser)msg.obj; |
| 3192 | u.expire(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3193 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3194 | } |
| 3195 | case EVENT_INET_CONDITION_CHANGE: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3196 | int netType = msg.arg1; |
| 3197 | int condition = msg.arg2; |
| 3198 | handleInetConditionChange(netType, condition); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3199 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3200 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3201 | case EVENT_INET_CONDITION_HOLD_END: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3202 | int netType = msg.arg1; |
| 3203 | int sequence = msg.arg2; |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3204 | handleInetConditionHoldEnd(netType, sequence); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3205 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3206 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3207 | case EVENT_SET_NETWORK_PREFERENCE: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3208 | int preference = msg.arg1; |
| 3209 | handleSetNetworkPreference(preference); |
| 3210 | break; |
| 3211 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3212 | case EVENT_SET_MOBILE_DATA: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3213 | boolean enabled = (msg.arg1 == ENABLED); |
| 3214 | handleSetMobileData(enabled); |
| 3215 | break; |
| 3216 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3217 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3218 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3219 | break; |
| 3220 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3221 | case EVENT_SET_DEPENDENCY_MET: { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3222 | boolean met = (msg.arg1 == ENABLED); |
| 3223 | handleSetDependencyMet(msg.arg2, met); |
| 3224 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3225 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3226 | case EVENT_SEND_STICKY_BROADCAST_INTENT: { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3227 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3228 | sendStickyBroadcast(intent); |
| 3229 | break; |
| 3230 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3231 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 3232 | final int networkType = msg.arg1; |
| 3233 | final boolean enabled = msg.arg2 == ENABLED; |
| 3234 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3235 | break; |
| 3236 | } |
| 3237 | case EVENT_VPN_STATE_CHANGED: { |
| 3238 | if (mLockdownTracker != null) { |
| 3239 | mLockdownTracker.onVpnStateChanged((NetworkInfo) msg.obj); |
| 3240 | } |
| 3241 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3242 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3243 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 3244 | int tag = mEnableFailFastMobileDataTag.get(); |
| 3245 | if (msg.arg1 == tag) { |
| 3246 | MobileDataStateTracker mobileDst = |
| 3247 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 3248 | if (mobileDst != null) { |
| 3249 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 3250 | } |
| 3251 | } else { |
| 3252 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 3253 | + " != tag:" + tag); |
| 3254 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3255 | break; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3256 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3257 | case EVENT_SAMPLE_INTERVAL_ELAPSED: { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3258 | handleNetworkSamplingTimeout(); |
| 3259 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3260 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3261 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3262 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3263 | break; |
| 3264 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3265 | case EVENT_REGISTER_NETWORK_FACTORY: { |
| 3266 | handleRegisterNetworkFactory((Messenger)msg.obj); |
| 3267 | break; |
| 3268 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3269 | } |
| 3270 | } |
| 3271 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3272 | |
| 3273 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3274 | public int tether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3275 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3276 | |
| 3277 | if (isTetheringSupported()) { |
| 3278 | return mTethering.tether(iface); |
| 3279 | } else { |
| 3280 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3281 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3285 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3286 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3287 | |
| 3288 | if (isTetheringSupported()) { |
| 3289 | return mTethering.untether(iface); |
| 3290 | } else { |
| 3291 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | // javadoc from interface |
| 3296 | public int getLastTetherError(String iface) { |
| 3297 | enforceTetherAccessPermission(); |
| 3298 | |
| 3299 | if (isTetheringSupported()) { |
| 3300 | return mTethering.getLastTetherError(iface); |
| 3301 | } else { |
| 3302 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3303 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | // TODO - proper iface API for selection by property, inspection, etc |
| 3307 | public String[] getTetherableUsbRegexs() { |
| 3308 | enforceTetherAccessPermission(); |
| 3309 | if (isTetheringSupported()) { |
| 3310 | return mTethering.getTetherableUsbRegexs(); |
| 3311 | } else { |
| 3312 | return new String[0]; |
| 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | public String[] getTetherableWifiRegexs() { |
| 3317 | enforceTetherAccessPermission(); |
| 3318 | if (isTetheringSupported()) { |
| 3319 | return mTethering.getTetherableWifiRegexs(); |
| 3320 | } else { |
| 3321 | return new String[0]; |
| 3322 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3323 | } |
| 3324 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3325 | public String[] getTetherableBluetoothRegexs() { |
| 3326 | enforceTetherAccessPermission(); |
| 3327 | if (isTetheringSupported()) { |
| 3328 | return mTethering.getTetherableBluetoothRegexs(); |
| 3329 | } else { |
| 3330 | return new String[0]; |
| 3331 | } |
| 3332 | } |
| 3333 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3334 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3335 | enforceTetherChangePermission(); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3336 | if (isTetheringSupported()) { |
| 3337 | return mTethering.setUsbTethering(enable); |
| 3338 | } else { |
| 3339 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3340 | } |
| 3341 | } |
| 3342 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3343 | // TODO - move iface listing, queries, etc to new module |
| 3344 | // javadoc from interface |
| 3345 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3346 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3347 | return mTethering.getTetherableIfaces(); |
| 3348 | } |
| 3349 | |
| 3350 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3351 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3352 | return mTethering.getTetheredIfaces(); |
| 3353 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3354 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3355 | public String[] getTetheringErroredIfaces() { |
| 3356 | enforceTetherAccessPermission(); |
| 3357 | return mTethering.getErroredIfaces(); |
| 3358 | } |
| 3359 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3360 | // if ro.tether.denied = true we default to no tethering |
| 3361 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3362 | // had previously been turned off. |
| 3363 | public boolean isTetheringSupported() { |
| 3364 | enforceTetherAccessPermission(); |
| 3365 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 3366 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
| 3367 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 3368 | return tetherEnabledInSettings && ((mTethering.getTetherableUsbRegexs().length != 0 || |
| 3369 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 3370 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 3371 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3372 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3373 | |
| 3374 | // An API NetworkStateTrackers can call when they lose their network. |
| 3375 | // This will automatically be cleared after X seconds or a network becomes CONNECTED, |
| 3376 | // whichever happens first. The timer is started by the first caller and not |
| 3377 | // restarted by subsequent callers. |
| 3378 | public void requestNetworkTransitionWakelock(String forWhom) { |
| 3379 | enforceConnectivityInternalPermission(); |
| 3380 | synchronized (this) { |
| 3381 | if (mNetTransitionWakeLock.isHeld()) return; |
| 3382 | mNetTransitionWakeLockSerialNumber++; |
| 3383 | mNetTransitionWakeLock.acquire(); |
| 3384 | mNetTransitionWakeLockCausedBy = forWhom; |
| 3385 | } |
| 3386 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 3387 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3388 | mNetTransitionWakeLockSerialNumber, 0), |
| 3389 | mNetTransitionWakeLockTimeout); |
| 3390 | return; |
| 3391 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3392 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3393 | // 100 percent is full good, 0 is full bad. |
| 3394 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3395 | if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")"); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3396 | mContext.enforceCallingOrSelfPermission( |
| 3397 | android.Manifest.permission.STATUS_BAR, |
| 3398 | "ConnectivityService"); |
| 3399 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3400 | if (DBG) { |
| 3401 | int pid = getCallingPid(); |
| 3402 | int uid = getCallingUid(); |
| 3403 | String s = pid + "(" + uid + ") reports inet is " + |
| 3404 | (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " + |
| 3405 | "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime(); |
| 3406 | mInetLog.add(s); |
| 3407 | while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) { |
| 3408 | mInetLog.remove(0); |
| 3409 | } |
| 3410 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3411 | mHandler.sendMessage(mHandler.obtainMessage( |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3412 | EVENT_INET_CONDITION_CHANGE, networkType, percentage)); |
| 3413 | } |
| 3414 | |
| 3415 | private void handleInetConditionChange(int netType, int condition) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3416 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3417 | if (DBG) log("handleInetConditionChange: no active default network - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3418 | return; |
| 3419 | } |
| 3420 | if (mActiveDefaultNetwork != netType) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3421 | if (DBG) log("handleInetConditionChange: net=" + netType + |
| 3422 | " != default=" + mActiveDefaultNetwork + " - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3423 | return; |
| 3424 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3425 | if (VDBG) { |
| 3426 | log("handleInetConditionChange: net=" + |
| 3427 | netType + ", condition=" + condition + |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3428 | ",mActiveDefaultNetwork=" + mActiveDefaultNetwork); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3429 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3430 | mDefaultInetCondition = condition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3431 | int delay; |
| 3432 | if (mInetConditionChangeInFlight == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3433 | if (VDBG) log("handleInetConditionChange: starting a change hold"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3434 | // setup a new hold to debounce this |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3435 | if (mDefaultInetCondition > 50) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3436 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3437 | Settings.Global.INET_CONDITION_DEBOUNCE_UP_DELAY, 500); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3438 | } else { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3439 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3440 | Settings.Global.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3441 | } |
| 3442 | mInetConditionChangeInFlight = true; |
| 3443 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END, |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3444 | mActiveDefaultNetwork, mDefaultConnectionSequence), delay); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3445 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3446 | // we've set the new condition, when this hold ends that will get picked up |
| 3447 | if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3451 | private void handleInetConditionHoldEnd(int netType, int sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3452 | if (DBG) { |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3453 | log("handleInetConditionHoldEnd: net=" + netType + |
| 3454 | ", condition=" + mDefaultInetCondition + |
| 3455 | ", published condition=" + mDefaultInetConditionPublished); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3456 | } |
| 3457 | mInetConditionChangeInFlight = false; |
| 3458 | |
| 3459 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3460 | if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3461 | return; |
| 3462 | } |
| 3463 | if (mDefaultConnectionSequence != sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3464 | if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3465 | return; |
| 3466 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3467 | // TODO: Figure out why this optimization sometimes causes a |
| 3468 | // change in mDefaultInetCondition to be missed and the |
| 3469 | // UI to not be updated. |
| 3470 | //if (mDefaultInetConditionPublished == mDefaultInetCondition) { |
| 3471 | // if (DBG) log("no change in condition - aborting"); |
| 3472 | // return; |
| 3473 | //} |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3474 | NetworkInfo networkInfo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
| 3475 | if (networkInfo.isConnected() == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3476 | if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3477 | return; |
| 3478 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3479 | mDefaultInetConditionPublished = mDefaultInetCondition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3480 | sendInetConditionBroadcast(networkInfo); |
| 3481 | return; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3482 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3483 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3484 | public ProxyInfo getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3485 | // this information is already available as a world read/writable jvm property |
| 3486 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3487 | // of proxy info to all the JVMs. |
| 3488 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3489 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3490 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3491 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 3492 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3493 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3496 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3497 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3498 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3499 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3500 | if (proxyProperties == mGlobalProxy) return; |
| 3501 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 3502 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 3503 | |
| 3504 | String host = ""; |
| 3505 | int port = 0; |
| 3506 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3507 | String pacFileUrl = ""; |
| 3508 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3509 | (proxyProperties.getPacFileUrl() != null))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3510 | if (!proxyProperties.isValid()) { |
| 3511 | if (DBG) |
| 3512 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3513 | return; |
| 3514 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3515 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3516 | host = mGlobalProxy.getHost(); |
| 3517 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3518 | exclList = mGlobalProxy.getExclusionListAsString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3519 | if (proxyProperties.getPacFileUrl() != null) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3520 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3521 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3522 | } else { |
| 3523 | mGlobalProxy = null; |
| 3524 | } |
| 3525 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3526 | final long token = Binder.clearCallingIdentity(); |
| 3527 | try { |
| 3528 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 3529 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 3530 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 3531 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3532 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3533 | } finally { |
| 3534 | Binder.restoreCallingIdentity(token); |
| 3535 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3536 | } |
| 3537 | |
| 3538 | if (mGlobalProxy == null) { |
| 3539 | proxyProperties = mDefaultProxy; |
| 3540 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3541 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3542 | } |
| 3543 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3544 | private void loadGlobalProxy() { |
| 3545 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3546 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 3547 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 3548 | String exclList = Settings.Global.getString(res, |
| 3549 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3550 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 3551 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3552 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3553 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3554 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3555 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3556 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3557 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3558 | if (!proxyProperties.isValid()) { |
| 3559 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3560 | return; |
| 3561 | } |
| 3562 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3563 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3564 | mGlobalProxy = proxyProperties; |
| 3565 | } |
| 3566 | } |
| 3567 | } |
| 3568 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3569 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3570 | // this information is already available as a world read/writable jvm property |
| 3571 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3572 | // of proxy info to all the JVMs. |
| 3573 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3574 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3575 | return mGlobalProxy; |
| 3576 | } |
| 3577 | } |
| 3578 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3579 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3580 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3581 | && (proxy.getPacFileUrl() == null)) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3582 | proxy = null; |
| 3583 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3584 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3585 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3586 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 3587 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3588 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 3589 | return; |
| 3590 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 3591 | |
| 3592 | // This call could be coming from the PacManager, containing the port of the local |
| 3593 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 3594 | // global (to get the correct local port), and send a broadcast. |
| 3595 | // TODO: Switch PacManager to have its own message to send back rather than |
| 3596 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
| 3597 | if ((mGlobalProxy != null) && (proxy != null) && (proxy.getPacFileUrl() != null) |
| 3598 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 3599 | mGlobalProxy = proxy; |
| 3600 | sendProxyBroadcast(mGlobalProxy); |
| 3601 | return; |
| 3602 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3603 | mDefaultProxy = proxy; |
| 3604 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3605 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3606 | if (!mDefaultProxyDisabled) { |
| 3607 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3608 | } |
| 3609 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3613 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 3614 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3615 | if (!TextUtils.isEmpty(proxy)) { |
| 3616 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3617 | if (data.length == 0) { |
| 3618 | return; |
| 3619 | } |
| 3620 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3621 | String proxyHost = data[0]; |
| 3622 | int proxyPort = 8080; |
| 3623 | if (data.length > 1) { |
| 3624 | try { |
| 3625 | proxyPort = Integer.parseInt(data[1]); |
| 3626 | } catch (NumberFormatException e) { |
| 3627 | return; |
| 3628 | } |
| 3629 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3630 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3631 | setGlobalProxy(p); |
| 3632 | } |
| 3633 | } |
| 3634 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3635 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 3636 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 3637 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3638 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3639 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3640 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3641 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3642 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3643 | final long ident = Binder.clearCallingIdentity(); |
| 3644 | try { |
| 3645 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3646 | } finally { |
| 3647 | Binder.restoreCallingIdentity(ident); |
| 3648 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | private static class SettingsObserver extends ContentObserver { |
| 3652 | private int mWhat; |
| 3653 | private Handler mHandler; |
| 3654 | SettingsObserver(Handler handler, int what) { |
| 3655 | super(handler); |
| 3656 | mHandler = handler; |
| 3657 | mWhat = what; |
| 3658 | } |
| 3659 | |
| 3660 | void observe(Context context) { |
| 3661 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3662 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 3663 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | @Override |
| 3667 | public void onChange(boolean selfChange) { |
| 3668 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 3669 | } |
| 3670 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3671 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3672 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3673 | Slog.d(TAG, s); |
| 3674 | } |
| 3675 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3676 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3677 | Slog.e(TAG, s); |
| 3678 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3679 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3680 | int convertFeatureToNetworkType(int networkType, String feature) { |
| 3681 | int usedNetworkType = networkType; |
| 3682 | |
| 3683 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 3684 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 3685 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 3686 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 3687 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 3688 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) || |
| 3689 | TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 3690 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 3691 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 3692 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 3693 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) { |
| 3694 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA; |
| 3695 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) { |
| 3696 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS; |
| 3697 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) { |
| 3698 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS; |
| 3699 | } else { |
| 3700 | Slog.e(TAG, "Can't match any mobile netTracker!"); |
| 3701 | } |
| 3702 | } else if (networkType == ConnectivityManager.TYPE_WIFI) { |
| 3703 | if (TextUtils.equals(feature, "p2p")) { |
| 3704 | usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P; |
| 3705 | } else { |
| 3706 | Slog.e(TAG, "Can't match any wifi netTracker!"); |
| 3707 | } |
| 3708 | } else { |
| 3709 | Slog.e(TAG, "Unexpected network type"); |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3710 | } |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3711 | return usedNetworkType; |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3712 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3713 | |
| 3714 | private static <T> T checkNotNull(T value, String message) { |
| 3715 | if (value == null) { |
| 3716 | throw new NullPointerException(message); |
| 3717 | } |
| 3718 | return value; |
| 3719 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3720 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3721 | /** |
| 3722 | * 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] | 3723 | * VpnBuilder and not available in ConnectivityManager. Permissions |
| 3724 | * are checked in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3725 | * @hide |
| 3726 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3727 | @Override |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3728 | public boolean protectVpn(ParcelFileDescriptor socket) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3729 | throwIfLockdownEnabled(); |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3730 | try { |
| 3731 | int type = mActiveDefaultNetwork; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3732 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3733 | if (ConnectivityManager.isNetworkTypeValid(type) && mNetTrackers[type] != null) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3734 | synchronized(mVpns) { |
Chad Brubaker | bcf12b3 | 2014-02-11 14:18:56 -0800 | [diff] [blame] | 3735 | mVpns.get(user).protect(socket); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3736 | } |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3737 | return true; |
| 3738 | } |
| 3739 | } catch (Exception e) { |
| 3740 | // ignore |
| 3741 | } finally { |
| 3742 | try { |
| 3743 | socket.close(); |
| 3744 | } catch (Exception e) { |
| 3745 | // ignore |
| 3746 | } |
| 3747 | } |
| 3748 | return false; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3751 | /** |
| 3752 | * Prepare for a VPN application. This method is used by VpnDialogs |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3753 | * and not available in ConnectivityManager. Permissions are checked |
| 3754 | * in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3755 | * @hide |
| 3756 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3757 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 3758 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3759 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3760 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3761 | synchronized(mVpns) { |
| 3762 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 3763 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3764 | } |
| 3765 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 3766 | @Override |
| 3767 | public void markSocketAsUser(ParcelFileDescriptor socket, int uid) { |
| 3768 | enforceMarkNetworkSocketPermission(); |
| 3769 | final long token = Binder.clearCallingIdentity(); |
| 3770 | try { |
| 3771 | int mark = mNetd.getMarkForUid(uid); |
| 3772 | // Clear the mark on the socket if no mark is needed to prevent socket reuse issues |
| 3773 | if (mark == -1) { |
| 3774 | mark = 0; |
| 3775 | } |
| 3776 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 3777 | } catch (RemoteException e) { |
| 3778 | } finally { |
| 3779 | Binder.restoreCallingIdentity(token); |
| 3780 | } |
| 3781 | } |
| 3782 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3783 | /** |
| 3784 | * Configure a TUN interface and return its file descriptor. Parameters |
| 3785 | * 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] | 3786 | * and not available in ConnectivityManager. Permissions are checked in |
| 3787 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3788 | * @hide |
| 3789 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3790 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3791 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3792 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3793 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3794 | synchronized(mVpns) { |
| 3795 | return mVpns.get(user).establish(config); |
| 3796 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3799 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3800 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 3801 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3802 | */ |
| 3803 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3804 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3805 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3806 | final LinkProperties egress = getActiveLinkProperties(); |
| 3807 | if (egress == null) { |
| 3808 | throw new IllegalStateException("Missing active network connection"); |
| 3809 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3810 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3811 | synchronized(mVpns) { |
| 3812 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 3813 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /** |
| 3817 | * Return the information of the ongoing legacy VPN. This method is used |
| 3818 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 3819 | * are checked in Vpn class. |
| 3820 | * @hide |
| 3821 | */ |
| 3822 | @Override |
| 3823 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3824 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3825 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3826 | synchronized(mVpns) { |
| 3827 | return mVpns.get(user).getLegacyVpnInfo(); |
| 3828 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3831 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3832 | * Returns the information of the ongoing VPN. This method is used by VpnDialogs and |
| 3833 | * not available in ConnectivityManager. |
| 3834 | * Permissions are checked in Vpn class. |
| 3835 | * @hide |
| 3836 | */ |
| 3837 | @Override |
| 3838 | public VpnConfig getVpnConfig() { |
| 3839 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3840 | synchronized(mVpns) { |
| 3841 | return mVpns.get(user).getVpnConfig(); |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | /** |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3846 | * Callback for VPN subsystem. Currently VPN is not adapted to the service |
| 3847 | * through NetworkStateTracker since it works differently. For example, it |
| 3848 | * needs to override DNS servers but never takes the default routes. It |
| 3849 | * relies on another data network, and it could keep existing connections |
| 3850 | * alive after reconnecting, switching between networks, or even resuming |
| 3851 | * from deep sleep. Calls from applications should be done synchronously |
| 3852 | * to avoid race conditions. As these are all hidden APIs, refactoring can |
| 3853 | * be done whenever a better abstraction is developed. |
| 3854 | */ |
| 3855 | public class VpnCallback { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3856 | private VpnCallback() { |
| 3857 | } |
| 3858 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3859 | public void onStateChanged(NetworkInfo info) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3860 | mHandler.obtainMessage(EVENT_VPN_STATE_CHANGED, info).sendToTarget(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 3861 | } |
| 3862 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3863 | public void override(String iface, List<String> dnsServers, List<String> searchDomains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3864 | if (dnsServers == null) { |
| 3865 | restore(); |
| 3866 | return; |
| 3867 | } |
| 3868 | |
| 3869 | // Convert DNS servers into addresses. |
| 3870 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 3871 | for (String address : dnsServers) { |
| 3872 | // Double check the addresses and remove invalid ones. |
| 3873 | try { |
| 3874 | addresses.add(InetAddress.parseNumericAddress(address)); |
| 3875 | } catch (Exception e) { |
| 3876 | // ignore |
| 3877 | } |
| 3878 | } |
| 3879 | if (addresses.isEmpty()) { |
| 3880 | restore(); |
| 3881 | return; |
| 3882 | } |
| 3883 | |
| 3884 | // Concatenate search domains into a string. |
| 3885 | StringBuilder buffer = new StringBuilder(); |
| 3886 | if (searchDomains != null) { |
| 3887 | for (String domain : searchDomains) { |
| 3888 | buffer.append(domain).append(' '); |
| 3889 | } |
| 3890 | } |
| 3891 | String domains = buffer.toString().trim(); |
| 3892 | |
| 3893 | // Apply DNS changes. |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3894 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 3895 | // TODO: Re-enable this when the netId of the VPN is known. |
| 3896 | // updateDnsLocked("VPN", netId, addresses, domains); |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3897 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3898 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3899 | // Temporarily disable the default proxy (not global). |
| 3900 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3901 | mDefaultProxyDisabled = true; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3902 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3903 | sendProxyBroadcast(null); |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | // TODO: support proxy per network. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 3910 | public void restore() { |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3911 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3912 | mDefaultProxyDisabled = false; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3913 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3914 | sendProxyBroadcast(mDefaultProxy); |
| 3915 | } |
| 3916 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3917 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3918 | |
| 3919 | public void protect(ParcelFileDescriptor socket) { |
| 3920 | try { |
| 3921 | final int mark = mNetd.getMarkForProtect(); |
| 3922 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 3923 | } catch (RemoteException e) { |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | public void setRoutes(String interfaze, List<RouteInfo> routes) { |
| 3928 | for (RouteInfo route : routes) { |
| 3929 | try { |
| 3930 | mNetd.setMarkedForwardingRoute(interfaze, route); |
| 3931 | } catch (RemoteException e) { |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | public void setMarkedForwarding(String interfaze) { |
| 3937 | try { |
| 3938 | mNetd.setMarkedForwarding(interfaze); |
| 3939 | } catch (RemoteException e) { |
| 3940 | } |
| 3941 | } |
| 3942 | |
| 3943 | public void clearMarkedForwarding(String interfaze) { |
| 3944 | try { |
| 3945 | mNetd.clearMarkedForwarding(interfaze); |
| 3946 | } catch (RemoteException e) { |
| 3947 | } |
| 3948 | } |
| 3949 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3950 | public void addUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3951 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 3952 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3953 | addUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3954 | } |
| 3955 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3956 | public void clearUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3957 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 3958 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3959 | clearUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3960 | } |
| 3961 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3962 | public void addUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 3963 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 3964 | // TODO: Re-enable this when the netId of the VPN is known. |
| 3965 | // try { |
| 3966 | // mNetd.setUidRangeRoute(netId, uidStart, uidEnd, forwardDns); |
| 3967 | // } catch (RemoteException e) { |
| 3968 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3969 | |
| 3970 | } |
| 3971 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 3972 | public void clearUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 3973 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 3974 | // TODO: Re-enable this when the netId of the VPN is known. |
| 3975 | // try { |
| 3976 | // mNetd.clearUidRangeRoute(interfaze, uidStart, uidEnd); |
| 3977 | // } catch (RemoteException e) { |
| 3978 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3979 | |
| 3980 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3981 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3982 | |
| 3983 | @Override |
| 3984 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 3985 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 3986 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 3987 | return false; |
| 3988 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3989 | |
| 3990 | // Tear down existing lockdown if profile was removed |
| 3991 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 3992 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 3993 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3994 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 3995 | return false; |
| 3996 | } |
| 3997 | |
| 3998 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 3999 | final VpnProfile profile = VpnProfile.decode( |
| 4000 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4001 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4002 | synchronized(mVpns) { |
| 4003 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 4004 | profile)); |
| 4005 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4006 | } else { |
| 4007 | setLockdownTracker(null); |
| 4008 | } |
| 4009 | |
| 4010 | return true; |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4015 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4016 | */ |
| 4017 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4018 | // Shutdown any existing tracker |
| 4019 | final LockdownVpnTracker existing = mLockdownTracker; |
| 4020 | mLockdownTracker = null; |
| 4021 | if (existing != null) { |
| 4022 | existing.shutdown(); |
| 4023 | } |
| 4024 | |
| 4025 | try { |
| 4026 | if (tracker != null) { |
| 4027 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 4028 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4029 | mLockdownTracker = tracker; |
| 4030 | mLockdownTracker.init(); |
| 4031 | } else { |
| 4032 | mNetd.setFirewallEnabled(false); |
| 4033 | } |
| 4034 | } catch (RemoteException e) { |
| 4035 | // ignored; NMS lives inside system_server |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | private void throwIfLockdownEnabled() { |
| 4040 | if (mLockdownEnabled) { |
| 4041 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4042 | } |
| 4043 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4044 | |
| 4045 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 4046 | enforceConnectivityInternalPermission(); |
| 4047 | |
| 4048 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 4049 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 4050 | } |
| 4051 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 4052 | |
| 4053 | public int findConnectionTypeForIface(String iface) { |
| 4054 | enforceConnectivityInternalPermission(); |
| 4055 | |
| 4056 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
| 4057 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 4058 | if (tracker != null) { |
| 4059 | LinkProperties lp = tracker.getLinkProperties(); |
| 4060 | if (lp != null && iface.equals(lp.getInterfaceName())) { |
| 4061 | return tracker.getNetworkInfo().getType(); |
| 4062 | } |
| 4063 | } |
| 4064 | } |
| 4065 | return ConnectivityManager.TYPE_NONE; |
| 4066 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4067 | |
| 4068 | /** |
| 4069 | * Have mobile data fail fast if enabled. |
| 4070 | * |
| 4071 | * @param enabled DctConstants.ENABLED/DISABLED |
| 4072 | */ |
| 4073 | private void setEnableFailFastMobileData(int enabled) { |
| 4074 | int tag; |
| 4075 | |
| 4076 | if (enabled == DctConstants.ENABLED) { |
| 4077 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 4078 | } else { |
| 4079 | tag = mEnableFailFastMobileDataTag.get(); |
| 4080 | } |
| 4081 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 4082 | enabled)); |
| 4083 | } |
| 4084 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4085 | private boolean isMobileDataStateTrackerReady() { |
| 4086 | MobileDataStateTracker mdst = |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4087 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4088 | return (mdst != null) && (mdst.isReady()); |
| 4089 | } |
| 4090 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4091 | /** |
| 4092 | * The ResultReceiver resultCode for checkMobileProvisioning (CMP_RESULT_CODE) |
| 4093 | */ |
| 4094 | |
| 4095 | /** |
| 4096 | * No connection was possible to the network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4097 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4098 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4099 | private static final int CMP_RESULT_CODE_NO_CONNECTION = 0; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4100 | |
| 4101 | /** |
| 4102 | * A connection was made to the internet, all is well. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4103 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4104 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4105 | private static final int CMP_RESULT_CODE_CONNECTABLE = 1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4106 | |
| 4107 | /** |
| 4108 | * 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] | 4109 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4110 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4111 | private static final int CMP_RESULT_CODE_NO_DNS = 2; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4112 | |
| 4113 | /** |
| 4114 | * A connection was made but could not open a TCP connection. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4115 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4116 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4117 | private static final int CMP_RESULT_CODE_NO_TCP_CONNECTION = 3; |
| 4118 | |
| 4119 | /** |
| 4120 | * A connection was made but there was a redirection, we appear to be in walled garden. |
| 4121 | * This is an indication of a warm sim on a mobile network such as T-Mobile. |
| 4122 | */ |
| 4123 | private static final int CMP_RESULT_CODE_REDIRECTED = 4; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4124 | |
| 4125 | /** |
| 4126 | * The mobile network is a provisioning network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4127 | * 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] | 4128 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4129 | private static final int CMP_RESULT_CODE_PROVISIONING_NETWORK = 5; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4130 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4131 | /** |
| 4132 | * The mobile network is provisioning |
| 4133 | */ |
| 4134 | private static final int CMP_RESULT_CODE_IS_PROVISIONING = 6; |
| 4135 | |
| 4136 | private AtomicBoolean mIsProvisioningNetwork = new AtomicBoolean(false); |
| 4137 | private AtomicBoolean mIsStartingProvisioning = new AtomicBoolean(false); |
| 4138 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4139 | private AtomicBoolean mIsCheckingMobileProvisioning = new AtomicBoolean(false); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4140 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4141 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4142 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
| 4143 | int timeOutMs = -1; |
| 4144 | if (DBG) log("checkMobileProvisioning: E suggestedTimeOutMs=" + suggestedTimeOutMs); |
| 4145 | enforceConnectivityInternalPermission(); |
Wink Saville | 68e6c64 | 2013-07-02 10:55:14 -0700 | [diff] [blame] | 4146 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4147 | final long token = Binder.clearCallingIdentity(); |
| 4148 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4149 | timeOutMs = suggestedTimeOutMs; |
| 4150 | if (suggestedTimeOutMs > CheckMp.MAX_TIMEOUT_MS) { |
| 4151 | timeOutMs = CheckMp.MAX_TIMEOUT_MS; |
| 4152 | } |
| 4153 | |
| 4154 | // Check that mobile networks are supported |
| 4155 | if (!isNetworkSupported(ConnectivityManager.TYPE_MOBILE) |
| 4156 | || !isNetworkSupported(ConnectivityManager.TYPE_MOBILE_HIPRI)) { |
| 4157 | if (DBG) log("checkMobileProvisioning: X no mobile network"); |
| 4158 | return timeOutMs; |
| 4159 | } |
| 4160 | |
| 4161 | // If we're already checking don't do it again |
| 4162 | // TODO: Add a queue of results... |
| 4163 | if (mIsCheckingMobileProvisioning.getAndSet(true)) { |
| 4164 | if (DBG) log("checkMobileProvisioning: X already checking ignore for the moment"); |
| 4165 | return timeOutMs; |
| 4166 | } |
| 4167 | |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4168 | // Start off with mobile notification off |
| 4169 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4170 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4171 | CheckMp checkMp = new CheckMp(mContext, this); |
| 4172 | CheckMp.CallBack cb = new CheckMp.CallBack() { |
| 4173 | @Override |
| 4174 | void onComplete(Integer result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4175 | if (DBG) log("CheckMp.onComplete: result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4176 | NetworkInfo ni = |
| 4177 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI].getNetworkInfo(); |
| 4178 | switch(result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4179 | case CMP_RESULT_CODE_CONNECTABLE: |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4180 | case CMP_RESULT_CODE_NO_CONNECTION: |
| 4181 | case CMP_RESULT_CODE_NO_DNS: |
| 4182 | case CMP_RESULT_CODE_NO_TCP_CONNECTION: { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4183 | if (DBG) log("CheckMp.onComplete: ignore, connected or no connection"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4184 | break; |
| 4185 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4186 | case CMP_RESULT_CODE_REDIRECTED: { |
| 4187 | if (DBG) log("CheckMp.onComplete: warm sim"); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4188 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4189 | if (TextUtils.isEmpty(url)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4190 | url = getMobileRedirectedProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4191 | } |
| 4192 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4193 | if (DBG) log("CheckMp.onComplete: warm (redirected), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4194 | setProvNotificationVisible(true, |
| 4195 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4196 | url); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4197 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4198 | if (DBG) log("CheckMp.onComplete: warm (redirected), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4199 | } |
| 4200 | break; |
| 4201 | } |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4202 | case CMP_RESULT_CODE_PROVISIONING_NETWORK: { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4203 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4204 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4205 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4206 | setProvNotificationVisible(true, |
| 4207 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4208 | url); |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4209 | // Mark that we've got a provisioning network and |
| 4210 | // Disable Mobile Data until user actually starts provisioning. |
| 4211 | mIsProvisioningNetwork.set(true); |
| 4212 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4213 | mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4214 | mdst.setInternalDataEnable(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4215 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4216 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4217 | } |
| 4218 | break; |
| 4219 | } |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4220 | case CMP_RESULT_CODE_IS_PROVISIONING: { |
| 4221 | // FIXME: Need to know when provisioning is done. Probably we can |
| 4222 | // check the completion status if successful we're done if we |
| 4223 | // "timedout" or still connected to provisioning APN turn off data? |
| 4224 | if (DBG) log("CheckMp.onComplete: provisioning started"); |
| 4225 | mIsStartingProvisioning.set(false); |
| 4226 | break; |
| 4227 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4228 | default: { |
| 4229 | loge("CheckMp.onComplete: ignore unexpected result=" + result); |
| 4230 | break; |
| 4231 | } |
| 4232 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4233 | mIsCheckingMobileProvisioning.set(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4234 | } |
| 4235 | }; |
| 4236 | CheckMp.Params params = |
| 4237 | new CheckMp.Params(checkMp.getDefaultUrl(), timeOutMs, cb); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4238 | if (DBG) log("checkMobileProvisioning: params=" + params); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4239 | checkMp.execute(params); |
| 4240 | } finally { |
| 4241 | Binder.restoreCallingIdentity(token); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4242 | if (DBG) log("checkMobileProvisioning: X"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4243 | } |
| 4244 | return timeOutMs; |
| 4245 | } |
| 4246 | |
| 4247 | static class CheckMp extends |
| 4248 | AsyncTask<CheckMp.Params, Void, Integer> { |
| 4249 | private static final String CHECKMP_TAG = "CheckMp"; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4250 | |
| 4251 | // adb shell setprop persist.checkmp.testfailures 1 to enable testing failures |
| 4252 | private static boolean mTestingFailures; |
| 4253 | |
| 4254 | // Choosing 4 loops as half of them will use HTTPS and the other half HTTP |
| 4255 | private static final int MAX_LOOPS = 4; |
| 4256 | |
| 4257 | // Number of milli-seconds to complete all of the retires |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4258 | public static final int MAX_TIMEOUT_MS = 60000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4259 | |
| 4260 | // The socket should retry only 5 seconds, the default is longer |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4261 | private static final int SOCKET_TIMEOUT_MS = 5000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4262 | |
| 4263 | // Sleep time for network errors |
| 4264 | private static final int NET_ERROR_SLEEP_SEC = 3; |
| 4265 | |
| 4266 | // Sleep time for network route establishment |
| 4267 | private static final int NET_ROUTE_ESTABLISHMENT_SLEEP_SEC = 3; |
| 4268 | |
| 4269 | // Short sleep time for polling :( |
| 4270 | private static final int POLLING_SLEEP_SEC = 1; |
| 4271 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4272 | private Context mContext; |
| 4273 | private ConnectivityService mCs; |
| 4274 | private TelephonyManager mTm; |
| 4275 | private Params mParams; |
| 4276 | |
| 4277 | /** |
| 4278 | * Parameters for AsyncTask.execute |
| 4279 | */ |
| 4280 | static class Params { |
| 4281 | private String mUrl; |
| 4282 | private long mTimeOutMs; |
| 4283 | private CallBack mCb; |
| 4284 | |
| 4285 | Params(String url, long timeOutMs, CallBack cb) { |
| 4286 | mUrl = url; |
| 4287 | mTimeOutMs = timeOutMs; |
| 4288 | mCb = cb; |
| 4289 | } |
| 4290 | |
| 4291 | @Override |
| 4292 | public String toString() { |
| 4293 | return "{" + " url=" + mUrl + " mTimeOutMs=" + mTimeOutMs + " mCb=" + mCb + "}"; |
| 4294 | } |
| 4295 | } |
| 4296 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4297 | // As explained to me by Brian Carlstrom and Kenny Root, Certificates can be |
| 4298 | // issued by name or ip address, for Google its by name so when we construct |
| 4299 | // this HostnameVerifier we'll pass the original Uri and use it to verify |
| 4300 | // the host. If the host name in the original uril fails we'll test the |
| 4301 | // hostname parameter just incase things change. |
| 4302 | static class CheckMpHostnameVerifier implements HostnameVerifier { |
| 4303 | Uri mOrgUri; |
| 4304 | |
| 4305 | CheckMpHostnameVerifier(Uri orgUri) { |
| 4306 | mOrgUri = orgUri; |
| 4307 | } |
| 4308 | |
| 4309 | @Override |
| 4310 | public boolean verify(String hostname, SSLSession session) { |
| 4311 | HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); |
| 4312 | String orgUriHost = mOrgUri.getHost(); |
| 4313 | boolean retVal = hv.verify(orgUriHost, session) || hv.verify(hostname, session); |
| 4314 | if (DBG) { |
| 4315 | log("isMobileOk: hostnameVerify retVal=" + retVal + " hostname=" + hostname |
| 4316 | + " orgUriHost=" + orgUriHost); |
| 4317 | } |
| 4318 | return retVal; |
| 4319 | } |
| 4320 | } |
| 4321 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4322 | /** |
| 4323 | * The call back object passed in Params. onComplete will be called |
| 4324 | * on the main thread. |
| 4325 | */ |
| 4326 | abstract static class CallBack { |
| 4327 | // Called on the main thread. |
| 4328 | abstract void onComplete(Integer result); |
| 4329 | } |
| 4330 | |
| 4331 | public CheckMp(Context context, ConnectivityService cs) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4332 | if (Build.IS_DEBUGGABLE) { |
| 4333 | mTestingFailures = |
| 4334 | SystemProperties.getInt("persist.checkmp.testfailures", 0) == 1; |
| 4335 | } else { |
| 4336 | mTestingFailures = false; |
| 4337 | } |
| 4338 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4339 | mContext = context; |
| 4340 | mCs = cs; |
| 4341 | |
| 4342 | // Setup access to TelephonyService we'll be using. |
| 4343 | mTm = (TelephonyManager) mContext.getSystemService( |
| 4344 | Context.TELEPHONY_SERVICE); |
| 4345 | } |
| 4346 | |
| 4347 | /** |
| 4348 | * Get the default url to use for the test. |
| 4349 | */ |
| 4350 | public String getDefaultUrl() { |
| 4351 | // See http://go/clientsdns for usage approval |
| 4352 | String server = Settings.Global.getString(mContext.getContentResolver(), |
| 4353 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 4354 | if (server == null) { |
| 4355 | server = "clients3.google.com"; |
| 4356 | } |
| 4357 | return "http://" + server + "/generate_204"; |
| 4358 | } |
| 4359 | |
| 4360 | /** |
| 4361 | * Detect if its possible to connect to the http url. DNS based detection techniques |
| 4362 | * do not work at all hotspots. The best way to check is to perform a request to |
| 4363 | * a known address that fetches the data we expect. |
| 4364 | */ |
| 4365 | private synchronized Integer isMobileOk(Params params) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4366 | Integer result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4367 | Uri orgUri = Uri.parse(params.mUrl); |
| 4368 | Random rand = new Random(); |
| 4369 | mParams = params; |
| 4370 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4371 | if (mCs.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4372 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4373 | log("isMobileOk: X not mobile capable result=" + result); |
| 4374 | return result; |
| 4375 | } |
| 4376 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4377 | if (mCs.mIsStartingProvisioning.get()) { |
| 4378 | result = CMP_RESULT_CODE_IS_PROVISIONING; |
| 4379 | log("isMobileOk: X is provisioning result=" + result); |
| 4380 | return result; |
| 4381 | } |
| 4382 | |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4383 | // See if we've already determined we've got a provisioning connection, |
| 4384 | // if so we don't need to do anything active. |
| 4385 | MobileDataStateTracker mdstDefault = (MobileDataStateTracker) |
| 4386 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4387 | boolean isDefaultProvisioning = mdstDefault.isProvisioningNetwork(); |
| 4388 | log("isMobileOk: isDefaultProvisioning=" + isDefaultProvisioning); |
| 4389 | |
| 4390 | MobileDataStateTracker mdstHipri = (MobileDataStateTracker) |
| 4391 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4392 | boolean isHipriProvisioning = mdstHipri.isProvisioningNetwork(); |
| 4393 | log("isMobileOk: isHipriProvisioning=" + isHipriProvisioning); |
| 4394 | |
| 4395 | if (isDefaultProvisioning || isHipriProvisioning) { |
| 4396 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
| 4397 | log("isMobileOk: X default || hipri is provisioning result=" + result); |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4398 | return result; |
| 4399 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4400 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4401 | try { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4402 | // Continue trying to connect until time has run out |
| 4403 | long endTime = SystemClock.elapsedRealtime() + params.mTimeOutMs; |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4404 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4405 | if (!mCs.isMobileDataStateTrackerReady()) { |
| 4406 | // Wait for MobileDataStateTracker to be ready. |
| 4407 | if (DBG) log("isMobileOk: mdst is not ready"); |
| 4408 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4409 | if (mCs.isMobileDataStateTrackerReady()) { |
| 4410 | // Enable fail fast as we'll do retries here and use a |
| 4411 | // hipri connection so the default connection stays active. |
| 4412 | if (DBG) log("isMobileOk: mdst ready, enable fail fast of mobile data"); |
| 4413 | mCs.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 4414 | break; |
| 4415 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4416 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4417 | } |
| 4418 | } |
| 4419 | |
| 4420 | log("isMobileOk: start hipri url=" + params.mUrl); |
| 4421 | |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4422 | // First wait until we can start using hipri |
| 4423 | Binder binder = new Binder(); |
| 4424 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4425 | int ret = mCs.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4426 | Phone.FEATURE_ENABLE_HIPRI, binder); |
| 4427 | if ((ret == PhoneConstants.APN_ALREADY_ACTIVE) |
| 4428 | || (ret == PhoneConstants.APN_REQUEST_STARTED)) { |
| 4429 | log("isMobileOk: hipri started"); |
| 4430 | break; |
| 4431 | } |
| 4432 | if (VDBG) log("isMobileOk: hipri not started yet"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4433 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4434 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4435 | } |
| 4436 | |
| 4437 | // Continue trying to connect until time has run out |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4438 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4439 | try { |
| 4440 | // Wait for hipri to connect. |
| 4441 | // TODO: Don't poll and handle situation where hipri fails |
| 4442 | // because default is retrying. See b/9569540 |
| 4443 | NetworkInfo.State state = mCs |
| 4444 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4445 | if (state != NetworkInfo.State.CONNECTED) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4446 | if (true/*VDBG*/) { |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4447 | log("isMobileOk: not connected ni=" + |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4448 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4449 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4450 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4451 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4452 | continue; |
| 4453 | } |
| 4454 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4455 | // Hipri has started check if this is a provisioning url |
| 4456 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4457 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4458 | if (mdst.isProvisioningNetwork()) { |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4459 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4460 | if (DBG) log("isMobileOk: X isProvisioningNetwork result=" + result); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4461 | return result; |
| 4462 | } else { |
| 4463 | if (DBG) log("isMobileOk: isProvisioningNetwork is false, continue"); |
| 4464 | } |
| 4465 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4466 | // Get of the addresses associated with the url host. We need to use the |
| 4467 | // address otherwise HttpURLConnection object will use the name to get |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4468 | // the addresses and will try every address but that will bypass the |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4469 | // route to host we setup and the connection could succeed as the default |
| 4470 | // interface might be connected to the internet via wifi or other interface. |
| 4471 | InetAddress[] addresses; |
| 4472 | try { |
| 4473 | addresses = InetAddress.getAllByName(orgUri.getHost()); |
| 4474 | } catch (UnknownHostException e) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4475 | result = CMP_RESULT_CODE_NO_DNS; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4476 | log("isMobileOk: X UnknownHostException result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4477 | return result; |
| 4478 | } |
| 4479 | log("isMobileOk: addresses=" + inetAddressesToString(addresses)); |
| 4480 | |
| 4481 | // Get the type of addresses supported by this link |
| 4482 | LinkProperties lp = mCs.getLinkProperties( |
| 4483 | ConnectivityManager.TYPE_MOBILE_HIPRI); |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 4484 | boolean linkHasIpv4 = lp.hasIPv4Address(); |
| 4485 | boolean linkHasIpv6 = lp.hasIPv6Address(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4486 | log("isMobileOk: linkHasIpv4=" + linkHasIpv4 |
| 4487 | + " linkHasIpv6=" + linkHasIpv6); |
| 4488 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4489 | final ArrayList<InetAddress> validAddresses = |
| 4490 | new ArrayList<InetAddress>(addresses.length); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4491 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4492 | for (InetAddress addr : addresses) { |
| 4493 | if (((addr instanceof Inet4Address) && linkHasIpv4) || |
| 4494 | ((addr instanceof Inet6Address) && linkHasIpv6)) { |
| 4495 | validAddresses.add(addr); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4496 | } |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | if (validAddresses.size() == 0) { |
| 4500 | return CMP_RESULT_CODE_NO_CONNECTION; |
| 4501 | } |
| 4502 | |
| 4503 | int addrTried = 0; |
| 4504 | while (true) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4505 | // Loop through at most MAX_LOOPS valid addresses or until |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4506 | // we run out of time |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4507 | if (addrTried++ >= MAX_LOOPS) { |
| 4508 | log("isMobileOk: too many loops tried - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4509 | break; |
| 4510 | } |
| 4511 | if (SystemClock.elapsedRealtime() >= endTime) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4512 | log("isMobileOk: spend too much time - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4513 | break; |
| 4514 | } |
| 4515 | |
| 4516 | InetAddress hostAddr = validAddresses.get(rand.nextInt( |
| 4517 | validAddresses.size())); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4518 | |
| 4519 | // Make a route to host so we check the specific interface. |
| 4520 | if (mCs.requestRouteToHostAddress(ConnectivityManager.TYPE_MOBILE_HIPRI, |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 4521 | hostAddr.getAddress(), null)) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4522 | // Wait a short time to be sure the route is established ?? |
| 4523 | log("isMobileOk:" |
| 4524 | + " wait to establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4525 | sleep(NET_ROUTE_ESTABLISHMENT_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4526 | } else { |
| 4527 | log("isMobileOk:" |
| 4528 | + " could not establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4529 | // Wait a short time before the next attempt |
| 4530 | sleep(NET_ERROR_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4531 | continue; |
| 4532 | } |
| 4533 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4534 | // Rewrite the url to have numeric address to use the specific route |
| 4535 | // using http for half the attempts and https for the other half. |
| 4536 | // Doing https first and http second as on a redirected walled garden |
| 4537 | // such as t-mobile uses we get a SocketTimeoutException: "SSL |
| 4538 | // handshake timed out" which we declare as |
| 4539 | // CMP_RESULT_CODE_NO_TCP_CONNECTION. We could change this, but by |
| 4540 | // having http second we will be using logic used for some time. |
| 4541 | URL newUrl; |
| 4542 | String scheme = (addrTried <= (MAX_LOOPS/2)) ? "https" : "http"; |
| 4543 | newUrl = new URL(scheme, hostAddr.getHostAddress(), |
| 4544 | orgUri.getPath()); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4545 | log("isMobileOk: newUrl=" + newUrl); |
| 4546 | |
| 4547 | HttpURLConnection urlConn = null; |
| 4548 | try { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4549 | // Open the connection set the request headers and get the response |
| 4550 | urlConn = (HttpURLConnection)newUrl.openConnection( |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4551 | java.net.Proxy.NO_PROXY); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4552 | if (scheme.equals("https")) { |
| 4553 | ((HttpsURLConnection)urlConn).setHostnameVerifier( |
| 4554 | new CheckMpHostnameVerifier(orgUri)); |
| 4555 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4556 | urlConn.setInstanceFollowRedirects(false); |
| 4557 | urlConn.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 4558 | urlConn.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 4559 | urlConn.setUseCaches(false); |
| 4560 | urlConn.setAllowUserInteraction(false); |
Lorenzo Colitti | 143a2cf | 2013-10-08 10:41:25 +0900 | [diff] [blame] | 4561 | // Set the "Connection" to "Close" as by default "Keep-Alive" |
| 4562 | // is used which is useless in this case. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4563 | urlConn.setRequestProperty("Connection", "close"); |
| 4564 | int responseCode = urlConn.getResponseCode(); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4565 | |
| 4566 | // For debug display the headers |
| 4567 | Map<String, List<String>> headers = urlConn.getHeaderFields(); |
| 4568 | log("isMobileOk: headers=" + headers); |
| 4569 | |
| 4570 | // Close the connection |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4571 | urlConn.disconnect(); |
| 4572 | urlConn = null; |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4573 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4574 | if (mTestingFailures) { |
| 4575 | // Pretend no connection, this tests using http and https |
| 4576 | result = CMP_RESULT_CODE_NO_CONNECTION; |
| 4577 | log("isMobileOk: TESTING_FAILURES, pretend no connction"); |
| 4578 | continue; |
| 4579 | } |
| 4580 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4581 | if (responseCode == 204) { |
| 4582 | // Return |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4583 | result = CMP_RESULT_CODE_CONNECTABLE; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4584 | log("isMobileOk: X got expected responseCode=" + responseCode |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4585 | + " result=" + result); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4586 | return result; |
| 4587 | } else { |
| 4588 | // Retry to be sure this was redirected, we've gotten |
| 4589 | // occasions where a server returned 200 even though |
| 4590 | // the device didn't have a "warm" sim. |
| 4591 | log("isMobileOk: not expected responseCode=" + responseCode); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4592 | // TODO - it would be nice in the single-address case to do |
| 4593 | // another DNS resolve here, but flushing the cache is a bit |
| 4594 | // heavy-handed. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4595 | result = CMP_RESULT_CODE_REDIRECTED; |
| 4596 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4597 | } catch (Exception e) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4598 | log("isMobileOk: HttpURLConnection Exception" + e); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4599 | result = CMP_RESULT_CODE_NO_TCP_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4600 | if (urlConn != null) { |
| 4601 | urlConn.disconnect(); |
| 4602 | urlConn = null; |
| 4603 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4604 | sleep(NET_ERROR_SLEEP_SEC); |
| 4605 | continue; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4606 | } |
| 4607 | } |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4608 | log("isMobileOk: X loops|timed out result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4609 | return result; |
| 4610 | } catch (Exception e) { |
| 4611 | log("isMobileOk: Exception e=" + e); |
| 4612 | continue; |
| 4613 | } |
| 4614 | } |
| 4615 | log("isMobileOk: timed out"); |
| 4616 | } finally { |
| 4617 | log("isMobileOk: F stop hipri"); |
| 4618 | mCs.setEnableFailFastMobileData(DctConstants.DISABLED); |
| 4619 | mCs.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4620 | Phone.FEATURE_ENABLE_HIPRI); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4621 | |
| 4622 | // Wait for hipri to disconnect. |
| 4623 | long endTime = SystemClock.elapsedRealtime() + 5000; |
| 4624 | |
| 4625 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4626 | NetworkInfo.State state = mCs |
| 4627 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4628 | if (state != NetworkInfo.State.DISCONNECTED) { |
| 4629 | if (VDBG) { |
| 4630 | log("isMobileOk: connected ni=" + |
| 4631 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
| 4632 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4633 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4634 | continue; |
| 4635 | } |
| 4636 | } |
| 4637 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4638 | log("isMobileOk: X result=" + result); |
| 4639 | } |
| 4640 | return result; |
| 4641 | } |
| 4642 | |
| 4643 | @Override |
| 4644 | protected Integer doInBackground(Params... params) { |
| 4645 | return isMobileOk(params[0]); |
| 4646 | } |
| 4647 | |
| 4648 | @Override |
| 4649 | protected void onPostExecute(Integer result) { |
| 4650 | log("onPostExecute: result=" + result); |
| 4651 | if ((mParams != null) && (mParams.mCb != null)) { |
| 4652 | mParams.mCb.onComplete(result); |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | private String inetAddressesToString(InetAddress[] addresses) { |
| 4657 | StringBuffer sb = new StringBuffer(); |
| 4658 | boolean firstTime = true; |
| 4659 | for(InetAddress addr : addresses) { |
| 4660 | if (firstTime) { |
| 4661 | firstTime = false; |
| 4662 | } else { |
| 4663 | sb.append(","); |
| 4664 | } |
| 4665 | sb.append(addr); |
| 4666 | } |
| 4667 | return sb.toString(); |
| 4668 | } |
| 4669 | |
| 4670 | private void printNetworkInfo() { |
| 4671 | boolean hasIccCard = mTm.hasIccCard(); |
| 4672 | int simState = mTm.getSimState(); |
| 4673 | log("hasIccCard=" + hasIccCard |
| 4674 | + " simState=" + simState); |
| 4675 | NetworkInfo[] ni = mCs.getAllNetworkInfo(); |
| 4676 | if (ni != null) { |
| 4677 | log("ni.length=" + ni.length); |
| 4678 | for (NetworkInfo netInfo: ni) { |
| 4679 | log("netInfo=" + netInfo.toString()); |
| 4680 | } |
| 4681 | } else { |
| 4682 | log("no network info ni=null"); |
| 4683 | } |
| 4684 | } |
| 4685 | |
| 4686 | /** |
| 4687 | * Sleep for a few seconds then return. |
| 4688 | * @param seconds |
| 4689 | */ |
| 4690 | private static void sleep(int seconds) { |
Jake Hamby | 8f9b33e | 2014-01-15 13:08:03 -0800 | [diff] [blame] | 4691 | long stopTime = System.nanoTime() + (seconds * 1000000000); |
| 4692 | long sleepTime; |
| 4693 | while ((sleepTime = stopTime - System.nanoTime()) > 0) { |
| 4694 | try { |
| 4695 | Thread.sleep(sleepTime / 1000000); |
| 4696 | } catch (InterruptedException ignored) { |
| 4697 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4701 | private static void log(String s) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4702 | Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s); |
| 4703 | } |
| 4704 | } |
| 4705 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4706 | // TODO: Move to ConnectivityManager and make public? |
| 4707 | private static final String CONNECTED_TO_PROVISIONING_NETWORK_ACTION = |
| 4708 | "com.android.server.connectivityservice.CONNECTED_TO_PROVISIONING_NETWORK_ACTION"; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4709 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4710 | private BroadcastReceiver mProvisioningReceiver = new BroadcastReceiver() { |
| 4711 | @Override |
| 4712 | public void onReceive(Context context, Intent intent) { |
| 4713 | if (intent.getAction().equals(CONNECTED_TO_PROVISIONING_NETWORK_ACTION)) { |
| 4714 | handleMobileProvisioningAction(intent.getStringExtra("EXTRA_URL")); |
| 4715 | } |
| 4716 | } |
| 4717 | }; |
| 4718 | |
| 4719 | private void handleMobileProvisioningAction(String url) { |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4720 | // Mark notification as not visible |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4721 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4722 | |
| 4723 | // If provisioning network handle as a special case, |
| 4724 | // otherwise launch browser with the intent directly. |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4725 | if (mIsProvisioningNetwork.get()) { |
| 4726 | if (DBG) log("handleMobileProvisioningAction: on prov network enable then launch"); |
| 4727 | mIsStartingProvisioning.set(true); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4728 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4729 | mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4730 | mdst.setEnableFailFastMobileData(DctConstants.ENABLED); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4731 | mdst.enableMobileProvisioning(url); |
| 4732 | } else { |
Robert Greenwalt | 1b98e90 | 2014-03-19 12:41:54 -0700 | [diff] [blame] | 4733 | if (DBG) log("handleMobileProvisioningAction: not prov network"); |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 4734 | // Check for apps that can handle provisioning first |
| 4735 | Intent provisioningIntent = new Intent(TelephonyIntents.ACTION_CARRIER_SETUP); |
| 4736 | provisioningIntent.addCategory(TelephonyIntents.CATEGORY_MCCMNC_PREFIX |
| 4737 | + mTelephonyManager.getSimOperator()); |
| 4738 | if (mContext.getPackageManager().resolveActivity(provisioningIntent, 0 /* flags */) |
| 4739 | != null) { |
| 4740 | provisioningIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4741 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4742 | mContext.startActivity(provisioningIntent); |
| 4743 | } else { |
| 4744 | // If no apps exist, use standard URL ACTION_VIEW method |
| 4745 | Intent newIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, |
| 4746 | Intent.CATEGORY_APP_BROWSER); |
| 4747 | newIntent.setData(Uri.parse(url)); |
| 4748 | newIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4749 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4750 | try { |
| 4751 | mContext.startActivity(newIntent); |
| 4752 | } catch (ActivityNotFoundException e) { |
| 4753 | loge("handleMobileProvisioningAction: startActivity failed" + e); |
| 4754 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4755 | } |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 4760 | private volatile boolean mIsNotificationVisible = false; |
| 4761 | |
| 4762 | private void setProvNotificationVisible(boolean visible, int networkType, String extraInfo, |
| 4763 | String url) { |
| 4764 | if (DBG) { |
| 4765 | log("setProvNotificationVisible: E visible=" + visible + " networkType=" + networkType |
| 4766 | + " extraInfo=" + extraInfo + " url=" + url); |
| 4767 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4768 | |
| 4769 | Resources r = Resources.getSystem(); |
| 4770 | NotificationManager notificationManager = (NotificationManager) mContext |
| 4771 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 4772 | |
| 4773 | if (visible) { |
| 4774 | CharSequence title; |
| 4775 | CharSequence details; |
| 4776 | int icon; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4777 | Intent intent; |
| 4778 | Notification notification = new Notification(); |
| 4779 | switch (networkType) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4780 | case ConnectivityManager.TYPE_WIFI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4781 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 4782 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4783 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4784 | icon = R.drawable.stat_notify_wifi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4785 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 4786 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4787 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4788 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4789 | break; |
| 4790 | case ConnectivityManager.TYPE_MOBILE: |
| 4791 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4792 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4793 | // TODO: Change this to pull from NetworkInfo once a printable |
| 4794 | // name has been added to it |
| 4795 | details = mTelephonyManager.getNetworkOperatorName(); |
| 4796 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4797 | intent = new Intent(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 4798 | intent.putExtra("EXTRA_URL", url); |
| 4799 | intent.setFlags(0); |
| 4800 | notification.contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4801 | break; |
| 4802 | default: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4803 | title = r.getString(R.string.network_available_sign_in, 0); |
| 4804 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4805 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4806 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4807 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 4808 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 4809 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 4810 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4811 | break; |
| 4812 | } |
| 4813 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4814 | notification.when = 0; |
| 4815 | notification.icon = icon; |
| 4816 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4817 | notification.tickerText = title; |
| 4818 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
| 4819 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4820 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4821 | notificationManager.notify(NOTIFICATION_ID, networkType, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4822 | } catch (NullPointerException npe) { |
| 4823 | loge("setNotificaitionVisible: visible notificationManager npe=" + npe); |
| 4824 | npe.printStackTrace(); |
| 4825 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4826 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4827 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4828 | notificationManager.cancel(NOTIFICATION_ID, networkType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4829 | } catch (NullPointerException npe) { |
| 4830 | loge("setNotificaitionVisible: cancel notificationManager npe=" + npe); |
| 4831 | npe.printStackTrace(); |
| 4832 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4833 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4834 | mIsNotificationVisible = visible; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4837 | /** Location to an updatable file listing carrier provisioning urls. |
| 4838 | * An example: |
| 4839 | * |
| 4840 | * <?xml version="1.0" encoding="utf-8"?> |
| 4841 | * <provisioningUrls> |
| 4842 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
| 4843 | * <redirectedUrl mcc="310" mnc="4">http://www.google.com</redirectedUrl> |
| 4844 | * </provisioningUrls> |
| 4845 | */ |
| 4846 | private static final String PROVISIONING_URL_PATH = |
| 4847 | "/data/misc/radio/provisioning_urls.xml"; |
| 4848 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4849 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4850 | /** XML tag for root element. */ |
| 4851 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 4852 | /** XML tag for individual url */ |
| 4853 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
| 4854 | /** XML tag for redirected url */ |
| 4855 | private static final String TAG_REDIRECTED_URL = "redirectedUrl"; |
| 4856 | /** XML attribute for mcc */ |
| 4857 | private static final String ATTR_MCC = "mcc"; |
| 4858 | /** XML attribute for mnc */ |
| 4859 | private static final String ATTR_MNC = "mnc"; |
| 4860 | |
| 4861 | private static final int REDIRECTED_PROVISIONING = 1; |
| 4862 | private static final int PROVISIONING = 2; |
| 4863 | |
| 4864 | private String getProvisioningUrlBaseFromFile(int type) { |
| 4865 | FileReader fileReader = null; |
| 4866 | XmlPullParser parser = null; |
| 4867 | Configuration config = mContext.getResources().getConfiguration(); |
| 4868 | String tagType; |
| 4869 | |
| 4870 | switch (type) { |
| 4871 | case PROVISIONING: |
| 4872 | tagType = TAG_PROVISIONING_URL; |
| 4873 | break; |
| 4874 | case REDIRECTED_PROVISIONING: |
| 4875 | tagType = TAG_REDIRECTED_URL; |
| 4876 | break; |
| 4877 | default: |
| 4878 | throw new RuntimeException("getProvisioningUrlBaseFromFile: Unexpected parameter " + |
| 4879 | type); |
| 4880 | } |
| 4881 | |
| 4882 | try { |
| 4883 | fileReader = new FileReader(mProvisioningUrlFile); |
| 4884 | parser = Xml.newPullParser(); |
| 4885 | parser.setInput(fileReader); |
| 4886 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 4887 | |
| 4888 | while (true) { |
| 4889 | XmlUtils.nextElement(parser); |
| 4890 | |
| 4891 | String element = parser.getName(); |
| 4892 | if (element == null) break; |
| 4893 | |
| 4894 | if (element.equals(tagType)) { |
| 4895 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 4896 | try { |
| 4897 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 4898 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 4899 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 4900 | parser.next(); |
| 4901 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 4902 | return parser.getText(); |
| 4903 | } |
| 4904 | } |
| 4905 | } |
| 4906 | } catch (NumberFormatException e) { |
| 4907 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 4908 | } |
| 4909 | } |
| 4910 | } |
| 4911 | return null; |
| 4912 | } catch (FileNotFoundException e) { |
| 4913 | loge("Carrier Provisioning Urls file not found"); |
| 4914 | } catch (XmlPullParserException e) { |
| 4915 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 4916 | } catch (IOException e) { |
| 4917 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 4918 | } finally { |
| 4919 | if (fileReader != null) { |
| 4920 | try { |
| 4921 | fileReader.close(); |
| 4922 | } catch (IOException e) {} |
| 4923 | } |
| 4924 | } |
| 4925 | return null; |
| 4926 | } |
| 4927 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4928 | @Override |
| 4929 | public String getMobileRedirectedProvisioningUrl() { |
| 4930 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4931 | String url = getProvisioningUrlBaseFromFile(REDIRECTED_PROVISIONING); |
| 4932 | if (TextUtils.isEmpty(url)) { |
| 4933 | url = mContext.getResources().getString(R.string.mobile_redirected_provisioning_url); |
| 4934 | } |
| 4935 | return url; |
| 4936 | } |
| 4937 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4938 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4939 | public String getMobileProvisioningUrl() { |
| 4940 | enforceConnectivityInternalPermission(); |
| 4941 | String url = getProvisioningUrlBaseFromFile(PROVISIONING); |
| 4942 | if (TextUtils.isEmpty(url)) { |
| 4943 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4944 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4945 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4946 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4947 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4948 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4949 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4950 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 4951 | if (TextUtils.isEmpty(phoneNumber)) { |
| 4952 | phoneNumber = "0000000000"; |
| 4953 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4954 | url = String.format(url, |
| 4955 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 4956 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4957 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4958 | } |
| 4959 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4960 | return url; |
| 4961 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4962 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4963 | @Override |
| 4964 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
| 4965 | String extraInfo, String url) { |
| 4966 | enforceConnectivityInternalPermission(); |
| 4967 | setProvNotificationVisible(visible, networkType, extraInfo, url); |
| 4968 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 4969 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4970 | @Override |
| 4971 | public void setAirplaneMode(boolean enable) { |
| 4972 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4973 | final long ident = Binder.clearCallingIdentity(); |
| 4974 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4975 | final ContentResolver cr = mContext.getContentResolver(); |
| 4976 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 4977 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 4978 | intent.putExtra("state", enable); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4979 | mContext.sendBroadcast(intent); |
| 4980 | } finally { |
| 4981 | Binder.restoreCallingIdentity(ident); |
| 4982 | } |
| 4983 | } |
| 4984 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4985 | private void onUserStart(int userId) { |
| 4986 | synchronized(mVpns) { |
| 4987 | Vpn userVpn = mVpns.get(userId); |
| 4988 | if (userVpn != null) { |
| 4989 | loge("Starting user already has a VPN"); |
| 4990 | return; |
| 4991 | } |
| 4992 | userVpn = new Vpn(mContext, mVpnCallback, mNetd, this, userId); |
| 4993 | mVpns.put(userId, userVpn); |
| 4994 | userVpn.startMonitoring(mContext, mTrackerHandler); |
| 4995 | } |
| 4996 | } |
| 4997 | |
| 4998 | private void onUserStop(int userId) { |
| 4999 | synchronized(mVpns) { |
| 5000 | Vpn userVpn = mVpns.get(userId); |
| 5001 | if (userVpn == null) { |
| 5002 | loge("Stopping user has no VPN"); |
| 5003 | return; |
| 5004 | } |
| 5005 | mVpns.delete(userId); |
| 5006 | } |
| 5007 | } |
| 5008 | |
| 5009 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 5010 | @Override |
| 5011 | public void onReceive(Context context, Intent intent) { |
| 5012 | final String action = intent.getAction(); |
| 5013 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 5014 | if (userId == UserHandle.USER_NULL) return; |
| 5015 | |
| 5016 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 5017 | onUserStart(userId); |
| 5018 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 5019 | onUserStop(userId); |
| 5020 | } |
| 5021 | } |
| 5022 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5023 | |
| 5024 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5025 | public LinkQualityInfo getLinkQualityInfo(int networkType) { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5026 | enforceAccessPermission(); |
| 5027 | if (isNetworkTypeValid(networkType)) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5028 | return mNetTrackers[networkType].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5029 | } else { |
| 5030 | return null; |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5035 | public LinkQualityInfo getActiveLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5036 | enforceAccessPermission(); |
| 5037 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5038 | return mNetTrackers[mActiveDefaultNetwork].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5039 | } else { |
| 5040 | return null; |
| 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5045 | public LinkQualityInfo[] getAllLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5046 | enforceAccessPermission(); |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5047 | final ArrayList<LinkQualityInfo> result = Lists.newArrayList(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5048 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5049 | if (tracker != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5050 | LinkQualityInfo li = tracker.getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5051 | if (li != null) { |
| 5052 | result.add(li); |
| 5053 | } |
| 5054 | } |
| 5055 | } |
| 5056 | |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5057 | return result.toArray(new LinkQualityInfo[result.size()]); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | /* Infrastructure for network sampling */ |
| 5061 | |
| 5062 | private void handleNetworkSamplingTimeout() { |
| 5063 | |
| 5064 | log("Sampling interval elapsed, updating statistics .."); |
| 5065 | |
| 5066 | // initialize list of interfaces .. |
| 5067 | Map<String, SamplingDataTracker.SamplingSnapshot> mapIfaceToSample = |
| 5068 | new HashMap<String, SamplingDataTracker.SamplingSnapshot>(); |
| 5069 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5070 | if (tracker != null) { |
| 5071 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5072 | if (ifaceName != null) { |
| 5073 | mapIfaceToSample.put(ifaceName, null); |
| 5074 | } |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | // Read samples for all interfaces |
| 5079 | SamplingDataTracker.getSamplingSnapshots(mapIfaceToSample); |
| 5080 | |
| 5081 | // process samples for all networks |
| 5082 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5083 | if (tracker != null) { |
| 5084 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5085 | SamplingDataTracker.SamplingSnapshot ss = mapIfaceToSample.get(ifaceName); |
| 5086 | if (ss != null) { |
| 5087 | // end the previous sampling cycle |
| 5088 | tracker.stopSampling(ss); |
| 5089 | // start a new sampling cycle .. |
| 5090 | tracker.startSampling(ss); |
| 5091 | } |
| 5092 | } |
| 5093 | } |
| 5094 | |
| 5095 | log("Done."); |
| 5096 | |
| 5097 | int samplingIntervalInSeconds = Settings.Global.getInt(mContext.getContentResolver(), |
| 5098 | Settings.Global.CONNECTIVITY_SAMPLING_INTERVAL_IN_SECONDS, |
| 5099 | DEFAULT_SAMPLING_INTERVAL_IN_SECONDS); |
| 5100 | |
| 5101 | if (DBG) log("Setting timer for " + String.valueOf(samplingIntervalInSeconds) + "seconds"); |
| 5102 | |
| 5103 | setAlarm(samplingIntervalInSeconds * 1000, mSampleIntervalElapsedIntent); |
| 5104 | } |
| 5105 | |
| 5106 | void setAlarm(int timeoutInMilliseconds, PendingIntent intent) { |
| 5107 | long wakeupTime = SystemClock.elapsedRealtime() + timeoutInMilliseconds; |
| 5108 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, intent); |
| 5109 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5110 | |
| 5111 | private final ArrayList<AsyncChannel> mNetworkFactories = new ArrayList<AsyncChannel>(); |
| 5112 | |
| 5113 | public void registerNetworkFactory(Messenger messenger) { |
| 5114 | enforceConnectivityInternalPermission(); |
| 5115 | |
| 5116 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, messenger)); |
| 5117 | } |
| 5118 | |
| 5119 | private void handleRegisterNetworkFactory(Messenger messenger) { |
| 5120 | if (VDBG) log("Got NetworkFactory Messenger"); |
| 5121 | AsyncChannel ac = new AsyncChannel(); |
| 5122 | mNetworkFactories.add(ac); |
| 5123 | ac.connect(mContext, mTrackerHandler, messenger); |
| 5124 | } |
| 5125 | |
| 5126 | private final SparseArray<NetworkRequest> mNetworkRequests = new SparseArray<NetworkRequest>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5127 | } |