FileDocCategorySizeDatePackage
NetworkTemplate.javaAPI DocAndroid 5.1 API14788Thu Mar 12 22:22:10 GMT 2015android.net

NetworkTemplate

public class NetworkTemplate extends Object implements android.os.Parcelable
Template definition used to generically match {@link NetworkIdentity}, usually when collecting statistics.
hide

Fields Summary
public static final int
MATCH_MOBILE_ALL
public static final int
MATCH_MOBILE_3G_LOWER
public static final int
MATCH_MOBILE_4G
public static final int
MATCH_WIFI
public static final int
MATCH_ETHERNET
public static final int
MATCH_MOBILE_WILDCARD
public static final int
MATCH_WIFI_WILDCARD
public static final int
MATCH_BLUETOOTH
private static final int[]
DATA_USAGE_NETWORK_TYPES
Set of {@link NetworkInfo#getType()} that reflect data usage.
private static boolean
sForceAllNetworkTypes
private final int
mMatchRule
private final String
mSubscriberId
private final String[]
mMatchSubscriberIds
Ugh, templates are designed to target a single subscriber, but we might need to match several "merged" subscribers. These are the subscribers that should be considered to match this template.

Since the merge set is dynamic, it should not be persisted or used for determining equality.

private final String
mNetworkId
public static final Creator
CREATOR
Constructors Summary
public NetworkTemplate(int matchRule, String subscriberId, String networkId)

        this(matchRule, subscriberId, new String[] { subscriberId }, networkId);
    
public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, String networkId)

        mMatchRule = matchRule;
        mSubscriberId = subscriberId;
        mMatchSubscriberIds = matchSubscriberIds;
        mNetworkId = networkId;
    
private NetworkTemplate(android.os.Parcel in)

        mMatchRule = in.readInt();
        mSubscriberId = in.readString();
        mMatchSubscriberIds = in.createStringArray();
        mNetworkId = in.readString();
    
Methods Summary
public static android.net.NetworkTemplatebuildTemplateBluetooth()
Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style networks together.

        return new NetworkTemplate(MATCH_BLUETOOTH, null, null);
    
public static android.net.NetworkTemplatebuildTemplateEthernet()
Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style networks together.

        return new NetworkTemplate(MATCH_ETHERNET, null, null);
    
public static android.net.NetworkTemplatebuildTemplateMobile3gLower(java.lang.String subscriberId)
Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with the given IMSI that roughly meet a "3G" definition, or lower.

        return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
    
public static android.net.NetworkTemplatebuildTemplateMobile4g(java.lang.String subscriberId)
Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with the given IMSI that roughly meet a "4G" definition.

        return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
    
public static android.net.NetworkTemplatebuildTemplateMobileAll(java.lang.String subscriberId)
Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with the given IMSI.

        return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
    
public static android.net.NetworkTemplatebuildTemplateMobileWildcard()
Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, regardless of IMSI.

        return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null);
    
public static android.net.NetworkTemplatebuildTemplateWifi()

        return buildTemplateWifiWildcard();
    
public static android.net.NetworkTemplatebuildTemplateWifi(java.lang.String networkId)
Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the given SSID.

        return new NetworkTemplate(MATCH_WIFI, null, networkId);
    
public static android.net.NetworkTemplatebuildTemplateWifiWildcard()
Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, regardless of SSID.

        return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
    
public intdescribeContents()

        return 0;
    
private static voidensureSubtypeAvailable()

        if (COMBINE_SUBTYPE_ENABLED) {
            throw new IllegalArgumentException(
                    "Unable to enforce 3G_LOWER template on combined data.");
        }
    
public booleanequals(java.lang.Object obj)

        if (obj instanceof NetworkTemplate) {
            final NetworkTemplate other = (NetworkTemplate) obj;
            return mMatchRule == other.mMatchRule
                    && Objects.equals(mSubscriberId, other.mSubscriberId)
                    && Objects.equals(mNetworkId, other.mNetworkId);
        }
        return false;
    
public static voidforceAllNetworkTypes()


    
        
        sForceAllNetworkTypes = true;
    
public intgetMatchRule()

        return mMatchRule;
    
private static java.lang.StringgetMatchRuleName(int matchRule)

        switch (matchRule) {
            case MATCH_MOBILE_3G_LOWER:
                return "MOBILE_3G_LOWER";
            case MATCH_MOBILE_4G:
                return "MOBILE_4G";
            case MATCH_MOBILE_ALL:
                return "MOBILE_ALL";
            case MATCH_WIFI:
                return "WIFI";
            case MATCH_ETHERNET:
                return "ETHERNET";
            case MATCH_MOBILE_WILDCARD:
                return "MOBILE_WILDCARD";
            case MATCH_WIFI_WILDCARD:
                return "WIFI_WILDCARD";
            case MATCH_BLUETOOTH:
                return "BLUETOOTH";
            default:
                return "UNKNOWN";
        }
    
public java.lang.StringgetNetworkId()

        return mNetworkId;
    
public java.lang.StringgetSubscriberId()

        return mSubscriberId;
    
public inthashCode()

        return Objects.hash(mMatchRule, mSubscriberId, mNetworkId);
    
