Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.connectivity.tethering; |
| 18 | |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 19 | import static android.content.Context.TELEPHONY_SERVICE; |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
| 22 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 23 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 24 | import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER; |
| 25 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 26 | import static com.android.internal.R.array.config_mobile_hotspot_provision_app; |
| 27 | import static com.android.internal.R.array.config_tether_bluetooth_regexs; |
| 28 | import static com.android.internal.R.array.config_tether_dhcp_range; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 29 | import static com.android.internal.R.array.config_tether_upstream_types; |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 30 | import static com.android.internal.R.array.config_tether_usb_regexs; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 31 | import static com.android.internal.R.array.config_tether_wifi_regexs; |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 32 | import static com.android.internal.R.bool.config_tether_upstream_automatic; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 33 | import static com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 34 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 35 | import android.content.ContentResolver; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.content.res.Resources; |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 38 | import android.net.ConnectivityManager; |
Erik Kline | e93ed61 | 2018-04-10 07:01:16 +0000 | [diff] [blame] | 39 | import android.net.util.SharedLog; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 40 | import android.provider.Settings; |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 41 | import android.telephony.SubscriptionManager; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 42 | import android.telephony.TelephonyManager; |
| 43 | import android.text.TextUtils; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 44 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 45 | import com.android.internal.annotations.VisibleForTesting; |
| 46 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 47 | import java.io.PrintWriter; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 48 | import java.util.ArrayList; |
| 49 | import java.util.Arrays; |
| 50 | import java.util.Collection; |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 51 | import java.util.StringJoiner; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 52 | |
| 53 | |
| 54 | /** |
| 55 | * A utility class to encapsulate the various tethering configuration elements. |
| 56 | * |
| 57 | * This configuration data includes elements describing upstream properties |
| 58 | * (preferred and required types of upstream connectivity as well as default |
| 59 | * DNS servers to use if none are available) and downstream properties (such |
| 60 | * as regular expressions use to match suitable downstream interfaces and the |
| 61 | * DHCPv4 ranges to use). |
| 62 | * |
| 63 | * @hide |
| 64 | */ |
| 65 | public class TetheringConfiguration { |
| 66 | private static final String TAG = TetheringConfiguration.class.getSimpleName(); |
| 67 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 68 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 69 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 70 | @VisibleForTesting |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 71 | public static final int DUN_NOT_REQUIRED = 0; |
| 72 | public static final int DUN_REQUIRED = 1; |
| 73 | public static final int DUN_UNSPECIFIED = 2; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 74 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 75 | // Default ranges used for the legacy DHCP server. |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 76 | // USB is 192.168.42.1 and 255.255.255.0 |
| 77 | // Wifi is 192.168.43.1 and 255.255.255.0 |
| 78 | // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1 |
| 79 | // with 255.255.255.0 |
| 80 | // P2P is 192.168.49.1 and 255.255.255.0 |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 81 | private static final String[] LEGACY_DHCP_DEFAULT_RANGE = { |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 82 | "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254", |
| 83 | "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254", |
| 84 | "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254", |
| 85 | "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254", |
| 86 | }; |
| 87 | |
| 88 | private final String[] DEFAULT_IPV4_DNS = {"8.8.4.4", "8.8.8.8"}; |
| 89 | |
| 90 | public final String[] tetherableUsbRegexs; |
| 91 | public final String[] tetherableWifiRegexs; |
| 92 | public final String[] tetherableBluetoothRegexs; |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 93 | public final int dunCheck; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 94 | public final boolean isDunRequired; |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 95 | public final boolean chooseUpstreamAutomatically; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 96 | public final Collection<Integer> preferredUpstreamIfaceTypes; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 97 | public final String[] legacyDhcpRanges; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 98 | public final String[] defaultIPv4DNS; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 99 | public final boolean enableLegacyDhcpServer; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 100 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 101 | public final String[] provisioningApp; |
| 102 | public final String provisioningAppNoUi; |
| 103 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 104 | public final int subId; |
| 105 | |
| 106 | public TetheringConfiguration(Context ctx, SharedLog log, int id) { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 107 | final SharedLog configLog = log.forSubComponent("config"); |
| 108 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 109 | subId = id; |
| 110 | Resources res = getResources(ctx, subId); |
| 111 | |
| 112 | tetherableUsbRegexs = getResourceStringArray(res, config_tether_usb_regexs); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 113 | // TODO: Evaluate deleting this altogether now that Wi-Fi always passes |
| 114 | // us an interface name. Careful consideration needs to be given to |
| 115 | // implications for Settings and for provisioning checks. |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 116 | tetherableWifiRegexs = getResourceStringArray(res, config_tether_wifi_regexs); |
| 117 | tetherableBluetoothRegexs = getResourceStringArray(res, config_tether_bluetooth_regexs); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 118 | |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 119 | dunCheck = checkDunRequired(ctx); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 120 | configLog.log("DUN check returned: " + dunCheckString(dunCheck)); |
| 121 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 122 | chooseUpstreamAutomatically = getResourceBoolean(res, config_tether_upstream_automatic); |
| 123 | preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, dunCheck); |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 124 | isDunRequired = preferredUpstreamIfaceTypes.contains(TYPE_MOBILE_DUN); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 125 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 126 | legacyDhcpRanges = getLegacyDhcpRanges(res); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 127 | defaultIPv4DNS = copy(DEFAULT_IPV4_DNS); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 128 | enableLegacyDhcpServer = getEnableLegacyDhcpServer(ctx); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 129 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 130 | provisioningApp = getResourceStringArray(res, config_mobile_hotspot_provision_app); |
| 131 | provisioningAppNoUi = getProvisioningAppNoUi(res); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 132 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 133 | configLog.log(toString()); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public boolean isUsb(String iface) { |
| 137 | return matchesDownstreamRegexs(iface, tetherableUsbRegexs); |
| 138 | } |
| 139 | |
| 140 | public boolean isWifi(String iface) { |
| 141 | return matchesDownstreamRegexs(iface, tetherableWifiRegexs); |
| 142 | } |
| 143 | |
| 144 | public boolean isBluetooth(String iface) { |
| 145 | return matchesDownstreamRegexs(iface, tetherableBluetoothRegexs); |
| 146 | } |
| 147 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 148 | public boolean hasMobileHotspotProvisionApp() { |
| 149 | return !TextUtils.isEmpty(provisioningAppNoUi); |
| 150 | } |
| 151 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 152 | public void dump(PrintWriter pw) { |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 153 | pw.print("subId: "); |
| 154 | pw.println(subId); |
| 155 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 156 | dumpStringArray(pw, "tetherableUsbRegexs", tetherableUsbRegexs); |
| 157 | dumpStringArray(pw, "tetherableWifiRegexs", tetherableWifiRegexs); |
| 158 | dumpStringArray(pw, "tetherableBluetoothRegexs", tetherableBluetoothRegexs); |
| 159 | |
| 160 | pw.print("isDunRequired: "); |
| 161 | pw.println(isDunRequired); |
| 162 | |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 163 | pw.print("chooseUpstreamAutomatically: "); |
| 164 | pw.println(chooseUpstreamAutomatically); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 165 | dumpStringArray(pw, "preferredUpstreamIfaceTypes", |
| 166 | preferredUpstreamNames(preferredUpstreamIfaceTypes)); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 167 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 168 | dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 169 | dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 170 | |
| 171 | dumpStringArray(pw, "provisioningApp", provisioningApp); |
| 172 | pw.print("provisioningAppNoUi: "); |
| 173 | pw.println(provisioningAppNoUi); |
Remi NGUYEN VAN | 9865cc1 | 2018-08-30 16:49:52 +0900 | [diff] [blame] | 174 | |
| 175 | pw.print("enableLegacyDhcpServer: "); |
| 176 | pw.println(enableLegacyDhcpServer); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 177 | } |
| 178 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 179 | public String toString() { |
| 180 | final StringJoiner sj = new StringJoiner(" "); |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 181 | sj.add(String.format("subId:%d", subId)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 182 | sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs))); |
| 183 | sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs))); |
| 184 | sj.add(String.format("tetherableBluetoothRegexs:%s", |
| 185 | makeString(tetherableBluetoothRegexs))); |
| 186 | sj.add(String.format("isDunRequired:%s", isDunRequired)); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 187 | sj.add(String.format("chooseUpstreamAutomatically:%s", chooseUpstreamAutomatically)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 188 | sj.add(String.format("preferredUpstreamIfaceTypes:%s", |
| 189 | makeString(preferredUpstreamNames(preferredUpstreamIfaceTypes)))); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 190 | sj.add(String.format("provisioningApp:%s", makeString(provisioningApp))); |
| 191 | sj.add(String.format("provisioningAppNoUi:%s", provisioningAppNoUi)); |
Remi NGUYEN VAN | 9865cc1 | 2018-08-30 16:49:52 +0900 | [diff] [blame] | 192 | sj.add(String.format("enableLegacyDhcpServer:%s", enableLegacyDhcpServer)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 193 | return String.format("TetheringConfiguration{%s}", sj.toString()); |
| 194 | } |
| 195 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 196 | private static void dumpStringArray(PrintWriter pw, String label, String[] values) { |
| 197 | pw.print(label); |
| 198 | pw.print(": "); |
| 199 | |
| 200 | if (values != null) { |
| 201 | final StringJoiner sj = new StringJoiner(", ", "[", "]"); |
| 202 | for (String value : values) { sj.add(value); } |
| 203 | pw.print(sj.toString()); |
| 204 | } else { |
| 205 | pw.print("null"); |
| 206 | } |
| 207 | |
| 208 | pw.println(); |
| 209 | } |
| 210 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 211 | private static String makeString(String[] strings) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 212 | if (strings == null) return "null"; |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 213 | final StringJoiner sj = new StringJoiner(",", "[", "]"); |
| 214 | for (String s : strings) sj.add(s); |
| 215 | return sj.toString(); |
| 216 | } |
| 217 | |
| 218 | private static String[] preferredUpstreamNames(Collection<Integer> upstreamTypes) { |
| 219 | String[] upstreamNames = null; |
| 220 | |
| 221 | if (upstreamTypes != null) { |
| 222 | upstreamNames = new String[upstreamTypes.size()]; |
| 223 | int i = 0; |
| 224 | for (Integer netType : upstreamTypes) { |
| 225 | upstreamNames[i] = ConnectivityManager.getNetworkTypeName(netType); |
| 226 | i++; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return upstreamNames; |
| 231 | } |
| 232 | |
Erik Kline | 6ee73da | 2017-07-08 20:36:37 +0900 | [diff] [blame] | 233 | public static int checkDunRequired(Context ctx) { |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 234 | final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE); |
| 235 | return (tm != null) ? tm.getTetherApnRequired() : DUN_UNSPECIFIED; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 238 | private static String dunCheckString(int dunCheck) { |
| 239 | switch (dunCheck) { |
| 240 | case DUN_NOT_REQUIRED: return "DUN_NOT_REQUIRED"; |
| 241 | case DUN_REQUIRED: return "DUN_REQUIRED"; |
| 242 | case DUN_UNSPECIFIED: return "DUN_UNSPECIFIED"; |
| 243 | default: |
| 244 | return String.format("UNKNOWN (%s)", dunCheck); |
| 245 | } |
| 246 | } |
| 247 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 248 | private static Collection<Integer> getUpstreamIfaceTypes(Resources res, int dunCheck) { |
| 249 | final int[] ifaceTypes = res.getIntArray(config_tether_upstream_types); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 250 | final ArrayList<Integer> upstreamIfaceTypes = new ArrayList<>(ifaceTypes.length); |
| 251 | for (int i : ifaceTypes) { |
| 252 | switch (i) { |
| 253 | case TYPE_MOBILE: |
| 254 | case TYPE_MOBILE_HIPRI: |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 255 | if (dunCheck == DUN_REQUIRED) continue; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 256 | break; |
| 257 | case TYPE_MOBILE_DUN: |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 258 | if (dunCheck == DUN_NOT_REQUIRED) continue; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 259 | break; |
| 260 | } |
| 261 | upstreamIfaceTypes.add(i); |
| 262 | } |
| 263 | |
| 264 | // Fix up upstream interface types for DUN or mobile. NOTE: independent |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 265 | // of the value of |dunCheck|, cell data of one form or another is |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 266 | // *always* an upstream, regardless of the upstream interface types |
| 267 | // specified by configuration resources. |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 268 | if (dunCheck == DUN_REQUIRED) { |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 269 | appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_DUN); |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 270 | } else if (dunCheck == DUN_NOT_REQUIRED) { |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 271 | appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE); |
| 272 | appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_HIPRI); |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 273 | } else { |
| 274 | // Fix upstream interface types for case DUN_UNSPECIFIED. |
| 275 | // Do not modify if a cellular interface type is already present in the |
| 276 | // upstream interface types. Add TYPE_MOBILE and TYPE_MOBILE_HIPRI if no |
| 277 | // cellular interface types are found in the upstream interface types. |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 278 | if (!(containsOneOf(upstreamIfaceTypes, |
| 279 | TYPE_MOBILE_DUN, TYPE_MOBILE, TYPE_MOBILE_HIPRI))) { |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 280 | upstreamIfaceTypes.add(TYPE_MOBILE); |
| 281 | upstreamIfaceTypes.add(TYPE_MOBILE_HIPRI); |
| 282 | } |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 285 | // Always make sure our good friend Ethernet is present. |
| 286 | // TODO: consider unilaterally forcing this at the front. |
| 287 | prependIfNotPresent(upstreamIfaceTypes, TYPE_ETHERNET); |
| 288 | |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 289 | return upstreamIfaceTypes; |
| 290 | } |
| 291 | |
| 292 | private static boolean matchesDownstreamRegexs(String iface, String[] regexs) { |
| 293 | for (String regex : regexs) { |
| 294 | if (iface.matches(regex)) return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 299 | private static String[] getLegacyDhcpRanges(Resources res) { |
| 300 | final String[] fromResource = getResourceStringArray(res, config_tether_dhcp_range); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 301 | if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) { |
| 302 | return fromResource; |
| 303 | } |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 304 | return copy(LEGACY_DHCP_DEFAULT_RANGE); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 305 | } |
| 306 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 307 | private static String getProvisioningAppNoUi(Resources res) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 308 | try { |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 309 | return res.getString(config_mobile_hotspot_provision_app_no_ui); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 310 | } catch (Resources.NotFoundException e) { |
| 311 | return ""; |
| 312 | } |
| 313 | } |
| 314 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 315 | private static boolean getResourceBoolean(Resources res, int resId) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 316 | try { |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 317 | return res.getBoolean(resId); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 318 | } catch (Resources.NotFoundException e404) { |
| 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 323 | private static String[] getResourceStringArray(Resources res, int resId) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 324 | try { |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 325 | final String[] strArray = res.getStringArray(resId); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 326 | return (strArray != null) ? strArray : EMPTY_STRING_ARRAY; |
| 327 | } catch (Resources.NotFoundException e404) { |
| 328 | return EMPTY_STRING_ARRAY; |
| 329 | } |
| 330 | } |
| 331 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 332 | private static boolean getEnableLegacyDhcpServer(Context ctx) { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 333 | final ContentResolver cr = ctx.getContentResolver(); |
Remi NGUYEN VAN | e0d8c0e | 2018-09-26 18:11:48 +0900 | [diff] [blame] | 334 | final int intVal = Settings.Global.getInt(cr, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 335 | return intVal != 0; |
| 336 | } |
| 337 | |
markchien | 293422f | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 338 | private Resources getResources(Context ctx, int subId) { |
| 339 | if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
| 340 | return getResourcesForSubIdWrapper(ctx, subId); |
| 341 | } else { |
| 342 | return ctx.getResources(); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @VisibleForTesting |
| 347 | protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) { |
| 348 | return SubscriptionManager.getResourcesForSubId(ctx, subId); |
| 349 | } |
| 350 | |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 351 | private static String[] copy(String[] strarray) { |
| 352 | return Arrays.copyOf(strarray, strarray.length); |
| 353 | } |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 354 | |
| 355 | private static void prependIfNotPresent(ArrayList<Integer> list, int value) { |
| 356 | if (list.contains(value)) return; |
| 357 | list.add(0, value); |
| 358 | } |
| 359 | |
| 360 | private static void appendIfNotPresent(ArrayList<Integer> list, int value) { |
| 361 | if (list.contains(value)) return; |
| 362 | list.add(value); |
| 363 | } |
| 364 | |
| 365 | private static boolean containsOneOf(ArrayList<Integer> list, Integer... values) { |
| 366 | for (Integer value : values) { |
| 367 | if (list.contains(value)) return true; |
| 368 | } |
| 369 | return false; |
| 370 | } |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 371 | } |