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; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 31 | import static com.android.internal.R.array.config_tether_wifi_p2p_regexs; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 32 | import static com.android.internal.R.array.config_tether_wifi_regexs; |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 33 | import static com.android.internal.R.bool.config_tether_upstream_automatic; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 34 | import static com.android.internal.R.integer.config_mobile_hotspot_provision_check_period; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 35 | 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] | 36 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 37 | import android.content.ContentResolver; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 38 | import android.content.Context; |
| 39 | import android.content.res.Resources; |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 40 | import android.net.ConnectivityManager; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 41 | import android.net.TetheringConfigurationParcel; |
Erik Kline | e93ed61 | 2018-04-10 07:01:16 +0000 | [diff] [blame] | 42 | import android.net.util.SharedLog; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 43 | import android.provider.Settings; |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 44 | import android.telephony.SubscriptionManager; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 45 | import android.telephony.TelephonyManager; |
| 46 | import android.text.TextUtils; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 47 | |
Chalard Jean | 918a68b | 2018-01-19 17:00:47 +0900 | [diff] [blame] | 48 | import com.android.internal.annotations.VisibleForTesting; |
| 49 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 50 | import java.io.PrintWriter; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 51 | import java.util.ArrayList; |
| 52 | import java.util.Arrays; |
| 53 | import java.util.Collection; |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 54 | import java.util.StringJoiner; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 55 | |
| 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 | */ |
| 68 | public class TetheringConfiguration { |
| 69 | private static final String TAG = TetheringConfiguration.class.getSimpleName(); |
| 70 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 71 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 72 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 73 | // Default ranges used for the legacy DHCP server. |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 74 | // 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 VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 79 | private static final String[] LEGACY_DHCP_DEFAULT_RANGE = { |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 80 | "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 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 86 | private static final String[] DEFAULT_IPV4_DNS = {"8.8.4.4", "8.8.8.8"}; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 87 | |
| 88 | public final String[] tetherableUsbRegexs; |
| 89 | public final String[] tetherableWifiRegexs; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 90 | public final String[] tetherableWifiP2pRegexs; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 91 | public final String[] tetherableBluetoothRegexs; |
| 92 | public final boolean isDunRequired; |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 93 | public final boolean chooseUpstreamAutomatically; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 94 | public final Collection<Integer> preferredUpstreamIfaceTypes; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 95 | public final String[] legacyDhcpRanges; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 96 | public final String[] defaultIPv4DNS; |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 97 | public final boolean enableLegacyDhcpServer; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 98 | |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 99 | public final String[] provisioningApp; |
| 100 | public final String provisioningAppNoUi; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 101 | public final int provisioningCheckPeriod; |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 102 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 103 | public final int subId; |
| 104 | |
| 105 | public TetheringConfiguration(Context ctx, SharedLog log, int id) { |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 106 | final SharedLog configLog = log.forSubComponent("config"); |
| 107 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 108 | subId = id; |
| 109 | Resources res = getResources(ctx, subId); |
| 110 | |
| 111 | tetherableUsbRegexs = getResourceStringArray(res, config_tether_usb_regexs); |
Erik Kline | 9e22554 | 2017-06-08 17:48:48 +0900 | [diff] [blame] | 112 | // 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. |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 115 | tetherableWifiRegexs = getResourceStringArray(res, config_tether_wifi_regexs); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 116 | tetherableWifiP2pRegexs = getResourceStringArray(res, config_tether_wifi_p2p_regexs); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 117 | tetherableBluetoothRegexs = getResourceStringArray(res, config_tether_bluetooth_regexs); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 118 | |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 119 | isDunRequired = checkDunRequired(ctx, subId); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 120 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 121 | chooseUpstreamAutomatically = getResourceBoolean(res, config_tether_upstream_automatic); |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 122 | preferredUpstreamIfaceTypes = getUpstreamIfaceTypes(res, isDunRequired); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 123 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 124 | legacyDhcpRanges = getLegacyDhcpRanges(res); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 125 | defaultIPv4DNS = copy(DEFAULT_IPV4_DNS); |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 126 | enableLegacyDhcpServer = getEnableLegacyDhcpServer(ctx); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 127 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 128 | provisioningApp = getResourceStringArray(res, config_mobile_hotspot_provision_app); |
| 129 | provisioningAppNoUi = getProvisioningAppNoUi(res); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 130 | provisioningCheckPeriod = getResourceInteger(res, |
| 131 | config_mobile_hotspot_provision_check_period, |
| 132 | 0 /* No periodic re-check */); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 133 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 134 | configLog.log(toString()); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 135 | } |
| 136 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 137 | /** Check whether input interface belong to usb.*/ |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 138 | public boolean isUsb(String iface) { |
| 139 | return matchesDownstreamRegexs(iface, tetherableUsbRegexs); |
| 140 | } |
| 141 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 142 | /** Check whether input interface belong to wifi.*/ |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 143 | public boolean isWifi(String iface) { |
| 144 | return matchesDownstreamRegexs(iface, tetherableWifiRegexs); |
| 145 | } |
| 146 | |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 147 | /** Check whether this interface is Wifi P2P interface. */ |
| 148 | public boolean isWifiP2p(String iface) { |
| 149 | return matchesDownstreamRegexs(iface, tetherableWifiP2pRegexs); |
| 150 | } |
| 151 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 152 | /** Check whether using legacy mode for wifi P2P. */ |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 153 | public boolean isWifiP2pLegacyTetheringMode() { |
| 154 | return (tetherableWifiP2pRegexs == null || tetherableWifiP2pRegexs.length == 0); |
| 155 | } |
| 156 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 157 | /** Check whether input interface belong to bluetooth.*/ |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 158 | public boolean isBluetooth(String iface) { |
| 159 | return matchesDownstreamRegexs(iface, tetherableBluetoothRegexs); |
| 160 | } |
| 161 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 162 | /** Check whether no ui entitlement application is available.*/ |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 163 | public boolean hasMobileHotspotProvisionApp() { |
| 164 | return !TextUtils.isEmpty(provisioningAppNoUi); |
| 165 | } |
| 166 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 167 | /** Does the dumping.*/ |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 168 | public void dump(PrintWriter pw) { |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 169 | pw.print("subId: "); |
| 170 | pw.println(subId); |
| 171 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 172 | dumpStringArray(pw, "tetherableUsbRegexs", tetherableUsbRegexs); |
| 173 | dumpStringArray(pw, "tetherableWifiRegexs", tetherableWifiRegexs); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 174 | dumpStringArray(pw, "tetherableWifiP2pRegexs", tetherableWifiP2pRegexs); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 175 | dumpStringArray(pw, "tetherableBluetoothRegexs", tetherableBluetoothRegexs); |
| 176 | |
| 177 | pw.print("isDunRequired: "); |
| 178 | pw.println(isDunRequired); |
| 179 | |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 180 | pw.print("chooseUpstreamAutomatically: "); |
| 181 | pw.println(chooseUpstreamAutomatically); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 182 | dumpStringArray(pw, "preferredUpstreamIfaceTypes", |
| 183 | preferredUpstreamNames(preferredUpstreamIfaceTypes)); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 184 | |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 185 | dumpStringArray(pw, "legacyDhcpRanges", legacyDhcpRanges); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 186 | dumpStringArray(pw, "defaultIPv4DNS", defaultIPv4DNS); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 187 | |
| 188 | dumpStringArray(pw, "provisioningApp", provisioningApp); |
| 189 | pw.print("provisioningAppNoUi: "); |
| 190 | pw.println(provisioningAppNoUi); |
Remi NGUYEN VAN | 9865cc1 | 2018-08-30 16:49:52 +0900 | [diff] [blame] | 191 | |
| 192 | pw.print("enableLegacyDhcpServer: "); |
| 193 | pw.println(enableLegacyDhcpServer); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 194 | } |
| 195 | |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 196 | /** Returns the string representation of this object.*/ |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 197 | public String toString() { |
| 198 | final StringJoiner sj = new StringJoiner(" "); |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 199 | sj.add(String.format("subId:%d", subId)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 200 | sj.add(String.format("tetherableUsbRegexs:%s", makeString(tetherableUsbRegexs))); |
| 201 | sj.add(String.format("tetherableWifiRegexs:%s", makeString(tetherableWifiRegexs))); |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 202 | sj.add(String.format("tetherableWifiP2pRegexs:%s", makeString(tetherableWifiP2pRegexs))); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 203 | sj.add(String.format("tetherableBluetoothRegexs:%s", |
| 204 | makeString(tetherableBluetoothRegexs))); |
| 205 | sj.add(String.format("isDunRequired:%s", isDunRequired)); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 206 | sj.add(String.format("chooseUpstreamAutomatically:%s", chooseUpstreamAutomatically)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 207 | sj.add(String.format("preferredUpstreamIfaceTypes:%s", |
| 208 | makeString(preferredUpstreamNames(preferredUpstreamIfaceTypes)))); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 209 | sj.add(String.format("provisioningApp:%s", makeString(provisioningApp))); |
| 210 | sj.add(String.format("provisioningAppNoUi:%s", provisioningAppNoUi)); |
Remi NGUYEN VAN | 9865cc1 | 2018-08-30 16:49:52 +0900 | [diff] [blame] | 211 | sj.add(String.format("enableLegacyDhcpServer:%s", enableLegacyDhcpServer)); |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 212 | return String.format("TetheringConfiguration{%s}", sj.toString()); |
| 213 | } |
| 214 | |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 215 | 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(", ", "[", "]"); |
markchien | a6ba54d | 2019-09-03 15:58:06 +0800 | [diff] [blame] | 221 | for (String value : values) sj.add(value); |
Erik Kline | 9db1b54 | 2017-03-16 14:10:27 +0900 | [diff] [blame] | 222 | pw.print(sj.toString()); |
| 223 | } else { |
| 224 | pw.print("null"); |
| 225 | } |
| 226 | |
| 227 | pw.println(); |
| 228 | } |
| 229 | |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 230 | private static String makeString(String[] strings) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 231 | if (strings == null) return "null"; |
Erik Kline | 6bd7453 | 2017-05-19 10:10:41 +0900 | [diff] [blame] | 232 | 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 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 252 | /** Check whether dun is required. */ |
markchien | 04bdf87 | 2019-06-17 21:05:34 +0800 | [diff] [blame] | 253 | public static boolean checkDunRequired(Context ctx, int id) { |
Erik Kline | 54f2f37 | 2017-05-15 21:11:47 +0900 | [diff] [blame] | 254 | final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE); |
SongFerngWang | 8a90fc0 | 2019-11-15 14:50:52 +0800 | [diff] [blame] | 255 | return (tm != null) ? tm.isTetheringApnRequired(id) : false; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 256 | } |
| 257 | |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 258 | private static Collection<Integer> getUpstreamIfaceTypes(Resources res, boolean dunRequired) { |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 259 | final int[] ifaceTypes = res.getIntArray(config_tether_upstream_types); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 260 | 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: |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 265 | if (dunRequired) continue; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 266 | break; |
| 267 | case TYPE_MOBILE_DUN: |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 268 | if (!dunRequired) continue; |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | upstreamIfaceTypes.add(i); |
| 272 | } |
| 273 | |
| 274 | // Fix up upstream interface types for DUN or mobile. NOTE: independent |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 275 | // 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] | 276 | // *always* an upstream, regardless of the upstream interface types |
| 277 | // specified by configuration resources. |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 278 | if (dunRequired) { |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 279 | appendIfNotPresent(upstreamIfaceTypes, TYPE_MOBILE_DUN); |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 280 | } else { |
Jayachandran C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 281 | // 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. |
markchien | db3a236 | 2018-10-05 12:36:08 +0800 | [diff] [blame] | 284 | // 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 C | 5805982 | 2017-05-17 23:53:59 -0700 | [diff] [blame] | 289 | upstreamIfaceTypes.add(TYPE_MOBILE); |
| 290 | upstreamIfaceTypes.add(TYPE_MOBILE_HIPRI); |
| 291 | } |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 292 | } |
| 293 | |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 294 | // 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 Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 298 | 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 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 308 | private static String[] getLegacyDhcpRanges(Resources res) { |
| 309 | final String[] fromResource = getResourceStringArray(res, config_tether_dhcp_range); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 310 | if ((fromResource.length > 0) && (fromResource.length % 2 == 0)) { |
| 311 | return fromResource; |
| 312 | } |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 313 | return copy(LEGACY_DHCP_DEFAULT_RANGE); |
Erik Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 314 | } |
| 315 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 316 | private static String getProvisioningAppNoUi(Resources res) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 317 | try { |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 318 | return res.getString(config_mobile_hotspot_provision_app_no_ui); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 319 | } catch (Resources.NotFoundException e) { |
| 320 | return ""; |
| 321 | } |
| 322 | } |
| 323 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 324 | private static boolean getResourceBoolean(Resources res, int resId) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 325 | try { |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 326 | return res.getBoolean(resId); |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 327 | } catch (Resources.NotFoundException e404) { |
| 328 | return false; |
| 329 | } |
| 330 | } |
| 331 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 332 | private static String[] getResourceStringArray(Resources res, int resId) { |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 333 | try { |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 334 | final String[] strArray = res.getStringArray(resId); |
Erik Kline | e0f3403 | 2018-02-28 15:01:35 +0900 | [diff] [blame] | 335 | return (strArray != null) ? strArray : EMPTY_STRING_ARRAY; |
| 336 | } catch (Resources.NotFoundException e404) { |
| 337 | return EMPTY_STRING_ARRAY; |
| 338 | } |
| 339 | } |
| 340 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 341 | 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 VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 349 | private static boolean getEnableLegacyDhcpServer(Context ctx) { |
Remi NGUYEN VAN | e3bb5c5 | 2018-06-12 15:57:04 +0900 | [diff] [blame] | 350 | final ContentResolver cr = ctx.getContentResolver(); |
Remi NGUYEN VAN | e0d8c0e | 2018-09-26 18:11:48 +0900 | [diff] [blame] | 351 | 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] | 352 | return intVal != 0; |
| 353 | } |
| 354 | |
markchien | 0b59507 | 2019-01-08 23:52:21 +0800 | [diff] [blame] | 355 | 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 Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 368 | private static String[] copy(String[] strarray) { |
| 369 | return Arrays.copyOf(strarray, strarray.length); |
| 370 | } |
Erik Kline | 1e54351 | 2017-03-09 11:44:11 +0900 | [diff] [blame] | 371 | |
| 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 | } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 388 | |
| 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 Kline | 79868f8 | 2017-01-21 14:33:56 +0900 | [diff] [blame] | 416 | } |