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