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; |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_PROXY; |
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; |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 26 | import static android.net.NetworkStats.DEFAULT_NETWORK_ALL; |
| 27 | import static android.net.NetworkStats.DEFAULT_NETWORK_NO; |
| 28 | import static android.net.NetworkStats.DEFAULT_NETWORK_YES; |
| 29 | import static android.net.NetworkStats.METERED_ALL; |
| 30 | import static android.net.NetworkStats.METERED_NO; |
| 31 | import static android.net.NetworkStats.METERED_YES; |
| 32 | import static android.net.NetworkStats.ROAMING_ALL; |
| 33 | import static android.net.NetworkStats.ROAMING_NO; |
| 34 | import static android.net.NetworkStats.ROAMING_YES; |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 35 | import static android.net.wifi.WifiInfo.removeDoubleQuotes; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 36 | |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 37 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 38 | import android.os.Parcel; |
| 39 | import android.os.Parcelable; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 40 | import android.util.BackupUtils; |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 41 | import android.util.Log; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 42 | |
Varun Anand | 472b926 | 2019-02-17 23:15:15 -0800 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 44 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 45 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 46 | import java.io.ByteArrayOutputStream; |
| 47 | import java.io.DataInputStream; |
| 48 | import java.io.DataOutputStream; |
| 49 | import java.io.IOException; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 50 | import java.util.Arrays; |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 51 | import java.util.List; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 52 | import java.util.Objects; |
| 53 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 54 | /** |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 55 | * Predicate used to match {@link NetworkIdentity}, usually when collecting |
| 56 | * statistics. (It should probably have been named {@code NetworkPredicate}.) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 57 | * |
| 58 | * @hide |
| 59 | */ |
| 60 | public class NetworkTemplate implements Parcelable { |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 61 | private static final String TAG = "NetworkTemplate"; |
| 62 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Current Version of the Backup Serializer. |
| 65 | */ |
| 66 | private static final int BACKUP_VERSION = 1; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 68 | public static final int MATCH_MOBILE = 1; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 69 | public static final int MATCH_WIFI = 4; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 70 | public static final int MATCH_ETHERNET = 5; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 71 | public static final int MATCH_MOBILE_WILDCARD = 6; |
| 72 | public static final int MATCH_WIFI_WILDCARD = 7; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 73 | public static final int MATCH_BLUETOOTH = 8; |
Sharvil Nanavati | 7f8d650 | 2016-03-21 14:19:37 -0700 | [diff] [blame] | 74 | public static final int MATCH_PROXY = 9; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 75 | |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 76 | private static boolean isKnownMatchRule(final int rule) { |
| 77 | switch (rule) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 78 | case MATCH_MOBILE: |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 79 | case MATCH_WIFI: |
| 80 | case MATCH_ETHERNET: |
| 81 | case MATCH_MOBILE_WILDCARD: |
| 82 | case MATCH_WIFI_WILDCARD: |
| 83 | case MATCH_BLUETOOTH: |
| 84 | case MATCH_PROXY: |
| 85 | return true; |
| 86 | |
| 87 | default: |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 92 | private static boolean sForceAllNetworkTypes = false; |
| 93 | |
Varun Anand | 472b926 | 2019-02-17 23:15:15 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Results in matching against all mobile network types. |
| 96 | * |
| 97 | * <p>See {@link #matchesMobile} and {@link matchesMobileWildcard}. |
| 98 | */ |
| 99 | @VisibleForTesting |
Jeff Sharkey | 70c7053 | 2012-05-16 14:51:19 -0700 | [diff] [blame] | 100 | public static void forceAllNetworkTypes() { |
| 101 | sForceAllNetworkTypes = true; |
| 102 | } |
| 103 | |
Varun Anand | 472b926 | 2019-02-17 23:15:15 -0800 | [diff] [blame] | 104 | /** Resets the affect of {@link #forceAllNetworkTypes}. */ |
| 105 | @VisibleForTesting |
| 106 | public static void resetForceAllNetworkTypes() { |
| 107 | sForceAllNetworkTypes = false; |
| 108 | } |
| 109 | |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 110 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 111 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks with |
| 112 | * the given IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 113 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 114 | @UnsupportedAppUsage |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 115 | public static NetworkTemplate buildTemplateMobileAll(String subscriberId) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 116 | return new NetworkTemplate(MATCH_MOBILE, subscriberId, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 117 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 118 | |
| 119 | /** |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 120 | * Template to match {@link ConnectivityManager#TYPE_MOBILE} networks, |
| 121 | * regardless of IMSI. |
| 122 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 123 | @UnsupportedAppUsage |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 124 | public static NetworkTemplate buildTemplateMobileWildcard() { |
| 125 | return new NetworkTemplate(MATCH_MOBILE_WILDCARD, null, null); |
| 126 | } |
| 127 | |
| 128 | /** |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 129 | * Template to match all {@link ConnectivityManager#TYPE_WIFI} networks, |
| 130 | * regardless of SSID. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 131 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 132 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 133 | public static NetworkTemplate buildTemplateWifiWildcard() { |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 134 | return new NetworkTemplate(MATCH_WIFI_WILDCARD, null, null); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Deprecated |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 138 | @UnsupportedAppUsage |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 139 | public static NetworkTemplate buildTemplateWifi() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 140 | return buildTemplateWifiWildcard(); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Template to match {@link ConnectivityManager#TYPE_WIFI} networks with the |
| 145 | * given SSID. |
| 146 | */ |
| 147 | public static NetworkTemplate buildTemplateWifi(String networkId) { |
| 148 | return new NetworkTemplate(MATCH_WIFI, null, networkId); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 149 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 150 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 151 | /** |
| 152 | * Template to combine all {@link ConnectivityManager#TYPE_ETHERNET} style |
| 153 | * networks together. |
| 154 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 155 | @UnsupportedAppUsage |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 156 | public static NetworkTemplate buildTemplateEthernet() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 157 | return new NetworkTemplate(MATCH_ETHERNET, null, null); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 158 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 159 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Template to combine all {@link ConnectivityManager#TYPE_BLUETOOTH} style |
| 162 | * networks together. |
| 163 | */ |
| 164 | public static NetworkTemplate buildTemplateBluetooth() { |
| 165 | return new NetworkTemplate(MATCH_BLUETOOTH, null, null); |
| 166 | } |
| 167 | |
Sharvil Nanavati | 7f8d650 | 2016-03-21 14:19:37 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Template to combine all {@link ConnectivityManager#TYPE_PROXY} style |
| 170 | * networks together. |
| 171 | */ |
| 172 | public static NetworkTemplate buildTemplateProxy() { |
| 173 | return new NetworkTemplate(MATCH_PROXY, null, null); |
| 174 | } |
| 175 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 176 | private final int mMatchRule; |
| 177 | private final String mSubscriberId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Ugh, templates are designed to target a single subscriber, but we might |
| 181 | * need to match several "merged" subscribers. These are the subscribers |
| 182 | * that should be considered to match this template. |
| 183 | * <p> |
| 184 | * Since the merge set is dynamic, it should <em>not</em> be persisted or |
| 185 | * used for determining equality. |
| 186 | */ |
| 187 | private final String[] mMatchSubscriberIds; |
| 188 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 189 | private final String mNetworkId; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 190 | |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 191 | // Matches for the NetworkStats constants METERED_*, ROAMING_* and DEFAULT_NETWORK_*. |
| 192 | private final int mMetered; |
| 193 | private final int mRoaming; |
| 194 | private final int mDefaultNetwork; |
| 195 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 196 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 197 | public NetworkTemplate(int matchRule, String subscriberId, String networkId) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 198 | this(matchRule, subscriberId, new String[] { subscriberId }, networkId); |
| 199 | } |
| 200 | |
| 201 | public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, |
| 202 | String networkId) { |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 203 | this(matchRule, subscriberId, matchSubscriberIds, networkId, METERED_ALL, ROAMING_ALL, |
| 204 | DEFAULT_NETWORK_ALL); |
| 205 | } |
| 206 | |
| 207 | public NetworkTemplate(int matchRule, String subscriberId, String[] matchSubscriberIds, |
| 208 | String networkId, int metered, int roaming, int defaultNetwork) { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 209 | mMatchRule = matchRule; |
| 210 | mSubscriberId = subscriberId; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 211 | mMatchSubscriberIds = matchSubscriberIds; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 212 | mNetworkId = networkId; |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 213 | mMetered = metered; |
| 214 | mRoaming = roaming; |
| 215 | mDefaultNetwork = defaultNetwork; |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 216 | |
| 217 | if (!isKnownMatchRule(matchRule)) { |
| 218 | Log.e(TAG, "Unknown network template rule " + matchRule |
| 219 | + " will not match any identity."); |
| 220 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 223 | private NetworkTemplate(Parcel in) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 224 | mMatchRule = in.readInt(); |
| 225 | mSubscriberId = in.readString(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 226 | mMatchSubscriberIds = in.createStringArray(); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 227 | mNetworkId = in.readString(); |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 228 | mMetered = in.readInt(); |
| 229 | mRoaming = in.readInt(); |
| 230 | mDefaultNetwork = in.readInt(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 233 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 234 | public void writeToParcel(Parcel dest, int flags) { |
| 235 | dest.writeInt(mMatchRule); |
| 236 | dest.writeString(mSubscriberId); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 237 | dest.writeStringArray(mMatchSubscriberIds); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 238 | dest.writeString(mNetworkId); |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 239 | dest.writeInt(mMetered); |
| 240 | dest.writeInt(mRoaming); |
| 241 | dest.writeInt(mDefaultNetwork); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 244 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 245 | public int describeContents() { |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String toString() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 251 | final StringBuilder builder = new StringBuilder("NetworkTemplate: "); |
| 252 | builder.append("matchRule=").append(getMatchRuleName(mMatchRule)); |
| 253 | if (mSubscriberId != null) { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 254 | builder.append(", subscriberId=").append( |
| 255 | NetworkIdentity.scrubSubscriberId(mSubscriberId)); |
| 256 | } |
| 257 | if (mMatchSubscriberIds != null) { |
| 258 | builder.append(", matchSubscriberIds=").append( |
| 259 | Arrays.toString(NetworkIdentity.scrubSubscriberId(mMatchSubscriberIds))); |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 260 | } |
| 261 | if (mNetworkId != null) { |
| 262 | builder.append(", networkId=").append(mNetworkId); |
| 263 | } |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 264 | if (mMetered != METERED_ALL) { |
| 265 | builder.append(", metered=").append(NetworkStats.meteredToString(mMetered)); |
| 266 | } |
| 267 | if (mRoaming != ROAMING_ALL) { |
| 268 | builder.append(", roaming=").append(NetworkStats.roamingToString(mRoaming)); |
| 269 | } |
| 270 | if (mDefaultNetwork != DEFAULT_NETWORK_ALL) { |
| 271 | builder.append(", defaultNetwork=").append(NetworkStats.defaultNetworkToString( |
| 272 | mDefaultNetwork)); |
| 273 | } |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 274 | return builder.toString(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
| 278 | public int hashCode() { |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 279 | return Objects.hash(mMatchRule, mSubscriberId, mNetworkId, mMetered, mRoaming, |
| 280 | mDefaultNetwork); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public boolean equals(Object obj) { |
| 285 | if (obj instanceof NetworkTemplate) { |
| 286 | final NetworkTemplate other = (NetworkTemplate) obj; |
| 287 | return mMatchRule == other.mMatchRule |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 288 | && Objects.equals(mSubscriberId, other.mSubscriberId) |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 289 | && Objects.equals(mNetworkId, other.mNetworkId) |
| 290 | && mMetered == other.mMetered |
| 291 | && mRoaming == other.mRoaming |
| 292 | && mDefaultNetwork == other.mDefaultNetwork; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 293 | } |
| 294 | return false; |
| 295 | } |
| 296 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 297 | public boolean isMatchRuleMobile() { |
| 298 | switch (mMatchRule) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 299 | case MATCH_MOBILE: |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 300 | case MATCH_MOBILE_WILDCARD: |
| 301 | return true; |
| 302 | default: |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | |
Jeff Sharkey | 7474fe7b | 2016-03-21 13:12:59 -0600 | [diff] [blame] | 307 | public boolean isPersistable() { |
| 308 | switch (mMatchRule) { |
| 309 | case MATCH_MOBILE_WILDCARD: |
| 310 | case MATCH_WIFI_WILDCARD: |
| 311 | return false; |
| 312 | default: |
| 313 | return true; |
| 314 | } |
| 315 | } |
| 316 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 317 | @UnsupportedAppUsage |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 318 | public int getMatchRule() { |
| 319 | return mMatchRule; |
| 320 | } |
| 321 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 322 | @UnsupportedAppUsage |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 323 | public String getSubscriberId() { |
| 324 | return mSubscriberId; |
| 325 | } |
| 326 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 327 | public String getNetworkId() { |
| 328 | return mNetworkId; |
| 329 | } |
| 330 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 331 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 332 | * Test if given {@link NetworkIdentity} matches this template. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 333 | */ |
| 334 | public boolean matches(NetworkIdentity ident) { |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 335 | if (!matchesMetered(ident)) return false; |
| 336 | if (!matchesRoaming(ident)) return false; |
| 337 | if (!matchesDefaultNetwork(ident)) return false; |
| 338 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 339 | switch (mMatchRule) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 340 | case MATCH_MOBILE: |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 341 | return matchesMobile(ident); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 342 | case MATCH_WIFI: |
| 343 | return matchesWifi(ident); |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 344 | case MATCH_ETHERNET: |
| 345 | return matchesEthernet(ident); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 346 | case MATCH_MOBILE_WILDCARD: |
| 347 | return matchesMobileWildcard(ident); |
| 348 | case MATCH_WIFI_WILDCARD: |
| 349 | return matchesWifiWildcard(ident); |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 350 | case MATCH_BLUETOOTH: |
| 351 | return matchesBluetooth(ident); |
Sharvil Nanavati | 7f8d650 | 2016-03-21 14:19:37 -0700 | [diff] [blame] | 352 | case MATCH_PROXY: |
| 353 | return matchesProxy(ident); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 354 | default: |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 355 | // We have no idea what kind of network template we are, so we |
| 356 | // just claim not to match anything. |
| 357 | return false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Lorenzo Colitti | 53ba2f0 | 2018-01-22 11:54:46 +0900 | [diff] [blame] | 361 | private boolean matchesMetered(NetworkIdentity ident) { |
| 362 | return (mMetered == METERED_ALL) |
| 363 | || (mMetered == METERED_YES && ident.mMetered) |
| 364 | || (mMetered == METERED_NO && !ident.mMetered); |
| 365 | } |
| 366 | |
| 367 | private boolean matchesRoaming(NetworkIdentity ident) { |
| 368 | return (mRoaming == ROAMING_ALL) |
| 369 | || (mRoaming == ROAMING_YES && ident.mRoaming) |
| 370 | || (mRoaming == ROAMING_NO && !ident.mRoaming); |
| 371 | } |
| 372 | |
| 373 | private boolean matchesDefaultNetwork(NetworkIdentity ident) { |
| 374 | return (mDefaultNetwork == DEFAULT_NETWORK_ALL) |
| 375 | || (mDefaultNetwork == DEFAULT_NETWORK_YES && ident.mDefaultNetwork) |
| 376 | || (mDefaultNetwork == DEFAULT_NETWORK_NO && !ident.mDefaultNetwork); |
| 377 | } |
| 378 | |
Jeff Sharkey | f4de294 | 2017-08-29 15:32:13 -0600 | [diff] [blame] | 379 | public boolean matchesSubscriberId(String subscriberId) { |
| 380 | return ArrayUtils.contains(mMatchSubscriberIds, subscriberId); |
| 381 | } |
| 382 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 383 | /** |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 384 | * Check if mobile network with matching IMSI. |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 385 | */ |
| 386 | private boolean matchesMobile(NetworkIdentity ident) { |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 387 | if (ident.mType == TYPE_WIMAX) { |
| 388 | // TODO: consider matching against WiMAX subscriber identity |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 389 | return true; |
Jeff Sharkey | 630a171 | 2011-09-26 10:47:10 -0700 | [diff] [blame] | 390 | } else { |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 391 | return (sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered)) |
| 392 | && !ArrayUtils.isEmpty(mMatchSubscriberIds) |
Jeff Sharkey | 56859f3 | 2015-10-19 16:52:08 -0700 | [diff] [blame] | 393 | && ArrayUtils.contains(mMatchSubscriberIds, ident.mSubscriberId); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 394 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 398 | * Check if matches Wi-Fi network template. |
| 399 | */ |
| 400 | private boolean matchesWifi(NetworkIdentity ident) { |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 401 | switch (ident.mType) { |
| 402 | case TYPE_WIFI: |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 403 | return Objects.equals( |
Jeff Sharkey | 2e4dce0 | 2012-12-18 17:06:06 -0800 | [diff] [blame] | 404 | removeDoubleQuotes(mNetworkId), removeDoubleQuotes(ident.mNetworkId)); |
Jeff Sharkey | 3ca7481 | 2012-01-24 15:37:07 -0800 | [diff] [blame] | 405 | default: |
| 406 | return false; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 407 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Check if matches Ethernet network template. |
| 412 | */ |
| 413 | private boolean matchesEthernet(NetworkIdentity ident) { |
| 414 | if (ident.mType == TYPE_ETHERNET) { |
| 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 420 | private boolean matchesMobileWildcard(NetworkIdentity ident) { |
| 421 | if (ident.mType == TYPE_WIMAX) { |
| 422 | return true; |
| 423 | } else { |
Jack Yu | 66a6be3 | 2016-03-30 11:14:39 -0700 | [diff] [blame] | 424 | return sForceAllNetworkTypes || (ident.mType == TYPE_MOBILE && ident.mMetered); |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | private boolean matchesWifiWildcard(NetworkIdentity ident) { |
| 429 | switch (ident.mType) { |
| 430 | case TYPE_WIFI: |
| 431 | case TYPE_WIFI_P2P: |
| 432 | return true; |
| 433 | default: |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 438 | /** |
| 439 | * Check if matches Bluetooth network template. |
| 440 | */ |
| 441 | private boolean matchesBluetooth(NetworkIdentity ident) { |
| 442 | if (ident.mType == TYPE_BLUETOOTH) { |
| 443 | return true; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
Sharvil Nanavati | 7f8d650 | 2016-03-21 14:19:37 -0700 | [diff] [blame] | 448 | /** |
| 449 | * Check if matches Proxy network template. |
| 450 | */ |
| 451 | private boolean matchesProxy(NetworkIdentity ident) { |
| 452 | return ident.mType == TYPE_PROXY; |
| 453 | } |
| 454 | |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 455 | private static String getMatchRuleName(int matchRule) { |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 456 | switch (matchRule) { |
Jeff Sharkey | e0c2995 | 2018-02-20 17:24:55 -0700 | [diff] [blame] | 457 | case MATCH_MOBILE: |
| 458 | return "MOBILE"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 459 | case MATCH_WIFI: |
| 460 | return "WIFI"; |
Jeff Sharkey | 4e814c3 | 2011-07-14 20:37:37 -0700 | [diff] [blame] | 461 | case MATCH_ETHERNET: |
| 462 | return "ETHERNET"; |
Jeff Sharkey | 234766a | 2012-04-10 19:48:07 -0700 | [diff] [blame] | 463 | case MATCH_MOBILE_WILDCARD: |
| 464 | return "MOBILE_WILDCARD"; |
| 465 | case MATCH_WIFI_WILDCARD: |
| 466 | return "WIFI_WILDCARD"; |
Jeff Sharkey | 55a442e | 2014-11-18 18:22:21 -0800 | [diff] [blame] | 467 | case MATCH_BLUETOOTH: |
| 468 | return "BLUETOOTH"; |
Sharvil Nanavati | 7f8d650 | 2016-03-21 14:19:37 -0700 | [diff] [blame] | 469 | case MATCH_PROXY: |
| 470 | return "PROXY"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 471 | default: |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 472 | return "UNKNOWN(" + matchRule + ")"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 476 | /** |
| 477 | * Examine the given template and normalize if it refers to a "merged" |
| 478 | * mobile subscriber. We pick the "lowest" merged subscriber as the primary |
| 479 | * for key purposes, and expand the template to match all other merged |
| 480 | * subscribers. |
| 481 | * <p> |
| 482 | * For example, given an incoming template matching B, and the currently |
| 483 | * active merge set [A,B], we'd return a new template that primarily matches |
| 484 | * A, but also matches B. |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 485 | * TODO: remove and use {@link #normalize(NetworkTemplate, List)}. |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 486 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 487 | @UnsupportedAppUsage |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 488 | public static NetworkTemplate normalize(NetworkTemplate template, String[] merged) { |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 489 | return normalize(template, Arrays.<String[]>asList(merged)); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Examine the given template and normalize if it refers to a "merged" |
| 494 | * mobile subscriber. We pick the "lowest" merged subscriber as the primary |
| 495 | * for key purposes, and expand the template to match all other merged |
| 496 | * subscribers. |
| 497 | * |
| 498 | * There can be multiple merged subscriberIds for multi-SIM devices. |
| 499 | * |
| 500 | * <p> |
| 501 | * For example, given an incoming template matching B, and the currently |
| 502 | * active merge set [A,B], we'd return a new template that primarily matches |
| 503 | * A, but also matches B. |
| 504 | */ |
| 505 | public static NetworkTemplate normalize(NetworkTemplate template, List<String[]> mergedList) { |
| 506 | if (!template.isMatchRuleMobile()) return template; |
| 507 | |
| 508 | for (String[] merged : mergedList) { |
| 509 | if (ArrayUtils.contains(merged, template.mSubscriberId)) { |
| 510 | // Requested template subscriber is part of the merge group; return |
| 511 | // a template that matches all merged subscribers. |
| 512 | return new NetworkTemplate(template.mMatchRule, merged[0], merged, |
| 513 | template.mNetworkId); |
| 514 | } |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 515 | } |
Malcolm Chen | 07fcb5b | 2019-07-02 22:29:35 -0700 | [diff] [blame] | 516 | |
| 517 | return template; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 520 | @UnsupportedAppUsage |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 521 | public static final @android.annotation.NonNull Creator<NetworkTemplate> CREATOR = new Creator<NetworkTemplate>() { |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 522 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 523 | public NetworkTemplate createFromParcel(Parcel in) { |
| 524 | return new NetworkTemplate(in); |
| 525 | } |
| 526 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 527 | @Override |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 528 | public NetworkTemplate[] newArray(int size) { |
| 529 | return new NetworkTemplate[size]; |
| 530 | } |
| 531 | }; |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 532 | |
| 533 | public byte[] getBytesForBackup() throws IOException { |
| 534 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 535 | DataOutputStream out = new DataOutputStream(baos); |
| 536 | |
| 537 | out.writeInt(BACKUP_VERSION); |
| 538 | |
| 539 | out.writeInt(mMatchRule); |
| 540 | BackupUtils.writeString(out, mSubscriberId); |
| 541 | BackupUtils.writeString(out, mNetworkId); |
| 542 | |
| 543 | return baos.toByteArray(); |
| 544 | } |
| 545 | |
| 546 | public static NetworkTemplate getNetworkTemplateFromBackup(DataInputStream in) |
| 547 | throws IOException, BackupUtils.BadVersionException { |
| 548 | int version = in.readInt(); |
| 549 | if (version < 1 || version > BACKUP_VERSION) { |
| 550 | throw new BackupUtils.BadVersionException("Unknown Backup Serialization Version"); |
| 551 | } |
| 552 | |
| 553 | int matchRule = in.readInt(); |
| 554 | String subscriberId = BackupUtils.readString(in); |
| 555 | String networkId = BackupUtils.readString(in); |
| 556 | |
Christopher Tate | 3bf0173 | 2017-05-15 16:34:52 -0700 | [diff] [blame] | 557 | if (!isKnownMatchRule(matchRule)) { |
| 558 | throw new BackupUtils.BadVersionException( |
| 559 | "Restored network template contains unknown match rule " + matchRule); |
| 560 | } |
| 561 | |
Ritesh Reddy | adca34a | 2016-02-04 18:33:30 +0000 | [diff] [blame] | 562 | return new NetworkTemplate(matchRule, subscriberId, networkId); |
| 563 | } |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 564 | } |