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; 252e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkeyimport static android.net.wifi.WifiInfo.removeDoubleQuotes; 261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_2_G; 271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_3_G; 281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_4_G; 291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN; 301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.getNetworkClass; 31630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport static com.android.internal.util.ArrayUtils.contains; 321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 33630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport android.content.res.Resources; 341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcel; 351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcelable; 361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 378b2c3a14603d163d7564e6f60286995079687690Jeff Sharkeyimport com.android.internal.annotations.VisibleForTesting; 381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport com.android.internal.util.Objects; 391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/** 411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity}, 421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics. 431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * 441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide 451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable { 471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_ALL = 1; 494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_3G_LOWER = 2; 504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_MOBILE_4G = 3; 514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_WIFI = 4; 524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static final int MATCH_ETHERNET = 5; 53234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static final int MATCH_MOBILE_WILDCARD = 6; 54234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static final int MATCH_WIFI_WILDCARD = 7; 554e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 57630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Set of {@link NetworkInfo#getType()} that reflect data usage. 58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey */ 59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey private static final int[] DATA_USAGE_NETWORK_TYPES; 60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey 61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey static { 62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray( 63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey com.android.internal.R.array.config_data_usage_network_types); 64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } 65630a1712168f402653039e368259cb9480454fa8Jeff Sharkey 6670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey private static boolean sForceAllNetworkTypes = false; 6770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 688b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey @VisibleForTesting 6970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey public static void forceAllNetworkTypes() { 7070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey sForceAllNetworkTypes = true; 7170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey } 7270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey 73630a1712168f402653039e368259cb9480454fa8Jeff Sharkey /** 748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 758fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI. 761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 774e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { 788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null); 794e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 828fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI that roughly meet a "3G" definition, or lower. 841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 864e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) { 878fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null); 884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 918fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with 928fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * the given IMSI that roughly meet a "4G" definition. 931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 948fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateMobile4g(String subscriberId) { 968fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null); 974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 100234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, 101234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey * regardless of IMSI. 102234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey */ 103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey public static NetworkTemplate buildTemplateMobileWildcard() { 104234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); 105234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 106234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 107234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey /** 1088fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, 1098fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * regardless of SSID. 1101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 1118fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public static NetworkTemplate buildTemplateWifiWildcard() { 112234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); 1138fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 1158fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Deprecated 1164e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateWifi() { 1178fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return buildTemplateWifiWildcard(); 1188fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1198fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 1208fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey /** 1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the 1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey * given SSID. 1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey */ 1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public static NetworkTemplate buildTemplateWifi(String networkId) { 1258fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_WIFI, null, networkId); 1264e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 1271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1284e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey /** 1294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style 1304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * networks together. 1314e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey */ 1324e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey public static NetworkTemplate buildTemplateEthernet() { 1338fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return new NetworkTemplate(MATCH_ETHERNET, null, null); 1344e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 1354e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 1364e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private final int mMatchRule; 1374e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private final String mSubscriberId; 1388fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey private final String mNetworkId; 1391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1408fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public NetworkTemplate(int matchRule, String subscriberId, String networkId) { 1418fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mMatchRule = matchRule; 1428fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mSubscriberId = subscriberId; 1438fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mNetworkId = networkId; 1441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1464e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private NetworkTemplate(Parcel in) { 1471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey mMatchRule = in.readInt(); 1481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey mSubscriberId = in.readString(); 1498fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey mNetworkId = in.readString(); 1501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1528fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 1531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public void writeToParcel(Parcel dest, int flags) { 1541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey dest.writeInt(mMatchRule); 1551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey dest.writeString(mSubscriberId); 1568fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey dest.writeString(mNetworkId); 1571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1598fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 1601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int describeContents() { 1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return 0; 1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public String toString() { 1668fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey final StringBuilder builder = new StringBuilder("NetworkTemplate: "); 1678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); 1688fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey if (mSubscriberId != null) { 1698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId)); 1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1718fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey if (mNetworkId != null) { 1728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey builder.append(", networkId=").append(mNetworkId); 1738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 1748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return builder.toString(); 1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int hashCode() { 1798fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId); 1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey @Override 1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public boolean equals(Object obj) { 1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey if (obj instanceof NetworkTemplate) { 1851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey final NetworkTemplate other = (NetworkTemplate) obj; 1861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mMatchRule == other.mMatchRule 1878fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey && Objects.equal(mSubscriberId, other.mSubscriberId) 1888fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey && Objects.equal(mNetworkId, other.mNetworkId); 1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public int getMatchRule() { 1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mMatchRule; 1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public String getSubscriberId() { 1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return mSubscriberId; 1991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2018fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey public String getNetworkId() { 2028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey return mNetworkId; 2038fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey } 2048fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey 2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 206630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Test if given {@link NetworkIdentity} matches this template. 2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public boolean matches(NetworkIdentity ident) { 2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (mMatchRule) { 2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_ALL: 2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile(ident); 2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_3G_LOWER: 2131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile3gLower(ident); 2141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_4G: 2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesMobile4g(ident); 2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_WIFI: 2171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return matchesWifi(ident); 2184e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey case MATCH_ETHERNET: 2194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return matchesEthernet(ident); 220234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_MOBILE_WILDCARD: 221234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return matchesMobileWildcard(ident); 222234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_WIFI_WILDCARD: 223234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return matchesWifiWildcard(ident); 2241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey default: 2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey throw new IllegalArgumentException("unknown network template"); 2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 230630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Check if mobile network with matching IMSI. 2311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile(NetworkIdentity ident) { 233630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 234630a1712168f402653039e368259cb9480454fa8Jeff Sharkey // TODO: consider matching against WiMAX subscriber identity 2351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 236630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else { 23770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType)) 238630a1712168f402653039e368259cb9480454fa8Jeff Sharkey && Objects.equal(mSubscriberId, ident.mSubscriberId)); 2391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 24302e21d6a5b5117d494777a36783909854854f751Jeff Sharkey * Check if mobile network classified 3G or lower with matching IMSI. 2441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile3gLower(NetworkIdentity ident) { 246d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey ensureSubtypeAvailable(); 247630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 248630a1712168f402653039e368259cb9480454fa8Jeff Sharkey return false; 249630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else if (matchesMobile(ident)) { 2501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (getNetworkClass(ident.mSubType)) { 2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_UNKNOWN: 2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_2_G: 2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_3_G: 2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 261630a1712168f402653039e368259cb9480454fa8Jeff Sharkey * Check if mobile network classified 4G with matching IMSI. 2621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesMobile4g(NetworkIdentity ident) { 264d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey ensureSubtypeAvailable(); 265630a1712168f402653039e368259cb9480454fa8Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 266630a1712168f402653039e368259cb9480454fa8Jeff Sharkey // TODO: consider matching against WiMAX subscriber identity 267630a1712168f402653039e368259cb9480454fa8Jeff Sharkey return true; 268630a1712168f402653039e368259cb9480454fa8Jeff Sharkey } else if (matchesMobile(ident)) { 2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (getNetworkClass(ident.mSubType)) { 2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case NETWORK_CLASS_4_G: 2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return true; 2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return false; 2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey /** 2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Check if matches Wi-Fi network template. 2791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */ 2801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey private boolean matchesWifi(NetworkIdentity ident) { 2813ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey switch (ident.mType) { 2823ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey case TYPE_WIFI: 2832e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkey return Objects.equal( 2842e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkey removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId)); 2853ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey default: 2863ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey return false; 2871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 2891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 2904e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey /** 2914e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey * Check if matches Ethernet network template. 2924e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey */ 2934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private boolean matchesEthernet(NetworkIdentity ident) { 2944e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey if (ident.mType == TYPE_ETHERNET) { 2954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return true; 2964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 2974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return false; 2984e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey } 2994e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey 300234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey private boolean matchesMobileWildcard(NetworkIdentity ident) { 301234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey if (ident.mType == TYPE_WIMAX) { 302234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return true; 303234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } else { 30470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType); 305234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 306234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 307234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 308234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey private boolean matchesWifiWildcard(NetworkIdentity ident) { 309234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey switch (ident.mType) { 310234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case TYPE_WIFI: 311234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case TYPE_WIFI_P2P: 312234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return true; 313234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey default: 314234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return false; 315234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 316234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey } 317234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey 3184e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey private static String getMatchRuleName(int matchRule) { 3191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey switch (matchRule) { 3201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_3G_LOWER: 3211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_3G_LOWER"; 3221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_4G: 3231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_4G"; 3241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_MOBILE_ALL: 3251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "MOBILE_ALL"; 3261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey case MATCH_WIFI: 3271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "WIFI"; 3284e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey case MATCH_ETHERNET: 3294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey return "ETHERNET"; 330234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_MOBILE_WILDCARD: 331234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return "MOBILE_WILDCARD"; 332234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey case MATCH_WIFI_WILDCARD: 333234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey return "WIFI_WILDCARD"; 3341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey default: 3351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return "UNKNOWN"; 3361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 339d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey private static void ensureSubtypeAvailable() { 340d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey if (COMBINE_SUBTYPE_ENABLED) { 341d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey throw new IllegalArgumentException( 342d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey "Unable to enforce 3G_LOWER template on combined data."); 343d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey } 344d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey } 345d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey 3461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { 3478fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 3481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public NetworkTemplate createFromParcel(Parcel in) { 3491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return new NetworkTemplate(in); 3501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey 3528fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey @Override 3531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey public NetworkTemplate[] newArray(int size) { 3541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey return new NetworkTemplate[size]; 3551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey } 3561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey }; 3571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey} 358