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