blob: 5761d66dde87594006adf2c11a0a0f41c1486dd7 [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;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey3ca74812012-01-24 15:37:07 -080022import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070024import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
Jeff Sharkey2e4dce02012-12-18 17:06:06 -080025import static android.net.wifi.WifiInfo.removeDoubleQuotes;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070026import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
27import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
28import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
29import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
30import static android.telephony.TelephonyManager.getNetworkClass;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000031
Jeff Sharkey630a1712011-09-26 10:47:10 -070032import static com.android.internal.util.ArrayUtils.contains;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070033
Jeff Sharkey630a1712011-09-26 10:47:10 -070034import android.content.res.Resources;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070035import android.os.Parcel;
36import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000037import android.util.BackupUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080039import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey32566012014-12-02 18:30:14 -080040import com.android.internal.util.ArrayUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070041
Ritesh Reddyadca34a2016-02-04 18:33:30 +000042import java.io.ByteArrayOutputStream;
43import java.io.DataInputStream;
44import java.io.DataOutputStream;
45import java.io.IOException;
Jeff Sharkey32566012014-12-02 18:30:14 -080046import java.util.Arrays;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080047import java.util.Objects;
48
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070049/**
50 * Template definition used to generically match {@link NetworkIdentity},
51 * usually when collecting statistics.
52 *
53 * @hide
54 */
55public class NetworkTemplate implements Parcelable {
Ritesh Reddyadca34a2016-02-04 18:33:30 +000056 /**
57 * Current Version of the Backup Serializer.
58 */
59 private static final int BACKUP_VERSION = 1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070060
Jeff Sharkey4e814c32011-07-14 20:37:37 -070061 public static final int MATCH_MOBILE_ALL = 1;
Jeff Sharkey69736342014-12-08 14:50:12 -080062 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070063 public static final int MATCH_MOBILE_3G_LOWER = 2;
Jeff Sharkey69736342014-12-08 14:50:12 -080064 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070065 public static final int MATCH_MOBILE_4G = 3;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070066 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070067 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070068 public static final int MATCH_MOBILE_WILDCARD = 6;
69 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080070 public static final int MATCH_BLUETOOTH = 8;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070071
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070072 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -070073 * Set of {@link NetworkInfo#getType()} that reflect data usage.
74 */
75 private static final int[] DATA_USAGE_NETWORK_TYPES;
76
77 static {
78 DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
79 com.android.internal.R.array.config_data_usage_network_types);
80 }
81
Jeff Sharkey70c70532012-05-16 14:51:19 -070082 private static boolean sForceAllNetworkTypes = false;
83
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080084 @VisibleForTesting
Jeff Sharkey70c70532012-05-16 14:51:19 -070085 public static void forceAllNetworkTypes() {
86 sForceAllNetworkTypes = true;
87 }
88
Jeff Sharkey630a1712011-09-26 10:47:10 -070089 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070090 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
91 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070092 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070093 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070094 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070095 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070096
97 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070098 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
99 * the given IMSI that roughly meet a "3G" definition, or lower.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700100 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700101 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700102 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700103 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700104 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700105
106 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700107 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
108 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700109 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700110 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700111 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700112 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700113 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700114
115 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -0700116 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
117 * regardless of IMSI.
118 */
119 public static NetworkTemplate buildTemplateMobileWildcard() {
120 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
121 }
122
123 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700124 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
125 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700126 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700127 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700128 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700129 }
130
131 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700132 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700133 return buildTemplateWifiWildcard();
134 }
135
136 /**
137 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
138 * given SSID.
139 */
140 public static NetworkTemplate buildTemplateWifi(String networkId) {
141 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700142 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700143
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700144 /**
145 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
146 * networks together.
147 */
148 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700149 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700150 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700151
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800152 /**
153 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
154 * networks together.
155 */
156 public static NetworkTemplate buildTemplateBluetooth() {
157 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
158 }
159
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700160 private final int mMatchRule;
161 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800162
163 /**
164 * Ugh, templates are designed to target a single subscriber, but we might
165 * need to match several "merged" subscribers. These are the subscribers
166 * that should be considered to match this template.
167 * <p>
168 * Since the merge set is dynamic, it should <em>not</em> be persisted or
169 * used for determining equality.
170 */
171 private final String[] mMatchSubscriberIds;
172
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700173 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700174
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700175 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800176 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
177 }
178
179 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
180 String networkId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700181 mMatchRule = matchRule;
182 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800183 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700184 mNetworkId = networkId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700185 }
186
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700187 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700188 mMatchRule = in.readInt();
189 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800190 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700191 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700192 }
193
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700194 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700195 public void writeToParcel(Parcel dest, int flags) {
196 dest.writeInt(mMatchRule);
197 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800198 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700199 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700200 }
201
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700202 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700203 public int describeContents() {
204 return 0;
205 }
206
207 @Override
208 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700209 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
210 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
211 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800212 builder.append(", subscriberId=").append(
213 NetworkIdentity.scrubSubscriberId(mSubscriberId));
214 }
215 if (mMatchSubscriberIds != null) {
216 builder.append(", matchSubscriberIds=").append(
217 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700218 }
219 if (mNetworkId != null) {
220 builder.append(", networkId=").append(mNetworkId);
221 }
222 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700223 }
224
225 @Override
226 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -0800227 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700228 }
229
230 @Override
231 public boolean equals(Object obj) {
232 if (obj instanceof NetworkTemplate) {
233 final NetworkTemplate other = (NetworkTemplate) obj;
234 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800235 && Objects.equals(mSubscriberId, other.mSubscriberId)
236 && Objects.equals(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700237 }
238 return false;
239 }
240
Jeff Sharkey32566012014-12-02 18:30:14 -0800241 public boolean isMatchRuleMobile() {
242 switch (mMatchRule) {
243 case MATCH_MOBILE_3G_LOWER:
244 case MATCH_MOBILE_4G:
245 case MATCH_MOBILE_ALL:
246 case MATCH_MOBILE_WILDCARD:
247 return true;
248 default:
249 return false;
250 }
251 }
252
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700253 public int getMatchRule() {
254 return mMatchRule;
255 }
256
257 public String getSubscriberId() {
258 return mSubscriberId;
259 }
260
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700261 public String getNetworkId() {
262 return mNetworkId;
263 }
264
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700265 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700266 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700267 */
268 public boolean matches(NetworkIdentity ident) {
269 switch (mMatchRule) {
270 case MATCH_MOBILE_ALL:
271 return matchesMobile(ident);
272 case MATCH_MOBILE_3G_LOWER:
273 return matchesMobile3gLower(ident);
274 case MATCH_MOBILE_4G:
275 return matchesMobile4g(ident);
276 case MATCH_WIFI:
277 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700278 case MATCH_ETHERNET:
279 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700280 case MATCH_MOBILE_WILDCARD:
281 return matchesMobileWildcard(ident);
282 case MATCH_WIFI_WILDCARD:
283 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800284 case MATCH_BLUETOOTH:
285 return matchesBluetooth(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700286 default:
287 throw new IllegalArgumentException("unknown network template");
288 }
289 }
290
291 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700292 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700293 */
294 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700295 if (ident.mType == TYPE_WIMAX) {
296 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700297 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700298 } else {
Jeff Sharkey32566012014-12-02 18:30:14 -0800299 final boolean matchesType = (sForceAllNetworkTypes
300 || contains(DATA_USAGE_NETWORK_TYPES, ident.mType));
Jeff Sharkey56859f32015-10-19 16:52:08 -0700301 return matchesType && !ArrayUtils.isEmpty(mMatchSubscriberIds)
302 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700303 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700304 }
305
306 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700307 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700308 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800309 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700310 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700311 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700312 if (ident.mType == TYPE_WIMAX) {
313 return false;
314 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700315 switch (getNetworkClass(ident.mSubType)) {
316 case NETWORK_CLASS_UNKNOWN:
317 case NETWORK_CLASS_2_G:
318 case NETWORK_CLASS_3_G:
319 return true;
320 }
321 }
322 return false;
323 }
324
325 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700326 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700327 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800328 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700329 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700330 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700331 if (ident.mType == TYPE_WIMAX) {
332 // TODO: consider matching against WiMAX subscriber identity
333 return true;
334 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700335 switch (getNetworkClass(ident.mSubType)) {
336 case NETWORK_CLASS_4_G:
337 return true;
338 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700339 }
340 return false;
341 }
342
343 /**
344 * Check if matches Wi-Fi network template.
345 */
346 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800347 switch (ident.mType) {
348 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800349 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800350 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800351 default:
352 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700353 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700354 }
355
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700356 /**
357 * Check if matches Ethernet network template.
358 */
359 private boolean matchesEthernet(NetworkIdentity ident) {
360 if (ident.mType == TYPE_ETHERNET) {
361 return true;
362 }
363 return false;
364 }
365
Jeff Sharkey234766a2012-04-10 19:48:07 -0700366 private boolean matchesMobileWildcard(NetworkIdentity ident) {
367 if (ident.mType == TYPE_WIMAX) {
368 return true;
369 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700370 return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700371 }
372 }
373
374 private boolean matchesWifiWildcard(NetworkIdentity ident) {
375 switch (ident.mType) {
376 case TYPE_WIFI:
377 case TYPE_WIFI_P2P:
378 return true;
379 default:
380 return false;
381 }
382 }
383
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800384 /**
385 * Check if matches Bluetooth network template.
386 */
387 private boolean matchesBluetooth(NetworkIdentity ident) {
388 if (ident.mType == TYPE_BLUETOOTH) {
389 return true;
390 }
391 return false;
392 }
393
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700394 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700395 switch (matchRule) {
396 case MATCH_MOBILE_3G_LOWER:
397 return "MOBILE_3G_LOWER";
398 case MATCH_MOBILE_4G:
399 return "MOBILE_4G";
400 case MATCH_MOBILE_ALL:
401 return "MOBILE_ALL";
402 case MATCH_WIFI:
403 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700404 case MATCH_ETHERNET:
405 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700406 case MATCH_MOBILE_WILDCARD:
407 return "MOBILE_WILDCARD";
408 case MATCH_WIFI_WILDCARD:
409 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800410 case MATCH_BLUETOOTH:
411 return "BLUETOOTH";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700412 default:
413 return "UNKNOWN";
414 }
415 }
416
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700417 private static void ensureSubtypeAvailable() {
418 if (COMBINE_SUBTYPE_ENABLED) {
419 throw new IllegalArgumentException(
420 "Unable to enforce 3G_LOWER template on combined data.");
421 }
422 }
423
Jeff Sharkey32566012014-12-02 18:30:14 -0800424 /**
425 * Examine the given template and normalize if it refers to a "merged"
426 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
427 * for key purposes, and expand the template to match all other merged
428 * subscribers.
429 * <p>
430 * For example, given an incoming template matching B, and the currently
431 * active merge set [A,B], we'd return a new template that primarily matches
432 * A, but also matches B.
433 */
434 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
435 if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
436 // Requested template subscriber is part of the merge group; return
437 // a template that matches all merged subscribers.
438 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
439 template.mNetworkId);
440 } else {
441 return template;
442 }
443 }
444
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700445 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700446 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700447 public NetworkTemplate createFromParcel(Parcel in) {
448 return new NetworkTemplate(in);
449 }
450
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700451 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700452 public NetworkTemplate[] newArray(int size) {
453 return new NetworkTemplate[size];
454 }
455 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000456
457 public byte[] getBytesForBackup() throws IOException {
458 ByteArrayOutputStream baos = new ByteArrayOutputStream();
459 DataOutputStream out = new DataOutputStream(baos);
460
461 out.writeInt(BACKUP_VERSION);
462
463 out.writeInt(mMatchRule);
464 BackupUtils.writeString(out, mSubscriberId);
465 BackupUtils.writeString(out, mNetworkId);
466
467 return baos.toByteArray();
468 }
469
470 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
471 throws IOException, BackupUtils.BadVersionException {
472 int version = in.readInt();
473 if (version < 1 || version > BACKUP_VERSION) {
474 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
475 }
476
477 int matchRule = in.readInt();
478 String subscriberId = BackupUtils.readString(in);
479 String networkId = BackupUtils.readString(in);
480
481 return new NetworkTemplate(matchRule, subscriberId, networkId);
482 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700483}