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