blob: 0ab4d634e84dac4fede70a10cba640fcbb227dab [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
17package com.android.server.connectivity.tethering;
18
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;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090024import static android.provider.Settings.Global.TETHER_ENABLE_LEGACY_DHCP_SERVER;
25
Erik Klinee0f34032018-02-28 15:01:35 +090026import static com.android.internal.R.array.config_mobile_hotspot_provision_app;
27import static com.android.internal.R.array.config_tether_bluetooth_regexs;
28import static com.android.internal.R.array.config_tether_dhcp_range;
Erik Klinee0f34032018-02-28 15:01:35 +090029import static com.android.internal.R.array.config_tether_upstream_types;
markchien0b595072019-01-08 23:52:21 +080030import static com.android.internal.R.array.config_tether_usb_regexs;
Jimmy Chenbcd86d02019-07-15 18:03:23 +080031import static com.android.internal.R.array.config_tether_wifi_p2p_regexs;
Erik Klinee0f34032018-02-28 15:01:35 +090032import static com.android.internal.R.array.config_tether_wifi_regexs;
Erik Kline72302902018-06-14 17:36:40 +090033import static com.android.internal.R.bool.config_tether_upstream_automatic;
markchien3b519632018-09-07 16:19:12 +080034import static com.android.internal.R.integer.config_mobile_hotspot_provision_check_period;
Erik Klinee0f34032018-02-28 15:01:35 +090035import static com.android.internal.R.string.config_mobile_hotspot_provision_app_no_ui;
Erik Kline79868f82017-01-21 14:33:56 +090036
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090037import android.content.ContentResolver;
Erik Kline79868f82017-01-21 14:33:56 +090038import android.content.Context;
39import android.content.res.Resources;
Erik Kline9db1b542017-03-16 14:10:27 +090040import android.net.ConnectivityManager;
markchien0df2ebc42019-09-30 14:40:57 +080041import android.net.TetheringConfigurationParcel;
Erik Klinee93ed612018-04-10 07:01:16 +000042import android.net.util.SharedLog;
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090043import android.provider.Settings;
markchien0b595072019-01-08 23:52:21 +080044import android.telephony.SubscriptionManager;
Erik Klinee0f34032018-02-28 15:01:35 +090045import android.telephony.TelephonyManager;
46import android.text.TextUtils;
Erik Kline79868f82017-01-21 14:33:56 +090047
Chalard Jean918a68b2018-01-19 17:00:47 +090048import com.android.internal.annotations.VisibleForTesting;
49
Erik Kline9db1b542017-03-16 14:10:27 +090050import java.io.PrintWriter;
Erik Kline79868f82017-01-21 14:33:56 +090051import java.util.ArrayList;
52import java.util.Arrays;
53import java.util.Collection;
Erik Kline9db1b542017-03-16 14:10:27 +090054import java.util.StringJoiner;
Erik Kline79868f82017-01-21 14:33:56 +090055
56
57/**
58 * A utility class to encapsulate the various tethering configuration elements.
59 *
60 * This configuration data includes elements describing upstream properties
61 * (preferred and required types of upstream connectivity as well as default
62 * DNS servers to use if none are available) and downstream properties (such
63 * as regular expressions use to match suitable downstream interfaces and the
64 * DHCPv4 ranges to use).
65 *
66 * @hide
67 */
68public class TetheringConfiguration {
69 private static final String TAG = TetheringConfiguration.class.getSimpleName();
70
Erik Klinee0f34032018-02-28 15:01:35 +090071 private static final String[] EMPTY_STRING_ARRAY = new String[0];
72
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090073 // Default ranges used for the legacy DHCP server.
Erik Kline79868f82017-01-21 14:33:56 +090074 // USB is 192.168.42.1 and 255.255.255.0
75 // Wifi is 192.168.43.1 and 255.255.255.0
76 // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
77 // with 255.255.255.0
78 // P2P is 192.168.49.1 and 255.255.255.0
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +090079 private static final String[] LEGACY_DHCP_DEFAULT_RANGE = {
Erik Kline79868f82017-01-21 14:33:56 +090080 "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
81 "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
82 "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
83 "192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
84 };
85
markchiena6ba54d2019-09-03 15:58:06 +080086 private static final String[] DEFAULT_IPV4_DNS = {"8.8.4.4", "8.8.8.8"};
Erik Kline79868f82017-01-21 14:33:56 +090087
88 public final String[] tetherableUsbRegexs;
89 public final String[] tetherableWifiRegexs;
Jimmy Chenbcd86d02019-07-15 18:03:23 +080090 public final String[] tetherableWifiP2pRegexs;
Erik Kline79868f82017-01-21 14:33:56 +090091 public final String[] tetherableBluetoothRegexs;
92 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
markchien0b595072019-01-08 23:52:21 +0800103 public final int subId;
104
105 public TetheringConfiguration(Context ctx, SharedLog log, int id) {
Erik Kline6bd74532017-05-19 10:10:41 +0900106 final SharedLog configLog = log.forSubComponent("config");
107
markchien0b595072019-01-08 23:52:21 +0800108 subId = id;
109 Resources res = getResources(ctx, subId);
110
111 tetherableUsbRegexs = getResourceStringArray(res, config_tether_usb_regexs);
Erik Kline9e225542017-06-08 17:48:48 +0900112 // TODO: Evaluate deleting this altogether now that Wi-Fi always passes
113 // us an interface name. Careful consideration needs to be given to
114 // implications for Settings and for provisioning checks.
markchien0b595072019-01-08 23:52:21 +0800115 tetherableWifiRegexs = getResourceStringArray(res, config_tether_wifi_regexs);
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800116 tetherableWifiP2pRegexs = getResourceStringArray(res, config_tether_wifi_p2p_regexs);
markchien0b595072019-01-08 23:52:21 +0800117 tetherableBluetoothRegexs = getResourceStringArray(res, config_tether_bluetooth_regexs);
Erik Kline79868f82017-01-21 14:33:56 +0900118
markchien04bdf872019-06-17 21:05:34 +0800119 isDunRequired = checkDunRequired(ctx, subId);
Erik Kline6bd74532017-05-19 10:10:41 +0900120
markchien0b595072019-01-08 23:52:21 +0800121 chooseUpstreamAutomatically = getResourceBoolean(res, config_tether_upstream_automatic);
markchiendb3a2362018-10-05 12:36:08 +0800122 preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired);
Erik Kline79868f82017-01-21 14:33:56 +0900123
markchien0b595072019-01-08 23:52:21 +0800124 legacyDhcpRanges = getLegacyDhcpRanges(res);
Erik Kline79868f82017-01-21 14:33:56 +0900125 defaultIPv4DNS = copy(DEFAULT_IPV4_DNS);
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900126 enableLegacyDhcpServer = getEnableLegacyDhcpServer(ctx);
Erik Kline6bd74532017-05-19 10:10:41 +0900127
markchien0b595072019-01-08 23:52:21 +0800128 provisioningApp = getResourceStringArray(res, config_mobile_hotspot_provision_app);
129 provisioningAppNoUi = getProvisioningAppNoUi(res);
markchien3b519632018-09-07 16:19:12 +0800130 provisioningCheckPeriod = getResourceInteger(res,
131 config_mobile_hotspot_provision_check_period,
132 0 /* No periodic re-check */);
Erik Klinee0f34032018-02-28 15:01:35 +0900133
Erik Kline6bd74532017-05-19 10:10:41 +0900134 configLog.log(toString());
Erik Kline79868f82017-01-21 14:33:56 +0900135 }
136
markchiena6ba54d2019-09-03 15:58:06 +0800137 /** Check whether input interface belong to usb.*/
Erik Kline79868f82017-01-21 14:33:56 +0900138 public boolean isUsb(String iface) {
139 return matchesDownstreamRegexs(iface, tetherableUsbRegexs);
140 }
141
markchiena6ba54d2019-09-03 15:58:06 +0800142 /** Check whether input interface belong to wifi.*/
Erik Kline79868f82017-01-21 14:33:56 +0900143 public boolean isWifi(String iface) {
144 return matchesDownstreamRegexs(iface, tetherableWifiRegexs);
145 }
146
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800147 /** Check whether this interface is Wifi P2P interface. */
148 public boolean isWifiP2p(String iface) {
149 return matchesDownstreamRegexs(iface, tetherableWifiP2pRegexs);
150 }
151
markchiena6ba54d2019-09-03 15:58:06 +0800152 /** Check whether using legacy mode for wifi P2P. */
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800153 public boolean isWifiP2pLegacyTetheringMode() {
154 return (tetherableWifiP2pRegexs == null || tetherableWifiP2pRegexs.length == 0);
155 }
156
markchiena6ba54d2019-09-03 15:58:06 +0800157 /** Check whether input interface belong to bluetooth.*/
Erik Kline79868f82017-01-21 14:33:56 +0900158 public boolean isBluetooth(String iface) {
159 return matchesDownstreamRegexs(iface, tetherableBluetoothRegexs);
160 }
161
markchiena6ba54d2019-09-03 15:58:06 +0800162 /** Check whether no ui entitlement application is available.*/
Erik Klinee0f34032018-02-28 15:01:35 +0900163 public boolean hasMobileHotspotProvisionApp() {
164 return !TextUtils.isEmpty(provisioningAppNoUi);
165 }
166
markchiena6ba54d2019-09-03 15:58:06 +0800167 /** Does the dumping.*/
Erik Kline9db1b542017-03-16 14:10:27 +0900168 public void dump(PrintWriter pw) {
markchien0b595072019-01-08 23:52:21 +0800169 pw.print("subId: ");
170 pw.println(subId);
171
Erik Kline9db1b542017-03-16 14:10:27 +0900172 dumpStringArray(pw, "tetherableUsbRegexs", tetherableUsbRegexs);
173 dumpStringArray(pw, "tetherableWifiRegexs", tetherableWifiRegexs);
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800174 dumpStringArray(pw, "tetherableWifiP2pRegexs", tetherableWifiP2pRegexs);
Erik Kline9db1b542017-03-16 14:10:27 +0900175 dumpStringArray(pw, "tetherableBluetoothRegexs", tetherableBluetoothRegexs);
176
177 pw.print("isDunRequired: ");
178 pw.println(isDunRequired);
179
Erik Kline72302902018-06-14 17:36:40 +0900180 pw.print("chooseUpstreamAutomatically: ");
181 pw.println(chooseUpstreamAutomatically);
Erik Kline6bd74532017-05-19 10:10:41 +0900182 dumpStringArray(pw, "preferredUpstreamIfaceTypes",
183 preferredUpstreamNames(preferredUpstreamIfaceTypes));
Erik Kline9db1b542017-03-16 14:10:27 +0900184
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900185 dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges);
Erik Kline9db1b542017-03-16 14:10:27 +0900186 dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS);
Erik Klinee0f34032018-02-28 15:01:35 +0900187
188 dumpStringArray(pw, "provisioningApp", provisioningApp);
189 pw.print("provisioningAppNoUi: ");
190 pw.println(provisioningAppNoUi);
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900191
192 pw.print("enableLegacyDhcpServer: ");
193 pw.println(enableLegacyDhcpServer);
Erik Kline9db1b542017-03-16 14:10:27 +0900194 }
195
markchiena6ba54d2019-09-03 15:58:06 +0800196 /** Returns the string representation of this object.*/
Erik Kline6bd74532017-05-19 10:10:41 +0900197 public String toString() {
198 final StringJoiner sj = new StringJoiner(" ");
markchien0b595072019-01-08 23:52:21 +0800199 sj.add(String.format("subId:%d", subId));
Erik Kline6bd74532017-05-19 10:10:41 +0900200 sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs)));
201 sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs)));
Jimmy Chenbcd86d02019-07-15 18:03:23 +0800202 sj.add(String.format("tetherableWifiP2pRegexs:%s", makeString(tetherableWifiP2pRegexs)));
Erik Kline6bd74532017-05-19 10:10:41 +0900203 sj.add(String.format("tetherableBluetoothRegexs:%s",
204 makeString(tetherableBluetoothRegexs)));
205 sj.add(String.format("isDunRequired:%s", isDunRequired));
Erik Kline72302902018-06-14 17:36:40 +0900206 sj.add(String.format("chooseUpstreamAutomatically:%s", chooseUpstreamAutomatically));
Erik Kline6bd74532017-05-19 10:10:41 +0900207 sj.add(String.format("preferredUpstreamIfaceTypes:%s",
208 makeString(preferredUpstreamNames(preferredUpstreamIfaceTypes))));
Erik Klinee0f34032018-02-28 15:01:35 +0900209 sj.add(String.format("provisioningApp:%s", makeString(provisioningApp)));
210 sj.add(String.format("provisioningAppNoUi:%s", provisioningAppNoUi));
Remi NGUYEN VAN9865cc12018-08-30 16:49:52 +0900211 sj.add(String.format("enableLegacyDhcpServer:%s", enableLegacyDhcpServer));
Erik Kline6bd74532017-05-19 10:10:41 +0900212 return String.format("TetheringConfiguration{%s}", sj.toString());
213 }
214
Erik Kline9db1b542017-03-16 14:10:27 +0900215 private static void dumpStringArray(PrintWriter pw, String label, String[] values) {
216 pw.print(label);
217 pw.print(": ");
218
219 if (values != null) {
220 final StringJoiner sj = new StringJoiner(", ", "[", "]");
markchiena6ba54d2019-09-03 15:58:06 +0800221 for (String value : values) sj.add(value);
Erik Kline9db1b542017-03-16 14:10:27 +0900222 pw.print(sj.toString());
223 } else {
224 pw.print("null");
225 }
226
227 pw.println();
228 }
229
Erik Kline6bd74532017-05-19 10:10:41 +0900230 private static String makeString(String[] strings) {
Erik Klinee0f34032018-02-28 15:01:35 +0900231 if (strings == null) return "null";
Erik Kline6bd74532017-05-19 10:10:41 +0900232 final StringJoiner sj = new StringJoiner(",", "[", "]");
233 for (String s : strings) sj.add(s);
234 return sj.toString();
235 }
236
237 private static String[] preferredUpstreamNames(Collection<Integer> upstreamTypes) {
238 String[] upstreamNames = null;
239
240 if (upstreamTypes != null) {
241 upstreamNames = new String[upstreamTypes.size()];
242 int i = 0;
243 for (Integer netType : upstreamTypes) {
244 upstreamNames[i] = ConnectivityManager.getNetworkTypeName(netType);
245 i++;
246 }
247 }
248
249 return upstreamNames;
250 }
251
markchiendb3a2362018-10-05 12:36:08 +0800252 /** Check whether dun is required. */
markchien04bdf872019-06-17 21:05:34 +0800253 public static boolean checkDunRequired(Context ctx, int id) {
Erik Kline54f2f372017-05-15 21:11:47 +0900254 final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE);
SongFerngWang8a90fc02019-11-15 14:50:52 +0800255 return (tm != null) ? tm.isTetheringApnRequired(id) : false;
Erik Kline79868f82017-01-21 14:33:56 +0900256 }
257
markchiendb3a2362018-10-05 12:36:08 +0800258 private static Collection<Integer> getUpstreamIfaceTypes(Resources res, boolean dunRequired) {
markchien0b595072019-01-08 23:52:21 +0800259 final int[] ifaceTypes = res.getIntArray(config_tether_upstream_types);
Erik Kline79868f82017-01-21 14:33:56 +0900260 final ArrayList<Integer> upstreamIfaceTypes = new ArrayList<>(ifaceTypes.length);
261 for (int i : ifaceTypes) {
262 switch (i) {
263 case TYPE_MOBILE:
264 case TYPE_MOBILE_HIPRI:
markchiendb3a2362018-10-05 12:36:08 +0800265 if (dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900266 break;
267 case TYPE_MOBILE_DUN:
markchiendb3a2362018-10-05 12:36:08 +0800268 if (!dunRequired) continue;
Erik Kline79868f82017-01-21 14:33:56 +0900269 break;
270 }
271 upstreamIfaceTypes.add(i);
272 }
273
274 // Fix up upstream interface types for DUN or mobile. NOTE: independent
markchiendb3a2362018-10-05 12:36:08 +0800275 // of the value of |dunRequired|, cell data of one form or another is
Erik Kline79868f82017-01-21 14:33:56 +0900276 // *always* an upstream, regardless of the upstream interface types
277 // specified by configuration resources.
markchiendb3a2362018-10-05 12:36:08 +0800278 if (dunRequired) {
Erik Kline1e543512017-03-09 11:44:11 +0900279 appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_DUN);
Jayachandran C58059822017-05-17 23:53:59 -0700280 } else {
Jayachandran C58059822017-05-17 23:53:59 -0700281 // Do not modify if a cellular interface type is already present in the
282 // upstream interface types. Add TYPE_MOBILE and TYPE_MOBILE_HIPRI if no
283 // cellular interface types are found in the upstream interface types.
markchiendb3a2362018-10-05 12:36:08 +0800284 // This preserves backwards compatibility and prevents the DUN and default
285 // mobile types incorrectly appearing together, which could happen on
286 // previous releases in the common case where checkDunRequired returned
287 // DUN_UNSPECIFIED.
288 if (!containsOneOf(upstreamIfaceTypes, TYPE_MOBILE, TYPE_MOBILE_HIPRI)) {
Jayachandran C58059822017-05-17 23:53:59 -0700289 upstreamIfaceTypes.add(TYPE_MOBILE);
290 upstreamIfaceTypes.add(TYPE_MOBILE_HIPRI);
291 }
Erik Kline79868f82017-01-21 14:33:56 +0900292 }
293
Erik Kline1e543512017-03-09 11:44:11 +0900294 // Always make sure our good friend Ethernet is present.
295 // TODO: consider unilaterally forcing this at the front.
296 prependIfNotPresent(upstreamIfaceTypes, TYPE_ETHERNET);
297
Erik Kline79868f82017-01-21 14:33:56 +0900298 return upstreamIfaceTypes;
299 }
300
301 private static boolean matchesDownstreamRegexs(String iface, String[] regexs) {
302 for (String regex : regexs) {
303 if (iface.matches(regex)) return true;
304 }
305 return false;
306 }
307
markchien0b595072019-01-08 23:52:21 +0800308 private static String[] getLegacyDhcpRanges(Resources res) {
309 final String[] fromResource = getResourceStringArray(res, config_tether_dhcp_range);
Erik Kline79868f82017-01-21 14:33:56 +0900310 if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) {
311 return fromResource;
312 }
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900313 return copy(LEGACY_DHCP_DEFAULT_RANGE);
Erik Kline79868f82017-01-21 14:33:56 +0900314 }
315
markchien0b595072019-01-08 23:52:21 +0800316 private static String getProvisioningAppNoUi(Resources res) {
Erik Klinee0f34032018-02-28 15:01:35 +0900317 try {
markchien0b595072019-01-08 23:52:21 +0800318 return res.getString(config_mobile_hotspot_provision_app_no_ui);
Erik Klinee0f34032018-02-28 15:01:35 +0900319 } catch (Resources.NotFoundException e) {
320 return "";
321 }
322 }
323
markchien0b595072019-01-08 23:52:21 +0800324 private static boolean getResourceBoolean(Resources res, int resId) {
Erik Kline72302902018-06-14 17:36:40 +0900325 try {
markchien0b595072019-01-08 23:52:21 +0800326 return res.getBoolean(resId);
Erik Kline72302902018-06-14 17:36:40 +0900327 } catch (Resources.NotFoundException e404) {
328 return false;
329 }
330 }
331
markchien0b595072019-01-08 23:52:21 +0800332 private static String[] getResourceStringArray(Resources res, int resId) {
Erik Klinee0f34032018-02-28 15:01:35 +0900333 try {
markchien0b595072019-01-08 23:52:21 +0800334 final String[] strArray = res.getStringArray(resId);
Erik Klinee0f34032018-02-28 15:01:35 +0900335 return (strArray != null) ? strArray : EMPTY_STRING_ARRAY;
336 } catch (Resources.NotFoundException e404) {
337 return EMPTY_STRING_ARRAY;
338 }
339 }
340
markchien3b519632018-09-07 16:19:12 +0800341 private static int getResourceInteger(Resources res, int resId, int defaultValue) {
342 try {
343 return res.getInteger(resId);
344 } catch (Resources.NotFoundException e404) {
345 return defaultValue;
346 }
347 }
348
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900349 private static boolean getEnableLegacyDhcpServer(Context ctx) {
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900350 final ContentResolver cr = ctx.getContentResolver();
Remi NGUYEN VANe0d8c0e2018-09-26 18:11:48 +0900351 final int intVal = Settings.Global.getInt(cr, TETHER_ENABLE_LEGACY_DHCP_SERVER, 0);
Remi NGUYEN VANe3bb5c52018-06-12 15:57:04 +0900352 return intVal != 0;
353 }
354
markchien0b595072019-01-08 23:52:21 +0800355 private Resources getResources(Context ctx, int subId) {
356 if (subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
357 return getResourcesForSubIdWrapper(ctx, subId);
358 } else {
359 return ctx.getResources();
360 }
361 }
362
363 @VisibleForTesting
364 protected Resources getResourcesForSubIdWrapper(Context ctx, int subId) {
365 return SubscriptionManager.getResourcesForSubId(ctx, subId);
366 }
367
Erik Kline79868f82017-01-21 14:33:56 +0900368 private static String[] copy(String[] strarray) {
369 return Arrays.copyOf(strarray, strarray.length);
370 }
Erik Kline1e543512017-03-09 11:44:11 +0900371
372 private static void prependIfNotPresent(ArrayList<Integer> list, int value) {
373 if (list.contains(value)) return;
374 list.add(0, value);
375 }
376
377 private static void appendIfNotPresent(ArrayList<Integer> list, int value) {
378 if (list.contains(value)) return;
379 list.add(value);
380 }
381
382 private static boolean containsOneOf(ArrayList<Integer> list, Integer... values) {
383 for (Integer value : values) {
384 if (list.contains(value)) return true;
385 }
386 return false;
387 }
markchien0df2ebc42019-09-30 14:40:57 +0800388
389 /**
390 * Convert this TetheringConfiguration to a TetheringConfigurationParcel.
391 */
392 public TetheringConfigurationParcel toStableParcelable() {
393 final TetheringConfigurationParcel parcel = new TetheringConfigurationParcel();
394 parcel.subId = subId;
395 parcel.tetherableUsbRegexs = tetherableUsbRegexs;
396 parcel.tetherableWifiRegexs = tetherableWifiRegexs;
397 parcel.tetherableBluetoothRegexs = tetherableBluetoothRegexs;
398 parcel.isDunRequired = isDunRequired;
399 parcel.chooseUpstreamAutomatically = chooseUpstreamAutomatically;
400
401 int[] preferredTypes = new int[preferredUpstreamIfaceTypes.size()];
402 int index = 0;
403 for (Integer type : preferredUpstreamIfaceTypes) {
404 preferredTypes[index++] = type;
405 }
406 parcel.preferredUpstreamIfaceTypes = preferredTypes;
407
408 parcel.legacyDhcpRanges = legacyDhcpRanges;
409 parcel.defaultIPv4DNS = defaultIPv4DNS;
410 parcel.enableLegacyDhcpServer = enableLegacyDhcpServer;
411 parcel.provisioningApp = provisioningApp;
412 parcel.provisioningAppNoUi = provisioningAppNoUi;
413 parcel.provisioningCheckPeriod = provisioningCheckPeriod;
414 return parcel;
415 }
Erik Kline79868f82017-01-21 14:33:56 +0900416}