NetworkTemplate.java revision 234766a36af6214644fa8205202287084ca9cf93
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
64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
658fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
668fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI.
671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
684e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
704e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "3G" definition, or lower.
751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
768fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
774e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, 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 "4G" definition.
841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
864e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
878fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
91234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
92234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * regardless of IMSI.
93234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     */
94234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static NetworkTemplate buildTemplateMobileWildcard() {
95234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
96234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
97234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
98234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    /**
998fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
1008fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * regardless of SSID.
1011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifiWildcard() {
103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
1048fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1058fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1068fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
1074e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
1088fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return buildTemplateWifiWildcard();
1098fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1108fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1118fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    /**
1128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
1138fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * given SSID.
1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     */
1158fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifi(String networkId) {
1168fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, networkId);
1174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1204e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1214e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1224e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1234e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null, null);
1254e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1264e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
1274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1284e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1298fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    private final String mNetworkId;
1301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1318fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
1328fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mMatchRule = matchRule;
1338fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mSubscriberId = subscriberId;
1348fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = networkId;
1351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1374e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1408fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = in.readString();
1411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1438fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1478fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        dest.writeString(mNetworkId);
1481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1508fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
1578fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
1588fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
1598fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mSubscriberId != null) {
1608fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
1618fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1628fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mNetworkId != null) {
1638fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", networkId=").append(mNetworkId);
1648fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1658fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return builder.toString();
1661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId);
1711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
1788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mSubscriberId, other.mSubscriberId)
1798fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mNetworkId, other.mNetworkId);
1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
1851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
1861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1928fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public String getNetworkId() {
1938fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return mNetworkId;
1948fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1958fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
197630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
2001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
2011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2021b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
2031b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2041b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
2094e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2104e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
211234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
212234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesMobileWildcard(ident);
213234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
214234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesWifiWildcard(ident);
2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
2171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
221630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
2221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
224630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
225630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
227630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
228630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return (contains(DATA_USAGE_NETWORK_TYPES, ident.mType)
229630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                    && Objects.equal(mSubscriberId, ident.mSubscriberId));
2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
23402e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
237d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
238630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
239630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
240630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
252630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
255d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
256630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
257630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
258630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
259630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2723ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2733ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
274234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return Objects.equal(mNetworkId, ident.mNetworkId);
2753ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2763ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2804e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
2814e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
2824e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
2834e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
2844e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
2854e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
2864e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
2874e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
2884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
2894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
290234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesMobileWildcard(NetworkIdentity ident) {
291234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
292234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            return true;
293234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        } else {
294234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            return contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
295234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
296234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
297234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
298234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesWifiWildcard(NetworkIdentity ident) {
299234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        switch (ident.mType) {
300234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI:
301234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI_P2P:
302234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return true;
303234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            default:
304234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return false;
305234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
306234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
307234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
3084e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
3091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
3101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
3111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
3121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
3131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
3141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
3151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
3161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
3171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
3184e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
3194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
320234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
321234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "MOBILE_WILDCARD";
322234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
323234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "WIFI_WILDCARD";
3241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
3251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
3261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
3281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
329d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    private static void ensureSubtypeAvailable() {
330d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        if (COMBINE_SUBTYPE_ENABLED) {
331d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey            throw new IllegalArgumentException(
332d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey                    "Unable to enforce 3G_LOWER template on combined data.");
333d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        }
334d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    }
335d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey
3361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
3378fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
3391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
3401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3428fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
3441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
3451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
3471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
348