2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import static android.net.ConnectivityManager.TYPE_BLUETOOTH;
20 import static android.net.ConnectivityManager.TYPE_ETHERNET;
21 import static android.net.ConnectivityManager.TYPE_WIFI;
22 import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
23 import static android.net.ConnectivityManager.TYPE_WIMAX;
24 import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
25 import static android.net.wifi.WifiInfo.removeDoubleQuotes;
26 import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
27 import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
28 import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
29 import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
30 import static android.telephony.TelephonyManager.getNetworkClass;
32 import static com.android.internal.util.ArrayUtils.contains;
34 import android.content.res.Resources;
35 import android.os.Parcel;
36 import android.os.Parcelable;
37 import android.util.BackupUtils;
39 import com.android.internal.annotations.VisibleForTesting;
40 import com.android.internal.util.ArrayUtils;
42 import java.io.ByteArrayOutputStream;
43 import java.io.DataInputStream;
44 import java.io.DataOutputStream;
45 import java.io.IOException;
46 import java.util.Arrays;
47 import java.util.Objects;
50 * Template definition used to generically match {@link NetworkIdentity},
51 * usually when collecting statistics.
55 public class NetworkTemplate implements Parcelable {
57 * Current Version of the Backup Serializer.
59 private static final int BACKUP_VERSION = 1;
61 public static final int MATCH_MOBILE_ALL = 1;
63 public static final int MATCH_MOBILE_3G_LOWER = 2;
65 public static final int MATCH_MOBILE_4G = 3;
66 public static final int MATCH_WIFI = 4;
67 public static final int MATCH_ETHERNET = 5;
68 public static final int MATCH_MOBILE_WILDCARD = 6;
69 public static final int MATCH_WIFI_WILDCARD = 7;
70 public static final int MATCH_BLUETOOTH = 8;
73 * Set of {@link NetworkInfo#getType()} that reflect data usage.
75 private static final int[] DATA_USAGE_NETWORK_TYPES;
78 DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
79 com.android.internal.R.array.config_data_usage_network_types);
82 private static boolean sForceAllNetworkTypes = false;
85 public static void forceAllNetworkTypes() {
86 sForceAllNetworkTypes = true;
90 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
93 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
94 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
98 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
99 * the given IMSI that roughly meet a "3G" definition, or lower.
102 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
103 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
107 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
108 * the given IMSI that roughly meet a "4G" definition.
111 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
112 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
116 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
117 * regardless of IMSI.
119 public static NetworkTemplate buildTemplateMobileWildcard() {
120 return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
124 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
125 * regardless of SSID.
127 public static NetworkTemplate buildTemplateWifiWildcard() {
128 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
132 public static NetworkTemplate buildTemplateWifi() {
133 return buildTemplateWifiWildcard();
137 * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
140 public static NetworkTemplate buildTemplateWifi(String networkId) {
141 return new NetworkTemplate(MATCH_WIFI, null, networkId);
145 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
148 public static NetworkTemplate buildTemplateEthernet() {
149 return new NetworkTemplate(MATCH_ETHERNET, null, null);
153 * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
156 public static NetworkTemplate buildTemplateBluetooth() {
157 return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
160 private final int mMatchRule;
161 private final String mSubscriberId;
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.
168 * Since the merge set is dynamic, it should <em>not</em> be persisted or
169 * used for determining equality.
171 private final String[] mMatchSubscriberIds;
173 private final String mNetworkId;
175 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
176 this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
179 public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds,
181 mMatchRule = matchRule;
182 mSubscriberId = subscriberId;
183 mMatchSubscriberIds = matchSubscriberIds;
184 mNetworkId = networkId;
187 private NetworkTemplate(Parcel in) {
188 mMatchRule = in.readInt();
189 mSubscriberId = in.readString();
190 mMatchSubscriberIds = in.createStringArray();
191 mNetworkId = in.readString();
195 public void writeToParcel(Parcel dest, int flags) {
196 dest.writeInt(mMatchRule);
197 dest.writeString(mSubscriberId);
198 dest.writeStringArray(mMatchSubscriberIds);
199 dest.writeString(mNetworkId);
203 public int describeContents() {
208 public String toString() {
209 final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
210 builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
211 if (mSubscriberId != null) {
212 builder.append(", subscriberId=").append(
213 NetworkIdentity.scrubSubscriberId(mSubscriberId));
215 if (mMatchSubscriberIds != null) {
216 builder.append(", matchSubscriberIds=").append(
217 Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
219 if (mNetworkId != null) {
220 builder.append(", networkId=").append(mNetworkId);
222 return builder.toString();
226 public int hashCode() {
227 return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
231 public boolean equals(Object obj) {
232 if (obj instanceof NetworkTemplate) {
233 final NetworkTemplate other = (NetworkTemplate) obj;
234 return mMatchRule == other.mMatchRule
235 && Objects.equals(mSubscriberId, other.mSubscriberId)
236 && Objects.equals(mNetworkId, other.mNetworkId);
241 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:
253 public int getMatchRule() {
257 public String getSubscriberId() {
258 return mSubscriberId;
261 public String getNetworkId() {
266 * Test if given {@link NetworkIdentity} matches this template.
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);
277 return matchesWifi(ident);
279 return matchesEthernet(ident);
280 case MATCH_MOBILE_WILDCARD:
281 return matchesMobileWildcard(ident);
282 case MATCH_WIFI_WILDCARD:
283 return matchesWifiWildcard(ident);
284 case MATCH_BLUETOOTH:
285 return matchesBluetooth(ident);
287 throw new IllegalArgumentException("unknown network template");
292 * Check if mobile network with matching IMSI.
294 private boolean matchesMobile(NetworkIdentity ident) {
295 if (ident.mType == TYPE_WIMAX) {
296 // TODO: consider matching against WiMAX subscriber identity
299 final boolean matchesType = (sForceAllNetworkTypes
300 || contains(DATA_USAGE_NETWORK_TYPES, ident.mType));
301 return matchesType && !ArrayUtils.isEmpty(mMatchSubscriberIds)
302 && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
307 * Check if mobile network classified 3G or lower with matching IMSI.
310 private boolean matchesMobile3gLower(NetworkIdentity ident) {
311 ensureSubtypeAvailable();
312 if (ident.mType == TYPE_WIMAX) {
314 } else if (matchesMobile(ident)) {
315 switch (getNetworkClass(ident.mSubType)) {
316 case NETWORK_CLASS_UNKNOWN:
317 case NETWORK_CLASS_2_G:
318 case NETWORK_CLASS_3_G:
326 * Check if mobile network classified 4G with matching IMSI.
329 private boolean matchesMobile4g(NetworkIdentity ident) {
330 ensureSubtypeAvailable();
331 if (ident.mType == TYPE_WIMAX) {
332 // TODO: consider matching against WiMAX subscriber identity
334 } else if (matchesMobile(ident)) {
335 switch (getNetworkClass(ident.mSubType)) {
336 case NETWORK_CLASS_4_G:
344 * Check if matches Wi-Fi network template.
346 private boolean matchesWifi(NetworkIdentity ident) {
347 switch (ident.mType) {
349 return Objects.equals(
350 removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
357 * Check if matches Ethernet network template.
359 private boolean matchesEthernet(NetworkIdentity ident) {
360 if (ident.mType == TYPE_ETHERNET) {
366 private boolean matchesMobileWildcard(NetworkIdentity ident) {
367 if (ident.mType == TYPE_WIMAX) {
370 return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
374 private boolean matchesWifiWildcard(NetworkIdentity ident) {
375 switch (ident.mType) {
385 * Check if matches Bluetooth network template.
387 private boolean matchesBluetooth(NetworkIdentity ident) {
388 if (ident.mType == TYPE_BLUETOOTH) {
394 private static String getMatchRuleName(int matchRule) {
396 case MATCH_MOBILE_3G_LOWER:
397 return "MOBILE_3G_LOWER";
398 case MATCH_MOBILE_4G:
400 case MATCH_MOBILE_ALL:
406 case MATCH_MOBILE_WILDCARD:
407 return "MOBILE_WILDCARD";
408 case MATCH_WIFI_WILDCARD:
409 return "WIFI_WILDCARD";
410 case MATCH_BLUETOOTH:
417 private static void ensureSubtypeAvailable() {
418 if (COMBINE_SUBTYPE_ENABLED) {
419 throw new IllegalArgumentException(
420 "Unable to enforce 3G_LOWER template on combined data.");
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
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.
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);
445 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
447 public NetworkTemplate createFromParcel(Parcel in) {
448 return new NetworkTemplate(in);
452 public NetworkTemplate[] newArray(int size) {
453 return new NetworkTemplate[size];
457 public byte[] getBytesForBackup() throws IOException {
458 ByteArrayOutputStream baos = new ByteArrayOutputStream();
459 DataOutputStream out = new DataOutputStream(baos);
461 out.writeInt(BACKUP_VERSION);
463 out.writeInt(mMatchRule);
464 BackupUtils.writeString(out, mSubscriberId);
465 BackupUtils.writeString(out, mNetworkId);
467 return baos.toByteArray();
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");
477 int matchRule = in.readInt();
478 String subscriberId = BackupUtils.readString(in);
479 String networkId = BackupUtils.readString(in);
481 return new NetworkTemplate(matchRule, subscriberId, networkId);