blob: d3839ad4355e538d3d83865ac09fd2b0fd9d5969 [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
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080036import com.android.internal.annotations.VisibleForTesting;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070037import 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 */
45public class NetworkTemplate implements Parcelable {
46
Jeff Sharkey4e814c32011-07-14 20:37:37 -070047 public static final int MATCH_MOBILE_ALL = 1;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070048 public static final int MATCH_MOBILE_3G_LOWER = 2;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070049 public static final int MATCH_MOBILE_4G = 3;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070050 public static final int MATCH_WIFI = 4;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070051 public static final int MATCH_ETHERNET = 5;
Jeff Sharkey234766a2012-04-10 19:48:07 -070052 public static final int MATCH_MOBILE_WILDCARD = 6;
53 public static final int MATCH_WIFI_WILDCARD = 7;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070054
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070055 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -070056 * 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 Sharkey70c70532012-05-16 14:51:19 -070065 private static boolean sForceAllNetworkTypes = false;
66
Jeff Sharkey8b2c3a142012-11-12 11:45:05 -080067 @VisibleForTesting
Jeff Sharkey70c70532012-05-16 14:51:19 -070068 public static void forceAllNetworkTypes() {
69 sForceAllNetworkTypes = true;
70 }
71
Jeff Sharkey630a1712011-09-26 10:47:10 -070072 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070073 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
74 * the given IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070075 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070076 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070077 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070078 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070079
80 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070081 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
82 * the given IMSI that roughly meet a "3G" definition, or lower.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070083 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070084 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070085 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070086 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070087 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070088
89 /**
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070090 * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with
91 * the given IMSI that roughly meet a "4G" definition.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070092 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070093 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -070094 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070095 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -070096 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070097
98 /**
Jeff Sharkey234766a2012-04-10 19:48:07 -070099 * 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 Sharkey8fc27e82012-04-04 20:40:58 -0700107 * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks,
108 * regardless of SSID.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700109 */
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700110 public static NetworkTemplate buildTemplateWifiWildcard() {
Jeff Sharkey234766a2012-04-10 19:48:07 -0700111 return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700112 }
113
114 @Deprecated
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700115 public static NetworkTemplate buildTemplateWifi() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700116 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 Sharkey4e814c32011-07-14 20:37:37 -0700125 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700126
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700127 /**
128 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
129 * networks together.
130 */
131 public static NetworkTemplate buildTemplateEthernet() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700132 return new NetworkTemplate(MATCH_ETHERNET, null, null);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700133 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700134
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700135 private final int mMatchRule;
136 private final String mSubscriberId;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700137 private final String mNetworkId;
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700138
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700139 public NetworkTemplate(int matchRule, String subscriberId, String networkId) {
140 mMatchRule = matchRule;
141 mSubscriberId = subscriberId;
142 mNetworkId = networkId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700143 }
144
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700145 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700146 mMatchRule = in.readInt();
147 mSubscriberId = in.readString();
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700148 mNetworkId = in.readString();
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700149 }
150
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700151 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700152 public void writeToParcel(Parcel dest, int flags) {
153 dest.writeInt(mMatchRule);
154 dest.writeString(mSubscriberId);
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700155 dest.writeString(mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700156 }
157
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700158 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700159 public int describeContents() {
160 return 0;
161 }
162
163 @Override
164 public String toString() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700165 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 Sharkey1b5a2a92011-06-18 18:34:16 -0700174 }
175
176 @Override
177 public int hashCode() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700178 return Objects.hashCode(mMatchRule, mSubscriberId, mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700179 }
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 Sharkey8fc27e82012-04-04 20:40:58 -0700186 && Objects.equal(mSubscriberId, other.mSubscriberId)
187 && Objects.equal(mNetworkId, other.mNetworkId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700188 }
189 return false;
190 }
191
192 public int getMatchRule() {
193 return mMatchRule;
194 }
195
196 public String getSubscriberId() {
197 return mSubscriberId;
198 }
199
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700200 public String getNetworkId() {
201 return mNetworkId;
202 }
203
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700204 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700205 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700206 */
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 Sharkey4e814c32011-07-14 20:37:37 -0700217 case MATCH_ETHERNET:
218 return matchesEthernet(ident);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700219 case MATCH_MOBILE_WILDCARD:
220 return matchesMobileWildcard(ident);
221 case MATCH_WIFI_WILDCARD:
222 return matchesWifiWildcard(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700223 default:
224 throw new IllegalArgumentException("unknown network template");
225 }
226 }
227
228 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700229 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700230 */
231 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700232 if (ident.mType == TYPE_WIMAX) {
233 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700234 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700235 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700236 return ((sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType))
Jeff Sharkey630a1712011-09-26 10:47:10 -0700237 && Objects.equal(mSubscriberId, ident.mSubscriberId));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700238 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700239 }
240
241 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700242 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700243 */
244 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700245 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700246 if (ident.mType == TYPE_WIMAX) {
247 return false;
248 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700249 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 Sharkey630a1712011-09-26 10:47:10 -0700260 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700261 */
262 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700263 ensureSubtypeAvailable();
Jeff Sharkey630a1712011-09-26 10:47:10 -0700264 if (ident.mType == TYPE_WIMAX) {
265 // TODO: consider matching against WiMAX subscriber identity
266 return true;
267 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700268 switch (getNetworkClass(ident.mSubType)) {
269 case NETWORK_CLASS_4_G:
270 return true;
271 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700272 }
273 return false;
274 }
275
276 /**
277 * Check if matches Wi-Fi network template.
278 */
279 private boolean matchesWifi(NetworkIdentity ident) {
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800280 switch (ident.mType) {
281 case TYPE_WIFI:
Jeff Sharkey234766a2012-04-10 19:48:07 -0700282 return Objects.equal(mNetworkId, ident.mNetworkId);
Jeff Sharkey3ca74812012-01-24 15:37:07 -0800283 default:
284 return false;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700285 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700286 }
287
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700288 /**
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 Sharkey234766a2012-04-10 19:48:07 -0700298 private boolean matchesMobileWildcard(NetworkIdentity ident) {
299 if (ident.mType == TYPE_WIMAX) {
300 return true;
301 } else {
Jeff Sharkey70c70532012-05-16 14:51:19 -0700302 return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType);
Jeff Sharkey234766a2012-04-10 19:48:07 -0700303 }
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 Sharkey4e814c32011-07-14 20:37:37 -0700316 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700317 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 Sharkey4e814c32011-07-14 20:37:37 -0700326 case MATCH_ETHERNET:
327 return "ETHERNET";
Jeff Sharkey234766a2012-04-10 19:48:07 -0700328 case MATCH_MOBILE_WILDCARD:
329 return "MOBILE_WILDCARD";
330 case MATCH_WIFI_WILDCARD:
331 return "WIFI_WILDCARD";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700332 default:
333 return "UNKNOWN";
334 }
335 }
336
Jeff Sharkeyd4dd7712012-03-16 11:11:54 -0700337 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 Sharkey1b5a2a92011-06-18 18:34:16 -0700344 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700345 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700346 public NetworkTemplate createFromParcel(Parcel in) {
347 return new NetworkTemplate(in);
348 }
349
Jeff Sharkey8fc27e82012-04-04 20:40:58 -0700350 @Override
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700351 public NetworkTemplate[] newArray(int size) {
352 return new NetworkTemplate[size];
353 }
354 };
355}