blob: 418b82fdeaa21c582146312ad18b48de0e8c4551 [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;
21import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey02e21d62011-07-17 15:53:33 -070022import static android.net.NetworkIdentity.scrubSubscriberId;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070023import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G;
24import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G;
25import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G;
26import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN;
27import static android.telephony.TelephonyManager.getNetworkClass;
Jeff Sharkey630a1712011-09-26 10:47:10 -070028import static com.android.internal.util.ArrayUtils.contains;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070029
Jeff Sharkey630a1712011-09-26 10:47:10 -070030import android.content.res.Resources;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070031import android.os.Parcel;
32import android.os.Parcelable;
33
34import com.android.internal.util.Objects;
35
36/**
37 * Template definition used to generically match {@link NetworkIdentity},
38 * usually when collecting statistics.
39 *
40 * @hide
41 */
42public class NetworkTemplate implements Parcelable {
43
Jeff Sharkey4e814c32011-07-14 20:37:37 -070044 /** {@hide} */
45 public static final int MATCH_MOBILE_ALL = 1;
46 /** {@hide} */
47 public static final int MATCH_MOBILE_3G_LOWER = 2;
48 /** {@hide} */
49 public static final int MATCH_MOBILE_4G = 3;
50 /** {@hide} */
51 public static final int MATCH_WIFI = 4;
52 /** {@hide} */
53 public static final int MATCH_ETHERNET = 5;
54
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
65 /**
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070066 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
67 * networks together. Only uses statistics for requested IMSI.
68 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070069 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) {
70 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
71 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070072
73 /**
74 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
75 * networks together that roughly meet a "3G" definition, or lower. Only
76 * uses statistics for requested IMSI.
77 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070078 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) {
79 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId);
80 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070081
82 /**
83 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style
84 * networks together that meet a "4G" definition. Only uses statistics for
85 * requested IMSI.
86 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070087 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) {
88 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId);
89 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070090
91 /**
92 * Template to combine all {@link ConnectivityManager#TYPE_WIFI} style
93 * networks together.
94 */
Jeff Sharkey4e814c32011-07-14 20:37:37 -070095 public static NetworkTemplate buildTemplateWifi() {
96 return new NetworkTemplate(MATCH_WIFI, null);
97 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070098
Jeff Sharkey4e814c32011-07-14 20:37:37 -070099 /**
100 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style
101 * networks together.
102 */
103 public static NetworkTemplate buildTemplateEthernet() {
104 return new NetworkTemplate(MATCH_ETHERNET, null);
105 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700106
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700107 private final int mMatchRule;
108 private final String mSubscriberId;
109
110 /** {@hide} */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700111 public NetworkTemplate(int matchRule, String subscriberId) {
112 this.mMatchRule = matchRule;
113 this.mSubscriberId = subscriberId;
114 }
115
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700116 private NetworkTemplate(Parcel in) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700117 mMatchRule = in.readInt();
118 mSubscriberId = in.readString();
119 }
120
121 /** {@inheritDoc} */
122 public void writeToParcel(Parcel dest, int flags) {
123 dest.writeInt(mMatchRule);
124 dest.writeString(mSubscriberId);
125 }
126
127 /** {@inheritDoc} */
128 public int describeContents() {
129 return 0;
130 }
131
132 @Override
133 public String toString() {
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700134 final String scrubSubscriberId = scrubSubscriberId(mSubscriberId);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700135 return "NetworkTemplate: matchRule=" + getMatchRuleName(mMatchRule) + ", subscriberId="
136 + scrubSubscriberId;
137 }
138
139 @Override
140 public int hashCode() {
141 return Objects.hashCode(mMatchRule, mSubscriberId);
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (obj instanceof NetworkTemplate) {
147 final NetworkTemplate other = (NetworkTemplate) obj;
148 return mMatchRule == other.mMatchRule
149 && Objects.equal(mSubscriberId, other.mSubscriberId);
150 }
151 return false;
152 }
153
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700154 /** {@hide} */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700155 public int getMatchRule() {
156 return mMatchRule;
157 }
158
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700159 /** {@hide} */
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700160 public String getSubscriberId() {
161 return mSubscriberId;
162 }
163
164 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700165 * Test if given {@link NetworkIdentity} matches this template.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700166 */
167 public boolean matches(NetworkIdentity ident) {
168 switch (mMatchRule) {
169 case MATCH_MOBILE_ALL:
170 return matchesMobile(ident);
171 case MATCH_MOBILE_3G_LOWER:
172 return matchesMobile3gLower(ident);
173 case MATCH_MOBILE_4G:
174 return matchesMobile4g(ident);
175 case MATCH_WIFI:
176 return matchesWifi(ident);
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700177 case MATCH_ETHERNET:
178 return matchesEthernet(ident);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700179 default:
180 throw new IllegalArgumentException("unknown network template");
181 }
182 }
183
184 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700185 * Check if mobile network with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700186 */
187 private boolean matchesMobile(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700188 if (ident.mType == TYPE_WIMAX) {
189 // TODO: consider matching against WiMAX subscriber identity
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700190 return true;
Jeff Sharkey630a1712011-09-26 10:47:10 -0700191 } else {
192 return (contains(DATA_USAGE_NETWORK_TYPES, ident.mType)
193 && Objects.equal(mSubscriberId, ident.mSubscriberId));
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700194 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700195 }
196
197 /**
Jeff Sharkey02e21d62011-07-17 15:53:33 -0700198 * Check if mobile network classified 3G or lower with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700199 */
200 private boolean matchesMobile3gLower(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700201 if (ident.mType == TYPE_WIMAX) {
202 return false;
203 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700204 switch (getNetworkClass(ident.mSubType)) {
205 case NETWORK_CLASS_UNKNOWN:
206 case NETWORK_CLASS_2_G:
207 case NETWORK_CLASS_3_G:
208 return true;
209 }
210 }
211 return false;
212 }
213
214 /**
Jeff Sharkey630a1712011-09-26 10:47:10 -0700215 * Check if mobile network classified 4G with matching IMSI.
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700216 */
217 private boolean matchesMobile4g(NetworkIdentity ident) {
Jeff Sharkey630a1712011-09-26 10:47:10 -0700218 if (ident.mType == TYPE_WIMAX) {
219 // TODO: consider matching against WiMAX subscriber identity
220 return true;
221 } else if (matchesMobile(ident)) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700222 switch (getNetworkClass(ident.mSubType)) {
223 case NETWORK_CLASS_4_G:
224 return true;
225 }
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700226 }
227 return false;
228 }
229
230 /**
231 * Check if matches Wi-Fi network template.
232 */
233 private boolean matchesWifi(NetworkIdentity ident) {
234 if (ident.mType == TYPE_WIFI) {
235 return true;
236 }
237 return false;
238 }
239
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700240 /**
241 * Check if matches Ethernet network template.
242 */
243 private boolean matchesEthernet(NetworkIdentity ident) {
244 if (ident.mType == TYPE_ETHERNET) {
245 return true;
246 }
247 return false;
248 }
249
250 private static String getMatchRuleName(int matchRule) {
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700251 switch (matchRule) {
252 case MATCH_MOBILE_3G_LOWER:
253 return "MOBILE_3G_LOWER";
254 case MATCH_MOBILE_4G:
255 return "MOBILE_4G";
256 case MATCH_MOBILE_ALL:
257 return "MOBILE_ALL";
258 case MATCH_WIFI:
259 return "WIFI";
Jeff Sharkey4e814c32011-07-14 20:37:37 -0700260 case MATCH_ETHERNET:
261 return "ETHERNET";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700262 default:
263 return "UNKNOWN";
264 }
265 }
266
267 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() {
268 public NetworkTemplate createFromParcel(Parcel in) {
269 return new NetworkTemplate(in);
270 }
271
272 public NetworkTemplate[] newArray(int size) {
273 return new NetworkTemplate[size];
274 }
275 };
276}