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