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