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