blob: 5e6c47a47a8e3f8112fccfbbf8bbca2c2f655d80 [file] [log] [blame]
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -07001/*
2 * Copyright (C) 2011 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 android.net;
18
Jeff Sharkey55a442e2014-11-18 18:22:21 -080019import static android.net.ConnectivityManager.TYPE_BLUETOOTH;
Jeff Sharkey02e21d62011-07-17 15:53:33 -070020import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jack Yu66a6be32016-03-30 11:14:39 -070021import static android.net.ConnectivityManager.TYPE_MOBILE;
Christopher Tate3bf01732017-05-15 16:34:52 -070022import static android.net.ConnectivityManager.TYPE_PROXY;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey3ca74812012-01-24 15:37:07 -080024import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.net.ConnectivityManager.TYPE_WIMAX;
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +090026import static android.net.NetworkStats.DEFAULT_NETWORK_ALL;
27import static android.net.NetworkStats.DEFAULT_NETWORK_NO;
28import static android.net.NetworkStats.DEFAULT_NETWORK_YES;
29import static android.net.NetworkStats.METERED_ALL;
30import static android.net.NetworkStats.METERED_NO;
31import static android.net.NetworkStats.METERED_YES;
32import static android.net.NetworkStats.ROAMING_ALL;
33import static android.net.NetworkStats.ROAMING_NO;
34import static android.net.NetworkStats.ROAMING_YES;
Jeff Sharkey2e4dce02012-12-18 17:06:06 -080035import static android.net.wifi.WifiInfo.removeDoubleQuotes;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000036
Artur Satayev26958002019-12-10 17:47:52 +000037import android.compat.annotation.UnsupportedAppUsage;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038import android.os.Parcel;
39import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000040import android.util.BackupUtils;
Christopher Tate3bf01732017-05-15 16:34:52 -070041import android.util.Log;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070042
Varun Anand472b9262019-02-17 23:15:15 -080043import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey32566012014-12-02 18:30:14 -080044import com.android.internal.util.ArrayUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070045
Ritesh Reddyadca34a2016-02-04 18:33:30 +000046import java.io.ByteArrayOutputStream;
47import java.io.DataInputStream;
48import java.io.DataOutputStream;
49import java.io.IOException;
Jeff Sharkey32566012014-12-02 18:30:14 -080050import java.util.Arrays;
Malcolm Chen07fcb5b2019-07-02 22:29:35 -070051import java.util.List;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080052import java.util.Objects;
53
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070054/**
Jeff Sharkeye0c29952018-02-20 17:24:55 -070055 * Predicate used to match {@link NetworkIdentity}, usually when collecting
56 * statistics. (It should probably have been named {@code NetworkPredicate}.)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070057 *
58 * @hide
59 */
60public class NetworkTemplate implements Parcelable {
Christopher Tate3bf01732017-05-15 16:34:52 -070061 private static final String TAG = "NetworkTemplate";
62
Ritesh Reddyadca34a2016-02-04 18:33:30 +000063 /**
64 * Current Version of the Backup Serializer.
65 */
66 private static final int BACKUP_VERSION = 1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070067
Jeff Sharkeye0c29952018-02-20 17:24:55 -070068 public static final int MATCH_MOBILE = 1;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070069 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070070 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070071 public static final int MATCH_MOBILE_WILDCARD = 6;
72 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080073 public static final int MATCH_BLUETOOTH = 8;
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -070074 public static final int MATCH_PROXY = 9;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070075
Christopher Tate3bf01732017-05-15 16:34:52 -070076 private static boolean isKnownMatchRule(final int rule) {
77 switch (rule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -070078 case MATCH_MOBILE:
Christopher Tate3bf01732017-05-15 16:34:52 -070079 case MATCH_WIFI:
80 case MATCH_ETHERNET:
81 case MATCH_MOBILE_WILDCARD:
82 case MATCH_WIFI_WILDCARD:
83 case MATCH_BLUETOOTH:
84 case MATCH_PROXY:
85 return true;
86
87 default:
88 return false;
89 }
90 }
91
Jeff Sharkey70c70532012-05-16 14:51:19 -070092 private static boolean sForceAllNetworkTypes = false;
93
Varun Anand472b9262019-02-17 23:15:15 -080094 /**
95 * Results in matching against all mobile network types.
96 *
97 * <p>See {@link #matchesMobile} and {@link matchesMobileWildcard}.
98 */
99 @VisibleForTesting
Jeff Sharkey70c70532012-05-16 14:51:19 -0700100 public static void forceAllNetworkTypes() {
101 sForceAllNetworkTypes = true;
102 }
103
Varun Anand472b9262019-02-17 23:15:15 -0800104 /** Resets the affect of {@link #forceAllNetworkTypes}. */
105 @VisibleForTesting
106 public static void resetForceAllNetworkTypes() {
107 sForceAllNetworkTypes = false;
108 }
109
Jeff Sharkey630a1712011-09-26 10:47:10 -0700110 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700111 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
112 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700113 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100114 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700115 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700116 return new NetworkTemplate(MATCH_MOBILE, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700117 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700118
119 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -0700120 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
121 * regardless of IMSI.
122 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100123 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700124 public static NetworkTemplate buildTemplateMobileWildcard() {
125 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
126 }
127
128 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700129 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
130 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700131 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100132 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700133 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700134 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700135 }
136
137 @Deprecated
Mathew Inwood53f089f2018-08-08 14:44:44 +0100138 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700139 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700140 return buildTemplateWifiWildcard();
141 }
142
143 /**
144 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
145 * given SSID.
146 */
147 public static NetworkTemplate buildTemplateWifi(String networkId) {
148 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700149 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700150
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700151 /**
152 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
153 * networks together.
154 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100155 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700156 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700157 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700158 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700159
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800160 /**
161 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
162 * networks together.
163 */
164 public static NetworkTemplate buildTemplateBluetooth() {
165 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
166 }
167
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700168 /**
169 * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style
170 * networks together.
171 */
172 public static NetworkTemplate buildTemplateProxy() {
173 return new NetworkTemplate(MATCH_PROXY, null, null);
174 }
175
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700176 private final int mMatchRule;
177 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800178
179 /**
180 * Ugh, templates are designed to target a single subscriber, but we might
181 * need to match several "merged" subscribers. These are the subscribers
182 * that should be considered to match this template.
183 * <p>
184 * Since the merge set is dynamic, it should <em>not</em> be persisted or
185 * used for determining equality.
186 */
187 private final String[] mMatchSubscriberIds;
188
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700189 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700190
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900191 // Matches for the NetworkStats constants METERED_*, ROAMING_* and DEFAULT_NETWORK_*.
192 private final int mMetered;
193 private final int mRoaming;
194 private final int mDefaultNetwork;
195
Mathew Inwood53f089f2018-08-08 14:44:44 +0100196 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700197 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800198 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
199 }
200
201 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
202 String networkId) {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900203 this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL,
204 DEFAULT_NETWORK_ALL);
205 }
206
207 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
208 String networkId, int metered, int roaming, int defaultNetwork) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700209 mMatchRule = matchRule;
210 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800211 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700212 mNetworkId = networkId;
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900213 mMetered = metered;
214 mRoaming = roaming;
215 mDefaultNetwork = defaultNetwork;
Christopher Tate3bf01732017-05-15 16:34:52 -0700216
217 if (!isKnownMatchRule(matchRule)) {
218 Log.e(TAG, "Unknown network template rule " + matchRule
219 + " will not match any identity.");
220 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700221 }
222
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700223 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700224 mMatchRule = in.readInt();
225 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800226 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700227 mNetworkId = in.readString();
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900228 mMetered = in.readInt();
229 mRoaming = in.readInt();
230 mDefaultNetwork = in.readInt();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700231 }
232
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700233 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700234 public void writeToParcel(Parcel dest, int flags) {
235 dest.writeInt(mMatchRule);
236 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800237 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700238 dest.writeString(mNetworkId);
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900239 dest.writeInt(mMetered);
240 dest.writeInt(mRoaming);
241 dest.writeInt(mDefaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700242 }
243
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700244 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700245 public int describeContents() {
246 return 0;
247 }
248
249 @Override
250 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700251 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
252 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
253 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800254 builder.append(", subscriberId=").append(
255 NetworkIdentity.scrubSubscriberId(mSubscriberId));
256 }
257 if (mMatchSubscriberIds != null) {
258 builder.append(", matchSubscriberIds=").append(
259 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700260 }
261 if (mNetworkId != null) {
262 builder.append(", networkId=").append(mNetworkId);
263 }
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900264 if (mMetered != METERED_ALL) {
265 builder.append(", metered=").append(NetworkStats.meteredToString(mMetered));
266 }
267 if (mRoaming != ROAMING_ALL) {
268 builder.append(", roaming=").append(NetworkStats.roamingToString(mRoaming));
269 }
270 if (mDefaultNetwork != DEFAULT_NETWORK_ALL) {
271 builder.append(", defaultNetwork=").append(NetworkStats.defaultNetworkToString(
272 mDefaultNetwork));
273 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700274 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700275 }
276
277 @Override
278 public int hashCode() {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900279 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming,
280 mDefaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700281 }
282
283 @Override
284 public boolean equals(Object obj) {
285 if (obj instanceof NetworkTemplate) {
286 final NetworkTemplate other = (NetworkTemplate) obj;
287 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800288 && Objects.equals(mSubscriberId, other.mSubscriberId)
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900289 && Objects.equals(mNetworkId, other.mNetworkId)
290 && mMetered == other.mMetered
291 && mRoaming == other.mRoaming
292 && mDefaultNetwork == other.mDefaultNetwork;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700293 }
294 return false;
295 }
296
Jeff Sharkey32566012014-12-02 18:30:14 -0800297 public boolean isMatchRuleMobile() {
298 switch (mMatchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700299 case MATCH_MOBILE:
Jeff Sharkey32566012014-12-02 18:30:14 -0800300 case MATCH_MOBILE_WILDCARD:
301 return true;
302 default:
303 return false;
304 }
305 }
306
Jeff Sharkey7474fe7b2016-03-21 13:12:59 -0600307 public boolean isPersistable() {
308 switch (mMatchRule) {
309 case MATCH_MOBILE_WILDCARD:
310 case MATCH_WIFI_WILDCARD:
311 return false;
312 default:
313 return true;
314 }
315 }
316
Mathew Inwood53f089f2018-08-08 14:44:44 +0100317 @UnsupportedAppUsage
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700318 public int getMatchRule() {
319 return mMatchRule;
320 }
321
Mathew Inwood53f089f2018-08-08 14:44:44 +0100322 @UnsupportedAppUsage
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700323 public String getSubscriberId() {
324 return mSubscriberId;
325 }
326
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700327 public String getNetworkId() {
328 return mNetworkId;
329 }
330
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700331 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700332 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700333 */
334 public boolean matches(NetworkIdentity ident) {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900335 if (!matchesMetered(ident)) return false;
336 if (!matchesRoaming(ident)) return false;
337 if (!matchesDefaultNetwork(ident)) return false;
338
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700339 switch (mMatchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700340 case MATCH_MOBILE:
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700341 return matchesMobile(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700342 case MATCH_WIFI:
343 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700344 case MATCH_ETHERNET:
345 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700346 case MATCH_MOBILE_WILDCARD:
347 return matchesMobileWildcard(ident);
348 case MATCH_WIFI_WILDCARD:
349 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800350 case MATCH_BLUETOOTH:
351 return matchesBluetooth(ident);
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700352 case MATCH_PROXY:
353 return matchesProxy(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700354 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700355 // We have no idea what kind of network template we are, so we
356 // just claim not to match anything.
357 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700358 }
359 }
360
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900361 private boolean matchesMetered(NetworkIdentity ident) {
362 return (mMetered == METERED_ALL)
363 || (mMetered == METERED_YES && ident.mMetered)
364 || (mMetered == METERED_NO && !ident.mMetered);
365 }
366
367 private boolean matchesRoaming(NetworkIdentity ident) {
368 return (mRoaming == ROAMING_ALL)
369 || (mRoaming == ROAMING_YES && ident.mRoaming)
370 || (mRoaming == ROAMING_NO && !ident.mRoaming);
371 }
372
373 private boolean matchesDefaultNetwork(NetworkIdentity ident) {
374 return (mDefaultNetwork == DEFAULT_NETWORK_ALL)
375 || (mDefaultNetwork == DEFAULT_NETWORK_YES && ident.mDefaultNetwork)
376 || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork);
377 }
378
Jeff Sharkeyf4de2942017-08-29 15:32:13 -0600379 public boolean matchesSubscriberId(String subscriberId) {
380 return ArrayUtils.contains(mMatchSubscriberIds, subscriberId);
381 }
382
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700383 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700384 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700385 */
386 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700387 if (ident.mType == TYPE_WIMAX) {
388 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700389 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700390 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700391 return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
392 && !ArrayUtils.isEmpty(mMatchSubscriberIds)
Jeff Sharkey56859f32015-10-19 16:52:08 -0700393 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700394 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700395 }
396
397 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700398 * Check if matches Wi-Fi network template.
399 */
400 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800401 switch (ident.mType) {
402 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800403 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800404 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800405 default:
406 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700407 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700408 }
409
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700410 /**
411 * Check if matches Ethernet network template.
412 */
413 private boolean matchesEthernet(NetworkIdentity ident) {
414 if (ident.mType == TYPE_ETHERNET) {
415 return true;
416 }
417 return false;
418 }
419
Jeff Sharkey234766a2012-04-10 19:48:07 -0700420 private boolean matchesMobileWildcard(NetworkIdentity ident) {
421 if (ident.mType == TYPE_WIMAX) {
422 return true;
423 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700424 return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700425 }
426 }
427
428 private boolean matchesWifiWildcard(NetworkIdentity ident) {
429 switch (ident.mType) {
430 case TYPE_WIFI:
431 case TYPE_WIFI_P2P:
432 return true;
433 default:
434 return false;
435 }
436 }
437
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800438 /**
439 * Check if matches Bluetooth network template.
440 */
441 private boolean matchesBluetooth(NetworkIdentity ident) {
442 if (ident.mType == TYPE_BLUETOOTH) {
443 return true;
444 }
445 return false;
446 }
447
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700448 /**
449 * Check if matches Proxy network template.
450 */
451 private boolean matchesProxy(NetworkIdentity ident) {
452 return ident.mType == TYPE_PROXY;
453 }
454
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700455 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700456 switch (matchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700457 case MATCH_MOBILE:
458 return "MOBILE";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700459 case MATCH_WIFI:
460 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700461 case MATCH_ETHERNET:
462 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700463 case MATCH_MOBILE_WILDCARD:
464 return "MOBILE_WILDCARD";
465 case MATCH_WIFI_WILDCARD:
466 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800467 case MATCH_BLUETOOTH:
468 return "BLUETOOTH";
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700469 case MATCH_PROXY:
470 return "PROXY";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700471 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700472 return "UNKNOWN(" + matchRule + ")";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700473 }
474 }
475
Jeff Sharkey32566012014-12-02 18:30:14 -0800476 /**
477 * Examine the given template and normalize if it refers to a "merged"
478 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
479 * for key purposes, and expand the template to match all other merged
480 * subscribers.
481 * <p>
482 * For example, given an incoming template matching B, and the currently
483 * active merge set [A,B], we'd return a new template that primarily matches
484 * A, but also matches B.
Malcolm Chen07fcb5b2019-07-02 22:29:35 -0700485 * TODO: remove and use {@link #normalize(NetworkTemplate, List)}.
Jeff Sharkey32566012014-12-02 18:30:14 -0800486 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100487 @UnsupportedAppUsage
Jeff Sharkey32566012014-12-02 18:30:14 -0800488 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
Malcolm Chen07fcb5b2019-07-02 22:29:35 -0700489 return normalize(template, Arrays.<String[]>asList(merged));
490 }
491
492 /**
493 * Examine the given template and normalize if it refers to a "merged"
494 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
495 * for key purposes, and expand the template to match all other merged
496 * subscribers.
497 *
498 * There can be multiple merged subscriberIds for multi-SIM devices.
499 *
500 * <p>
501 * For example, given an incoming template matching B, and the currently
502 * active merge set [A,B], we'd return a new template that primarily matches
503 * A, but also matches B.
504 */
505 public static NetworkTemplate normalize(NetworkTemplate template, List<String[]> mergedList) {
506 if (!template.isMatchRuleMobile()) return template;
507
508 for (String[] merged : mergedList) {
509 if (ArrayUtils.contains(merged, template.mSubscriberId)) {
510 // Requested template subscriber is part of the merge group; return
511 // a template that matches all merged subscribers.
512 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
513 template.mNetworkId);
514 }
Jeff Sharkey32566012014-12-02 18:30:14 -0800515 }
Malcolm Chen07fcb5b2019-07-02 22:29:35 -0700516
517 return template;
Jeff Sharkey32566012014-12-02 18:30:14 -0800518 }
519
Mathew Inwood53f089f2018-08-08 14:44:44 +0100520 @UnsupportedAppUsage
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700521 public static final @android.annotation.NonNull Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700522 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700523 public NetworkTemplate createFromParcel(Parcel in) {
524 return new NetworkTemplate(in);
525 }
526
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700527 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700528 public NetworkTemplate[] newArray(int size) {
529 return new NetworkTemplate[size];
530 }
531 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000532
533 public byte[] getBytesForBackup() throws IOException {
534 ByteArrayOutputStream baos = new ByteArrayOutputStream();
535 DataOutputStream out = new DataOutputStream(baos);
536
537 out.writeInt(BACKUP_VERSION);
538
539 out.writeInt(mMatchRule);
540 BackupUtils.writeString(out, mSubscriberId);
541 BackupUtils.writeString(out, mNetworkId);
542
543 return baos.toByteArray();
544 }
545
546 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
547 throws IOException, BackupUtils.BadVersionException {
548 int version = in.readInt();
549 if (version < 1 || version > BACKUP_VERSION) {
550 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
551 }
552
553 int matchRule = in.readInt();
554 String subscriberId = BackupUtils.readString(in);
555 String networkId = BackupUtils.readString(in);
556
Christopher Tate3bf01732017-05-15 16:34:52 -0700557 if (!isKnownMatchRule(matchRule)) {
558 throw new BackupUtils.BadVersionException(
559 "Restored network template contains unknown match rule " + matchRule);
560 }
561
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000562 return new NetworkTemplate(matchRule, subscriberId, networkId);
563 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700564}