blob: 9d4e74732729f1b091bf93570f5ebde9f34aeddb [file] [log] [blame]
Erik Kline79868f82017-01-21 14:33:56 +09001/*
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
markchien503be612020-04-12 21:41:29 +080017package com.android.networkstack.tethering;
Erik Kline79868f82017-01-21 14:33:56 +090018
Erik Kline54f2f372017-05-15 21:11:47 +090019import static android.content.Context.TELEPHONY_SERVICE;
Erik Kline1e543512017-03-09 11:44:11 +090020import static android.net.ConnectivityManager.TYPE_ETHERNET;
Erik Kline79868f82017-01-21 14:33:56 +090021import static android.net.ConnectivityManager.TYPE_MOBILE;
22import static android.net.ConnectivityManager.TYPE_MOBILE_DUN;
23import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI;
markchien2dfee022020-01-13 16:09:42 +080024import static android.provider.DeviceConfig.NAMESPACE_CONNECTIVITY;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090025
Erik Kline79868f82017-01-21 14:33:56 +090026import android.content.Context;
27import android.content.res.Resources;
markchien0df2ebc42019-09-30 14:40:57 +080028import android.net.TetheringConfigurationParcel;
Erik Klinee93ed612018-04-10 07:01:16 +000029import android.net.util.SharedLog;
markchien2dfee022020-01-13 16:09:42 +080030import android.provider.DeviceConfig;
markchien0b595072019-01-08 23:52:21 +080031import android.telephony.SubscriptionManager;
Erik Klinee0f34032018-02-28 15:01:35 +090032import android.telephony.TelephonyManager;
33import android.text.TextUtils;
Erik Kline79868f82017-01-21 14:33:56 +090034
Chalard Jean918a68b2018-01-19 17:00:47 +090035import com.android.internal.annotations.VisibleForTesting;
36
Erik Kline9db1b542017-03-16 14:10:27 +090037import java.io.PrintWriter;
Erik Kline79868f82017-01-21 14:33:56 +090038import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.Collection;
Erik Kline9db1b542017-03-16 14:10:27 +090041import java.util.StringJoiner;
Erik Kline79868f82017-01-21 14:33:56 +090042
43
44/**
45 * A utility class to encapsulate the various tethering configuration elements.
46 *
47 * This configuration data includes elements describing upstream properties
48 * (preferred and required types of upstream connectivity as well as default
49 * DNS servers to use if none are available) and downstream properties (such
50 * as regular expressions use to match suitable downstream interfaces and the
51 * DHCPv4 ranges to use).
52 *
53 * @hide
54 */
55public class TetheringConfiguration {
56 private static final String TAG = TetheringConfiguration.class.getSimpleName();
57
Erik Klinee0f34032018-02-28 15:01:35 +090058 private static final String[] EMPTY_STRING_ARRAY = new String[0];
59
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090060 // Default ranges used for the legacy DHCP server.
Erik Kline79868f82017-01-21 14:33:56 +090061 // USB is 192.168.42.1 and 255.255.255.0
62 // Wifi is 192.168.43.1 and 255.255.255.0
63 // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
64 // with 255.255.255.0
65 // P2P is 192.168.49.1 and 255.255.255.0
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090066 private static final String[] LEGACY_DHCP_DEFAULT_RANGE = {
Erik Kline79868f82017-01-21 14:33:56 +090067 "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
68 "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
69 "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
70 "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
71 };
72
markchiena6ba54d2019-09-03 15:58:06 +080073 private static final String[] DEFAULT_IPV4_DNS = {"8.8.4.4", "8.8.8.8"};
Erik Kline79868f82017-01-21 14:33:56 +090074
markchien2dfee022020-01-13 16:09:42 +080075 /**
76 * Use the old dnsmasq DHCP server for tethering instead of the framework implementation.
77 */
78 public static final String TETHER_ENABLE_LEGACY_DHCP_SERVER =
79 "tether_enable_legacy_dhcp_server";
80
junyulaiaea13ae2020-04-30 15:21:55 +080081 /**
82 * Default value that used to periodic polls tether offload stats from tethering offload HAL
83 * to make the data warnings work.
84 */
85 public static final int DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS = 5000;
86
Erik Kline79868f82017-01-21 14:33:56 +090087 public final String[] tetherableUsbRegexs;
88 public final String[] tetherableWifiRegexs;
Jimmy Chenbcd86d02019-07-15 18:03:23 +080089 public final String[] tetherableWifiP2pRegexs;
Erik Kline79868f82017-01-21 14:33:56 +090090 public final String[] tetherableBluetoothRegexs;
Milim Lee31ef4c02019-10-17 05:02:33 +090091 public final String[] tetherableNcmRegexs;
Erik Kline79868f82017-01-21 14:33:56 +090092 public final boolean isDunRequired;
Erik Kline72302902018-06-14 17:36:40 +090093 public final boolean chooseUpstreamAutomatically;
Erik Kline79868f82017-01-21 14:33:56 +090094 public final Collection<Integer> preferredUpstreamIfaceTypes;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090095 public final String[] legacyDhcpRanges;
Erik Kline79868f82017-01-21 14:33:56 +090096 public final String[] defaultIPv4DNS;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090097 public final boolean enableLegacyDhcpServer;
Erik Kline79868f82017-01-21 14:33:56 +090098
Erik Klinee0f34032018-02-28 15:01:35 +090099 public final String[] provisioningApp;
100 public final String provisioningAppNoUi;
markchien3b519632018-09-07 16:19:12 +0800101 public final int provisioningCheckPeriod;
Erik Klinee0f34032018-02-28 15:01:35 +0900102
markchien986750b2019-12-06 15:24:53 +0800103 public final int activeDataSubId;
markchien0b595072019-01-08 23:52:21 +0800104
junyulaiaea13ae2020-04-30 15:21:55 +0800105 private final int mOffloadPollInterval;
106
markchien0b595072019-01-08 23:52:21 +0800107 public TetheringConfiguration(Context ctx, SharedLog log, int id) {
Erik Kline6bd74532017-05-19 10:10:41 +0900108 final SharedLog configLog = log.forSubComponent("config");
109
markchien986750b2019-12-06 15:24:53 +0800110 activeDataSubId = id;
111 Resources res = getResources(ctx, activeDataSubId);
markchien0b595072019-01-08 23:52:21 +0800112
markchienda4519a2020-01-14 12:46:53 +0800113 tetherableUsbRegexs = getResourceStringArray(res, R.array.config_tether_usb_regexs);
Milim Lee31ef4c02019-10-17 05:02:33 +0900114 tetherableNcmRegexs = getResourceStringArray(res, R.array.config_tether_ncm_regexs);
Erik Kline9e225542017-06-08 17:48:48 +0900115 // TODO: Evaluate deleting this altogether now that Wi-Fi always passes
116 // us an interface name. Careful consideration needs to be given to
117 // implications for Settings and for provisioning checks.
markchienda4519a2020-01-14 12:46:53 +0800118 tetherableWifiRegexs = getResourceStringArray(res, R.array.config_tether_wifi_regexs);
119 tetherableWifiP2pRegexs = getResourceStringArray(
120 res, R.array.config_tether_wifi_p2p_regexs);
121 tetherableBluetoothRegexs = getResourceStringArray(
122 res, R.array.config_tether_bluetooth_regexs);
Erik Kline79868f82017-01-21 14:33:56 +0900123
markchien986750b2019-12-06 15:24:53 +0800124 isDunRequired = checkDunRequired(ctx);
Erik Kline6bd74532017-05-19 10:10:41 +0900125
markchienda4519a2020-01-14 12:46:53 +0800126 chooseUpstreamAutomatically = getResourceBoolean(
127 res, R.bool.config_tether_upstream_automatic);
markchiendb3a2362018-10-05 12:36:08 +0800128 preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired);
Erik Kline79868f82017-01-21 14:33:56 +0900129
markchien0b595072019-01-08 23:52:21 +0800130 legacyDhcpRanges = getLegacyDhcpRanges(res);
Erik Kline79868f82017-01-21 14:33:56 +0900131 defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
markchien2dfee022020-01-13 16:09:42 +0800132 enableLegacyDhcpServer = getEnableLegacyDhcpServer(res);
Erik Kline6bd74532017-05-19 10:10:41 +0900133
markchienda4519a2020-01-14 12:46:53 +0800134 provisioningApp = getResourceStringArray(res, R.array.config_mobile_hotspot_provision_app);
markchien0b595072019-01-08 23:52:21 +0800135 provisioningAppNoUi = getProvisioningAppNoUi(res);
markchien3b519632018-09-07 16:19:12 +0800136 provisioningCheckPeriod = getResourceInteger(res,
markchienda4519a2020-01-14 12:46:53 +0800137 R.integer.config_mobile_hotspot_provision_check_period,
markchien3b519632018-09-07 16:19:12 +0800138 0 /* No periodic re-check */);
Erik Klinee0f34032018-02-28 15:01:35 +0900139
junyulaiaea13ae2020-04-30 15:21:55 +0800140 mOffloadPollInterval = getResourceInteger(res,
141 R.integer.config_tether_offload_poll_interval,
142 DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS);
143
Erik Kline6bd74532017-05-19 10:10:41 +0900144 configLog.log(toString());
Erik Kline79868f82017-01-21 14:33:56 +0900145 }
146
markchiena6ba54d2019-09-03 15:58:06 +0800147 /** Check whether input interface belong to usb.*/
Erik Kline79868f82017-01-21 14:33:56 +0900148 public boolean isUsb(String iface) {
149 return matchesDownstreamRegexs(iface, tetherableUsbRegexs);
150 }
151
markchiena6ba54d2019-09-03 15:58:06 +0800152 /** Check whether input interface belong to wifi.*/
Erik Kline79868f82017-01-21 14:33:56 +0900153 public boolean isWifi(String iface) {
154 return matchesDownstreamRegexs(iface, tetherableWifiRegexs);
155 }
156
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800157 /** Check whether this interface is Wifi P2P interface. */
158 public boolean isWifiP2p(String iface) {
159 return matchesDownstreamRegexs(iface, tetherableWifiP2pRegexs);
160 }
161
markchiena6ba54d2019-09-03 15:58:06 +0800162 /** Check whether using legacy mode for wifi P2P. */
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800163 public boolean isWifiP2pLegacyTetheringMode() {
164 return (tetherableWifiP2pRegexs == null || tetherableWifiP2pRegexs.length == 0);
165 }
166
markchiena6ba54d2019-09-03 15:58:06 +0800167 /** Check whether input interface belong to bluetooth.*/
Erik Kline79868f82017-01-21 14:33:56 +0900168 public boolean isBluetooth(String iface) {
169 return matchesDownstreamRegexs(iface, tetherableBluetoothRegexs);
170 }
171
Milim Lee31ef4c02019-10-17 05:02:33 +0900172 /** Check if interface is ncm */
173 public boolean isNcm(String iface) {
174 return matchesDownstreamRegexs(iface, tetherableNcmRegexs);
175 }
176
markchiena6ba54d2019-09-03 15:58:06 +0800177 /** Check whether no ui entitlement application is available.*/
Erik Klinee0f34032018-02-28 15:01:35 +0900178 public boolean hasMobileHotspotProvisionApp() {
179 return !TextUtils.isEmpty(provisioningAppNoUi);
180 }
181
markchiena6ba54d2019-09-03 15:58:06 +0800182 /** Does the dumping.*/
Erik Kline9db1b542017-03-16 14:10:27 +0900183 public void dump(PrintWriter pw) {
markchien986750b2019-12-06 15:24:53 +0800184 pw.print("activeDataSubId: ");
185 pw.println(activeDataSubId);
markchien0b595072019-01-08 23:52:21 +0800186
Erik Kline9db1b542017-03-16 14:10:27 +0900187 dumpStringArray(pw, "tetherableUsbRegexs", tetherableUsbRegexs);
188 dumpStringArray(pw, "tetherableWifiRegexs", tetherableWifiRegexs);
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800189 dumpStringArray(pw, "tetherableWifiP2pRegexs", tetherableWifiP2pRegexs);
Erik Kline9db1b542017-03-16 14:10:27 +0900190 dumpStringArray(pw, "tetherableBluetoothRegexs", tetherableBluetoothRegexs);
Milim Lee31ef4c02019-10-17 05:02:33 +0900191 dumpStringArray(pw, "tetherableNcmRegexs", tetherableNcmRegexs);
Erik Kline9db1b542017-03-16 14:10:27 +0900192
193 pw.print("isDunRequired: ");
194 pw.println(isDunRequired);
195
Erik Kline72302902018-06-14 17:36:40 +0900196 pw.print("chooseUpstreamAutomatically: ");
197 pw.println(chooseUpstreamAutomatically);
markchien87d3f7d2020-01-08 20:58:23 +0800198 pw.print("legacyPreredUpstreamIfaceTypes: ");
199 pw.println(Arrays.toString(toIntArray(preferredUpstreamIfaceTypes)));
Erik Kline9db1b542017-03-16 14:10:27 +0900200
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900201 dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges);
Erik Kline9db1b542017-03-16 14:10:27 +0900202 dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS);
Erik Klinee0f34032018-02-28 15:01:35 +0900203
junyulaiaea13ae2020-04-30 15:21:55 +0800204 pw.print("offloadPollInterval: ");
205 pw.println(mOffloadPollInterval);
206
Erik Klinee0f34032018-02-28 15:01:35 +0900207 dumpStringArray(pw, "provisioningApp", provisioningApp);
208 pw.print("provisioningAppNoUi: ");
209 pw.println(provisioningAppNoUi);
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900210
211 pw.print("enableLegacyDhcpServer: ");
212 pw.println(enableLegacyDhcpServer);
Erik Kline9db1b542017-03-16 14:10:27 +0900213 }
214
markchiena6ba54d2019-09-03 15:58:06 +0800215 /** Returns the string representation of this object.*/
Erik Kline6bd74532017-05-19 10:10:41 +0900216 public String toString() {
217 final StringJoiner sj = new StringJoiner(" ");
markchien986750b2019-12-06 15:24:53 +0800218 sj.add(String.format("activeDataSubId:%d", activeDataSubId));
Erik Kline6bd74532017-05-19 10:10:41 +0900219 sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs)));
220 sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs)));
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800221 sj.add(String.format("tetherableWifiP2pRegexs:%s", makeString(tetherableWifiP2pRegexs)));
Erik Kline6bd74532017-05-19 10:10:41 +0900222 sj.add(String.format("tetherableBluetoothRegexs:%s",
223 makeString(tetherableBluetoothRegexs)));
224 sj.add(String.format("isDunRequired:%s", isDunRequired));
Erik Kline72302902018-06-14 17:36:40 +0900225 sj.add(String.format("chooseUpstreamAutomatically:%s", chooseUpstreamAutomatically));
junyulaiaea13ae2020-04-30 15:21:55 +0800226 sj.add(String.format("offloadPollInterval:%d", mOffloadPollInterval));
Erik Kline6bd74532017-05-19 10:10:41 +0900227 sj.add(String.format("preferredUpstreamIfaceTypes:%s",
markchien87d3f7d2020-01-08 20:58:23 +0800228 toIntArray(preferredUpstreamIfaceTypes)));
Erik Klinee0f34032018-02-28 15:01:35 +0900229 sj.add(String.format("provisioningApp:%s", makeString(provisioningApp)));
230 sj.add(String.format("provisioningAppNoUi:%s", provisioningAppNoUi));
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900231 sj.add(String.format("enableLegacyDhcpServer:%s", enableLegacyDhcpServer));
Erik Kline6bd74532017-05-19 10:10:41 +0900232 return String.format("TetheringConfiguration{%s}", sj.toString());
233 }
234
Erik Kline9db1b542017-03-16 14:10:27 +0900235 private static void dumpStringArray(PrintWriter pw, String label, String[] values) {
236 pw.print(label);
237 pw.print(": ");
238
239 if (values != null) {
240 final StringJoiner sj = new StringJoiner(", ", "[", "]");
markchiena6ba54d2019-09-03 15:58:06 +0800241 for (String value : values) sj.add(value);
Erik Kline9db1b542017-03-16 14:10:27 +0900242 pw.print(sj.toString());
243 } else {
244 pw.print("null");
245 }
246
247 pw.println();
248 }
249
Erik Kline6bd74532017-05-19 10:10:41 +0900250 private static String makeString(String[] strings) {
Erik Klinee0f34032018-02-28 15:01:35 +0900251 if (strings == null) return "null";
Erik Kline6bd74532017-05-19 10:10:41 +0900252 final StringJoiner sj = new StringJoiner(",", "[", "]");
253 for (String s : strings) sj.add(s);
254 return sj.toString();
255 }
256
markchiendb3a2362018-10-05 12:36:08 +0800257 /** Check whether dun is required. */
markchien986750b2019-12-06 15:24:53 +0800258 public static boolean checkDunRequired(Context ctx) {
Erik Kline54f2f372017-05-15 21:11:47 +0900259 final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
markchien986750b2019-12-06 15:24:53 +0800260 // TelephonyManager would uses the active data subscription, which should be the one used
261 // by tethering.
262 return (tm != null) ? tm.isTetheringApnRequired() : false;
Erik Kline79868f82017-01-21 14:33:56 +0900263 }
264
junyulaiaea13ae2020-04-30 15:21:55 +0800265 public int getOffloadPollInterval() {
266 return mOffloadPollInterval;
267 }
268
markchiendb3a2362018-10-05 12:36:08 +0800269 private static Collection<Integer> getUpstreamIfaceTypes(Resources res, boolean dunRequired) {
markchienda4519a2020-01-14 12:46:53 +0800270 final int[] ifaceTypes = res.getIntArray(R.array.config_tether_upstream_types);
Erik Kline79868f82017-01-21 14:33:56 +0900271 final ArrayList<Integer> upstreamIfaceTypes = new ArrayList<>(ifaceTypes.length);
272 for (int i : ifaceTypes) {
273 switch (i) {
274 case TYPE_MOBILE:
275 case TYPE_MOBILE_HIPRI:
markchiendb3a2362018-10-05 12:36:08 +0800276 if (dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900277 break;
278 case TYPE_MOBILE_DUN:
markchiendb3a2362018-10-05 12:36:08 +0800279 if (!dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900280 break;
281 }
282 upstreamIfaceTypes.add(i);
283 }
284
285 // Fix up upstream interface types for DUN or mobile. NOTE: independent
markchiendb3a2362018-10-05 12:36:08 +0800286 // of the value of |dunRequired|, cell data of one form or another is
Erik Kline79868f82017-01-21 14:33:56 +0900287 // *always* an upstream, regardless of the upstream interface types
288 // specified by configuration resources.
markchiendb3a2362018-10-05 12:36:08 +0800289 if (dunRequired) {
Erik Kline1e543512017-03-09 11:44:11 +0900290 appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_DUN);
Jayachandran C58059822017-05-17 23:53:59 -0700291 } else {
Jayachandran C58059822017-05-17 23:53:59 -0700292 // Do not modify if a cellular interface type is already present in the
293 // upstream interface types. Add TYPE_MOBILE and TYPE_MOBILE_HIPRI if no
294 // cellular interface types are found in the upstream interface types.
markchiendb3a2362018-10-05 12:36:08 +0800295 // This preserves backwards compatibility and prevents the DUN and default
296 // mobile types incorrectly appearing together, which could happen on
297 // previous releases in the common case where checkDunRequired returned
298 // DUN_UNSPECIFIED.
299 if (!containsOneOf(upstreamIfaceTypes, TYPE_MOBILE, TYPE_MOBILE_HIPRI)) {
Jayachandran C58059822017-05-17 23:53:59 -0700300 upstreamIfaceTypes.add(TYPE_MOBILE);
301 upstreamIfaceTypes.add(TYPE_MOBILE_HIPRI);
302 }
Erik Kline79868f82017-01-21 14:33:56 +0900303 }
304
Erik Kline1e543512017-03-09 11:44:11 +0900305 // Always make sure our good friend Ethernet is present.
306 // TODO: consider unilaterally forcing this at the front.
307 prependIfNotPresent(upstreamIfaceTypes, TYPE_ETHERNET);
308
Erik Kline79868f82017-01-21 14:33:56 +0900309 return upstreamIfaceTypes;
310 }
311
312 private static boolean matchesDownstreamRegexs(String iface, String[] regexs) {
313 for (String regex : regexs) {
314 if (iface.matches(regex)) return true;
315 }
316 return false;
317 }
318
markchien0b595072019-01-08 23:52:21 +0800319 private static String[] getLegacyDhcpRanges(Resources res) {
markchienda4519a2020-01-14 12:46:53 +0800320 final String[] fromResource = getResourceStringArray(res, R.array.config_tether_dhcp_range);
Erik Kline79868f82017-01-21 14:33:56 +0900321 if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) {
322 return fromResource;
323 }
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900324 return copy(LEGACY_DHCP_DEFAULT_RANGE);
Erik Kline79868f82017-01-21 14:33:56 +0900325 }
326
markchien0b595072019-01-08 23:52:21 +0800327 private static String getProvisioningAppNoUi(Resources res) {
Erik Klinee0f34032018-02-28 15:01:35 +0900328 try {
markchienda4519a2020-01-14 12:46:53 +0800329 return res.getString(R.string.config_mobile_hotspot_provision_app_no_ui);
Erik Klinee0f34032018-02-28 15:01:35 +0900330 } catch (Resources.NotFoundException e) {
331 return "";
332 }
333 }
334
markchien0b595072019-01-08 23:52:21 +0800335 private static boolean getResourceBoolean(Resources res, int resId) {
Erik Kline72302902018-06-14 17:36:40 +0900336 try {
markchien0b595072019-01-08 23:52:21 +0800337 return res.getBoolean(resId);
Erik Kline72302902018-06-14 17:36:40 +0900338 } catch (Resources.NotFoundException e404) {
339 return false;
340 }
341 }
342
markchien0b595072019-01-08 23:52:21 +0800343 private static String[] getResourceStringArray(Resources res, int resId) {
Erik Klinee0f34032018-02-28 15:01:35 +0900344 try {
markchien0b595072019-01-08 23:52:21 +0800345 final String[] strArray = res.getStringArray(resId);
Erik Klinee0f34032018-02-28 15:01:35 +0900346 return (strArray != null) ? strArray : EMPTY_STRING_ARRAY;
347 } catch (Resources.NotFoundException e404) {
348 return EMPTY_STRING_ARRAY;
349 }
350 }
351
markchien3b519632018-09-07 16:19:12 +0800352 private static int getResourceInteger(Resources res, int resId, int defaultValue) {
353 try {
354 return res.getInteger(resId);
355 } catch (Resources.NotFoundException e404) {
356 return defaultValue;
357 }
358 }
359
markchien2dfee022020-01-13 16:09:42 +0800360 private boolean getEnableLegacyDhcpServer(final Resources res) {
markchienda4519a2020-01-14 12:46:53 +0800361 return getResourceBoolean(res, R.bool.config_tether_enable_legacy_dhcp_server)
markchien2dfee022020-01-13 16:09:42 +0800362 || getDeviceConfigBoolean(TETHER_ENABLE_LEGACY_DHCP_SERVER);
363 }
364
365 @VisibleForTesting
366 protected boolean getDeviceConfigBoolean(final String name) {
367 return DeviceConfig.getBoolean(NAMESPACE_CONNECTIVITY, name, false /** defaultValue */);
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900368 }
369
markchien0b595072019-01-08 23:52:21 +0800370 private Resources getResources(Context ctx, int subId) {
371 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
372 return getResourcesForSubIdWrapper(ctx, subId);
373 } else {
374 return ctx.getResources();
375 }
376 }
377
378 @VisibleForTesting
379 protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) {
380 return SubscriptionManager.getResourcesForSubId(ctx, subId);
381 }
382
Erik Kline79868f82017-01-21 14:33:56 +0900383 private static String[] copy(String[] strarray) {
384 return Arrays.copyOf(strarray, strarray.length);
385 }
Erik Kline1e543512017-03-09 11:44:11 +0900386
387 private static void prependIfNotPresent(ArrayList<Integer> list, int value) {
388 if (list.contains(value)) return;
389 list.add(0, value);
390 }
391
392 private static void appendIfNotPresent(ArrayList<Integer> list, int value) {
393 if (list.contains(value)) return;
394 list.add(value);
395 }
396
397 private static boolean containsOneOf(ArrayList<Integer> list, Integer... values) {
398 for (Integer value : values) {
399 if (list.contains(value)) return true;
400 }
401 return false;
402 }
markchien0df2ebc42019-09-30 14:40:57 +0800403
markchien87d3f7d2020-01-08 20:58:23 +0800404 private static int[] toIntArray(Collection<Integer> values) {
405 final int[] result = new int[values.size()];
406 int index = 0;
407 for (Integer value : values) {
408 result[index++] = value;
409 }
410 return result;
411 }
412
markchien0df2ebc42019-09-30 14:40:57 +0800413 /**
414 * Convert this TetheringConfiguration to a TetheringConfigurationParcel.
415 */
416 public TetheringConfigurationParcel toStableParcelable() {
417 final TetheringConfigurationParcel parcel = new TetheringConfigurationParcel();
markchien986750b2019-12-06 15:24:53 +0800418 parcel.subId = activeDataSubId;
markchien0df2ebc42019-09-30 14:40:57 +0800419 parcel.tetherableUsbRegexs = tetherableUsbRegexs;
420 parcel.tetherableWifiRegexs = tetherableWifiRegexs;
421 parcel.tetherableBluetoothRegexs = tetherableBluetoothRegexs;
422 parcel.isDunRequired = isDunRequired;
423 parcel.chooseUpstreamAutomatically = chooseUpstreamAutomatically;
424
markchien87d3f7d2020-01-08 20:58:23 +0800425 parcel.preferredUpstreamIfaceTypes = toIntArray(preferredUpstreamIfaceTypes);
markchien0df2ebc42019-09-30 14:40:57 +0800426
427 parcel.legacyDhcpRanges = legacyDhcpRanges;
428 parcel.defaultIPv4DNS = defaultIPv4DNS;
429 parcel.enableLegacyDhcpServer = enableLegacyDhcpServer;
430 parcel.provisioningApp = provisioningApp;
431 parcel.provisioningAppNoUi = provisioningAppNoUi;
432 parcel.provisioningCheckPeriod = provisioningCheckPeriod;
433 return parcel;
434 }
Erik Kline79868f82017-01-21 14:33:56 +0900435}