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