blob: d42fce3a4c83072386ef7639a62756e5eeb73d66 [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
Mathew Inwood53f089f2018-08-08 14:44:44 +010037import android.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
Jeff Sharkey32566012014-12-02 18:30:14 -080043import com.android.internal.util.ArrayUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070044
Ritesh Reddyadca34a2016-02-04 18:33:30 +000045import java.io.ByteArrayOutputStream;
46import java.io.DataInputStream;
47import java.io.DataOutputStream;
48import java.io.IOException;
Jeff Sharkey32566012014-12-02 18:30:14 -080049import java.util.Arrays;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080050import java.util.Objects;
51
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070052/**
Jeff Sharkeye0c29952018-02-20 17:24:55 -070053 * Predicate used to match {@link NetworkIdentity}, usually when collecting
54 * statistics. (It should probably have been named {@code NetworkPredicate}.)
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070055 *
56 * @hide
57 */
58public class NetworkTemplate implements Parcelable {
Christopher Tate3bf01732017-05-15 16:34:52 -070059 private static final String TAG = "NetworkTemplate";
60
Ritesh Reddyadca34a2016-02-04 18:33:30 +000061 /**
62 * Current Version of the Backup Serializer.
63 */
64 private static final int BACKUP_VERSION = 1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070065
Jeff Sharkeye0c29952018-02-20 17:24:55 -070066 public static final int MATCH_MOBILE = 1;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070067 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070068 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070069 public static final int MATCH_MOBILE_WILDCARD = 6;
70 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080071 public static final int MATCH_BLUETOOTH = 8;
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -070072 public static final int MATCH_PROXY = 9;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070073
Christopher Tate3bf01732017-05-15 16:34:52 -070074 private static boolean isKnownMatchRule(final int rule) {
75 switch (rule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -070076 case MATCH_MOBILE:
Christopher Tate3bf01732017-05-15 16:34:52 -070077 case MATCH_WIFI:
78 case MATCH_ETHERNET:
79 case MATCH_MOBILE_WILDCARD:
80 case MATCH_WIFI_WILDCARD:
81 case MATCH_BLUETOOTH:
82 case MATCH_PROXY:
83 return true;
84
85 default:
86 return false;
87 }
88 }
89
Jeff Sharkey70c70532012-05-16 14:51:19 -070090 private static boolean sForceAllNetworkTypes = false;
91
Jeff Sharkey70c70532012-05-16 14:51:19 -070092 public static void forceAllNetworkTypes() {
93 sForceAllNetworkTypes = true;
94 }
95
Jeff Sharkey630a1712011-09-26 10:47:10 -070096 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070097 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
98 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070099 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100100 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700101 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700102 return new NetworkTemplate(MATCH_MOBILE, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700103 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700104
105 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -0700106 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
107 * regardless of IMSI.
108 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100109 @UnsupportedAppUsage
Jeff Sharkey234766a2012-04-10 19:48:07 -0700110 public static NetworkTemplate buildTemplateMobileWildcard() {
111 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
112 }
113
114 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700115 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
116 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700117 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100118 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700119 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700120 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700121 }
122
123 @Deprecated
Mathew Inwood53f089f2018-08-08 14:44:44 +0100124 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700125 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700126 return buildTemplateWifiWildcard();
127 }
128
129 /**
130 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
131 * given SSID.
132 */
133 public static NetworkTemplate buildTemplateWifi(String networkId) {
134 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700135 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700136
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700137 /**
138 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
139 * networks together.
140 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100141 @UnsupportedAppUsage
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700142 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700143 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700144 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700145
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800146 /**
147 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
148 * networks together.
149 */
150 public static NetworkTemplate buildTemplateBluetooth() {
151 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
152 }
153
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700154 /**
155 * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style
156 * networks together.
157 */
158 public static NetworkTemplate buildTemplateProxy() {
159 return new NetworkTemplate(MATCH_PROXY, null, null);
160 }
161
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700162 private final int mMatchRule;
163 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800164
165 /**
166 * Ugh, templates are designed to target a single subscriber, but we might
167 * need to match several "merged" subscribers. These are the subscribers
168 * that should be considered to match this template.
169 * <p>
170 * Since the merge set is dynamic, it should <em>not</em> be persisted or
171 * used for determining equality.
172 */
173 private final String[] mMatchSubscriberIds;
174
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700175 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700176
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900177 // Matches for the NetworkStats constants METERED_*, ROAMING_* and DEFAULT_NETWORK_*.
178 private final int mMetered;
179 private final int mRoaming;
180 private final int mDefaultNetwork;
181
Mathew Inwood53f089f2018-08-08 14:44:44 +0100182 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700183 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800184 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
185 }
186
187 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
188 String networkId) {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900189 this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL,
190 DEFAULT_NETWORK_ALL);
191 }
192
193 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
194 String networkId, int metered, int roaming, int defaultNetwork) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700195 mMatchRule = matchRule;
196 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800197 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700198 mNetworkId = networkId;
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900199 mMetered = metered;
200 mRoaming = roaming;
201 mDefaultNetwork = defaultNetwork;
Christopher Tate3bf01732017-05-15 16:34:52 -0700202
203 if (!isKnownMatchRule(matchRule)) {
204 Log.e(TAG, "Unknown network template rule " + matchRule
205 + " will not match any identity.");
206 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700207 }
208
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700209 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700210 mMatchRule = in.readInt();
211 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800212 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700213 mNetworkId = in.readString();
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900214 mMetered = in.readInt();
215 mRoaming = in.readInt();
216 mDefaultNetwork = in.readInt();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700217 }
218
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700219 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700220 public void writeToParcel(Parcel dest, int flags) {
221 dest.writeInt(mMatchRule);
222 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800223 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700224 dest.writeString(mNetworkId);
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900225 dest.writeInt(mMetered);
226 dest.writeInt(mRoaming);
227 dest.writeInt(mDefaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700228 }
229
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700230 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700231 public int describeContents() {
232 return 0;
233 }
234
235 @Override
236 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700237 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
238 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
239 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800240 builder.append(", subscriberId=").append(
241 NetworkIdentity.scrubSubscriberId(mSubscriberId));
242 }
243 if (mMatchSubscriberIds != null) {
244 builder.append(", matchSubscriberIds=").append(
245 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700246 }
247 if (mNetworkId != null) {
248 builder.append(", networkId=").append(mNetworkId);
249 }
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900250 if (mMetered != METERED_ALL) {
251 builder.append(", metered=").append(NetworkStats.meteredToString(mMetered));
252 }
253 if (mRoaming != ROAMING_ALL) {
254 builder.append(", roaming=").append(NetworkStats.roamingToString(mRoaming));
255 }
256 if (mDefaultNetwork != DEFAULT_NETWORK_ALL) {
257 builder.append(", defaultNetwork=").append(NetworkStats.defaultNetworkToString(
258 mDefaultNetwork));
259 }
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700260 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700261 }
262
263 @Override
264 public int hashCode() {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900265 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming,
266 mDefaultNetwork);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700267 }
268
269 @Override
270 public boolean equals(Object obj) {
271 if (obj instanceof NetworkTemplate) {
272 final NetworkTemplate other = (NetworkTemplate) obj;
273 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800274 && Objects.equals(mSubscriberId, other.mSubscriberId)
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900275 && Objects.equals(mNetworkId, other.mNetworkId)
276 && mMetered == other.mMetered
277 && mRoaming == other.mRoaming
278 && mDefaultNetwork == other.mDefaultNetwork;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700279 }
280 return false;
281 }
282
Jeff Sharkey32566012014-12-02 18:30:14 -0800283 public boolean isMatchRuleMobile() {
284 switch (mMatchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700285 case MATCH_MOBILE:
Jeff Sharkey32566012014-12-02 18:30:14 -0800286 case MATCH_MOBILE_WILDCARD:
287 return true;
288 default:
289 return false;
290 }
291 }
292
Jeff Sharkey7474fe7b2016-03-21 13:12:59 -0600293 public boolean isPersistable() {
294 switch (mMatchRule) {
295 case MATCH_MOBILE_WILDCARD:
296 case MATCH_WIFI_WILDCARD:
297 return false;
298 default:
299 return true;
300 }
301 }
302
Mathew Inwood53f089f2018-08-08 14:44:44 +0100303 @UnsupportedAppUsage
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700304 public int getMatchRule() {
305 return mMatchRule;
306 }
307
Mathew Inwood53f089f2018-08-08 14:44:44 +0100308 @UnsupportedAppUsage
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700309 public String getSubscriberId() {
310 return mSubscriberId;
311 }
312
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700313 public String getNetworkId() {
314 return mNetworkId;
315 }
316
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700317 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700318 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700319 */
320 public boolean matches(NetworkIdentity ident) {
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900321 if (!matchesMetered(ident)) return false;
322 if (!matchesRoaming(ident)) return false;
323 if (!matchesDefaultNetwork(ident)) return false;
324
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700325 switch (mMatchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700326 case MATCH_MOBILE:
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700327 return matchesMobile(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700328 case MATCH_WIFI:
329 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700330 case MATCH_ETHERNET:
331 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700332 case MATCH_MOBILE_WILDCARD:
333 return matchesMobileWildcard(ident);
334 case MATCH_WIFI_WILDCARD:
335 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800336 case MATCH_BLUETOOTH:
337 return matchesBluetooth(ident);
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700338 case MATCH_PROXY:
339 return matchesProxy(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700340 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700341 // We have no idea what kind of network template we are, so we
342 // just claim not to match anything.
343 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700344 }
345 }
346
Lorenzo Colitti53ba2f02018-01-22 11:54:46 +0900347 private boolean matchesMetered(NetworkIdentity ident) {
348 return (mMetered == METERED_ALL)
349 || (mMetered == METERED_YES && ident.mMetered)
350 || (mMetered == METERED_NO && !ident.mMetered);
351 }
352
353 private boolean matchesRoaming(NetworkIdentity ident) {
354 return (mRoaming == ROAMING_ALL)
355 || (mRoaming == ROAMING_YES && ident.mRoaming)
356 || (mRoaming == ROAMING_NO && !ident.mRoaming);
357 }
358
359 private boolean matchesDefaultNetwork(NetworkIdentity ident) {
360 return (mDefaultNetwork == DEFAULT_NETWORK_ALL)
361 || (mDefaultNetwork == DEFAULT_NETWORK_YES && ident.mDefaultNetwork)
362 || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork);
363 }
364
Jeff Sharkeyf4de2942017-08-29 15:32:13 -0600365 public boolean matchesSubscriberId(String subscriberId) {
366 return ArrayUtils.contains(mMatchSubscriberIds, subscriberId);
367 }
368
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700369 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700370 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700371 */
372 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700373 if (ident.mType == TYPE_WIMAX) {
374 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700375 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700376 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700377 return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
378 && !ArrayUtils.isEmpty(mMatchSubscriberIds)
Jeff Sharkey56859f32015-10-19 16:52:08 -0700379 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700380 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700381 }
382
383 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700384 * Check if matches Wi-Fi network template.
385 */
386 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800387 switch (ident.mType) {
388 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800389 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800390 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800391 default:
392 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700393 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700394 }
395
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700396 /**
397 * Check if matches Ethernet network template.
398 */
399 private boolean matchesEthernet(NetworkIdentity ident) {
400 if (ident.mType == TYPE_ETHERNET) {
401 return true;
402 }
403 return false;
404 }
405
Jeff Sharkey234766a2012-04-10 19:48:07 -0700406 private boolean matchesMobileWildcard(NetworkIdentity ident) {
407 if (ident.mType == TYPE_WIMAX) {
408 return true;
409 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700410 return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700411 }
412 }
413
414 private boolean matchesWifiWildcard(NetworkIdentity ident) {
415 switch (ident.mType) {
416 case TYPE_WIFI:
417 case TYPE_WIFI_P2P:
418 return true;
419 default:
420 return false;
421 }
422 }
423
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800424 /**
425 * Check if matches Bluetooth network template.
426 */
427 private boolean matchesBluetooth(NetworkIdentity ident) {
428 if (ident.mType == TYPE_BLUETOOTH) {
429 return true;
430 }
431 return false;
432 }
433
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700434 /**
435 * Check if matches Proxy network template.
436 */
437 private boolean matchesProxy(NetworkIdentity ident) {
438 return ident.mType == TYPE_PROXY;
439 }
440
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700441 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700442 switch (matchRule) {
Jeff Sharkeye0c29952018-02-20 17:24:55 -0700443 case MATCH_MOBILE:
444 return "MOBILE";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700445 case MATCH_WIFI:
446 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700447 case MATCH_ETHERNET:
448 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700449 case MATCH_MOBILE_WILDCARD:
450 return "MOBILE_WILDCARD";
451 case MATCH_WIFI_WILDCARD:
452 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800453 case MATCH_BLUETOOTH:
454 return "BLUETOOTH";
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700455 case MATCH_PROXY:
456 return "PROXY";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700457 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700458 return "UNKNOWN(" + matchRule + ")";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700459 }
460 }
461
Jeff Sharkey32566012014-12-02 18:30:14 -0800462 /**
463 * Examine the given template and normalize if it refers to a "merged"
464 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
465 * for key purposes, and expand the template to match all other merged
466 * subscribers.
467 * <p>
468 * For example, given an incoming template matching B, and the currently
469 * active merge set [A,B], we'd return a new template that primarily matches
470 * A, but also matches B.
471 */
Mathew Inwood53f089f2018-08-08 14:44:44 +0100472 @UnsupportedAppUsage
Jeff Sharkey32566012014-12-02 18:30:14 -0800473 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
474 if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
475 // Requested template subscriber is part of the merge group; return
476 // a template that matches all merged subscribers.
477 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
478 template.mNetworkId);
479 } else {
480 return template;
481 }
482 }
483
Mathew Inwood53f089f2018-08-08 14:44:44 +0100484 @UnsupportedAppUsage
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700485 public static final @android.annotation.NonNull Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700486 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700487 public NetworkTemplate createFromParcel(Parcel in) {
488 return new NetworkTemplate(in);
489 }
490
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700491 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700492 public NetworkTemplate[] newArray(int size) {
493 return new NetworkTemplate[size];
494 }
495 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000496
497 public byte[] getBytesForBackup() throws IOException {
498 ByteArrayOutputStream baos = new ByteArrayOutputStream();
499 DataOutputStream out = new DataOutputStream(baos);
500
501 out.writeInt(BACKUP_VERSION);
502
503 out.writeInt(mMatchRule);
504 BackupUtils.writeString(out, mSubscriberId);
505 BackupUtils.writeString(out, mNetworkId);
506
507 return baos.toByteArray();
508 }
509
510 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
511 throws IOException, BackupUtils.BadVersionException {
512 int version = in.readInt();
513 if (version < 1 || version > BACKUP_VERSION) {
514 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
515 }
516
517 int matchRule = in.readInt();
518 String subscriberId = BackupUtils.readString(in);
519 String networkId = BackupUtils.readString(in);
520
Christopher Tate3bf01732017-05-15 16:34:52 -0700521 if (!isKnownMatchRule(matchRule)) {
522 throw new BackupUtils.BadVersionException(
523 "Restored network template contains unknown match rule " + matchRule);
524 }
525
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000526 return new NetworkTemplate(matchRule, subscriberId, networkId);
527 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700528}