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 android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.text.TextUtils;
25 
26 import java.util.Objects;
27 
28 /**
29  * A {@link NetworkSpecifier} used to identify test interfaces.
30  *
31  * @see TestNetworkManager
32  * @hide
33  */
34 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
35 public final class TestNetworkSpecifier extends NetworkSpecifier implements Parcelable {
36 
37     /**
38      * Name of the network interface.
39      */
40     @NonNull
41     private final String mInterfaceName;
42 
43     public TestNetworkSpecifier(@NonNull String interfaceName) {
44         if (TextUtils.isEmpty(interfaceName)) {
45             throw new IllegalArgumentException("Empty interfaceName");
46         }
47         mInterfaceName = interfaceName;
48     }
49 
50     // This may be null in the future to support specifiers based on data other than the interface
51     // name.
52     @Nullable
53     public String getInterfaceName() {
54         return mInterfaceName;
55     }
56 
57     @Override
58     public boolean canBeSatisfiedBy(@Nullable NetworkSpecifier other) {
59         return equals(other);
60     }
61 
62     @Override
63     public boolean equals(Object o) {
64         if (!(o instanceof TestNetworkSpecifier)) return false;
65         return TextUtils.equals(mInterfaceName, ((TestNetworkSpecifier) o).mInterfaceName);
66     }
67 
68     @Override
69     public int hashCode() {
70         return Objects.hashCode(mInterfaceName);
71     }
72 
73     @Override
74     public String toString() {
75         return "TestNetworkSpecifier (" + mInterfaceName + ")";
76     }
77 
78     @Override
79     public int describeContents() {
80         return 0;
81     }
82 
83     @Override
84     public void writeToParcel(@NonNull Parcel dest, int flags) {
85         dest.writeString(mInterfaceName);
86     }
87 
88     public static final @NonNull Creator<TestNetworkSpecifier> CREATOR =
89             new Creator<TestNetworkSpecifier>() {
90         public TestNetworkSpecifier createFromParcel(Parcel in) {
91             return new TestNetworkSpecifier(in.readString());
92         }
93         public TestNetworkSpecifier[] newArray(int size) {
94             return new TestNetworkSpecifier[size];
95         }
96     };
97 }
98