blob: caf79829db9c3a15a540a9f31a668f43f379bbf2 [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;
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -070021import static android.net.ConnectivityManager.TYPE_PROXY;
Jack Yu66a6be32016-03-30 11:14:39 -070022import static android.net.ConnectivityManager.TYPE_MOBILE;
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;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070026import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
Jeff Sharkey2e4dce02012-12-18 17:06:06 -080027import static android.net.wifi.WifiInfo.removeDoubleQuotes;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070028import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
29import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
30import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
31import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
32import static android.telephony.TelephonyManager.getNetworkClass;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000033
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070034import android.os.Parcel;
35import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000036import android.util.BackupUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080038import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey32566012014-12-02 18:30:14 -080039import com.android.internal.util.ArrayUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070040
Ritesh Reddyadca34a2016-02-04 18:33:30 +000041import java.io.ByteArrayOutputStream;
42import java.io.DataInputStream;
43import java.io.DataOutputStream;
44import java.io.IOException;
Jeff Sharkey32566012014-12-02 18:30:14 -080045import java.util.Arrays;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080046import java.util.Objects;
47
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070048/**
49 * Template definition used to generically match {@link NetworkIdentity},
50 * usually when collecting statistics.
51 *
52 * @hide
53 */
54public class NetworkTemplate implements Parcelable {
Ritesh Reddyadca34a2016-02-04 18:33:30 +000055 /**
56 * Current Version of the Backup Serializer.
57 */
58 private static final int BACKUP_VERSION = 1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070059
Jeff Sharkey4e814c32011-07-14 20:37:37 -070060 public static final int MATCH_MOBILE_ALL = 1;
Jeff Sharkey69736342014-12-08 14:50:12 -080061 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070062 public static final int MATCH_MOBILE_3G_LOWER = 2;
Jeff Sharkey69736342014-12-08 14:50:12 -080063 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070064 public static final int MATCH_MOBILE_4G = 3;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070065 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070066 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070067 public static final int MATCH_MOBILE_WILDCARD = 6;
68 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080069 public static final int MATCH_BLUETOOTH = 8;
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -070070 public static final int MATCH_PROXY = 9;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070071
Jeff Sharkey70c70532012-05-16 14:51:19 -070072 private static boolean sForceAllNetworkTypes = false;
73
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080074 @VisibleForTesting
Jeff Sharkey70c70532012-05-16 14:51:19 -070075 public static void forceAllNetworkTypes() {
76 sForceAllNetworkTypes = true;
77 }
78
Jeff Sharkey630a1712011-09-26 10:47:10 -070079 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070080 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
81 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070082 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070083 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070084 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070085 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070086
87 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070088 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
89 * the given IMSI that roughly meet a "3G" definition, or lower.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070090 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070091 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070092 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070093 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070094 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070095
96 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070097 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
98 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070099 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700100 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700101 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700102 return new NetworkTemplate(MATCH_MOBILE_4G, 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 */
109 public static NetworkTemplate buildTemplateMobileWildcard() {
110 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
111 }
112
113 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700114 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
115 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700116 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700117 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700118 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700119 }
120
121 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700122 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700123 return buildTemplateWifiWildcard();
124 }
125
126 /**
127 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
128 * given SSID.
129 */
130 public static NetworkTemplate buildTemplateWifi(String networkId) {
131 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700132 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700133
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700134 /**
135 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
136 * networks together.
137 */
138 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700139 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700140 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700141
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800142 /**
143 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
144 * networks together.
145 */
146 public static NetworkTemplate buildTemplateBluetooth() {
147 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
148 }
149
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700150 /**
151 * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style
152 * networks together.
153 */
154 public static NetworkTemplate buildTemplateProxy() {
155 return new NetworkTemplate(MATCH_PROXY, null, null);
156 }
157
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700158 private final int mMatchRule;
159 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800160
161 /**
162 * Ugh, templates are designed to target a single subscriber, but we might
163 * need to match several "merged" subscribers. These are the subscribers
164 * that should be considered to match this template.
165 * <p>
166 * Since the merge set is dynamic, it should <em>not</em> be persisted or
167 * used for determining equality.
168 */
169 private final String[] mMatchSubscriberIds;
170
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700171 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700172
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700173 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800174 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
175 }
176
177 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
178 String networkId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700179 mMatchRule = matchRule;
180 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800181 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700182 mNetworkId = networkId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700183 }
184
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700185 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700186 mMatchRule = in.readInt();
187 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800188 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700189 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700190 }
191
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700192 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700193 public void writeToParcel(Parcel dest, int flags) {
194 dest.writeInt(mMatchRule);
195 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800196 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700197 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700198 }
199
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700200 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700201 public int describeContents() {
202 return 0;
203 }
204
205 @Override
206 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700207 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
208 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
209 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800210 builder.append(", subscriberId=").append(
211 NetworkIdentity.scrubSubscriberId(mSubscriberId));
212 }
213 if (mMatchSubscriberIds != null) {
214 builder.append(", matchSubscriberIds=").append(
215 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700216 }
217 if (mNetworkId != null) {
218 builder.append(", networkId=").append(mNetworkId);
219 }
220 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700221 }
222
223 @Override
224 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -0800225 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700226 }
227
228 @Override
229 public boolean equals(Object obj) {
230 if (obj instanceof NetworkTemplate) {
231 final NetworkTemplate other = (NetworkTemplate) obj;
232 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800233 && Objects.equals(mSubscriberId, other.mSubscriberId)
234 && Objects.equals(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700235 }
236 return false;
237 }
238
Jeff Sharkey32566012014-12-02 18:30:14 -0800239 public boolean isMatchRuleMobile() {
240 switch (mMatchRule) {
241 case MATCH_MOBILE_3G_LOWER:
242 case MATCH_MOBILE_4G:
243 case MATCH_MOBILE_ALL:
244 case MATCH_MOBILE_WILDCARD:
245 return true;
246 default:
247 return false;
248 }
249 }
250
Jeff Sharkey7474fe7b2016-03-21 13:12:59 -0600251 public boolean isPersistable() {
252 switch (mMatchRule) {
253 case MATCH_MOBILE_WILDCARD:
254 case MATCH_WIFI_WILDCARD:
255 return false;
256 default:
257 return true;
258 }
259 }
260
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700261 public int getMatchRule() {
262 return mMatchRule;
263 }
264
265 public String getSubscriberId() {
266 return mSubscriberId;
267 }
268
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700269 public String getNetworkId() {
270 return mNetworkId;
271 }
272
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700273 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700274 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700275 */
276 public boolean matches(NetworkIdentity ident) {
277 switch (mMatchRule) {
278 case MATCH_MOBILE_ALL:
279 return matchesMobile(ident);
280 case MATCH_MOBILE_3G_LOWER:
281 return matchesMobile3gLower(ident);
282 case MATCH_MOBILE_4G:
283 return matchesMobile4g(ident);
284 case MATCH_WIFI:
285 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700286 case MATCH_ETHERNET:
287 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700288 case MATCH_MOBILE_WILDCARD:
289 return matchesMobileWildcard(ident);
290 case MATCH_WIFI_WILDCARD:
291 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800292 case MATCH_BLUETOOTH:
293 return matchesBluetooth(ident);
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700294 case MATCH_PROXY:
295 return matchesProxy(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700296 default:
297 throw new IllegalArgumentException("unknown network template");
298 }
299 }
300
301 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700302 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700303 */
304 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700305 if (ident.mType == TYPE_WIMAX) {
306 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700307 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700308 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700309 return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
310 && !ArrayUtils.isEmpty(mMatchSubscriberIds)
Jeff Sharkey56859f32015-10-19 16:52:08 -0700311 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700312 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700313 }
314
315 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700316 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700317 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800318 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700319 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700320 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700321 if (ident.mType == TYPE_WIMAX) {
322 return false;
323 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700324 switch (getNetworkClass(ident.mSubType)) {
325 case NETWORK_CLASS_UNKNOWN:
326 case NETWORK_CLASS_2_G:
327 case NETWORK_CLASS_3_G:
328 return true;
329 }
330 }
331 return false;
332 }
333
334 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700335 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700336 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800337 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700338 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700339 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700340 if (ident.mType == TYPE_WIMAX) {
341 // TODO: consider matching against WiMAX subscriber identity
342 return true;
343 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700344 switch (getNetworkClass(ident.mSubType)) {
345 case NETWORK_CLASS_4_G:
346 return true;
347 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700348 }
349 return false;
350 }
351
352 /**
353 * Check if matches Wi-Fi network template.
354 */
355 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800356 switch (ident.mType) {
357 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800358 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800359 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800360 default:
361 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700362 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700363 }
364
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700365 /**
366 * Check if matches Ethernet network template.
367 */
368 private boolean matchesEthernet(NetworkIdentity ident) {
369 if (ident.mType == TYPE_ETHERNET) {
370 return true;
371 }
372 return false;
373 }
374
Jeff Sharkey234766a2012-04-10 19:48:07 -0700375 private boolean matchesMobileWildcard(NetworkIdentity ident) {
376 if (ident.mType == TYPE_WIMAX) {
377 return true;
378 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700379 return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700380 }
381 }
382
383 private boolean matchesWifiWildcard(NetworkIdentity ident) {
384 switch (ident.mType) {
385 case TYPE_WIFI:
386 case TYPE_WIFI_P2P:
387 return true;
388 default:
389 return false;
390 }
391 }
392
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800393 /**
394 * Check if matches Bluetooth network template.
395 */
396 private boolean matchesBluetooth(NetworkIdentity ident) {
397 if (ident.mType == TYPE_BLUETOOTH) {
398 return true;
399 }
400 return false;
401 }
402
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700403 /**
404 * Check if matches Proxy network template.
405 */
406 private boolean matchesProxy(NetworkIdentity ident) {
407 return ident.mType == TYPE_PROXY;
408 }
409
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700410 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700411 switch (matchRule) {
412 case MATCH_MOBILE_3G_LOWER:
413 return "MOBILE_3G_LOWER";
414 case MATCH_MOBILE_4G:
415 return "MOBILE_4G";
416 case MATCH_MOBILE_ALL:
417 return "MOBILE_ALL";
418 case MATCH_WIFI:
419 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700420 case MATCH_ETHERNET:
421 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700422 case MATCH_MOBILE_WILDCARD:
423 return "MOBILE_WILDCARD";
424 case MATCH_WIFI_WILDCARD:
425 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800426 case MATCH_BLUETOOTH:
427 return "BLUETOOTH";
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700428 case MATCH_PROXY:
429 return "PROXY";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700430 default:
431 return "UNKNOWN";
432 }
433 }
434
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700435 private static void ensureSubtypeAvailable() {
436 if (COMBINE_SUBTYPE_ENABLED) {
437 throw new IllegalArgumentException(
438 "Unable to enforce 3G_LOWER template on combined data.");
439 }
440 }
441
Jeff Sharkey32566012014-12-02 18:30:14 -0800442 /**
443 * Examine the given template and normalize if it refers to a "merged"
444 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
445 * for key purposes, and expand the template to match all other merged
446 * subscribers.
447 * <p>
448 * For example, given an incoming template matching B, and the currently
449 * active merge set [A,B], we'd return a new template that primarily matches
450 * A, but also matches B.
451 */
452 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
453 if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
454 // Requested template subscriber is part of the merge group; return
455 // a template that matches all merged subscribers.
456 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
457 template.mNetworkId);
458 } else {
459 return template;
460 }
461 }
462
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700463 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700464 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700465 public NetworkTemplate createFromParcel(Parcel in) {
466 return new NetworkTemplate(in);
467 }
468
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700469 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700470 public NetworkTemplate[] newArray(int size) {
471 return new NetworkTemplate[size];
472 }
473 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000474
475 public byte[] getBytesForBackup() throws IOException {
476 ByteArrayOutputStream baos = new ByteArrayOutputStream();
477 DataOutputStream out = new DataOutputStream(baos);
478
479 out.writeInt(BACKUP_VERSION);
480
481 out.writeInt(mMatchRule);
482 BackupUtils.writeString(out, mSubscriberId);
483 BackupUtils.writeString(out, mNetworkId);
484
485 return baos.toByteArray();
486 }
487
488 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
489 throws IOException, BackupUtils.BadVersionException {
490 int version = in.readInt();
491 if (version < 1 || version > BACKUP_VERSION) {
492 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
493 }
494
495 int matchRule = in.readInt();
496 String subscriberId = BackupUtils.readString(in);
497 String networkId = BackupUtils.readString(in);
498
499 return new NetworkTemplate(matchRule, subscriberId, networkId);
500 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700501}