Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.annotation.SuppressLint; |
| 22 | import android.annotation.SystemApi; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 23 | import android.compat.annotation.UnsupportedAppUsage; |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 24 | import android.os.Parcel; |
| 25 | import android.os.Parcelable; |
| 26 | |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | /** |
| 30 | * A class representing a configured network. |
| 31 | * @hide |
| 32 | */ |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 33 | @SystemApi |
| 34 | public final class IpConfiguration implements Parcelable { |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 35 | private static final String TAG = "IpConfiguration"; |
| 36 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 37 | // This enum has been used by apps through reflection for many releases. |
| 38 | // Therefore they can't just be removed. Duplicating these constants to |
| 39 | // give an alternate SystemApi is a worse option than exposing them. |
| 40 | @SuppressLint("Enum") |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 41 | public enum IpAssignment { |
| 42 | /* Use statically configured IP settings. Configuration can be accessed |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 43 | * with staticIpConfiguration */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 44 | STATIC, |
Blake Lawson | 0c9ed96 | 2018-08-23 08:43:07 -0700 | [diff] [blame] | 45 | /* Use dynamically configured IP settings */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 46 | DHCP, |
| 47 | /* no IP details are assigned, this is used to indicate |
| 48 | * that any existing IP settings should be retained */ |
| 49 | UNASSIGNED |
| 50 | } |
| 51 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 52 | /** @hide */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 53 | public IpAssignment ipAssignment; |
| 54 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 55 | /** @hide */ |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 56 | public StaticIpConfiguration staticIpConfiguration; |
| 57 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 58 | // This enum has been used by apps through reflection for many releases. |
| 59 | // Therefore they can't just be removed. Duplicating these constants to |
| 60 | // give an alternate SystemApi is a worse option than exposing them. |
| 61 | @SuppressLint("Enum") |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 62 | public enum ProxySettings { |
| 63 | /* No proxy is to be used. Any existing proxy settings |
| 64 | * should be cleared. */ |
| 65 | NONE, |
| 66 | /* Use statically configured proxy. Configuration can be accessed |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 67 | * with httpProxy. */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 68 | STATIC, |
| 69 | /* no proxy details are assigned, this is used to indicate |
| 70 | * that any existing proxy settings should be retained */ |
| 71 | UNASSIGNED, |
| 72 | /* Use a Pac based proxy. |
| 73 | */ |
| 74 | PAC |
| 75 | } |
| 76 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 77 | /** @hide */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 78 | public ProxySettings proxySettings; |
| 79 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 80 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 81 | @UnsupportedAppUsage |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 82 | public ProxyInfo httpProxy; |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 83 | |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 84 | private void init(IpAssignment ipAssignment, |
| 85 | ProxySettings proxySettings, |
| 86 | StaticIpConfiguration staticIpConfiguration, |
| 87 | ProxyInfo httpProxy) { |
| 88 | this.ipAssignment = ipAssignment; |
| 89 | this.proxySettings = proxySettings; |
| 90 | this.staticIpConfiguration = (staticIpConfiguration == null) ? |
| 91 | null : new StaticIpConfiguration(staticIpConfiguration); |
| 92 | this.httpProxy = (httpProxy == null) ? |
| 93 | null : new ProxyInfo(httpProxy); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | public IpConfiguration() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 97 | init(IpAssignment.UNASSIGNED, ProxySettings.UNASSIGNED, null, null); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 100 | /** @hide */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 101 | @UnsupportedAppUsage |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 102 | public IpConfiguration(IpAssignment ipAssignment, |
| 103 | ProxySettings proxySettings, |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 104 | StaticIpConfiguration staticIpConfiguration, |
| 105 | ProxyInfo httpProxy) { |
| 106 | init(ipAssignment, proxySettings, staticIpConfiguration, httpProxy); |
| 107 | } |
| 108 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 109 | public IpConfiguration(@NonNull IpConfiguration source) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 110 | this(); |
| 111 | if (source != null) { |
| 112 | init(source.ipAssignment, source.proxySettings, |
| 113 | source.staticIpConfiguration, source.httpProxy); |
| 114 | } |
| 115 | } |
| 116 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 117 | public @NonNull IpAssignment getIpAssignment() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 118 | return ipAssignment; |
| 119 | } |
| 120 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 121 | public void setIpAssignment(@NonNull IpAssignment ipAssignment) { |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 122 | this.ipAssignment = ipAssignment; |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 123 | } |
| 124 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 125 | public @Nullable StaticIpConfiguration getStaticIpConfiguration() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 126 | return staticIpConfiguration; |
| 127 | } |
| 128 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 129 | public void setStaticIpConfiguration(@Nullable StaticIpConfiguration staticIpConfiguration) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 130 | this.staticIpConfiguration = staticIpConfiguration; |
| 131 | } |
| 132 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 133 | public @NonNull ProxySettings getProxySettings() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 134 | return proxySettings; |
| 135 | } |
| 136 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 137 | public void setProxySettings(@NonNull ProxySettings proxySettings) { |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 138 | this.proxySettings = proxySettings; |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 141 | public @Nullable ProxyInfo getHttpProxy() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 142 | return httpProxy; |
| 143 | } |
| 144 | |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 145 | public void setHttpProxy(@Nullable ProxyInfo httpProxy) { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 146 | this.httpProxy = httpProxy; |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public String toString() { |
| 151 | StringBuilder sbuf = new StringBuilder(); |
| 152 | sbuf.append("IP assignment: " + ipAssignment.toString()); |
| 153 | sbuf.append("\n"); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 154 | if (staticIpConfiguration != null) { |
| 155 | sbuf.append("Static configuration: " + staticIpConfiguration.toString()); |
| 156 | sbuf.append("\n"); |
| 157 | } |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 158 | sbuf.append("Proxy settings: " + proxySettings.toString()); |
| 159 | sbuf.append("\n"); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 160 | if (httpProxy != null) { |
| 161 | sbuf.append("HTTP proxy: " + httpProxy.toString()); |
| 162 | sbuf.append("\n"); |
| 163 | } |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 164 | |
| 165 | return sbuf.toString(); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public boolean equals(Object o) { |
| 170 | if (o == this) { |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | if (!(o instanceof IpConfiguration)) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | IpConfiguration other = (IpConfiguration) o; |
| 179 | return this.ipAssignment == other.ipAssignment && |
| 180 | this.proxySettings == other.proxySettings && |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 181 | Objects.equals(this.staticIpConfiguration, other.staticIpConfiguration) && |
| 182 | Objects.equals(this.httpProxy, other.httpProxy); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public int hashCode() { |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 187 | return 13 + (staticIpConfiguration != null ? staticIpConfiguration.hashCode() : 0) + |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 188 | 17 * ipAssignment.ordinal() + |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 189 | 47 * proxySettings.ordinal() + |
| 190 | 83 * httpProxy.hashCode(); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 191 | } |
| 192 | |
Aaron Huang | 42daaca | 2019-11-28 14:17:32 +0800 | [diff] [blame] | 193 | /** Implement the Parcelable interface */ |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 194 | public int describeContents() { |
| 195 | return 0; |
| 196 | } |
| 197 | |
Aaron Huang | 42daaca | 2019-11-28 14:17:32 +0800 | [diff] [blame] | 198 | /** Implement the Parcelable interface */ |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 199 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 200 | dest.writeString(ipAssignment.name()); |
| 201 | dest.writeString(proxySettings.name()); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 202 | dest.writeParcelable(staticIpConfiguration, flags); |
| 203 | dest.writeParcelable(httpProxy, flags); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** Implement the Parcelable interface */ |
Aaron Huang | 6763aec | 2019-10-02 23:37:02 +0800 | [diff] [blame] | 207 | public static final @NonNull Creator<IpConfiguration> CREATOR = |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 208 | new Creator<IpConfiguration>() { |
| 209 | public IpConfiguration createFromParcel(Parcel in) { |
| 210 | IpConfiguration config = new IpConfiguration(); |
| 211 | config.ipAssignment = IpAssignment.valueOf(in.readString()); |
| 212 | config.proxySettings = ProxySettings.valueOf(in.readString()); |
Lorenzo Colitti | 0a82e80 | 2014-07-31 00:48:01 +0900 | [diff] [blame] | 213 | config.staticIpConfiguration = in.readParcelable(null); |
| 214 | config.httpProxy = in.readParcelable(null); |
Jaewan Kim | 6346155 | 2014-03-10 17:10:51 +0900 | [diff] [blame] | 215 | return config; |
| 216 | } |
| 217 | |
| 218 | public IpConfiguration[] newArray(int size) { |
| 219 | return new IpConfiguration[size]; |
| 220 | } |
| 221 | }; |
| 222 | } |