1 /*
2  * Copyright (C) 2015 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 
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.Objects;
30 
31 /**
32  * A lightweight container used to carry information on the networks that underly a given
33  * virtual network.
34  *
35  * @hide
36  */
37 @SystemApi(client = MODULE_LIBRARIES)
38 public final class UnderlyingNetworkInfo implements Parcelable {
39     /** The owner of this network. */
40     private final int mOwnerUid;
41 
42     /** The interface name of this network. */
43     @NonNull
44     private final String mIface;
45 
46     /** The names of the interfaces underlying this network. */
47     @NonNull
48     private final List<String> mUnderlyingIfaces;
49 
UnderlyingNetworkInfo(int ownerUid, @NonNull String iface, @NonNull List<String> underlyingIfaces)50     public UnderlyingNetworkInfo(int ownerUid, @NonNull String iface,
51             @NonNull List<String> underlyingIfaces) {
52         Objects.requireNonNull(iface);
53         Objects.requireNonNull(underlyingIfaces);
54         mOwnerUid = ownerUid;
55         mIface = iface;
56         mUnderlyingIfaces = Collections.unmodifiableList(new ArrayList<>(underlyingIfaces));
57     }
58 
UnderlyingNetworkInfo(@onNull Parcel in)59     private UnderlyingNetworkInfo(@NonNull Parcel in) {
60         mOwnerUid = in.readInt();
61         mIface = in.readString();
62         List<String> underlyingIfaces = new ArrayList<>();
63         in.readList(underlyingIfaces, null /*classLoader*/, java.lang.String.class);
64         mUnderlyingIfaces = Collections.unmodifiableList(underlyingIfaces);
65     }
66 
67     /** Get the owner of this network. */
getOwnerUid()68     public int getOwnerUid() {
69         return mOwnerUid;
70     }
71 
72     /** Get the interface name of this network. */
73     @NonNull
getInterface()74     public String getInterface() {
75         return mIface;
76     }
77 
78     /** Get the names of the interfaces underlying this network. */
79     @NonNull
getUnderlyingInterfaces()80     public List<String> getUnderlyingInterfaces() {
81         return mUnderlyingIfaces;
82     }
83 
84     @Override
toString()85     public String toString() {
86         return "UnderlyingNetworkInfo{"
87                 + "ownerUid=" + mOwnerUid
88                 + ", iface='" + mIface + '\''
89                 + ", underlyingIfaces='" + mUnderlyingIfaces.toString() + '\''
90                 + '}';
91     }
92 
93     @Override
describeContents()94     public int describeContents() {
95         return 0;
96     }
97 
98     @Override
writeToParcel(@onNull Parcel dest, int flags)99     public void writeToParcel(@NonNull Parcel dest, int flags) {
100         dest.writeInt(mOwnerUid);
101         dest.writeString(mIface);
102         dest.writeList(mUnderlyingIfaces);
103     }
104 
105     @NonNull
106     public static final Parcelable.Creator<UnderlyingNetworkInfo> CREATOR =
107             new Parcelable.Creator<UnderlyingNetworkInfo>() {
108         @NonNull
109         @Override
110         public UnderlyingNetworkInfo createFromParcel(@NonNull Parcel in) {
111             return new UnderlyingNetworkInfo(in);
112         }
113 
114         @NonNull
115         @Override
116         public UnderlyingNetworkInfo[] newArray(int size) {
117             return new UnderlyingNetworkInfo[size];
118         }
119     };
120 
121     @Override
equals(Object o)122     public boolean equals(Object o) {
123         if (this == o) return true;
124         if (!(o instanceof UnderlyingNetworkInfo)) return false;
125         final UnderlyingNetworkInfo that = (UnderlyingNetworkInfo) o;
126         return mOwnerUid == that.getOwnerUid()
127                 && Objects.equals(mIface, that.getInterface())
128                 && Objects.equals(mUnderlyingIfaces, that.getUnderlyingInterfaces());
129     }
130 
131     @Override
hashCode()132     public int hashCode() {
133         return Objects.hash(mOwnerUid, mIface, mUnderlyingIfaces);
134     }
135 }
136