blob: d8e53d523f0748ffe0e41810b846a9632b7d26e7 [file] [log] [blame]
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -07001/*
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
17package android.net;
18
Jeff Sharkey02e21d62011-07-17 15:53:33 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070020import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey3ca74812012-01-24 15:37:07 -080021import static android.net.ConnectivityManager.TYPE_WIFI_P2P;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -070023import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED;
Jeff Sharkey02e21d62011-07-17 15:53:33 -070024import static android.net.NetworkIdentity.scrubSubscriberId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070025import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
26import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
27import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
28import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
29import static android.telephony.TelephonyManager.getNetworkClass;
Jeff Sharkey630a1712011-09-26 10:47:10 -070030import static com.android.internal.util.ArrayUtils.contains;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070031
Jeff Sharkey630a1712011-09-26 10:47:10 -070032import android.content.res.Resources;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070033import android.os.Parcel;
34import android.os.Parcelable;
35
36import 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 */
44public class NetworkTemplate implements Parcelable {
45
Jeff Sharkey4e814c32011-07-14 20:37:37 -070046 public static final int MATCH_MOBILE_ALL = 1;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070047 public static final int MATCH_MOBILE_3G_LOWER = 2;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070048 public static final int MATCH_MOBILE_4G = 3;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070049 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070050 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070051 public static final int MATCH_MOBILE_WILDCARD = 6;
52 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070053
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070054 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -070055 * 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 Sharkey70c70532012-05-16 14:51:19 -070064 private static boolean sForceAllNetworkTypes = false;
65
66 // @VisibleForTesting
67 public static void forceAllNetworkTypes() {
68 sForceAllNetworkTypes = true;
69 }
70
Jeff Sharkey630a1712011-09-26 10:47:10 -070071 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070072 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
73 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070074 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070075 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070076 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070077 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070078
79 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070080 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
81 * the given IMSI that roughly meet a "3G" definition, or lower.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070082 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070083 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070084 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070085 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070086 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070087
88 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070089 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
90 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070091 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070092 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070093 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070094 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070095 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070096
97 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -070098 * 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 Sharkey8fc27e82012-04-04 20:40:58 -0700106 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
107 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700108 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700109 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700110 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700111 }
112
113 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700114 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700115 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 Sharkey4e814c32011-07-14 20:37:37 -0700124 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700125
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700126 /**
127 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
128 * networks together.
129 */
130 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700131 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700132 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700133
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700134 private final int mMatchRule;
135 private final String mSubscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700136 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700137
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700138 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
139 mMatchRule = matchRule;
140 mSubscriberId = subscriberId;
141 mNetworkId = networkId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700142 }
143
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700144 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700145 mMatchRule = in.readInt();
146 mSubscriberId = in.readString();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700147 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700148 }
149
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700150 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700151 public void writeToParcel(Parcel dest, int flags) {
152 dest.writeInt(mMatchRule);
153 dest.writeString(mSubscriberId);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700154 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700155 }
156
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700157 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700158 public int describeContents() {
159 return 0;
160 }
161
162 @Override
163 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700164 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 Sharkey1b5a2a92011-06-18 18:34:16 -0700173 }
174
175 @Override
176 public int hashCode() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700177 return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700178 }
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 Sharkey8fc27e82012-04-04 20:40:58 -0700185 && Objects.equal(mSubscriberId, other.mSubscriberId)
186 && Objects.equal(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700187 }
188 return false;
189 }
190
191 public int getMatchRule() {
192 return mMatchRule;
193 }
194
195 public String getSubscriberId() {
196 return mSubscriberId;
197 }
198
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700199 public String getNetworkId() {
200 return mNetworkId;
201 }
202
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700203 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700204 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700205 */
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 Sharkey4e814c32011-07-14 20:37:37 -0700216 case MATCH_ETHERNET:
217 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700218 case MATCH_MOBILE_WILDCARD:
219 return matchesMobileWildcard(ident);
220 case MATCH_WIFI_WILDCARD:
221 return matchesWifiWildcard(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700222 default:
223 throw new IllegalArgumentException("unknown network template");
224 }
225 }
226
227 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700228 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700229 */
230 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700231 if (ident.mType == TYPE_WIMAX) {
232 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700233 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700234 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700235 return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType))
Jeff Sharkey630a1712011-09-26 10:47:10 -0700236 && Objects.equal(mSubscriberId, ident.mSubscriberId));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700237 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700238 }
239
240 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700241 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700242 */
243 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700244 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700245 if (ident.mType == TYPE_WIMAX) {
246 return false;
247 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700248 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 Sharkey630a1712011-09-26 10:47:10 -0700259 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700260 */
261 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700262 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700263 if (ident.mType == TYPE_WIMAX) {
264 // TODO: consider matching against WiMAX subscriber identity
265 return true;
266 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700267 switch (getNetworkClass(ident.mSubType)) {
268 case NETWORK_CLASS_4_G:
269 return true;
270 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700271 }
272 return false;
273 }
274
275 /**
276 * Check if matches Wi-Fi network template.
277 */
278 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800279 switch (ident.mType) {
280 case TYPE_WIFI:
Jeff Sharkey234766a2012-04-10 19:48:07 -0700281 return Objects.equal(mNetworkId, ident.mNetworkId);
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800282 default:
283 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700284 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700285 }
286
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700287 /**
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 Sharkey234766a2012-04-10 19:48:07 -0700297 private boolean matchesMobileWildcard(NetworkIdentity ident) {
298 if (ident.mType == TYPE_WIMAX) {
299 return true;
300 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700301 return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700302 }
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 Sharkey4e814c32011-07-14 20:37:37 -0700315 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700316 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 Sharkey4e814c32011-07-14 20:37:37 -0700325 case MATCH_ETHERNET:
326 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700327 case MATCH_MOBILE_WILDCARD:
328 return "MOBILE_WILDCARD";
329 case MATCH_WIFI_WILDCARD:
330 return "WIFI_WILDCARD";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700331 default:
332 return "UNKNOWN";
333 }
334 }
335
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700336 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 Sharkey1b5a2a92011-06-18 18:34:16 -0700343 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700344 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700345 public NetworkTemplate createFromParcel(Parcel in) {
346 return new NetworkTemplate(in);
347 }
348
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700349 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700350 public NetworkTemplate[] newArray(int size) {
351 return new NetworkTemplate[size];
352 }
353 };
354}