NetworkTemplate.java revision 4e814c348ce205fcc1a273427f95ef1d100ed60c
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 17package android.net; 18 19import static android.net.ConnectivityManager.TYPE_WIFI; 20import static android.net.ConnectivityManager.TYPE_WIMAX; 21import static android.net.ConnectivityManager.TYPE_ETHERNET; 22import static android.net.ConnectivityManager.isNetworkTypeMobile; 23import 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; 28 29import android.os.Parcel; 30import android.os.Parcelable; 31 32import com.android.internal.util.Objects; 33 34/** 35 * Template definition used to generically match {@link NetworkIdentity}, 36 * usually when collecting statistics. 37 * 38 * @hide 39 */ 40public class NetworkTemplate implements Parcelable { 41 42 /** {@hide} */ 43 public static final int MATCH_MOBILE_ALL = 1; 44 /** {@hide} */ 45 public static final int MATCH_MOBILE_3G_LOWER = 2; 46 /** {@hide} */ 47 public static final int MATCH_MOBILE_4G = 3; 48 /** {@hide} */ 49 public static final int MATCH_WIFI = 4; 50 /** {@hide} */ 51 public static final int MATCH_ETHERNET = 5; 52 53 /** 54 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style 55 * networks together. Only uses statistics for requested IMSI. 56 */ 57 public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { 58 return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId); 59 } 60 61 /** 62 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style 63 * networks together that roughly meet a "3G" definition, or lower. Only 64 * uses statistics for requested IMSI. 65 */ 66 public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) { 67 return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId); 68 } 69 70 /** 71 * Template to combine all {@link ConnectivityManager#TYPE_MOBILE} style 72 * networks together that meet a "4G" definition. Only uses statistics for 73 * requested IMSI. 74 */ 75 public static NetworkTemplate buildTemplateMobile4g(String subscriberId) { 76 return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId); 77 } 78 79 /** 80 * Template to combine all {@link ConnectivityManager#TYPE_WIFI} style 81 * networks together. 82 */ 83 public static NetworkTemplate buildTemplateWifi() { 84 return new NetworkTemplate(MATCH_WIFI, null); 85 } 86 87 /** 88 * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style 89 * networks together. 90 */ 91 public static NetworkTemplate buildTemplateEthernet() { 92 return new NetworkTemplate(MATCH_ETHERNET, null); 93 } 94 95 private final int mMatchRule; 96 private final String mSubscriberId; 97 98 /** {@hide} */ 99 public NetworkTemplate(int matchRule, String subscriberId) { 100 this.mMatchRule = matchRule; 101 this.mSubscriberId = subscriberId; 102 } 103 104 private NetworkTemplate(Parcel in) { 105 mMatchRule = in.readInt(); 106 mSubscriberId = in.readString(); 107 } 108 109 /** {@inheritDoc} */ 110 public void writeToParcel(Parcel dest, int flags) { 111 dest.writeInt(mMatchRule); 112 dest.writeString(mSubscriberId); 113 } 114 115 /** {@inheritDoc} */ 116 public int describeContents() { 117 return 0; 118 } 119 120 @Override 121 public String toString() { 122 final String scrubSubscriberId = mSubscriberId != null ? "valid" : "null"; 123 return "NetworkTemplate: matchRule=" + getMatchRuleName(mMatchRule) + ", subscriberId=" 124 + scrubSubscriberId; 125 } 126 127 @Override 128 public int hashCode() { 129 return Objects.hashCode(mMatchRule, mSubscriberId); 130 } 131 132 @Override 133 public boolean equals(Object obj) { 134 if (obj instanceof NetworkTemplate) { 135 final NetworkTemplate other = (NetworkTemplate) obj; 136 return mMatchRule == other.mMatchRule 137 && Objects.equal(mSubscriberId, other.mSubscriberId); 138 } 139 return false; 140 } 141 142 /** {@hide} */ 143 public int getMatchRule() { 144 return mMatchRule; 145 } 146 147 /** {@hide} */ 148 public String getSubscriberId() { 149 return mSubscriberId; 150 } 151 152 /** 153 * Test if this network matches the given template and IMEI. 154 */ 155 public boolean matches(NetworkIdentity ident) { 156 switch (mMatchRule) { 157 case MATCH_MOBILE_ALL: 158 return matchesMobile(ident); 159 case MATCH_MOBILE_3G_LOWER: 160 return matchesMobile3gLower(ident); 161 case MATCH_MOBILE_4G: 162 return matchesMobile4g(ident); 163 case MATCH_WIFI: 164 return matchesWifi(ident); 165 case MATCH_ETHERNET: 166 return matchesEthernet(ident); 167 default: 168 throw new IllegalArgumentException("unknown network template"); 169 } 170 } 171 172 /** 173 * Check if mobile network with matching IMEI. Also matches 174 * {@link #TYPE_WIMAX}. 175 */ 176 private boolean matchesMobile(NetworkIdentity ident) { 177 if (isNetworkTypeMobile(ident.mType) && Objects.equal(mSubscriberId, ident.mSubscriberId)) { 178 return true; 179 } else if (ident.mType == TYPE_WIMAX) { 180 return true; 181 } 182 return false; 183 } 184 185 /** 186 * Check if mobile network classified 3G or lower with matching IMEI. 187 */ 188 private boolean matchesMobile3gLower(NetworkIdentity ident) { 189 if (isNetworkTypeMobile(ident.mType) && Objects.equal(mSubscriberId, ident.mSubscriberId)) { 190 switch (getNetworkClass(ident.mSubType)) { 191 case NETWORK_CLASS_UNKNOWN: 192 case NETWORK_CLASS_2_G: 193 case NETWORK_CLASS_3_G: 194 return true; 195 } 196 } 197 return false; 198 } 199 200 /** 201 * Check if mobile network classified 4G with matching IMEI. Also matches 202 * {@link #TYPE_WIMAX}. 203 */ 204 private boolean matchesMobile4g(NetworkIdentity ident) { 205 if (isNetworkTypeMobile(ident.mType) && Objects.equal(mSubscriberId, ident.mSubscriberId)) { 206 switch (getNetworkClass(ident.mSubType)) { 207 case NETWORK_CLASS_4_G: 208 return true; 209 } 210 } else if (ident.mType == TYPE_WIMAX) { 211 return true; 212 } 213 return false; 214 } 215 216 /** 217 * Check if matches Wi-Fi network template. 218 */ 219 private boolean matchesWifi(NetworkIdentity ident) { 220 if (ident.mType == TYPE_WIFI) { 221 return true; 222 } 223 return false; 224 } 225 226 /** 227 * Check if matches Ethernet network template. 228 */ 229 private boolean matchesEthernet(NetworkIdentity ident) { 230 if (ident.mType == TYPE_ETHERNET) { 231 return true; 232 } 233 return false; 234 } 235 236 private static String getMatchRuleName(int matchRule) { 237 switch (matchRule) { 238 case MATCH_MOBILE_3G_LOWER: 239 return "MOBILE_3G_LOWER"; 240 case MATCH_MOBILE_4G: 241 return "MOBILE_4G"; 242 case MATCH_MOBILE_ALL: 243 return "MOBILE_ALL"; 244 case MATCH_WIFI: 245 return "WIFI"; 246 case MATCH_ETHERNET: 247 return "ETHERNET"; 248 default: 249 return "UNKNOWN"; 250 } 251 } 252 253 public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { 254 public NetworkTemplate createFromParcel(Parcel in) { 255 return new NetworkTemplate(in); 256 } 257 258 public NetworkTemplate[] newArray(int size) { 259 return new NetworkTemplate[size]; 260 } 261 }; 262} 263