1 /*
2  * Copyright (C) 2024 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;
18 
19 import android.annotation.FlaggedApi;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.wifi.flags.Flags;
27 
28 import java.util.Objects;
29 
30 /**
31  * Contains information extracted from URI
32  *
33  * @hide
34  */
35 @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
36 @SystemApi
37 public final class UriParserResults implements Parcelable {
38 
39     /**
40      * Return value for {@link #getUriScheme()} indicating that the URI contains
41      * a ZXing WiFi configuration.
42      */
43     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
44     public static final int URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG = 1;
45 
46     /**
47      * Return value for {@link #getUriScheme()} indicating that the URI contains
48      * a DPP (Easy Connect) configuration.
49      */
50     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
51     public static final int URI_SCHEME_DPP = 2;
52 
53     /**
54      * URI_SCHEME_DPP for standard Wi-Fi device provision protocol;
55      * URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG for ZXing reader library's Wi-Fi Network config format.
56      */
57     @WifiAnnotations.UriScheme private int mScheme;
58 
59     /** Public key from parsed Wi-Fi DPP URI, it is valid when mScheme is URI_SCHEME_DPP. */
60     @Nullable private String mPublicKey;
61 
62     /**
63      * Optional device specific information from the Wi-Fi DPP URI,
64      * it is valid when mScheme is URI_SCHEME_DPP
65      */
66     @Nullable private String mInformation;
67 
68     /**
69      * WifiConfiguration from parsed ZXing reader library's Wi-Fi Network config format. Valid or
70      * Not null when mScheme is URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG
71      */
72     @Nullable private WifiConfiguration mWifiConfig;
73 
74     /** @hide */
75     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
UriParserResults( int scheme, @Nullable String publicKey, @Nullable String information, @Nullable WifiConfiguration config)76     public UriParserResults(
77             int scheme,
78             @Nullable String publicKey,
79             @Nullable String information,
80             @Nullable WifiConfiguration config) {
81         mScheme = scheme;
82         mPublicKey = publicKey;
83         mInformation = information;
84         if (config != null) {
85             mWifiConfig = new WifiConfiguration(config);
86         }
87     }
88 
89     /**
90      * The scheme described by the URI.
91      *
92      * <p>URI_SCHEME_DPP for standard Wi-Fi device provision protocol.
93      * URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG for ZXing reader library's Wi-Fi Network config format.
94      */
95     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
96     @WifiAnnotations.UriScheme
getUriScheme()97     public int getUriScheme() {
98         return mScheme;
99     }
100 
101     /**
102      * The public key of the DPP (Wi-Fi Easy Connect).
103      *
104      * If {@code getUriScheme()} returns URI_SCHEME_DPP, this field contains the public key
105      * of the DPP (Wi-Fi Easy Connect). Otherwise, it is null.
106      */
107     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
108     @Nullable
getPublicKey()109     public String getPublicKey() {
110         return mPublicKey;
111     }
112 
113     /**
114      * The information of the DPP (Wi-Fi Easy Connect).
115      *
116      * If {@code getUriScheme()} returns URI_SCHEME_DPP, this field contains the information
117      * of the DPP (Wi-Fi Easy Connect). Otherwise, it is null.
118      */
119     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
120     @Nullable
getInformation()121     public String getInformation() {
122         return mInformation;
123     }
124 
125     /**
126      * The WifiConfiguration of the zxing wifi network.
127      *
128      * If {@code getUriScheme()} returns URI_SCHEME_ZXING_WIFI_NETWORK_CONFIG, this field contains
129      * the WifiConfiguration of the zxing wifi network. Otherwise, it is null.
130      */
131     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
132     @Nullable
getWifiConfiguration()133     public WifiConfiguration getWifiConfiguration() {
134         return mWifiConfig;
135     }
136 
137     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
138     @Override
139     /**
140      * Implement the Parcelable interface.
141      */
describeContents()142     public int describeContents() {
143         return 0;
144     }
145 
146     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
147     @Override
148     /**
149      * Implement the Parcelable interface.
150      */
writeToParcel(@onNull Parcel dest, int flags)151     public void writeToParcel(@NonNull Parcel dest, int flags) {
152         dest.writeInt(mScheme);
153         dest.writeString(mPublicKey);
154         dest.writeString(mInformation);
155         dest.writeParcelable(mWifiConfig, flags);
156     }
157 
158     @FlaggedApi(Flags.FLAG_ANDROID_V_WIFI_API)
159     @NonNull
160     /**
161      * Implement the Parcelable interface.
162      */
163     public static final Creator<UriParserResults> CREATOR =
164             new Creator<UriParserResults>() {
165                 public UriParserResults createFromParcel(Parcel in) {
166                     return new UriParserResults(
167                             in.readInt(),
168                             in.readString(),
169                             in.readString(),
170                             in.readParcelable(WifiConfiguration.class.getClassLoader()));
171                 }
172 
173                 public UriParserResults[] newArray(int size) {
174                     return new UriParserResults[size];
175                 }
176             };
177 
178     @NonNull
179     @Override
toString()180     public String toString() {
181         StringBuilder sbuf = new StringBuilder();
182         sbuf.append("UriParserResults{");
183         sbuf.append(", mScheme= ").append(mScheme);
184         sbuf.append(", mPublicKey= ").append(mPublicKey);
185         sbuf.append(", mInformation= ").append(mInformation);
186         if (mWifiConfig != null) sbuf.append(", mWifiConfig=").append(mWifiConfig.toString());
187         sbuf.append("}");
188         return sbuf.toString();
189     }
190 
191     @Override
equals(Object o)192     public boolean equals(Object o) {
193         if (this == o) return true;
194         if (!(o instanceof UriParserResults)) return false;
195         UriParserResults results = (UriParserResults) o;
196         return mScheme == results.mScheme
197                 && Objects.equals(mPublicKey, results.mPublicKey)
198                 && Objects.equals(mInformation, results.mInformation)
199                 && Objects.equals(
200                         mWifiConfig != null ? mWifiConfig.toString() : null,
201                         results.mWifiConfig != null ? results.mWifiConfig.toString() : null);
202     }
203 
204     @Override
hashCode()205     public int hashCode() {
206         return Objects.hash(
207                 mScheme,
208                 mPublicKey,
209                 mInformation,
210                 mWifiConfig != null ? mWifiConfig.toString() : null);
211     }
212 }
213