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
368b2c3a14603d163d7564e6f60286995079687690Jeff Sharkeyimport com.android.internal.annotations.VisibleForTesting;
371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport com.android.internal.util.Objects;
381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/**
401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity},
411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics.
421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey *
431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide
441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */
451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable {
461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_ALL = 1;
484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_3G_LOWER = 2;
494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_4G = 3;
504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_WIFI = 4;
514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_ETHERNET = 5;
52234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_MOBILE_WILDCARD = 6;
53234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_WIFI_WILDCARD = 7;
544e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
56630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Set of {@link NetworkInfo#getType()} that reflect data usage.
57630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     */
58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    private static final int[] DATA_USAGE_NETWORK_TYPES;
59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    static {
61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                com.android.internal.R.array.config_data_usage_network_types);
63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    }
64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
6570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    private static boolean sForceAllNetworkTypes = false;
6670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
678b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey    @VisibleForTesting
6870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    public static void forceAllNetworkTypes() {
6970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey        sForceAllNetworkTypes = true;
7070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    }
7170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
72630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI.
751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
764e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
778fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
784e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
818fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
828fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "3G" definition, or lower.
831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
848fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
854e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
868fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
874e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
908fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
918fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "4G" definition.
921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
938fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
944e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
958fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
99234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
100234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * regardless of IMSI.
101234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     */
102234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static NetworkTemplate buildTemplateMobileWildcard() {
103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
104234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
105234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
106234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    /**
1078fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
1088fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * regardless of SSID.
1091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1108fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifiWildcard() {
111234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
1128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1138fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
1154e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
1168fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return buildTemplateWifiWildcard();
1178fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1188fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1198fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    /**
1208fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * given SSID.
1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     */
1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifi(String networkId) {
1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, networkId);
1254e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1284e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1314e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1328fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null, null);
1334e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1344e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
1354e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1364e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1378fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    private final String mNetworkId;
1381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1398fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
1408fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mMatchRule = matchRule;
1418fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mSubscriberId = subscriberId;
1428fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = networkId;
1431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1454e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1488fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = in.readString();
1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1518fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1558fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        dest.writeString(mNetworkId);
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1588fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
1658fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
1668fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
1678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mSubscriberId != null) {
1688fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
1698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mNetworkId != null) {
1718fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", networkId=").append(mNetworkId);
1728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return builder.toString();
1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
1788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId);
1791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
1868fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mSubscriberId, other.mSubscriberId)
1878fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mNetworkId, other.mNetworkId);
1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2008fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public String getNetworkId() {
2018fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return mNetworkId;
2028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
2038fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
2041b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
205630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
2131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
2174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2184e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
219234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
220234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesMobileWildcard(ident);
221234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
222234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesWifiWildcard(ident);
2231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
229630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
232630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
233630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
2341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
235630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
23670c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType))
237630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                    && Objects.equal(mSubscriberId, ident.mSubscriberId));
2381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
24202e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
245d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
246630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
247630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
248630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
260630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
263d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
264630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
265630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
266630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
267630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2803ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2813ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
282234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return Objects.equal(mNetworkId, ident.mNetworkId);
2833ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2843ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
2851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
2894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
2904e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
2914e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
2924e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
2934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
2944e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
2954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
2964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
2974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
298234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesMobileWildcard(NetworkIdentity ident) {
299234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
300234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            return true;
301234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        } else {
30270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
303234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
304234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
305234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
306234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesWifiWildcard(NetworkIdentity ident) {
307234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        switch (ident.mType) {
308234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI:
309234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI_P2P:
310234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return true;
311234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            default:
312234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return false;
313234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
314234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
315234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
3164e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
3171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
3181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
3191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
3201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
3211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
3221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
3231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
3241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
3251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
3264e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
3274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
328234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
329234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "MOBILE_WILDCARD";
330234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
331234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "WIFI_WILDCARD";
3321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
3331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
3341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
3361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
337d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    private static void ensureSubtypeAvailable() {
338d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        if (COMBINE_SUBTYPE_ENABLED) {
339d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey            throw new IllegalArgumentException(
340d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey                    "Unable to enforce 3G_LOWER template on combined data.");
341d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        }
342d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    }
343d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey
3441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
3458fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
3471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
3481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3508fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
3521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
3531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
3551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
356