1 /*
2  * Copyright (C) 2021 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 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 import static android.net.NetworkCapabilities.REDACT_FOR_NETWORK_SETTINGS;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.SystemApi;
25 import android.net.NetworkCapabilities.RedactionType;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.text.TextUtils;
29 
30 import java.util.Objects;
31 
32 /**
33  * Container for VPN-specific transport information.
34  *
35  * @see android.net.TransportInfo
36  * @see NetworkCapabilities#getTransportInfo()
37  *
38  * @hide
39  */
40 @SystemApi(client = MODULE_LIBRARIES)
41 public final class VpnTransportInfo implements TransportInfo, Parcelable {
42     /** Type of this VPN. */
43     private final int mType;
44 
45     @Nullable
46     private final String mSessionId;
47 
48     @Override
49     public @RedactionType long getApplicableRedactions() {
50         return REDACT_FOR_NETWORK_SETTINGS;
51     }
52 
53     /**
54      * Create a copy of a {@link VpnTransportInfo} with the sessionId redacted if necessary.
55      */
56     @NonNull
57     public VpnTransportInfo makeCopy(@RedactionType long redactions) {
58         return new VpnTransportInfo(mType,
59             ((redactions & REDACT_FOR_NETWORK_SETTINGS) != 0) ? null : mSessionId);
60     }
61 
62     public VpnTransportInfo(int type, @Nullable String sessionId) {
63         this.mType = type;
64         this.mSessionId = sessionId;
65     }
66 
67     /**
68      * Returns the session Id of this VpnTransportInfo.
69      */
70     @Nullable
71     public String getSessionId() {
72         return mSessionId;
73     }
74 
75     /**
76      * Returns the type of this VPN.
77      */
78     public int getType() {
79         return mType;
80     }
81 
82     @Override
83     public boolean equals(Object o) {
84         if (!(o instanceof VpnTransportInfo)) return false;
85 
86         VpnTransportInfo that = (VpnTransportInfo) o;
87         return (this.mType == that.mType) && TextUtils.equals(this.mSessionId, that.mSessionId);
88     }
89 
90     @Override
91     public int hashCode() {
92         return Objects.hash(mType, mSessionId);
93     }
94 
95     @Override
96     public String toString() {
97         return String.format("VpnTransportInfo{type=%d, sessionId=%s}", mType, mSessionId);
98     }
99 
100     @Override
101     public int describeContents() {
102         return 0;
103     }
104 
105     @Override
106     public void writeToParcel(@NonNull Parcel dest, int flags) {
107         dest.writeInt(mType);
108         dest.writeString(mSessionId);
109     }
110 
111     public static final @NonNull Creator<VpnTransportInfo> CREATOR =
112             new Creator<VpnTransportInfo>() {
113         public VpnTransportInfo createFromParcel(Parcel in) {
114             return new VpnTransportInfo(in.readInt(), in.readString());
115         }
116         public VpnTransportInfo[] newArray(int size) {
117             return new VpnTransportInfo[size];
118         }
119     };
120 }
121