Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_WIFI_P2P; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 23 | import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 24 | import static android.net.NetworkIdentity.scrubSubscriberId; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 25 | import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G; |
| 26 | import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G; |
| 27 | import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G; |
| 28 | import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN; |
| 29 | import static android.telephony.TelephonyManager.getNetworkClass; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 30 | import static com.android.internal.util.ArrayUtils.contains; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 31 | |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
| 35 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 36 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 37 | import com.android.internal.util.Objects; |
| 38 | |
| 39 | /** |
| 40 | * Template definition used to generically match {@link NetworkIdentity}, |
| 41 | * usually when collecting statistics. |
| 42 | * |
| 43 | * @hide |
| 44 | */ |
| 45 | public class NetworkTemplate implements Parcelable { |
| 46 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 47 | public static final int MATCH_MOBILE_ALL = 1; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 48 | public static final int MATCH_MOBILE_3G_LOWER = 2; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 49 | public static final int MATCH_MOBILE_4G = 3; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 50 | public static final int MATCH_WIFI = 4; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 51 | public static final int MATCH_ETHERNET = 5; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 52 | public static final int MATCH_MOBILE_WILDCARD = 6; |
| 53 | public static final int MATCH_WIFI_WILDCARD = 7; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 54 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 55 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 56 | * Set of {@link NetworkInfo#getType()} that reflect data usage. |
| 57 | */ |
| 58 | private static final int[] DATA_USAGE_NETWORK_TYPES; |
| 59 | |
| 60 | static { |
| 61 | DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray( |
| 62 | com.android.internal.R.array.config_data_usage_network_types); |
| 63 | } |
| 64 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 65 | private static boolean sForceAllNetworkTypes = false; |
| 66 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 67 | @VisibleForTesting |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 68 | public static void forceAllNetworkTypes() { |
| 69 | sForceAllNetworkTypes = true; |
| 70 | } |
| 71 | |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 72 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 73 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 74 | * the given IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 75 | */ |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 76 | public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 77 | return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 78 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 81 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 82 | * the given IMSI that roughly meet a "3G" definition, or lower. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 83 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 84 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 85 | public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 86 | return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 87 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 90 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 91 | * the given IMSI that roughly meet a "4G" definition. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 92 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 93 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 94 | public static NetworkTemplate buildTemplateMobile4g(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 95 | return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 96 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 99 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, |
| 100 | * regardless of IMSI. |
| 101 | */ |
| 102 | public static NetworkTemplate buildTemplateMobileWildcard() { |
| 103 | return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); |
| 104 | } |
| 105 | |
| 106 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 107 | * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, |
| 108 | * regardless of SSID. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 109 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 110 | public static NetworkTemplate buildTemplateWifiWildcard() { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 111 | return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 115 | public static NetworkTemplate buildTemplateWifi() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 116 | return buildTemplateWifiWildcard(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the |
| 121 | * given SSID. |
| 122 | */ |
| 123 | public static NetworkTemplate buildTemplateWifi(String networkId) { |
| 124 | return new NetworkTemplate(MATCH_WIFI, null, networkId); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 125 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 126 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style |
| 129 | * networks together. |
| 130 | */ |
| 131 | public static NetworkTemplate buildTemplateEthernet() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 132 | return new NetworkTemplate(MATCH_ETHERNET, null, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 133 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 135 | private final int mMatchRule; |
| 136 | private final String mSubscriberId; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 137 | private final String mNetworkId; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 138 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 139 | public NetworkTemplate(int matchRule, String subscriberId, String networkId) { |
| 140 | mMatchRule = matchRule; |
| 141 | mSubscriberId = subscriberId; |
| 142 | mNetworkId = networkId; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 145 | private NetworkTemplate(Parcel in) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 146 | mMatchRule = in.readInt(); |
| 147 | mSubscriberId = in.readString(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 148 | mNetworkId = in.readString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 151 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 152 | public void writeToParcel(Parcel dest, int flags) { |
| 153 | dest.writeInt(mMatchRule); |
| 154 | dest.writeString(mSubscriberId); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 155 | dest.writeString(mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 158 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 159 | public int describeContents() { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public String toString() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 165 | final StringBuilder builder = new StringBuilder("NetworkTemplate: "); |
| 166 | builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); |
| 167 | if (mSubscriberId != null) { |
| 168 | builder.append(", subscriberId=").append(scrubSubscriberId(mSubscriberId)); |
| 169 | } |
| 170 | if (mNetworkId != null) { |
| 171 | builder.append(", networkId=").append(mNetworkId); |
| 172 | } |
| 173 | return builder.toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public int hashCode() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 178 | return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public boolean equals(Object obj) { |
| 183 | if (obj instanceof NetworkTemplate) { |
| 184 | final NetworkTemplate other = (NetworkTemplate) obj; |
| 185 | return mMatchRule == other.mMatchRule |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 186 | && Objects.equal(mSubscriberId, other.mSubscriberId) |
| 187 | && Objects.equal(mNetworkId, other.mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | public int getMatchRule() { |
| 193 | return mMatchRule; |
| 194 | } |
| 195 | |
| 196 | public String getSubscriberId() { |
| 197 | return mSubscriberId; |
| 198 | } |
| 199 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 200 | public String getNetworkId() { |
| 201 | return mNetworkId; |
| 202 | } |
| 203 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 204 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 205 | * Test if given {@link NetworkIdentity} matches this template. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 206 | */ |
| 207 | public boolean matches(NetworkIdentity ident) { |
| 208 | switch (mMatchRule) { |
| 209 | case MATCH_MOBILE_ALL: |
| 210 | return matchesMobile(ident); |
| 211 | case MATCH_MOBILE_3G_LOWER: |
| 212 | return matchesMobile3gLower(ident); |
| 213 | case MATCH_MOBILE_4G: |
| 214 | return matchesMobile4g(ident); |
| 215 | case MATCH_WIFI: |
| 216 | return matchesWifi(ident); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 217 | case MATCH_ETHERNET: |
| 218 | return matchesEthernet(ident); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 219 | case MATCH_MOBILE_WILDCARD: |
| 220 | return matchesMobileWildcard(ident); |
| 221 | case MATCH_WIFI_WILDCARD: |
| 222 | return matchesWifiWildcard(ident); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 223 | default: |
| 224 | throw new IllegalArgumentException("unknown network template"); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 229 | * Check if mobile network with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 230 | */ |
| 231 | private boolean matchesMobile(NetworkIdentity ident) { |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 232 | if (ident.mType == TYPE_WIMAX) { |
| 233 | // TODO: consider matching against WiMAX subscriber identity |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 234 | return true; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 235 | } else { |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 236 | return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType)) |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 237 | && Objects.equal(mSubscriberId, ident.mSubscriberId)); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 238 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 242 | * Check if mobile network classified 3G or lower with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 243 | */ |
| 244 | private boolean matchesMobile3gLower(NetworkIdentity ident) { |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 245 | ensureSubtypeAvailable(); |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 246 | if (ident.mType == TYPE_WIMAX) { |
| 247 | return false; |
| 248 | } else if (matchesMobile(ident)) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 249 | switch (getNetworkClass(ident.mSubType)) { |
| 250 | case NETWORK_CLASS_UNKNOWN: |
| 251 | case NETWORK_CLASS_2_G: |
| 252 | case NETWORK_CLASS_3_G: |
| 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 260 | * Check if mobile network classified 4G with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 261 | */ |
| 262 | private boolean matchesMobile4g(NetworkIdentity ident) { |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 263 | ensureSubtypeAvailable(); |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 264 | if (ident.mType == TYPE_WIMAX) { |
| 265 | // TODO: consider matching against WiMAX subscriber identity |
| 266 | return true; |
| 267 | } else if (matchesMobile(ident)) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 268 | switch (getNetworkClass(ident.mSubType)) { |
| 269 | case NETWORK_CLASS_4_G: |
| 270 | return true; |
| 271 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 272 | } |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Check if matches Wi-Fi network template. |
| 278 | */ |
| 279 | private boolean matchesWifi(NetworkIdentity ident) { |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 280 | switch (ident.mType) { |
| 281 | case TYPE_WIFI: |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 282 | return Objects.equal(mNetworkId, ident.mNetworkId); |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 283 | default: |
| 284 | return false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 285 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Check if matches Ethernet network template. |
| 290 | */ |
| 291 | private boolean matchesEthernet(NetworkIdentity ident) { |
| 292 | if (ident.mType == TYPE_ETHERNET) { |
| 293 | return true; |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 298 | private boolean matchesMobileWildcard(NetworkIdentity ident) { |
| 299 | if (ident.mType == TYPE_WIMAX) { |
| 300 | return true; |
| 301 | } else { |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 302 | return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | private boolean matchesWifiWildcard(NetworkIdentity ident) { |
| 307 | switch (ident.mType) { |
| 308 | case TYPE_WIFI: |
| 309 | case TYPE_WIFI_P2P: |
| 310 | return true; |
| 311 | default: |
| 312 | return false; |
| 313 | } |
| 314 | } |
| 315 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 316 | private static String getMatchRuleName(int matchRule) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 317 | switch (matchRule) { |
| 318 | case MATCH_MOBILE_3G_LOWER: |
| 319 | return "MOBILE_3G_LOWER"; |
| 320 | case MATCH_MOBILE_4G: |
| 321 | return "MOBILE_4G"; |
| 322 | case MATCH_MOBILE_ALL: |
| 323 | return "MOBILE_ALL"; |
| 324 | case MATCH_WIFI: |
| 325 | return "WIFI"; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 326 | case MATCH_ETHERNET: |
| 327 | return "ETHERNET"; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 328 | case MATCH_MOBILE_WILDCARD: |
| 329 | return "MOBILE_WILDCARD"; |
| 330 | case MATCH_WIFI_WILDCARD: |
| 331 | return "WIFI_WILDCARD"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 332 | default: |
| 333 | return "UNKNOWN"; |
| 334 | } |
| 335 | } |
| 336 | |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 337 | private static void ensureSubtypeAvailable() { |
| 338 | if (COMBINE_SUBTYPE_ENABLED) { |
| 339 | throw new IllegalArgumentException( |
| 340 | "Unable to enforce 3G_LOWER template on combined data."); |
| 341 | } |
| 342 | } |
| 343 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 344 | public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 345 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 346 | public NetworkTemplate createFromParcel(Parcel in) { |
| 347 | return new NetworkTemplate(in); |
| 348 | } |
| 349 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 350 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 351 | public NetworkTemplate[] newArray(int size) { |
| 352 | return new NetworkTemplate[size]; |
| 353 | } |
| 354 | }; |
| 355 | } |