blob: b32b2ccd7667d7096f80470745b187f5ec387a18 [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;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070022import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey3ca74812012-01-24 15:37:07 -080023import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070024import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070025import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
Jeff Sharkey2e4dce02012-12-18 17:06:06 -080026import static android.net.wifi.WifiInfo.removeDoubleQuotes;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070027import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
28import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
29import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
30import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
31import static android.telephony.TelephonyManager.getNetworkClass;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000032
Jeff Sharkey630a1712011-09-26 10:47:10 -070033import static com.android.internal.util.ArrayUtils.contains;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070034
Jeff Sharkey630a1712011-09-26 10:47:10 -070035import android.content.res.Resources;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070036import android.os.Parcel;
37import android.os.Parcelable;
Ritesh Reddyadca34a2016-02-04 18:33:30 +000038import android.util.BackupUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070039
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080040import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey32566012014-12-02 18:30:14 -080041import com.android.internal.util.ArrayUtils;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070042
Ritesh Reddyadca34a2016-02-04 18:33:30 +000043import java.io.ByteArrayOutputStream;
44import java.io.DataInputStream;
45import java.io.DataOutputStream;
46import java.io.IOException;
Jeff Sharkey32566012014-12-02 18:30:14 -080047import java.util.Arrays;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080048import java.util.Objects;
49
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070050/**
51 * Template definition used to generically match {@link NetworkIdentity},
52 * usually when collecting statistics.
53 *
54 * @hide
55 */
56public class NetworkTemplate implements Parcelable {
Ritesh Reddyadca34a2016-02-04 18:33:30 +000057 /**
58 * Current Version of the Backup Serializer.
59 */
60 private static final int BACKUP_VERSION = 1;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070061
Jeff Sharkey4e814c32011-07-14 20:37:37 -070062 public static final int MATCH_MOBILE_ALL = 1;
Jeff Sharkey69736342014-12-08 14:50:12 -080063 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070064 public static final int MATCH_MOBILE_3G_LOWER = 2;
Jeff Sharkey69736342014-12-08 14:50:12 -080065 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070066 public static final int MATCH_MOBILE_4G = 3;
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
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070074 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -070075 * Set of {@link NetworkInfo#getType()} that reflect data usage.
76 */
77 private static final int[] DATA_USAGE_NETWORK_TYPES;
78
79 static {
80 DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
81 com.android.internal.R.array.config_data_usage_network_types);
82 }
83
Jeff Sharkey70c70532012-05-16 14:51:19 -070084 private static boolean sForceAllNetworkTypes = false;
85
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080086 @VisibleForTesting
Jeff Sharkey70c70532012-05-16 14:51:19 -070087 public static void forceAllNetworkTypes() {
88 sForceAllNetworkTypes = true;
89 }
90
Jeff Sharkey630a1712011-09-26 10:47:10 -070091 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070092 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
93 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070094 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070095 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070096 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070097 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070098
99 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700100 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
101 * the given IMSI that roughly meet a "3G" definition, or lower.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700102 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700103 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700104 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700105 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700106 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700107
108 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700109 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
110 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700111 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700112 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700113 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700114 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700115 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700116
117 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -0700118 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
119 * regardless of IMSI.
120 */
121 public static NetworkTemplate buildTemplateMobileWildcard() {
122 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
123 }
124
125 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700126 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
127 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700128 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700129 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700130 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700131 }
132
133 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700134 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700135 return buildTemplateWifiWildcard();
136 }
137
138 /**
139 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
140 * given SSID.
141 */
142 public static NetworkTemplate buildTemplateWifi(String networkId) {
143 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700144 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700145
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700146 /**
147 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
148 * networks together.
149 */
150 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700151 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700152 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700153
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800154 /**
155 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
156 * networks together.
157 */
158 public static NetworkTemplate buildTemplateBluetooth() {
159 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
160 }
161
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700162 /**
163 * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style
164 * networks together.
165 */
166 public static NetworkTemplate buildTemplateProxy() {
167 return new NetworkTemplate(MATCH_PROXY, null, null);
168 }
169
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700170 private final int mMatchRule;
171 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800172
173 /**
174 * Ugh, templates are designed to target a single subscriber, but we might
175 * need to match several "merged" subscribers. These are the subscribers
176 * that should be considered to match this template.
177 * <p>
178 * Since the merge set is dynamic, it should <em>not</em> be persisted or
179 * used for determining equality.
180 */
181 private final String[] mMatchSubscriberIds;
182
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700183 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700184
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700185 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800186 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
187 }
188
189 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
190 String networkId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700191 mMatchRule = matchRule;
192 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800193 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700194 mNetworkId = networkId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700195 }
196
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700197 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700198 mMatchRule = in.readInt();
199 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800200 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700201 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700202 }
203
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700204 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700205 public void writeToParcel(Parcel dest, int flags) {
206 dest.writeInt(mMatchRule);
207 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800208 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700209 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700210 }
211
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700212 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700213 public int describeContents() {
214 return 0;
215 }
216
217 @Override
218 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700219 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
220 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
221 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800222 builder.append(", subscriberId=").append(
223 NetworkIdentity.scrubSubscriberId(mSubscriberId));
224 }
225 if (mMatchSubscriberIds != null) {
226 builder.append(", matchSubscriberIds=").append(
227 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700228 }
229 if (mNetworkId != null) {
230 builder.append(", networkId=").append(mNetworkId);
231 }
232 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700233 }
234
235 @Override
236 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -0800237 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700238 }
239
240 @Override
241 public boolean equals(Object obj) {
242 if (obj instanceof NetworkTemplate) {
243 final NetworkTemplate other = (NetworkTemplate) obj;
244 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800245 && Objects.equals(mSubscriberId, other.mSubscriberId)
246 && Objects.equals(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700247 }
248 return false;
249 }
250
Jeff Sharkey32566012014-12-02 18:30:14 -0800251 public boolean isMatchRuleMobile() {
252 switch (mMatchRule) {
253 case MATCH_MOBILE_3G_LOWER:
254 case MATCH_MOBILE_4G:
255 case MATCH_MOBILE_ALL:
256 case MATCH_MOBILE_WILDCARD:
257 return true;
258 default:
259 return false;
260 }
261 }
262
Jeff Sharkey7474fe7b2016-03-21 13:12:59 -0600263 public boolean isPersistable() {
264 switch (mMatchRule) {
265 case MATCH_MOBILE_WILDCARD:
266 case MATCH_WIFI_WILDCARD:
267 return false;
268 default:
269 return true;
270 }
271 }
272
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700273 public int getMatchRule() {
274 return mMatchRule;
275 }
276
277 public String getSubscriberId() {
278 return mSubscriberId;
279 }
280
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700281 public String getNetworkId() {
282 return mNetworkId;
283 }
284
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700285 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700286 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700287 */
288 public boolean matches(NetworkIdentity ident) {
289 switch (mMatchRule) {
290 case MATCH_MOBILE_ALL:
291 return matchesMobile(ident);
292 case MATCH_MOBILE_3G_LOWER:
293 return matchesMobile3gLower(ident);
294 case MATCH_MOBILE_4G:
295 return matchesMobile4g(ident);
296 case MATCH_WIFI:
297 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700298 case MATCH_ETHERNET:
299 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700300 case MATCH_MOBILE_WILDCARD:
301 return matchesMobileWildcard(ident);
302 case MATCH_WIFI_WILDCARD:
303 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800304 case MATCH_BLUETOOTH:
305 return matchesBluetooth(ident);
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700306 case MATCH_PROXY:
307 return matchesProxy(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700308 default:
309 throw new IllegalArgumentException("unknown network template");
310 }
311 }
312
313 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700314 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700315 */
316 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700317 if (ident.mType == TYPE_WIMAX) {
318 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700319 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700320 } else {
Jeff Sharkey32566012014-12-02 18:30:14 -0800321 final boolean matchesType = (sForceAllNetworkTypes
322 || contains(DATA_USAGE_NETWORK_TYPES, ident.mType));
Jeff Sharkey56859f32015-10-19 16:52:08 -0700323 return matchesType && !ArrayUtils.isEmpty(mMatchSubscriberIds)
324 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700325 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700326 }
327
328 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700329 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700330 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800331 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700332 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700333 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700334 if (ident.mType == TYPE_WIMAX) {
335 return false;
336 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700337 switch (getNetworkClass(ident.mSubType)) {
338 case NETWORK_CLASS_UNKNOWN:
339 case NETWORK_CLASS_2_G:
340 case NETWORK_CLASS_3_G:
341 return true;
342 }
343 }
344 return false;
345 }
346
347 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700348 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700349 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800350 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700351 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700352 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700353 if (ident.mType == TYPE_WIMAX) {
354 // TODO: consider matching against WiMAX subscriber identity
355 return true;
356 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700357 switch (getNetworkClass(ident.mSubType)) {
358 case NETWORK_CLASS_4_G:
359 return true;
360 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700361 }
362 return false;
363 }
364
365 /**
366 * Check if matches Wi-Fi network template.
367 */
368 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800369 switch (ident.mType) {
370 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800371 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800372 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800373 default:
374 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700375 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700376 }
377
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700378 /**
379 * Check if matches Ethernet network template.
380 */
381 private boolean matchesEthernet(NetworkIdentity ident) {
382 if (ident.mType == TYPE_ETHERNET) {
383 return true;
384 }
385 return false;
386 }
387
Jeff Sharkey234766a2012-04-10 19:48:07 -0700388 private boolean matchesMobileWildcard(NetworkIdentity ident) {
389 if (ident.mType == TYPE_WIMAX) {
390 return true;
391 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700392 return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700393 }
394 }
395
396 private boolean matchesWifiWildcard(NetworkIdentity ident) {
397 switch (ident.mType) {
398 case TYPE_WIFI:
399 case TYPE_WIFI_P2P:
400 return true;
401 default:
402 return false;
403 }
404 }
405
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800406 /**
407 * Check if matches Bluetooth network template.
408 */
409 private boolean matchesBluetooth(NetworkIdentity ident) {
410 if (ident.mType == TYPE_BLUETOOTH) {
411 return true;
412 }
413 return false;
414 }
415
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700416 /**
417 * Check if matches Proxy network template.
418 */
419 private boolean matchesProxy(NetworkIdentity ident) {
420 return ident.mType == TYPE_PROXY;
421 }
422
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700423 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700424 switch (matchRule) {
425 case MATCH_MOBILE_3G_LOWER:
426 return "MOBILE_3G_LOWER";
427 case MATCH_MOBILE_4G:
428 return "MOBILE_4G";
429 case MATCH_MOBILE_ALL:
430 return "MOBILE_ALL";
431 case MATCH_WIFI:
432 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700433 case MATCH_ETHERNET:
434 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700435 case MATCH_MOBILE_WILDCARD:
436 return "MOBILE_WILDCARD";
437 case MATCH_WIFI_WILDCARD:
438 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800439 case MATCH_BLUETOOTH:
440 return "BLUETOOTH";
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700441 case MATCH_PROXY:
442 return "PROXY";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700443 default:
444 return "UNKNOWN";
445 }
446 }
447
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700448 private static void ensureSubtypeAvailable() {
449 if (COMBINE_SUBTYPE_ENABLED) {
450 throw new IllegalArgumentException(
451 "Unable to enforce 3G_LOWER template on combined data.");
452 }
453 }
454
Jeff Sharkey32566012014-12-02 18:30:14 -0800455 /**
456 * Examine the given template and normalize if it refers to a "merged"
457 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
458 * for key purposes, and expand the template to match all other merged
459 * subscribers.
460 * <p>
461 * For example, given an incoming template matching B, and the currently
462 * active merge set [A,B], we'd return a new template that primarily matches
463 * A, but also matches B.
464 */
465 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
466 if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
467 // Requested template subscriber is part of the merge group; return
468 // a template that matches all merged subscribers.
469 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
470 template.mNetworkId);
471 } else {
472 return template;
473 }
474 }
475
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700476 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700477 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700478 public NetworkTemplate createFromParcel(Parcel in) {
479 return new NetworkTemplate(in);
480 }
481
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700482 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700483 public NetworkTemplate[] newArray(int size) {
484 return new NetworkTemplate[size];
485 }
486 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000487
488 public byte[] getBytesForBackup() throws IOException {
489 ByteArrayOutputStream baos = new ByteArrayOutputStream();
490 DataOutputStream out = new DataOutputStream(baos);
491
492 out.writeInt(BACKUP_VERSION);
493
494 out.writeInt(mMatchRule);
495 BackupUtils.writeString(out, mSubscriberId);
496 BackupUtils.writeString(out, mNetworkId);
497
498 return baos.toByteArray();
499 }
500
501 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
502 throws IOException, BackupUtils.BadVersionException {
503 int version = in.readInt();
504 if (version < 1 || version > BACKUP_VERSION) {
505 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
506 }
507
508 int matchRule = in.readInt();
509 String subscriberId = BackupUtils.readString(in);
510 String networkId = BackupUtils.readString(in);
511
512 return new NetworkTemplate(matchRule, subscriberId, networkId);
513 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700514}