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.wifi.hotspot2;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.graphics.drawable.Icon;
23 import android.net.Uri;
24 import android.net.wifi.WifiSsid;
25 import android.os.Bundle;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 import android.text.TextUtils;
29 
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Locale;
34 import java.util.Map;
35 import java.util.Objects;
36 
37 /**
38  * Contained information for a Hotspot 2.0 OSU (Online Sign-Up provider).
39  *
40  * @hide
41  */
42 @SystemApi
43 public final class OsuProvider implements Parcelable {
44     /**
45      * OSU (Online Sign-Up) method: OMA DM (Open Mobile Alliance Device Management).
46      * For more info, refer to Section 8.3 of the Hotspot 2.0 Release 2 Technical Specification.
47      * @hide
48      */
49     public static final int METHOD_OMA_DM = 0;
50 
51     /**
52      * OSU (Online Sign-Up) method: SOAP XML SPP (Subscription Provisioning Protocol).
53      * For more info, refer to Section 8.4 of the Hotspot 2.0 Release 2 Technical Specification.
54      * @hide
55      */
56     public static final int METHOD_SOAP_XML_SPP = 1;
57 
58     /**
59      * SSID of the network to connect for service sign-up.
60      */
61     private WifiSsid mOsuSsid;
62 
63     /**
64      * Map of friendly names expressed as different language for the OSU provider.
65      */
66     private final Map<String, String> mFriendlyNames;
67 
68     /**
69      * Description of the OSU provider.
70      */
71     private final String mServiceDescription;
72 
73     /**
74      * URI to browse to for service sign-up.
75      */
76     private final Uri mServerUri;
77 
78     /**
79      * Network Access Identifier used for authenticating with the OSU network when OSEN is used.
80      */
81     private final String mNetworkAccessIdentifier;
82 
83     /**
84      * List of OSU (Online Sign-Up) method supported.
85      */
86     private final List<Integer> mMethodList;
87 
88     /**
89      * Icon data for the OSU (Online Sign-Up) provider.
90      */
91     private final Icon mIcon;
92 
93     /** @hide */
OsuProvider(WifiSsid osuSsid, Map<String, String> friendlyNames, String serviceDescription, Uri serverUri, String nai, List<Integer> methodList, Icon icon)94     public OsuProvider(WifiSsid osuSsid, Map<String, String> friendlyNames,
95             String serviceDescription, Uri serverUri, String nai, List<Integer> methodList,
96             Icon icon) {
97         mOsuSsid = osuSsid;
98         mFriendlyNames = friendlyNames;
99         mServiceDescription = serviceDescription;
100         mServerUri = serverUri;
101         mNetworkAccessIdentifier = nai;
102         if (methodList == null) {
103             mMethodList = new ArrayList<>();
104         } else {
105             mMethodList = new ArrayList<>(methodList);
106         }
107         mIcon = icon;
108     }
109 
110     /**
111      * Copy constructor.
112      *
113      * @param source The source to copy from
114      * @hide
115      */
OsuProvider(OsuProvider source)116     public OsuProvider(OsuProvider source) {
117         if (source == null) {
118             mOsuSsid = null;
119             mFriendlyNames = null;
120             mServiceDescription = null;
121             mServerUri = null;
122             mNetworkAccessIdentifier = null;
123             mMethodList = new ArrayList<>();
124             mIcon = null;
125             return;
126         }
127 
128         mOsuSsid = source.mOsuSsid;
129         mFriendlyNames = source.mFriendlyNames;
130         mServiceDescription = source.mServiceDescription;
131         mServerUri = source.mServerUri;
132         mNetworkAccessIdentifier = source.mNetworkAccessIdentifier;
133         if (source.mMethodList == null) {
134             mMethodList = new ArrayList<>();
135         } else {
136             mMethodList = new ArrayList<>(source.mMethodList);
137         }
138         mIcon = source.mIcon;
139     }
140 
141     /** @hide */
getOsuSsid()142     public WifiSsid getOsuSsid() {
143         return mOsuSsid;
144     }
145 
146     /** @hide */
setOsuSsid(WifiSsid osuSsid)147     public void setOsuSsid(WifiSsid osuSsid) {
148         mOsuSsid = osuSsid;
149     }
150 
151     /**
152      * Return the friendly Name for current language from the list of friendly names of OSU
153      * provider.
154      *
155      * The string matching the default locale will be returned if it is found, otherwise the string
156      * in english or the first string in the list will be returned if english is not found.
157      * A null will be returned if the list is empty.
158      *
159      * @return String matching the default locale, null otherwise
160      */
getFriendlyName()161     public @Nullable String getFriendlyName() {
162         if (mFriendlyNames == null || mFriendlyNames.isEmpty()) return null;
163         String lang = Locale.getDefault().getLanguage();
164         String friendlyName = mFriendlyNames.get(lang);
165         if (friendlyName != null) {
166             return friendlyName;
167         }
168         friendlyName = mFriendlyNames.get("en");
169         if (friendlyName != null) {
170             return friendlyName;
171         }
172         return mFriendlyNames.get(mFriendlyNames.keySet().stream().findFirst().get());
173     }
174 
175     /** @hide */
getFriendlyNameList()176     public Map<String, String> getFriendlyNameList() {
177         return mFriendlyNames;
178     }
179 
180     /** @hide */
getServiceDescription()181     public String getServiceDescription() {
182         return mServiceDescription;
183     }
184 
getServerUri()185     public @Nullable Uri getServerUri() {
186         return mServerUri;
187     }
188 
189     /** @hide */
getNetworkAccessIdentifier()190     public String getNetworkAccessIdentifier() {
191         return mNetworkAccessIdentifier;
192     }
193 
194     /** @hide */
getMethodList()195     public List<Integer> getMethodList() {
196         return mMethodList;
197     }
198 
199     /** @hide */
getIcon()200     public Icon getIcon() {
201         return mIcon;
202     }
203 
204     @Override
describeContents()205     public int describeContents() {
206         return 0;
207     }
208 
209     @Override
writeToParcel(Parcel dest, int flags)210     public void writeToParcel(Parcel dest, int flags) {
211         dest.writeParcelable(mOsuSsid, flags);
212         dest.writeString(mServiceDescription);
213         dest.writeParcelable(mServerUri, flags);
214         dest.writeString(mNetworkAccessIdentifier);
215         dest.writeList(mMethodList);
216         dest.writeParcelable(mIcon, flags);
217         Bundle bundle = new Bundle();
218         bundle.putSerializable("friendlyNameMap", (HashMap<String, String>) mFriendlyNames);
219         dest.writeBundle(bundle);
220     }
221 
222     @Override
equals(@ullable Object thatObject)223     public boolean equals(@Nullable Object thatObject) {
224         if (this == thatObject) {
225             return true;
226         }
227         if (!(thatObject instanceof OsuProvider)) {
228             return false;
229         }
230         OsuProvider that = (OsuProvider) thatObject;
231         return (mOsuSsid == null ? that.mOsuSsid == null : mOsuSsid.equals(that.mOsuSsid))
232                 && (mFriendlyNames == null) ? that.mFriendlyNames == null
233                             : mFriendlyNames.equals(that.mFriendlyNames)
234                 && TextUtils.equals(mServiceDescription, that.mServiceDescription)
235                 && (mServerUri == null ? that.mServerUri == null
236                             : mServerUri.equals(that.mServerUri))
237                 && TextUtils.equals(mNetworkAccessIdentifier, that.mNetworkAccessIdentifier)
238                 && (mMethodList == null ? that.mMethodList == null
239                             : mMethodList.equals(that.mMethodList))
240                 && (mIcon == null ? that.mIcon == null : mIcon.sameAs(that.mIcon));
241     }
242 
243     @Override
hashCode()244     public int hashCode() {
245         // mIcon is not hashable, skip the variable.
246         return Objects.hash(mOsuSsid, mServiceDescription, mFriendlyNames,
247                 mServerUri, mNetworkAccessIdentifier, mMethodList);
248     }
249 
250     @NonNull
251     @Override
toString()252     public String toString() {
253         return "OsuProvider{mOsuSsid=" + mOsuSsid
254                 + " mFriendlyNames=" + mFriendlyNames
255                 + " mServiceDescription=" + mServiceDescription
256                 + " mServerUri=" + mServerUri
257                 + " mNetworkAccessIdentifier=" + mNetworkAccessIdentifier
258                 + " mMethodList=" + mMethodList
259                 + " mIcon=" + mIcon;
260     }
261 
262     public static final @android.annotation.NonNull Creator<OsuProvider> CREATOR =
263             new Creator<OsuProvider>() {
264                 @Override
265                 public OsuProvider createFromParcel(Parcel in) {
266                     WifiSsid osuSsid = in.readParcelable(null);
267                     String serviceDescription = in.readString();
268                     Uri serverUri = in.readParcelable(null);
269                     String nai = in.readString();
270                     List<Integer> methodList = new ArrayList<>();
271                     in.readList(methodList, null);
272                     Icon icon = in.readParcelable(null);
273                     Bundle bundle = in.readBundle();
274                     Map<String, String> friendlyNamesMap = (HashMap) bundle.getSerializable(
275                             "friendlyNameMap");
276                     return new OsuProvider(osuSsid, friendlyNamesMap, serviceDescription,
277                             serverUri, nai, methodList, icon);
278                 }
279 
280             @Override
281             public OsuProvider[] newArray(int size) {
282                 return new OsuProvider[size];
283             }
284         };
285 }
286