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