NetworkTemplate.java revision 3ca7481439c9b65a137d7705d0f4a16766529e75
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;
2302e21d6a5b5117d494777a36783909854854f751Jeff Sharkeyimport static android.net.NetworkIdentity.scrubSubscriberId;
241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport static android.telephony.TelephonyManager.getNetworkClass;
29630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport static com.android.internal.util.ArrayUtils.contains;
301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
31630a1712168f402653039e368259cb9480454fa8Jeff Sharkeyimport android.content.res.Resources;
321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcel;
331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport android.os.Parcelable;
341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
351b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeyimport com.android.internal.util.Objects;
361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey/**
381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * Template definition used to generically match {@link NetworkIdentity},
391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * usually when collecting statistics.
401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey *
411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey * @hide
421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey */
431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkeypublic class NetworkTemplate implements Parcelable {
441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
454e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
464e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_ALL = 1;
474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_3G_LOWER = 2;
494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_MOBILE_4G = 3;
514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_WIFI = 4;
534e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
544e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static final int MATCH_ETHERNET = 5;
554e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
57630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Set of {@link NetworkInfo#getType()} that reflect data usage.
58630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     */
59630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    private static final int[] DATA_USAGE_NETWORK_TYPES;
60630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
61630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    static {
62630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray(
63630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                com.android.internal.R.array.config_data_usage_network_types);
64630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    }
65630a1712168f402653039e368259cb9480454fa8Jeff Sharkey
66630a1712168f402653039e368259cb9480454fa8Jeff Sharkey    /**
671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * networks together. Only uses statistics for requested IMSI.
691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
704e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
714e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
724e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * networks together that roughly meet a "3G" definition, or lower. Only
771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * uses statistics for requested IMSI.
781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
794e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
804e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId);
814e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * networks together that meet a "4G" definition. Only uses statistics for
861b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * requested IMSI.
871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
884e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
894e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId);
904e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
921b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
931b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_WIFI} style
941b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * networks together.
951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
964e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateWifi() {
974e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return new NetworkTemplate(MATCH_WIFI, null);
984e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
991b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1004e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
1014e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
1024e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * networks together.
1034e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
1044e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    public static NetworkTemplate buildTemplateEthernet() {
1054e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return new NetworkTemplate(MATCH_ETHERNET, null);
1064e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
1074e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
1084e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final int mMatchRule;
1094e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private final String mSubscriberId;
1101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1114e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
1121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public NetworkTemplate(int matchRule, String subscriberId) {
1131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        this.mMatchRule = matchRule;
1141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        this.mSubscriberId = subscriberId;
1151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1161b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1174e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private NetworkTemplate(Parcel in) {
1181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mMatchRule = in.readInt();
1191b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        mSubscriberId = in.readString();
1201b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1211b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1221b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /** {@inheritDoc} */
1231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public void writeToParcel(Parcel dest, int flags) {
1241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeInt(mMatchRule);
1251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        dest.writeString(mSubscriberId);
1261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /** {@inheritDoc} */
1291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int describeContents() {
1301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return 0;
1311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String toString() {
13502e21d6a5b5117d494777a36783909854854f751Jeff Sharkey        final String scrubSubscriberId = scrubSubscriberId(mSubscriberId);
1361b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return "NetworkTemplate: matchRule=" + getMatchRuleName(mMatchRule) + ", subscriberId="
1371b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                + scrubSubscriberId;
1381b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1391b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1401b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int hashCode() {
1421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return Objects.hashCode(mMatchRule, mSubscriberId);
1431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1441b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1451b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    @Override
1461b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean equals(Object obj) {
1471b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        if (obj instanceof NetworkTemplate) {
1481b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            final NetworkTemplate other = (NetworkTemplate) obj;
1491b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return mMatchRule == other.mMatchRule
1501b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    && Objects.equal(mSubscriberId, other.mSubscriberId);
1511b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1521b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
1531b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1541b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1554e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
1561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public int getMatchRule() {
1571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mMatchRule;
1581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1604e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /** {@hide} */
1611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public String getSubscriberId() {
1621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return mSubscriberId;
1631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1641b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1651b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
166630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Test if given {@link NetworkIdentity} matches this template.
1671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public boolean matches(NetworkIdentity ident) {
1691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (mMatchRule) {
1701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
1711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile(ident);
1721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
1731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile3gLower(ident);
1741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
1751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesMobile4g(ident);
1761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
1771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return matchesWifi(ident);
1784e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
1794e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return matchesEthernet(ident);
1801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
1811b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                throw new IllegalArgumentException("unknown network template");
1821b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1831b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1841b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1851b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
186630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network with matching IMSI.
1871b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
1881b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile(NetworkIdentity ident) {
189630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
190630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
1911b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return true;
192630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else {
193630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return (contains(DATA_USAGE_NETWORK_TYPES, ident.mType)
194630a1712168f402653039e368259cb9480454fa8Jeff Sharkey                    && Objects.equal(mSubscriberId, ident.mSubscriberId));
1951b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
1961b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
1971b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
1981b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
19902e21d6a5b5117d494777a36783909854854f751Jeff Sharkey     * Check if mobile network classified 3G or lower with matching IMSI.
2001b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2011b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile3gLower(NetworkIdentity ident) {
202630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
203630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return false;
204630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2051b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2061b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_UNKNOWN:
2071b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_2_G:
2081b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_3_G:
2091b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2101b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2111b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2121b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2131b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2141b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2151b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
216630a1712168f402653039e368259cb9480454fa8Jeff Sharkey     * Check if mobile network classified 4G with matching IMSI.
2171b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2181b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesMobile4g(NetworkIdentity ident) {
219630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        if (ident.mType == TYPE_WIMAX) {
220630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            // TODO: consider matching against WiMAX subscriber identity
221630a1712168f402653039e368259cb9480454fa8Jeff Sharkey            return true;
222630a1712168f402653039e368259cb9480454fa8Jeff Sharkey        } else if (matchesMobile(ident)) {
2231b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            switch (getNetworkClass(ident.mSubType)) {
2241b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                case NETWORK_CLASS_4_G:
2251b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                    return true;
2261b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            }
2271b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2281b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        return false;
2291b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2301b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2311b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    /**
2321b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     * Check if matches Wi-Fi network template.
2331b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey     */
2341b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    private boolean matchesWifi(NetworkIdentity ident) {
2353ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey        switch (ident.mType) {
2363ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI:
2373ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            case TYPE_WIFI_P2P:
2383ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return true;
2393ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey            default:
2403ca7481439c9b65a137d7705d0f4a16766529e75Jeff Sharkey                return false;
2411b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2421b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2431b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2444e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    /**
2454e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     * Check if matches Ethernet network template.
2464e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey     */
2474e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private boolean matchesEthernet(NetworkIdentity ident) {
2484e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        if (ident.mType == TYPE_ETHERNET) {
2494e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            return true;
2504e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        }
2514e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey        return false;
2524e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    }
2534e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey
2544e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey    private static String getMatchRuleName(int matchRule) {
2551b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        switch (matchRule) {
2561b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_3G_LOWER:
2571b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_3G_LOWER";
2581b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_4G:
2591b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_4G";
2601b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_MOBILE_ALL:
2611b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "MOBILE_ALL";
2621b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            case MATCH_WIFI:
2631b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "WIFI";
2644e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey            case MATCH_ETHERNET:
2654e814c348ce205fcc1a273427f95ef1d100ed60cJeff Sharkey                return "ETHERNET";
2661b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            default:
2671b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey                return "UNKNOWN";
2681b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2691b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    }
2701b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2711b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
2721b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate createFromParcel(Parcel in) {
2731b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate(in);
2741b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2751b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey
2761b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        public NetworkTemplate[] newArray(int size) {
2771b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey            return new NetworkTemplate[size];
2781b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey        }
2791b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey    };
2801b5a2a96f793211bfbd39aa29cc41031dfa23950Jeff Sharkey}
281