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