NetworkTemplate.java revision e6585b32ea586743258a5457e2184ffc087f2d2f
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
37e6585b32ea586743258a5457e2184ffc087f2d2fKenny Rootimport java.util.Objects;
38e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root
398b2c3a14603d163d7564e6f60286995079687690Jeff Sharkeyimport com.android.internal.annotations.VisibleForTesting;
401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/**
421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity},
431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics.
441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey *
451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide
461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */
471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable {
481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_ALL = 1;
504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_3G_LOWER = 2;
514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_4G = 3;
524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_WIFI = 4;
534e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_ETHERNET = 5;
54234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_MOBILE_WILDCARD = 6;
55234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_WIFI_WILDCARD = 7;
564e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Set of {@link NetworkInfo#getType()} that reflect data usage.
59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     */
60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    private static final int[] DATA_USAGE_NETWORK_TYPES;
61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    static {
63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                com.android.internal.R.array.config_data_usage_network_types);
65630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    }
66630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
6770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    private static boolean sForceAllNetworkTypes = false;
6870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
698b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey    @VisibleForTesting
7070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    public static void forceAllNetworkTypes() {
7170c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey        sForceAllNetworkTypes = true;
7270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    }
7370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
74630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
758fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
768fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI.
771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
784e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
798fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
804e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
848fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "3G" definition, or lower.
851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
868fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
874e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
888fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
928fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
938fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "4G" definition.
941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
958fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
978fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
984e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
101234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
102234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * regardless of IMSI.
103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     */
104234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static NetworkTemplate buildTemplateMobileWildcard() {
105234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
106234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
107234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
108234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    /**
1098fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
1108fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * regardless of SSID.
1111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifiWildcard() {
113234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1158fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1168fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
1174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
1188fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return buildTemplateWifiWildcard();
1198fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1208fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    /**
1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * given SSID.
1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     */
1258fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifi(String networkId) {
1268fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, networkId);
1274e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1314e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1324e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1334e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1348fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null, null);
1354e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1364e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
1374e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1384e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1398fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    private final String mNetworkId;
1401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1418fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
1428fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mMatchRule = matchRule;
1438fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mSubscriberId = subscriberId;
1448fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = networkId;
1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1508fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = in.readString();
1511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1538fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1578fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        dest.writeString(mNetworkId);
1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1608fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
1678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
1688fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
1698fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mSubscriberId != null) {
1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
1718fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mNetworkId != null) {
1738fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", networkId=").append(mNetworkId);
1748fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1758fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return builder.toString();
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
180e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root        return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
188e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mSubscriberId, other.mSubscriberId)
189e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mNetworkId, other.mNetworkId);
1901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
1991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
2001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2028fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public String getNetworkId() {
2038fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return mNetworkId;
2048fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
2058fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
207630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
2131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
2171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
2194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2204e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
221234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
222234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesMobileWildcard(ident);
223234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
224234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesWifiWildcard(ident);
2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
231630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
2321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
234630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
235630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
2361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
237630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
23870c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType))
239e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mSubscriberId, ident.mSubscriberId));
2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
24402e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
247d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
248630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
249630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
250630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
262630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
265d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
266630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
267630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
268630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
269630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2823ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2833ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
284e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                return Objects.equals(
2852e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkey                        removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
2863ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2873ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
2881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2914e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
2924e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
2934e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
2944e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
2954e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
2964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
2974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
2984e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
2994e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
3004e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
301234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesMobileWildcard(NetworkIdentity ident) {
302234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
303234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            return true;
304234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        } else {
30570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
306234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
307234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
308234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
309234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesWifiWildcard(NetworkIdentity ident) {
310234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        switch (ident.mType) {
311234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI:
312234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI_P2P:
313234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return true;
314234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            default:
315234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return false;
316234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
317234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
318234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
3194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
3201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
3211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
3221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
3231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
3241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
3251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
3261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
3271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
3281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
3294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
3304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
331234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
332234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "MOBILE_WILDCARD";
333234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
334234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "WIFI_WILDCARD";
3351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
3361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
3371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
3391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
340d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    private static void ensureSubtypeAvailable() {
341d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        if (COMBINE_SUBTYPE_ENABLED) {
342d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey            throw new IllegalArgumentException(
343d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey                    "Unable to enforce 3G_LOWER template on combined data.");
344d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        }
345d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    }
346d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey
3471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
3488fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
3501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
3511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3538fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
3551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
3561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
3581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
359