Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 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 | |
| 17 | package android.net; |
| 18 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_BLUETOOTH; |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIFI_P2P; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 24 | import static android.net.NetworkIdentity.COMBINE_SUBTYPE_ENABLED; |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 25 | import static android.net.wifi.WifiInfo.removeDoubleQuotes; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 26 | import static android.telephony.TelephonyManager.NETWORK_CLASS_2_G; |
| 27 | import static android.telephony.TelephonyManager.NETWORK_CLASS_3_G; |
| 28 | import static android.telephony.TelephonyManager.NETWORK_CLASS_4_G; |
| 29 | import static android.telephony.TelephonyManager.NETWORK_CLASS_UNKNOWN; |
| 30 | import static android.telephony.TelephonyManager.getNetworkClass; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 31 | import static com.android.internal.util.ArrayUtils.contains; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 32 | |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 33 | import android.content.res.Resources; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 34 | import android.os.Parcel; |
| 35 | import android.os.Parcelable; |
| 36 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 37 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 38 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 39 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 40 | import java.util.Arrays; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 41 | import java.util.Objects; |
| 42 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Template definition used to generically match {@link NetworkIdentity}, |
| 45 | * usually when collecting statistics. |
| 46 | * |
| 47 | * @hide |
| 48 | */ |
| 49 | public class NetworkTemplate implements Parcelable { |
| 50 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 51 | public static final int MATCH_MOBILE_ALL = 1; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 52 | public static final int MATCH_MOBILE_3G_LOWER = 2; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 53 | public static final int MATCH_MOBILE_4G = 3; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 54 | public static final int MATCH_WIFI = 4; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 55 | public static final int MATCH_ETHERNET = 5; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 56 | public static final int MATCH_MOBILE_WILDCARD = 6; |
| 57 | public static final int MATCH_WIFI_WILDCARD = 7; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 58 | public static final int MATCH_BLUETOOTH = 8; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 59 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 60 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 61 | * Set of {@link NetworkInfo#getType()} that reflect data usage. |
| 62 | */ |
| 63 | private static final int[] DATA_USAGE_NETWORK_TYPES; |
| 64 | |
| 65 | static { |
| 66 | DATA_USAGE_NETWORK_TYPES = Resources.getSystem().getIntArray( |
| 67 | com.android.internal.R.array.config_data_usage_network_types); |
| 68 | } |
| 69 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 70 | private static boolean sForceAllNetworkTypes = false; |
| 71 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 72 | @VisibleForTesting |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 73 | public static void forceAllNetworkTypes() { |
| 74 | sForceAllNetworkTypes = true; |
| 75 | } |
| 76 | |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 77 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 78 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 79 | * the given IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 80 | */ |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 81 | public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 82 | return new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 83 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 86 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 87 | * the given IMSI that roughly meet a "3G" definition, or lower. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 88 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 89 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 90 | public static NetworkTemplate buildTemplateMobile3gLower(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 91 | return new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 92 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 95 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 96 | * the given IMSI that roughly meet a "4G" definition. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 97 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 98 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 99 | public static NetworkTemplate buildTemplateMobile4g(String subscriberId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 100 | return new NetworkTemplate(MATCH_MOBILE_4G, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 101 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 102 | |
| 103 | /** |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 104 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, |
| 105 | * regardless of IMSI. |
| 106 | */ |
| 107 | public static NetworkTemplate buildTemplateMobileWildcard() { |
| 108 | return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); |
| 109 | } |
| 110 | |
| 111 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 112 | * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, |
| 113 | * regardless of SSID. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 114 | */ |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 115 | public static NetworkTemplate buildTemplateWifiWildcard() { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 116 | return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | @Deprecated |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 120 | public static NetworkTemplate buildTemplateWifi() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 121 | return buildTemplateWifiWildcard(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the |
| 126 | * given SSID. |
| 127 | */ |
| 128 | public static NetworkTemplate buildTemplateWifi(String networkId) { |
| 129 | return new NetworkTemplate(MATCH_WIFI, null, networkId); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 130 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style |
| 134 | * networks together. |
| 135 | */ |
| 136 | public static NetworkTemplate buildTemplateEthernet() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 137 | return new NetworkTemplate(MATCH_ETHERNET, null, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 138 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 140 | /** |
| 141 | * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style |
| 142 | * networks together. |
| 143 | */ |
| 144 | public static NetworkTemplate buildTemplateBluetooth() { |
| 145 | return new NetworkTemplate(MATCH_BLUETOOTH, null, null); |
| 146 | } |
| 147 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 148 | private final int mMatchRule; |
| 149 | private final String mSubscriberId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 150 | |
| 151 | /** |
| 152 | * Ugh, templates are designed to target a single subscriber, but we might |
| 153 | * need to match several "merged" subscribers. These are the subscribers |
| 154 | * that should be considered to match this template. |
| 155 | * <p> |
| 156 | * Since the merge set is dynamic, it should <em>not</em> be persisted or |
| 157 | * used for determining equality. |
| 158 | */ |
| 159 | private final String[] mMatchSubscriberIds; |
| 160 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 161 | private final String mNetworkId; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 163 | public NetworkTemplate(int matchRule, String subscriberId, String networkId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 164 | this(matchRule, subscriberId, new String[] { subscriberId }, networkId); |
| 165 | } |
| 166 | |
| 167 | public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, |
| 168 | String networkId) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 169 | mMatchRule = matchRule; |
| 170 | mSubscriberId = subscriberId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 171 | mMatchSubscriberIds = matchSubscriberIds; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 172 | mNetworkId = networkId; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 175 | private NetworkTemplate(Parcel in) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 176 | mMatchRule = in.readInt(); |
| 177 | mSubscriberId = in.readString(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 178 | mMatchSubscriberIds = in.createStringArray(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 179 | mNetworkId = in.readString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 182 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 183 | public void writeToParcel(Parcel dest, int flags) { |
| 184 | dest.writeInt(mMatchRule); |
| 185 | dest.writeString(mSubscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 186 | dest.writeStringArray(mMatchSubscriberIds); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 187 | dest.writeString(mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 190 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 191 | public int describeContents() { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public String toString() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 197 | final StringBuilder builder = new StringBuilder("NetworkTemplate: "); |
| 198 | builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); |
| 199 | if (mSubscriberId != null) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 200 | builder.append(", subscriberId=").append( |
| 201 | NetworkIdentity.scrubSubscriberId(mSubscriberId)); |
| 202 | } |
| 203 | if (mMatchSubscriberIds != null) { |
| 204 | builder.append(", matchSubscriberIds=").append( |
| 205 | Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds))); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 206 | } |
| 207 | if (mNetworkId != null) { |
| 208 | builder.append(", networkId=").append(mNetworkId); |
| 209 | } |
| 210 | return builder.toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public int hashCode() { |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 215 | return Objects.hash(mMatchRule, mSubscriberId, mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public boolean equals(Object obj) { |
| 220 | if (obj instanceof NetworkTemplate) { |
| 221 | final NetworkTemplate other = (NetworkTemplate) obj; |
| 222 | return mMatchRule == other.mMatchRule |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 223 | && Objects.equals(mSubscriberId, other.mSubscriberId) |
| 224 | && Objects.equals(mNetworkId, other.mNetworkId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 229 | public boolean isMatchRuleMobile() { |
| 230 | switch (mMatchRule) { |
| 231 | case MATCH_MOBILE_3G_LOWER: |
| 232 | case MATCH_MOBILE_4G: |
| 233 | case MATCH_MOBILE_ALL: |
| 234 | case MATCH_MOBILE_WILDCARD: |
| 235 | return true; |
| 236 | default: |
| 237 | return false; |
| 238 | } |
| 239 | } |
| 240 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 241 | public int getMatchRule() { |
| 242 | return mMatchRule; |
| 243 | } |
| 244 | |
| 245 | public String getSubscriberId() { |
| 246 | return mSubscriberId; |
| 247 | } |
| 248 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 249 | public String getNetworkId() { |
| 250 | return mNetworkId; |
| 251 | } |
| 252 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 253 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 254 | * Test if given {@link NetworkIdentity} matches this template. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 255 | */ |
| 256 | public boolean matches(NetworkIdentity ident) { |
| 257 | switch (mMatchRule) { |
| 258 | case MATCH_MOBILE_ALL: |
| 259 | return matchesMobile(ident); |
| 260 | case MATCH_MOBILE_3G_LOWER: |
| 261 | return matchesMobile3gLower(ident); |
| 262 | case MATCH_MOBILE_4G: |
| 263 | return matchesMobile4g(ident); |
| 264 | case MATCH_WIFI: |
| 265 | return matchesWifi(ident); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 266 | case MATCH_ETHERNET: |
| 267 | return matchesEthernet(ident); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 268 | case MATCH_MOBILE_WILDCARD: |
| 269 | return matchesMobileWildcard(ident); |
| 270 | case MATCH_WIFI_WILDCARD: |
| 271 | return matchesWifiWildcard(ident); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 272 | case MATCH_BLUETOOTH: |
| 273 | return matchesBluetooth(ident); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 274 | default: |
| 275 | throw new IllegalArgumentException("unknown network template"); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 280 | * Check if mobile network with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 281 | */ |
| 282 | private boolean matchesMobile(NetworkIdentity ident) { |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 283 | if (ident.mType == TYPE_WIMAX) { |
| 284 | // TODO: consider matching against WiMAX subscriber identity |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 285 | return true; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 286 | } else { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 287 | final boolean matchesType = (sForceAllNetworkTypes |
| 288 | || contains(DATA_USAGE_NETWORK_TYPES, ident.mType)); |
| 289 | return matchesType && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 290 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /** |
Jeff Sharkey | 02e21d6 | 2011-07-17 15:53:33 -0700 | [diff] [blame] | 294 | * Check if mobile network classified 3G or lower with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 295 | */ |
| 296 | private boolean matchesMobile3gLower(NetworkIdentity ident) { |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 297 | ensureSubtypeAvailable(); |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 298 | if (ident.mType == TYPE_WIMAX) { |
| 299 | return false; |
| 300 | } else if (matchesMobile(ident)) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 301 | switch (getNetworkClass(ident.mSubType)) { |
| 302 | case NETWORK_CLASS_UNKNOWN: |
| 303 | case NETWORK_CLASS_2_G: |
| 304 | case NETWORK_CLASS_3_G: |
| 305 | return true; |
| 306 | } |
| 307 | } |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 312 | * Check if mobile network classified 4G with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 313 | */ |
| 314 | private boolean matchesMobile4g(NetworkIdentity ident) { |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 315 | ensureSubtypeAvailable(); |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 316 | if (ident.mType == TYPE_WIMAX) { |
| 317 | // TODO: consider matching against WiMAX subscriber identity |
| 318 | return true; |
| 319 | } else if (matchesMobile(ident)) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 320 | switch (getNetworkClass(ident.mSubType)) { |
| 321 | case NETWORK_CLASS_4_G: |
| 322 | return true; |
| 323 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Check if matches Wi-Fi network template. |
| 330 | */ |
| 331 | private boolean matchesWifi(NetworkIdentity ident) { |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 332 | switch (ident.mType) { |
| 333 | case TYPE_WIFI: |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 334 | return Objects.equals( |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 335 | removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId)); |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 336 | default: |
| 337 | return false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 338 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 341 | /** |
| 342 | * Check if matches Ethernet network template. |
| 343 | */ |
| 344 | private boolean matchesEthernet(NetworkIdentity ident) { |
| 345 | if (ident.mType == TYPE_ETHERNET) { |
| 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 351 | private boolean matchesMobileWildcard(NetworkIdentity ident) { |
| 352 | if (ident.mType == TYPE_WIMAX) { |
| 353 | return true; |
| 354 | } else { |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 355 | return sForceAllNetworkTypes || contains(DATA_USAGE_NETWORK_TYPES, ident.mType); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | private boolean matchesWifiWildcard(NetworkIdentity ident) { |
| 360 | switch (ident.mType) { |
| 361 | case TYPE_WIFI: |
| 362 | case TYPE_WIFI_P2P: |
| 363 | return true; |
| 364 | default: |
| 365 | return false; |
| 366 | } |
| 367 | } |
| 368 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 369 | /** |
| 370 | * Check if matches Bluetooth network template. |
| 371 | */ |
| 372 | private boolean matchesBluetooth(NetworkIdentity ident) { |
| 373 | if (ident.mType == TYPE_BLUETOOTH) { |
| 374 | return true; |
| 375 | } |
| 376 | return false; |
| 377 | } |
| 378 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 379 | private static String getMatchRuleName(int matchRule) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 380 | switch (matchRule) { |
| 381 | case MATCH_MOBILE_3G_LOWER: |
| 382 | return "MOBILE_3G_LOWER"; |
| 383 | case MATCH_MOBILE_4G: |
| 384 | return "MOBILE_4G"; |
| 385 | case MATCH_MOBILE_ALL: |
| 386 | return "MOBILE_ALL"; |
| 387 | case MATCH_WIFI: |
| 388 | return "WIFI"; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 389 | case MATCH_ETHERNET: |
| 390 | return "ETHERNET"; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 391 | case MATCH_MOBILE_WILDCARD: |
| 392 | return "MOBILE_WILDCARD"; |
| 393 | case MATCH_WIFI_WILDCARD: |
| 394 | return "WIFI_WILDCARD"; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 395 | case MATCH_BLUETOOTH: |
| 396 | return "BLUETOOTH"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 397 | default: |
| 398 | return "UNKNOWN"; |
| 399 | } |
| 400 | } |
| 401 | |
Jeff Sharkey | d4dd771 | 2012-03-16 11:11:54 -0700 | [diff] [blame] | 402 | private static void ensureSubtypeAvailable() { |
| 403 | if (COMBINE_SUBTYPE_ENABLED) { |
| 404 | throw new IllegalArgumentException( |
| 405 | "Unable to enforce 3G_LOWER template on combined data."); |
| 406 | } |
| 407 | } |
| 408 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame^] | 409 | /** |
| 410 | * Examine the given template and normalize if it refers to a "merged" |
| 411 | * mobile subscriber. We pick the "lowest" merged subscriber as the primary |
| 412 | * for key purposes, and expand the template to match all other merged |
| 413 | * subscribers. |
| 414 | * <p> |
| 415 | * For example, given an incoming template matching B, and the currently |
| 416 | * active merge set [A,B], we'd return a new template that primarily matches |
| 417 | * A, but also matches B. |
| 418 | */ |
| 419 | public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) { |
| 420 | if (template.isMatchRuleMobile() && ArrayUtils.contains(merged, template.mSubscriberId)) { |
| 421 | // Requested template subscriber is part of the merge group; return |
| 422 | // a template that matches all merged subscribers. |
| 423 | return new NetworkTemplate(template.mMatchRule, merged[0], merged, |
| 424 | template.mNetworkId); |
| 425 | } else { |
| 426 | return template; |
| 427 | } |
| 428 | } |
| 429 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 430 | public static final Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 431 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 432 | public NetworkTemplate createFromParcel(Parcel in) { |
| 433 | return new NetworkTemplate(in); |
| 434 | } |
| 435 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 436 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 437 | public NetworkTemplate[] newArray(int size) { |
| 438 | return new NetworkTemplate[size]; |
| 439 | } |
| 440 | }; |
| 441 | } |