public booleanisMatchRuleMobile()

        switch (mMatchRule) {
            case MATCH_MOBILE_3G_LOWER:
            case MATCH_MOBILE_4G:
            case MATCH_MOBILE_ALL:
            case MATCH_MOBILE_WILDCARD:
                return true;
            default:
                return false;
        }
    
public booleanmatches(NetworkIdentity ident)
Test if given {@link NetworkIdentity} matches this template.

        switch (mMatchRule) {
            case MATCH_MOBILE_ALL:
                return matchesMobile(ident);
            case MATCH_MOBILE_3G_LOWER:
                return matchesMobile3gLower(ident);
            case MATCH_MOBILE_4G:
                return matchesMobile4g(ident);
            case MATCH_WIFI:
                return matchesWifi(ident);
            case MATCH_ETHERNET:
                return matchesEthernet(ident);
            case MATCH_MOBILE_WILDCARD:
                return matchesMobileWildcard(ident);
            case MATCH_WIFI_WILDCARD:
                return matchesWifiWildcard(ident);
            case MATCH_BLUETOOTH:
                return matchesBluetooth(ident);
            default:
                throw new IllegalArgumentException("unknown network template");
        }
    
private booleanmatchesBluetooth(NetworkIdentity ident)
Check if matches Bluetooth network template.

        if (ident.mType == TYPE_BLUETOOTH) {
            return true;
        }
        return false;
    
private booleanmatchesEthernet(NetworkIdentity ident)
Check if matches Ethernet network template.

        if (ident.mType == TYPE_ETHERNET) {
            return true;
        }
        return false;
    
private booleanmatchesMobile(NetworkIdentity ident)
Check if mobile network with matching IMSI.

        if (ident.mType == TYPE_WIMAX) {
            // TODO: consider matching against WiMAX subscriber identity
            return true;
        } else {
            final boolean matchesType = (sForceAllNetworkTypes
                    || contains(DATA_USAGE_NETWORK_TYPES, ident.mType));
            return matchesType && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId);
        }
    
private booleanmatchesMobile3gLower(NetworkIdentity ident)
Check if mobile network classified 3G or lower with matching IMSI.

        ensureSubtypeAvailable();
        if (ident.mType == TYPE_WIMAX) {
            return false;
        } else if (matchesMobile(ident)) {
            switch (getNetworkClass(ident.mSubType)) {
                case NETWORK_CLASS_UNKNOWN:
                case NETWORK_CLASS_2_G:
                case NETWORK_CLASS_3_G:
                    return true;
            }
        }
        return false;
    
private booleanmatchesMobile4g(NetworkIdentity ident)
Check if mobile network classified 4G with matching IMSI.

        ensureSubtypeAvailable();
        if (ident.mType == TYPE_WIMAX) {
            // TODO: consider matching against WiMAX subscriber identity
            return true;
        } else if (matchesMobile(ident)) {
            switch (getNetworkClass(ident.mSubType)) {
                case NETWORK_CLASS_4_G:
                    return true;
            }
        }
        return false;
    
private booleanmatchesMobileWildcard(NetworkIdentity ident)

        if (ident.mType == TYPE_WIMAX) {
            return true;
        } else {
            return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
        }
    
private booleanmatchesWifi(NetworkIdentity ident)
Check if matches Wi-Fi network template.

        switch (ident.mType) {
            case TYPE_WIFI:
                return Objects.equals(
                        removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId));
            default:
                return false;
        }
    
private booleanmatchesWifiWildcard(NetworkIdentity ident)

        switch (ident.mType) {
            case TYPE_WIFI:
            case TYPE_WIFI_P2P:
                return true;
            default:
                return false;
        }
    
public static android.net.NetworkTemplatenormalize(android.net.NetworkTemplate template, java.lang.String[] merged)
Examine the given template and normalize if it refers to a "merged" mobile subscriber. We pick the "lowest" merged subscriber as the primary for key purposes, and expand the template to match all other merged subscribers.

For example, given an incoming template matching B, and the currently active merge set [A,B], we'd return a new template that primarily matches A, but also matches B.

        if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) {
            // Requested template subscriber is part of the merge group; return
            // a template that matches all merged subscribers.
            return new NetworkTemplate(template.mMatchRule, merged[0], merged,
                    template.mNetworkId);
        } else {
            return template;
        }
    
public java.lang.StringtoString()

        final StringBuilder builder = new StringBuilder("NetworkTemplate: ");
        builder.append("matchRule=").append(getMatchRuleName(mMatchRule));
        if (mSubscriberId != null) {
            builder.append(", subscriberId=").append(
                    NetworkIdentity.scrubSubscriberId(mSubscriberId));
        }
        if (mMatchSubscriberIds != null) {
            builder.append(", matchSubscriberIds=").append(
                    Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds)));
        }
        if (mNetworkId != null) {
            builder.append(", networkId=").append(mNetworkId);
        }
        return builder.toString();
    
public voidwriteToParcel(android.os.Parcel dest, int flags)

        dest.writeInt(mMatchRule);
        dest.writeString(mSubscriberId);
        dest.writeStringArray(mMatchSubscriberIds);
        dest.writeString(mNetworkId);