1 /*
2  * Copyright (C) 2023 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.wifi.sharedconnectivity.app;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.SystemApi;
22 import android.os.Bundle;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 import java.util.Objects;
29 
30 /**
31  * The status of a connection to a known network after the client called
32  * {@link SharedConnectivityManager#connectKnownNetwork}.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class KnownNetworkConnectionStatus implements Parcelable {
38 
39     /**
40      * Connection status is unknown.
41      */
42     public static final int CONNECTION_STATUS_UNKNOWN  = 0;
43 
44     /**
45      * The connection's data was saved successfully in the Wi-Fi configuration.
46      */
47     public static final int CONNECTION_STATUS_SAVED  = 1;
48 
49     /**
50      * Failed to save the connection's data in the Wi-Fi configuration.
51      */
52     public static final int CONNECTION_STATUS_SAVE_FAILED = 2;
53 
54     /**
55      * @hide
56      */
57     @Retention(RetentionPolicy.SOURCE)
58     @IntDef({
59             CONNECTION_STATUS_UNKNOWN,
60             CONNECTION_STATUS_SAVED,
61             CONNECTION_STATUS_SAVE_FAILED,
62     })
63     public @interface ConnectionStatus {}
64 
65     @ConnectionStatus private final int mStatus;
66     private final KnownNetwork mKnownNetwork;
67     private final Bundle mExtras;
68 
69     /**
70      * Builder class for {@link KnownNetworkConnectionStatus}.
71      */
72     public static final class Builder {
73         @ConnectionStatus private int mStatus;
74         private KnownNetwork mKnownNetwork;
75         private Bundle mExtras = Bundle.EMPTY;
76 
Builder()77         public Builder() {}
78 
79         /**
80          * Sets the status of the connection
81          *
82          * @return Returns the Builder object.
83          */
84         @NonNull
setStatus(@onnectionStatus int status)85         public Builder setStatus(@ConnectionStatus int status) {
86             mStatus = status;
87             return this;
88         }
89 
90         /**
91          * Sets the {@link KnownNetwork} object of the connection.
92          *
93          * @return Returns the Builder object.
94          */
95         @NonNull
setKnownNetwork(@onNull KnownNetwork knownNetwork)96         public Builder setKnownNetwork(@NonNull KnownNetwork knownNetwork) {
97             mKnownNetwork = knownNetwork;
98             return this;
99         }
100 
101         /**
102          * Sets the extras bundle
103          *
104          * @return Returns the Builder object.
105          */
106         @NonNull
setExtras(@onNull Bundle extras)107         public Builder setExtras(@NonNull Bundle extras) {
108             mExtras = extras;
109             return this;
110         }
111 
112         /**
113          * Builds the {@link KnownNetworkConnectionStatus} object.
114          *
115          * @return Returns the built {@link KnownNetworkConnectionStatus} object.
116          */
117         @NonNull
build()118         public KnownNetworkConnectionStatus build() {
119             return new KnownNetworkConnectionStatus(mStatus, mKnownNetwork, mExtras);
120         }
121     }
122 
validate(@onnectionStatus int status)123     private static void validate(@ConnectionStatus int status) {
124         if (status != CONNECTION_STATUS_UNKNOWN && status != CONNECTION_STATUS_SAVED
125                 && status != CONNECTION_STATUS_SAVE_FAILED) {
126             throw new IllegalArgumentException("Illegal connection status");
127         }
128     }
129 
KnownNetworkConnectionStatus(@onnectionStatus int status, KnownNetwork knownNetwork, @NonNull Bundle extras)130     private KnownNetworkConnectionStatus(@ConnectionStatus int status, KnownNetwork knownNetwork,
131             @NonNull Bundle extras) {
132         validate(status);
133         mStatus = status;
134         mKnownNetwork = knownNetwork;
135         mExtras = extras;
136     }
137 
138     /**
139      * Gets the status of the connection
140      *
141      * @return Returns true for enabled, false otherwise.
142      */
143     @ConnectionStatus
getStatus()144     public int getStatus() {
145         return mStatus;
146     }
147 
148     /**
149      * Gets the {@link KnownNetwork} object of the connection.
150      *
151      * @return Returns a KnownNetwork object.
152      */
153     @NonNull
getKnownNetwork()154     public KnownNetwork getKnownNetwork() {
155         return mKnownNetwork;
156     }
157 
158     /**
159      * Gets the extras Bundle.
160      *
161      * @return Returns a Bundle object.
162      */
163     @NonNull
getExtras()164     public Bundle getExtras() {
165         return mExtras;
166     }
167 
168     @Override
equals(Object obj)169     public boolean equals(Object obj) {
170         if (!(obj instanceof KnownNetworkConnectionStatus)) return false;
171         KnownNetworkConnectionStatus other = (KnownNetworkConnectionStatus) obj;
172         return mStatus == other.getStatus()
173                 && Objects.equals(mKnownNetwork, other.getKnownNetwork());
174     }
175 
176     @Override
hashCode()177     public int hashCode() {
178         return Objects.hash(mStatus, mKnownNetwork);
179     }
180 
181     @Override
describeContents()182     public int describeContents() {
183         return 0;
184     }
185 
186     @Override
writeToParcel(@onNull Parcel dest, int flags)187     public void writeToParcel(@NonNull Parcel dest, int flags) {
188         dest.writeInt(mStatus);
189         mKnownNetwork.writeToParcel(dest, flags);
190         dest.writeBundle(mExtras);
191     }
192 
193     /**
194      * Creates a {@link KnownNetworkConnectionStatus} object from a parcel.
195      *
196      * @hide
197      */
198     @NonNull
readFromParcel(@onNull Parcel in)199     public static KnownNetworkConnectionStatus readFromParcel(@NonNull Parcel in) {
200         return new KnownNetworkConnectionStatus(in.readInt(),
201                 KnownNetwork.readFromParcel(in),
202                 in.readBundle());
203     }
204 
205     @NonNull
206     public static final Creator<KnownNetworkConnectionStatus> CREATOR = new Creator<>() {
207                 @Override
208                 public KnownNetworkConnectionStatus createFromParcel(Parcel in) {
209                     return readFromParcel(in);
210                 }
211 
212                 @Override
213                 public KnownNetworkConnectionStatus[] newArray(int size) {
214                     return new KnownNetworkConnectionStatus[size];
215                 }
216             };
217 
218     @Override
toString()219     public String toString() {
220         return new StringBuilder("KnownNetworkConnectionStatus[")
221                 .append("status=").append(mStatus)
222                 .append("known network=").append(mKnownNetwork.toString())
223                 .append("extras=").append(mExtras.toString())
224                 .append("]").toString();
225     }
226 }
227