NetworkTemplate.java revision 8fc27e8b87bd1def854a03d84009143b315d4176
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;
514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
53630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Set of {@link NetworkInfo#getType()} that reflect data usage.
54630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     */
55630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    private static final int[] DATA_USAGE_NETWORK_TYPES;
56630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
57630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    static {
58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                com.android.internal.R.array.config_data_usage_network_types);
60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    }
61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
638fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
648fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI.
651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
664e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
684e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
718fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "3G" definition, or lower.
731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
754e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
768fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, 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 "4G" definition.
821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
844e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
864e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
898fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
908fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * regardless of SSID.
911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
928fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifiWildcard() {
938fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, null);
948fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
958fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
968fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
988fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return buildTemplateWifiWildcard();
998fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1008fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1018fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    /**
1028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
1038fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * given SSID.
1048fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     */
1058fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifi(String networkId) {
1068fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, networkId);
1074e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1094e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1104e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1114e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1124e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1134e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null, null);
1154e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1164e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
1174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1184e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1198fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    private final String mNetworkId;
1201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mMatchRule = matchRule;
1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mSubscriberId = subscriberId;
1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = networkId;
1251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1308fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = in.readString();
1311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1338fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1378fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        dest.writeString(mNetworkId);
1381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1408fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
1478fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
1488fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
1498fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mSubscriberId != null) {
1508fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
1518fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1528fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mNetworkId != null) {
1538fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", networkId=").append(mNetworkId);
1548fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1558fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return builder.toString();
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
1608fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId);
1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
1688fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mSubscriberId, other.mSubscriberId)
1698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    && Objects.equal(mNetworkId, other.mNetworkId);
1701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
1721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
1791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1828fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public String getNetworkId() {
1838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return mNetworkId;
1848fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
187630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
1994e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2004e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
2011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2021b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
2031b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2041b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
207630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
210630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
211630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
213630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
214630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return (contains(DATA_USAGE_NETWORK_TYPES, ident.mType)
215630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                    && Objects.equal(mSubscriberId, ident.mSubscriberId));
2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
22002e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
223d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
224630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
225630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
226630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
238630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
241d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
242630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
243630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
244630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
245630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2583ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2593ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
2608fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                if (mNetworkId == null) {
2618fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    return true;
2628fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                } else {
2638fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                    return Objects.equal(mNetworkId, ident.mNetworkId);
2648fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                }
2653ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI_P2P:
2668fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey                return mNetworkId == null;
2673ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2683ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2724e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
2734e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
2744e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
2754e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
2764e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
2774e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
2784e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
2794e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
2804e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
2814e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
2824e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
2831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
2841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
2861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
2881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
2901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
2924e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
2941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
2961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
299d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    private static void ensureSubtypeAvailable() {
300d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        if (COMBINE_SUBTYPE_ENABLED) {
301d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey            throw new IllegalArgumentException(
302d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey                    "Unable to enforce 3G_LOWER template on combined data.");
303d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        }
304d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    }
305d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey
3061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
3078fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
3091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
3101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
3141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
3151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
3171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
318