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