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