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