NetworkTemplate.java revision 70c70530bd6793869736ec894498e4ebf5dc9b20
11b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/* 21b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Copyright (C) 2011 The Android Open Source Project 31b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * 41b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License"); 51b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * you may not use this file except in compliance with the License. 61b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * You may obtain a copy of the License at 71b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * 81b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * http://www.apache.org/licenses/LICENSE-2.0 91b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * 101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Unless required by applicable law or agreed to in writing, software 111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS, 121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * See the License for the specific language governing permissions and 141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * limitations under the License. 151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypackage android.net; 181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1902e21d6a5b5117d494777a36783909854854f751Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_ETHERNET; 201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIFI; 213ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIFI_P2P; 221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIMAX; 23d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkeyimport static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED; 2402e21d6a5b5117d494777a36783909854854f751Jeff Sharkeyimport static android.net.NetworkIdentity.scrubSubscriberId; 251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_2_G; 261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_3_G; 271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_4_G; 281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN; 291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.getNetworkClass; 30630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport static com.android.internal.util.ArrayUtils.contains; 311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 32630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport android.content.res.Resources; 331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcel; 341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcelable; 351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport com.android.internal.util.Objects; 371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/** 391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity}, 401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics. 411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * 421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide 431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable { 451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 464e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_ALL = 1; 474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_3G_LOWER = 2; 484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_4G = 3; 494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_WIFI = 4; 504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_ETHERNET = 5; 51234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static final int MATCH_MOBILE_WILDCARD = 6; 52234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static final int MATCH_WIFI_WILDCARD = 7; 534e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 55630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Set of {@link NetworkInfo#getType()} that reflect data usage. 56630a1712168f402653039e368259cb9480454fa8Jeff Sharkey */ 57630a1712168f402653039e368259cb9480454fa8Jeff Sharkey private static final int[] DATA_USAGE_NETWORK_TYPES; 58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey 59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey static { 60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray( 61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey com.android.internal.R.array.config_data_usage_network_types); 62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } 63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey 6470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey private static boolean sForceAllNetworkTypes = false; 6570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 6670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey // @VisibleForTesting 6770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey public static void forceAllNetworkTypes() { 6870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey sForceAllNetworkTypes = true; 6970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey } 7070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 71630a1712168f402653039e368259cb9480454fa8Jeff Sharkey /** 728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI. 741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 754e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { 768fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null); 774e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 808fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 818fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI that roughly meet a "3G" definition, or lower. 821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 844e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) { 858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null); 864e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 898fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 908fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI that roughly meet a "4G" definition. 911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 928fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobile4g(String subscriberId) { 948fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null); 954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 98234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, 99234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey * regardless of IMSI. 100234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey */ 101234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static NetworkTemplate buildTemplateMobileWildcard() { 102234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); 103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 104234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 105234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey /** 1068fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, 1078fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * regardless of SSID. 1081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 1098fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public static NetworkTemplate buildTemplateWifiWildcard() { 110234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); 1118fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 1138fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 1144e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateWifi() { 1158fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return buildTemplateWifiWildcard(); 1168fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1178fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 1188fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey /** 1198fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the 1208fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * given SSID. 1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey */ 1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public static NetworkTemplate buildTemplateWifi(String networkId) { 1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_WIFI, null, networkId); 1244e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 1251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1264e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey /** 1274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style 1284e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * networks together. 1294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey */ 1304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateEthernet() { 1318fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_ETHERNET, null, null); 1324e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 1334e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 1344e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private final int mMatchRule; 1354e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private final String mSubscriberId; 1368fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey private final String mNetworkId; 1371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1388fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public NetworkTemplate(int matchRule, String subscriberId, String networkId) { 1398fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mMatchRule = matchRule; 1408fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mSubscriberId = subscriberId; 1418fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mNetworkId = networkId; 1421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1444e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private NetworkTemplate(Parcel in) { 1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey mMatchRule = in.readInt(); 1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey mSubscriberId = in.readString(); 1478fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mNetworkId = in.readString(); 1481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1508fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 1511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public void writeToParcel(Parcel dest, int flags) { 1521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey dest.writeInt(mMatchRule); 1531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey dest.writeString(mSubscriberId); 1548fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey dest.writeString(mNetworkId); 1551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1578fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int describeContents() { 1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return 0; 1601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public String toString() { 1648fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey final StringBuilder builder = new StringBuilder("NetworkTemplate: "); 1658fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); 1668fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey if (mSubscriberId != null) { 1678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId)); 1688fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey if (mNetworkId != null) { 1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append(", networkId=").append(mNetworkId); 1718fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return builder.toString(); 1731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int hashCode() { 1778fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId); 1781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public boolean equals(Object obj) { 1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey if (obj instanceof NetworkTemplate) { 1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkTemplate other = (NetworkTemplate) obj; 1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mMatchRule == other.mMatchRule 1858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey && Objects.equal(mSubscriberId, other.mSubscriberId) 1868fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey && Objects.equal(mNetworkId, other.mNetworkId); 1871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int getMatchRule() { 1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mMatchRule; 1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public String getSubscriberId() { 1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mSubscriberId; 1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1998fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public String getNetworkId() { 2008fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return mNetworkId; 2018fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 2028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 2031b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 204630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Test if given {@link NetworkIdentity} matches this template. 2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public boolean matches(NetworkIdentity ident) { 2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (mMatchRule) { 2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_ALL: 2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile(ident); 2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_3G_LOWER: 2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile3gLower(ident); 2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_4G: 2131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile4g(ident); 2141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_WIFI: 2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesWifi(ident); 2164e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey case MATCH_ETHERNET: 2174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return matchesEthernet(ident); 218234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_MOBILE_WILDCARD: 219234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return matchesMobileWildcard(ident); 220234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_WIFI_WILDCARD: 221234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return matchesWifiWildcard(ident); 2221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey default: 2231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey throw new IllegalArgumentException("unknown network template"); 2241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 228630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Check if mobile network with matching IMSI. 2291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile(NetworkIdentity ident) { 231630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 232630a1712168f402653039e368259cb9480454fa8Jeff Sharkey // TODO: consider matching against WiMAX subscriber identity 2331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 234630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else { 23570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType)) 236630a1712168f402653039e368259cb9480454fa8Jeff Sharkey && Objects.equal(mSubscriberId, ident.mSubscriberId)); 2371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 24102e21d6a5b5117d494777a36783909854854f751Jeff Sharkey * Check if mobile network classified 3G or lower with matching IMSI. 2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile3gLower(NetworkIdentity ident) { 244d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey ensureSubtypeAvailable(); 245630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 246630a1712168f402653039e368259cb9480454fa8Jeff Sharkey return false; 247630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else if (matchesMobile(ident)) { 2481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (getNetworkClass(ident.mSubType)) { 2491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_UNKNOWN: 2501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_2_G: 2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_3_G: 2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 259630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Check if mobile network classified 4G with matching IMSI. 2601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile4g(NetworkIdentity ident) { 262d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey ensureSubtypeAvailable(); 263630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 264630a1712168f402653039e368259cb9480454fa8Jeff Sharkey // TODO: consider matching against WiMAX subscriber identity 265630a1712168f402653039e368259cb9480454fa8Jeff Sharkey return true; 266630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else if (matchesMobile(ident)) { 2671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (getNetworkClass(ident.mSubType)) { 2681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_4_G: 2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Check if matches Wi-Fi network template. 2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesWifi(NetworkIdentity ident) { 2793ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey switch (ident.mType) { 2803ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey case TYPE_WIFI: 281234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return Objects.equal(mNetworkId, ident.mNetworkId); 2823ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey default: 2833ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey return false; 2841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2874e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey /** 2884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * Check if matches Ethernet network template. 2894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey */ 2904e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private boolean matchesEthernet(NetworkIdentity ident) { 2914e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey if (ident.mType == TYPE_ETHERNET) { 2924e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return true; 2934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 2944e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return false; 2954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 2964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 297234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey private boolean matchesMobileWildcard(NetworkIdentity ident) { 298234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 299234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return true; 300234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } else { 30170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType); 302234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 303234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 304234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 305234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey private boolean matchesWifiWildcard(NetworkIdentity ident) { 306234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey switch (ident.mType) { 307234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case TYPE_WIFI: 308234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case TYPE_WIFI_P2P: 309234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return true; 310234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey default: 311234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return false; 312234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 313234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 314234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 3154e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private static String getMatchRuleName(int matchRule) { 3161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (matchRule) { 3171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_3G_LOWER: 3181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_3G_LOWER"; 3191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_4G: 3201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_4G"; 3211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_ALL: 3221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_ALL"; 3231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_WIFI: 3241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "WIFI"; 3254e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey case MATCH_ETHERNET: 3264e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return "ETHERNET"; 327234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_MOBILE_WILDCARD: 328234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return "MOBILE_WILDCARD"; 329234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_WIFI_WILDCARD: 330234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return "WIFI_WILDCARD"; 3311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey default: 3321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "UNKNOWN"; 3331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 336d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey private static void ensureSubtypeAvailable() { 337d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey if (COMBINE_SUBTYPE_ENABLED) { 338d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey throw new IllegalArgumentException( 339d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey "Unable to enforce 3G_LOWER template on combined data."); 340d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey } 341d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey } 342d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey 3431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { 3448fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 3451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public NetworkTemplate createFromParcel(Parcel in) { 3461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return new NetworkTemplate(in); 3471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 3498fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 3501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public NetworkTemplate[] newArray(int size) { 3511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return new NetworkTemplate[size]; 3521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey }; 3541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey} 355