1 /*
2  * Copyright (C) 2017 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.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 /**
25  * MatchAllNetworkSpecifier is a marker class used by NetworkFactory classes to indicate
26  * that they accept (match) any network specifier in requests.
27  *
28  * The class must never be used as part of a network request (those semantics aren't specified).
29  *
30  * @hide
31  */
32 @SystemApi
33 public final class MatchAllNetworkSpecifier extends NetworkSpecifier implements Parcelable {
34     /**
35      * Utility method which verifies that the ns argument is not a MatchAllNetworkSpecifier and
36      * throws an IllegalArgumentException if it is.
37      * @hide
38      */
checkNotMatchAllNetworkSpecifier(NetworkSpecifier ns)39     public static void checkNotMatchAllNetworkSpecifier(NetworkSpecifier ns) {
40         if (ns instanceof MatchAllNetworkSpecifier) {
41             throw new IllegalArgumentException("A MatchAllNetworkSpecifier is not permitted");
42         }
43     }
44 
45     /** @hide */
46     @Override
canBeSatisfiedBy(NetworkSpecifier other)47     public boolean canBeSatisfiedBy(NetworkSpecifier other) {
48         /*
49          * The method is called by a NetworkRequest to see if it is satisfied by a proposed
50          * network (e.g. as offered by a network factory). Since MatchAllNetweorkSpecifier must
51          * not be used in network requests this method should never be called.
52          */
53         throw new IllegalStateException(
54                 "MatchAllNetworkSpecifier must not be used in NetworkRequests");
55     }
56 
57     @Override
equals(Object o)58     public boolean equals(Object o) {
59         return o instanceof MatchAllNetworkSpecifier;
60     }
61 
62     @Override
hashCode()63     public int hashCode() {
64         return 0;
65     }
66 
67     @Override
describeContents()68     public int describeContents() {
69         return 0;
70     }
71 
72     @Override
writeToParcel(@onNull Parcel dest, int flags)73     public void writeToParcel(@NonNull Parcel dest, int flags) {
74         // Nothing to write.
75     }
76 
77     public static final @NonNull Parcelable.Creator<MatchAllNetworkSpecifier> CREATOR =
78             new Parcelable.Creator<MatchAllNetworkSpecifier>() {
79         public MatchAllNetworkSpecifier createFromParcel(Parcel in) {
80             return new MatchAllNetworkSpecifier();
81         }
82         public MatchAllNetworkSpecifier[] newArray(int size) {
83             return new MatchAllNetworkSpecifier[size];
84         }
85     };
86 }
87