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