blob: 0d2fcd0740476536c715b2ef0383e61a7fd6e1e2 [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;
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;
Christopher Tate3bf01732017-05-15 16:34:52 -070037import android.util.Log;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070038
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 {
Christopher Tate3bf01732017-05-15 16:34:52 -070055 private static final String TAG = "NetworkTemplate";
56
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;
Christopher Tate3bf01732017-05-15 16:34:52 -070063 /** @deprecated don't use this any more */
Jeff Sharkey69736342014-12-08 14:50:12 -080064 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070065 public static final int MATCH_MOBILE_3G_LOWER = 2;
Christopher Tate3bf01732017-05-15 16:34:52 -070066 /** @deprecated don't use this any more */
Jeff Sharkey69736342014-12-08 14:50:12 -080067 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070068 public static final int MATCH_MOBILE_4G = 3;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070069 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070070 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070071 public static final int MATCH_MOBILE_WILDCARD = 6;
72 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey55a442e2014-11-18 18:22:21 -080073 public static final int MATCH_BLUETOOTH = 8;
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -070074 public static final int MATCH_PROXY = 9;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070075
Christopher Tate3bf01732017-05-15 16:34:52 -070076 private static boolean isKnownMatchRule(final int rule) {
77 switch (rule) {
78 case MATCH_MOBILE_ALL:
79 case MATCH_MOBILE_3G_LOWER:
80 case MATCH_MOBILE_4G:
81 case MATCH_WIFI:
82 case MATCH_ETHERNET:
83 case MATCH_MOBILE_WILDCARD:
84 case MATCH_WIFI_WILDCARD:
85 case MATCH_BLUETOOTH:
86 case MATCH_PROXY:
87 return true;
88
89 default:
90 return false;
91 }
92 }
93
Jeff Sharkey70c70532012-05-16 14:51:19 -070094 private static boolean sForceAllNetworkTypes = false;
95
Jeff Sharkey70c70532012-05-16 14:51:19 -070096 public static void forceAllNetworkTypes() {
97 sForceAllNetworkTypes = true;
98 }
99
Jeff Sharkey630a1712011-09-26 10:47:10 -0700100 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700101 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
102 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700103 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700104 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700105 return new NetworkTemplate(MATCH_MOBILE_ALL, 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 "3G" definition, or lower.
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 buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700114 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700115 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700116
117 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700118 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
119 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700120 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700121 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700122 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700123 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700124 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700125
126 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -0700127 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
128 * regardless of IMSI.
129 */
130 public static NetworkTemplate buildTemplateMobileWildcard() {
131 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
132 }
133
134 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700135 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
136 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700137 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700138 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700139 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700140 }
141
142 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700143 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700144 return buildTemplateWifiWildcard();
145 }
146
147 /**
148 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
149 * given SSID.
150 */
151 public static NetworkTemplate buildTemplateWifi(String networkId) {
152 return new NetworkTemplate(MATCH_WIFI, null, networkId);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700153 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700154
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700155 /**
156 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
157 * networks together.
158 */
159 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700160 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700161 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700162
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800163 /**
164 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
165 * networks together.
166 */
167 public static NetworkTemplate buildTemplateBluetooth() {
168 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
169 }
170
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700171 /**
172 * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style
173 * networks together.
174 */
175 public static NetworkTemplate buildTemplateProxy() {
176 return new NetworkTemplate(MATCH_PROXY, null, null);
177 }
178
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700179 private final int mMatchRule;
180 private final String mSubscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800181
182 /**
183 * Ugh, templates are designed to target a single subscriber, but we might
184 * need to match several "merged" subscribers. These are the subscribers
185 * that should be considered to match this template.
186 * <p>
187 * Since the merge set is dynamic, it should <em>not</em> be persisted or
188 * used for determining equality.
189 */
190 private final String[] mMatchSubscriberIds;
191
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700192 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700193
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700194 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800195 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
196 }
197
198 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
199 String networkId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700200 mMatchRule = matchRule;
201 mSubscriberId = subscriberId;
Jeff Sharkey32566012014-12-02 18:30:14 -0800202 mMatchSubscriberIds = matchSubscriberIds;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700203 mNetworkId = networkId;
Christopher Tate3bf01732017-05-15 16:34:52 -0700204
205 if (!isKnownMatchRule(matchRule)) {
206 Log.e(TAG, "Unknown network template rule " + matchRule
207 + " will not match any identity.");
208 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700209 }
210
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700211 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700212 mMatchRule = in.readInt();
213 mSubscriberId = in.readString();
Jeff Sharkey32566012014-12-02 18:30:14 -0800214 mMatchSubscriberIds = in.createStringArray();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700215 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700216 }
217
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700218 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700219 public void writeToParcel(Parcel dest, int flags) {
220 dest.writeInt(mMatchRule);
221 dest.writeString(mSubscriberId);
Jeff Sharkey32566012014-12-02 18:30:14 -0800222 dest.writeStringArray(mMatchSubscriberIds);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700223 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700224 }
225
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700226 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700227 public int describeContents() {
228 return 0;
229 }
230
231 @Override
232 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700233 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
234 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
235 if (mSubscriberId != null) {
Jeff Sharkey32566012014-12-02 18:30:14 -0800236 builder.append(", subscriberId=").append(
237 NetworkIdentity.scrubSubscriberId(mSubscriberId));
238 }
239 if (mMatchSubscriberIds != null) {
240 builder.append(", matchSubscriberIds=").append(
241 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700242 }
243 if (mNetworkId != null) {
244 builder.append(", networkId=").append(mNetworkId);
245 }
246 return builder.toString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700247 }
248
249 @Override
250 public int hashCode() {
Kenny Roote6585b32013-12-13 12:00:26 -0800251 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700252 }
253
254 @Override
255 public boolean equals(Object obj) {
256 if (obj instanceof NetworkTemplate) {
257 final NetworkTemplate other = (NetworkTemplate) obj;
258 return mMatchRule == other.mMatchRule
Kenny Roote6585b32013-12-13 12:00:26 -0800259 && Objects.equals(mSubscriberId, other.mSubscriberId)
260 && Objects.equals(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700261 }
262 return false;
263 }
264
Jeff Sharkey32566012014-12-02 18:30:14 -0800265 public boolean isMatchRuleMobile() {
266 switch (mMatchRule) {
267 case MATCH_MOBILE_3G_LOWER:
268 case MATCH_MOBILE_4G:
269 case MATCH_MOBILE_ALL:
270 case MATCH_MOBILE_WILDCARD:
271 return true;
272 default:
273 return false;
274 }
275 }
276
Jeff Sharkey7474fe7b2016-03-21 13:12:59 -0600277 public boolean isPersistable() {
278 switch (mMatchRule) {
279 case MATCH_MOBILE_WILDCARD:
280 case MATCH_WIFI_WILDCARD:
281 return false;
282 default:
283 return true;
284 }
285 }
286
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700287 public int getMatchRule() {
288 return mMatchRule;
289 }
290
291 public String getSubscriberId() {
292 return mSubscriberId;
293 }
294
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700295 public String getNetworkId() {
296 return mNetworkId;
297 }
298
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700299 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700300 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700301 */
302 public boolean matches(NetworkIdentity ident) {
303 switch (mMatchRule) {
304 case MATCH_MOBILE_ALL:
305 return matchesMobile(ident);
306 case MATCH_MOBILE_3G_LOWER:
307 return matchesMobile3gLower(ident);
308 case MATCH_MOBILE_4G:
309 return matchesMobile4g(ident);
310 case MATCH_WIFI:
311 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700312 case MATCH_ETHERNET:
313 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700314 case MATCH_MOBILE_WILDCARD:
315 return matchesMobileWildcard(ident);
316 case MATCH_WIFI_WILDCARD:
317 return matchesWifiWildcard(ident);
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800318 case MATCH_BLUETOOTH:
319 return matchesBluetooth(ident);
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700320 case MATCH_PROXY:
321 return matchesProxy(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700322 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700323 // We have no idea what kind of network template we are, so we
324 // just claim not to match anything.
325 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700326 }
327 }
328
329 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700330 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700331 */
332 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700333 if (ident.mType == TYPE_WIMAX) {
334 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700335 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700336 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700337 return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered))
338 && !ArrayUtils.isEmpty(mMatchSubscriberIds)
Jeff Sharkey56859f32015-10-19 16:52:08 -0700339 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700340 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700341 }
342
343 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700344 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700345 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800346 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700347 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700348 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700349 if (ident.mType == TYPE_WIMAX) {
350 return false;
351 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700352 switch (getNetworkClass(ident.mSubType)) {
353 case NETWORK_CLASS_UNKNOWN:
354 case NETWORK_CLASS_2_G:
355 case NETWORK_CLASS_3_G:
356 return true;
357 }
358 }
359 return false;
360 }
361
362 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700363 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700364 */
Jeff Sharkey69736342014-12-08 14:50:12 -0800365 @Deprecated
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700366 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700367 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700368 if (ident.mType == TYPE_WIMAX) {
369 // TODO: consider matching against WiMAX subscriber identity
370 return true;
371 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700372 switch (getNetworkClass(ident.mSubType)) {
373 case NETWORK_CLASS_4_G:
374 return true;
375 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700376 }
377 return false;
378 }
379
380 /**
381 * Check if matches Wi-Fi network template.
382 */
383 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800384 switch (ident.mType) {
385 case TYPE_WIFI:
Kenny Roote6585b32013-12-13 12:00:26 -0800386 return Objects.equals(
Jeff Sharkey2e4dce02012-12-18 17:06:06 -0800387 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800388 default:
389 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700390 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700391 }
392
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700393 /**
394 * Check if matches Ethernet network template.
395 */
396 private boolean matchesEthernet(NetworkIdentity ident) {
397 if (ident.mType == TYPE_ETHERNET) {
398 return true;
399 }
400 return false;
401 }
402
Jeff Sharkey234766a2012-04-10 19:48:07 -0700403 private boolean matchesMobileWildcard(NetworkIdentity ident) {
404 if (ident.mType == TYPE_WIMAX) {
405 return true;
406 } else {
Jack Yu66a6be32016-03-30 11:14:39 -0700407 return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700408 }
409 }
410
411 private boolean matchesWifiWildcard(NetworkIdentity ident) {
412 switch (ident.mType) {
413 case TYPE_WIFI:
414 case TYPE_WIFI_P2P:
415 return true;
416 default:
417 return false;
418 }
419 }
420
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800421 /**
422 * Check if matches Bluetooth network template.
423 */
424 private boolean matchesBluetooth(NetworkIdentity ident) {
425 if (ident.mType == TYPE_BLUETOOTH) {
426 return true;
427 }
428 return false;
429 }
430
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700431 /**
432 * Check if matches Proxy network template.
433 */
434 private boolean matchesProxy(NetworkIdentity ident) {
435 return ident.mType == TYPE_PROXY;
436 }
437
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700438 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700439 switch (matchRule) {
440 case MATCH_MOBILE_3G_LOWER:
441 return "MOBILE_3G_LOWER";
442 case MATCH_MOBILE_4G:
443 return "MOBILE_4G";
444 case MATCH_MOBILE_ALL:
445 return "MOBILE_ALL";
446 case MATCH_WIFI:
447 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700448 case MATCH_ETHERNET:
449 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700450 case MATCH_MOBILE_WILDCARD:
451 return "MOBILE_WILDCARD";
452 case MATCH_WIFI_WILDCARD:
453 return "WIFI_WILDCARD";
Jeff Sharkey55a442e2014-11-18 18:22:21 -0800454 case MATCH_BLUETOOTH:
455 return "BLUETOOTH";
Sharvil Nanavati7f8d6502016-03-21 14:19:37 -0700456 case MATCH_PROXY:
457 return "PROXY";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700458 default:
Christopher Tate3bf01732017-05-15 16:34:52 -0700459 return "UNKNOWN(" + matchRule + ")";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700460 }
461 }
462
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700463 private static void ensureSubtypeAvailable() {
464 if (COMBINE_SUBTYPE_ENABLED) {
465 throw new IllegalArgumentException(
466 "Unable to enforce 3G_LOWER template on combined data.");
467 }
468 }
469
Jeff Sharkey32566012014-12-02 18:30:14 -0800470 /**
471 * Examine the given template and normalize if it refers to a "merged"
472 * mobile subscriber. We pick the "lowest" merged subscriber as the primary
473 * for key purposes, and expand the template to match all other merged
474 * subscribers.
475 * <p>
476 * For example, given an incoming template matching B, and the currently
477 * active merge set [A,B], we'd return a new template that primarily matches
478 * A, but also matches B.
479 */
480 public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) {
481 if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
482 // Requested template subscriber is part of the merge group; return
483 // a template that matches all merged subscribers.
484 return new NetworkTemplate(template.mMatchRule, merged[0], merged,
485 template.mNetworkId);
486 } else {
487 return template;
488 }
489 }
490
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700491 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700492 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700493 public NetworkTemplate createFromParcel(Parcel in) {
494 return new NetworkTemplate(in);
495 }
496
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700497 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700498 public NetworkTemplate[] newArray(int size) {
499 return new NetworkTemplate[size];
500 }
501 };
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000502
503 public byte[] getBytesForBackup() throws IOException {
504 ByteArrayOutputStream baos = new ByteArrayOutputStream();
505 DataOutputStream out = new DataOutputStream(baos);
506
507 out.writeInt(BACKUP_VERSION);
508
509 out.writeInt(mMatchRule);
510 BackupUtils.writeString(out, mSubscriberId);
511 BackupUtils.writeString(out, mNetworkId);
512
513 return baos.toByteArray();
514 }
515
516 public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in)
517 throws IOException, BackupUtils.BadVersionException {
518 int version = in.readInt();
519 if (version < 1 || version > BACKUP_VERSION) {
520 throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version");
521 }
522
523 int matchRule = in.readInt();
524 String subscriberId = BackupUtils.readString(in);
525 String networkId = BackupUtils.readString(in);
526
Christopher Tate3bf01732017-05-15 16:34:52 -0700527 if (!isKnownMatchRule(matchRule)) {
528 throw new BackupUtils.BadVersionException(
529 "Restored network template contains unknown match rule " + matchRule);
530 }
531
Ritesh Reddyadca34a2016-02-04 18:33:30 +0000532 return new NetworkTemplate(matchRule, subscriberId, networkId);
533 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700534}