NetworkTemplate.java revision 55a442e58262e253df965d652bd8219c8d1e72d3
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
1955a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_BLUETOOTH;
2002e21d6a5b5117d494777a36783909854854f751Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_ETHERNET;
211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIFI;
223ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIFI_P2P;
231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.net.ConnectivityManager.TYPE_WIMAX;
24d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkeyimport static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
2502e21d6a5b5117d494777a36783909854854f751Jeff Sharkeyimport static android.net.NetworkIdentity.scrubSubscriberId;
262e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkeyimport static android.net.wifi.WifiInfo.removeDoubleQuotes;
271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.getNetworkClass;
32630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport static com.android.internal.util.ArrayUtils.contains;
331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
34630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport android.content.res.Resources;
351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcel;
361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcelable;
371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
388b2c3a14603d163d7564e6f60286995079687690Jeff Sharkeyimport com.android.internal.annotations.VisibleForTesting;
391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
4055a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkeyimport java.util.Objects;
4155a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey
421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/**
431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity},
441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics.
451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey *
461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide
471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */
481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable {
491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_ALL = 1;
514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_3G_LOWER = 2;
524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_4G = 3;
534e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_WIFI = 4;
544e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_ETHERNET = 5;
55234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_MOBILE_WILDCARD = 6;
56234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static final int MATCH_WIFI_WILDCARD = 7;
5755a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    public static final int MATCH_BLUETOOTH = 8;
584e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Set of {@link NetworkInfo#getType()} that reflect data usage.
61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     */
62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    private static final int[] DATA_USAGE_NETWORK_TYPES;
63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    static {
65630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
66630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                com.android.internal.R.array.config_data_usage_network_types);
67630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    }
68630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
6970c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    private static boolean sForceAllNetworkTypes = false;
7070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
718b2c3a14603d163d7564e6f60286995079687690Jeff Sharkey    @VisibleForTesting
7270c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    public static void forceAllNetworkTypes() {
7370c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey        sForceAllNetworkTypes = true;
7470c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey    }
7570c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey
76630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
778fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI.
791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
804e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
818fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
824e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
868fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "3G" definition, or lower.
871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
888fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
908fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
914e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
948fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
958fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * the given IMSI that roughly meet a "4G" definition.
961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
978fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
984e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
998fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
1004e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1021b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
103234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks,
104234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     * regardless of IMSI.
105234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey     */
106234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    public static NetworkTemplate buildTemplateMobileWildcard() {
107234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
108234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
109234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
110234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    /**
1118fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
1128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * regardless of SSID.
1131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifiWildcard() {
115234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
1168fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1178fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1188fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Deprecated
1194e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
1208fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return buildTemplateWifiWildcard();
1218fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
1228fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
1238fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    /**
1248fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the
1258fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     * given SSID.
1268fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey     */
1278fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public static NetworkTemplate buildTemplateWifi(String networkId) {
1288fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null, networkId);
1294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1314e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1324e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1334e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1344e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1354e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1368fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null, null);
1374e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1384e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
13955a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    /**
14055a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style
14155a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey     * networks together.
14255a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey     */
14355a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    public static NetworkTemplate buildTemplateBluetooth() {
14455a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey        return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
14555a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    }
14655a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey
1474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1498fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    private final String mNetworkId;
1501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1518fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
1528fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mMatchRule = matchRule;
1538fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mSubscriberId = subscriberId;
1548fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = networkId;
1551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1574e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1608fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        mNetworkId = in.readString();
1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1638fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1678fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        dest.writeString(mNetworkId);
1681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1708fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    @Override
1711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
1778fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
1788fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
1798fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mSubscriberId != null) {
1808fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId));
1818fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1828fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        if (mNetworkId != null) {
1838fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey            builder.append(", networkId=").append(mNetworkId);
1848fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        }
1858fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return builder.toString();
1861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
190e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root        return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
198e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mSubscriberId, other.mSubscriberId)
199e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mNetworkId, other.mNetworkId);
2001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2021b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2031b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2041b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2128fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    public String getNetworkId() {
2138fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        return mNetworkId;
2148fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey    }
2158fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey
2161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
217630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
2181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
2201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
2211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
2231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
2294e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2304e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
231234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
232234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesMobileWildcard(ident);
233234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
234234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return matchesWifiWildcard(ident);
23555a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey            case MATCH_BLUETOOTH:
23655a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey                return matchesBluetooth(ident);
2371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
2391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
243630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
2441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
246630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
247630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
2481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
249630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
25070c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType))
251e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                    && Objects.equals(mSubscriberId, ident.mSubscriberId));
2521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
25602e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
259d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
260630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
261630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
262630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
274630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
277d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        ensureSubtypeAvailable();
278630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
279630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
280630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
281630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2891b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2901b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2943ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2953ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
296e6585b32ea586743258a5457e2184ffc087f2d2fKenny Root                return Objects.equals(
2972e4dce0dd24aa89ca6adf6559f13d3e342ff8558Jeff Sharkey                        removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
2983ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2993ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
3001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
3021b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3034e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
3044e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
3054e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
3064e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
3074e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
3084e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
3094e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
3104e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
3114e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
3124e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
313234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesMobileWildcard(NetworkIdentity ident) {
314234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
315234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            return true;
316234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        } else {
31770c70530bd6793869736ec894498e4ebf5dc9b20Jeff Sharkey            return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
318234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
319234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
320234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
321234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    private boolean matchesWifiWildcard(NetworkIdentity ident) {
322234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        switch (ident.mType) {
323234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI:
324234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case TYPE_WIFI_P2P:
325234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return true;
326234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            default:
327234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return false;
328234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey        }
329234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey    }
330234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey
33155a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    /**
33255a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey     * Check if matches Bluetooth network template.
33355a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey     */
33455a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    private boolean matchesBluetooth(NetworkIdentity ident) {
33555a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey        if (ident.mType == TYPE_BLUETOOTH) {
33655a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey            return true;
33755a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey        }
33855a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey        return false;
33955a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey    }
34055a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey
3414e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
3421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
3431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
3441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
3451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
3461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
3471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
3481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
3491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
3501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
3514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
3524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
353234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_MOBILE_WILDCARD:
354234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "MOBILE_WILDCARD";
355234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey            case MATCH_WIFI_WILDCARD:
356234766a36af6214644fa8205202287084ca9cf93Jeff Sharkey                return "WIFI_WILDCARD";
35755a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey            case MATCH_BLUETOOTH:
35855a442e58262e253df965d652bd8219c8d1e72d3Jeff Sharkey                return "BLUETOOTH";
3591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
3601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
3611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
3631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
364d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    private static void ensureSubtypeAvailable() {
365d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        if (COMBINE_SUBTYPE_ENABLED) {
366d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey            throw new IllegalArgumentException(
367d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey                    "Unable to enforce 3G_LOWER template on combined data.");
368d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey        }
369d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey    }
370d4dd7716fb825f29a609c5c4cb31204eea78183aJeff Sharkey
3711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
3728fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
3741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
3751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
3778fc27e8b87bd1def854a03d84009143b315d4176Jeff Sharkey        @Override
3781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
3791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
3801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
3811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
3821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
383