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 com.android.tv.settings.connectivity.setup;
18 
19 import android.net.wifi.ScanResult;
20 import android.net.wifi.WifiConfiguration;
21 import android.text.TextUtils;
22 import android.util.ArrayMap;
23 
24 import androidx.annotation.IntDef;
25 import androidx.lifecycle.ViewModel;
26 
27 import com.android.wifitrackerlib.WifiEntry;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.util.HashMap;
32 import java.util.Map;
33 
34 /**
35  * Class that stores the user choice information for basic Wi-Fi flow.
36  */
37 public class UserChoiceInfo extends ViewModel {
38     public static final int SELECT_WIFI = 1;
39     public static final int PASSWORD = 2;
40     public static final int SECURITY = 3;
41     public static final int SSID = 4;
42     public static final int EAP_METHOD = 5;
43     public static final int PHASE_2_AUTHENTICATION = 6;
44     public static final int CA_CERTIFICATE = 7;
45     public static final int USER_CERT = 8;
46     public static final int DOMAIN = 9;
47     public static final int IDENTITY = 10;
48     public static final int ANONYMOUS_IDENTITY = 11;
49     Map<Integer, Boolean> mIsPageVisible = new ArrayMap<>();
50     private HashMap<Integer, String> mDataSummary = new HashMap<>();
51     private HashMap<Integer, Integer> mChoiceSummary = new HashMap<>();
52     private WifiConfiguration mWifiConfiguration = new WifiConfiguration();
53     private int mWifiSecurity;
54     private String mConnectedNetwork;
55     private boolean mIsPasswordHidden = true;
56     private ConnectionFailedStatus mConnectionFailedStatus;
57     private int mEasyConnectNetworkId = -1;
58     private WifiEntry wifiEntry;
59     private boolean mIsAlreadyConnected;
60 
61     /**
62      * Store the page summary into a HashMap.
63      *
64      * @param page The page as the key.
65      * @param info The info as the value.
66      */
put(@AGE int page, String info)67     public void put(@PAGE int page, String info) {
68         mDataSummary.put(page, info);
69     }
70 
put(@AGE int page, int choice)71     public void put(@PAGE int page, int choice) {
72         mChoiceSummary.put(page, choice);
73     }
74 
75     /**
76      * Check if the summary of the queried page matches with expected string.
77      *
78      * @param choice The expected string.
79      * @param page   The page queried.
80      * @return true if matched.
81      */
choiceChosen(CharSequence choice, @PAGE int page)82     public boolean choiceChosen(CharSequence choice, @PAGE int page) {
83         if (!mDataSummary.containsKey(page)) {
84             return false;
85         }
86         return TextUtils.equals(choice, mDataSummary.get(page));
87     }
88 
getChoice(@AGE int page)89     public Integer getChoice(@PAGE int page) {
90         if (!mChoiceSummary.containsKey(page)) {
91             return null;
92         }
93         return mChoiceSummary.get(page);
94     }
95 
96     /**
97      * Get summary of a page.
98      *
99      * @param page The queried page.
100      * @return The summary of the page.
101      */
getPageSummary(@AGE int page)102     public String getPageSummary(@PAGE int page) {
103         if (!mDataSummary.containsKey(page)) {
104             return null;
105         }
106         return mDataSummary.get(page);
107     }
108 
109     /**
110      * Remove the summary of a page.
111      *
112      * @param page The page.
113      */
removePageSummary(@AGE int page)114     public void removePageSummary(@PAGE int page) {
115         mDataSummary.remove(page);
116     }
117 
118     /**
119      * Get {@link ScanResult} of the chosen network.
120      */
121 
122     /**
123      * Get {@link WifiConfiguration}
124      */
getWifiConfiguration()125     public WifiConfiguration getWifiConfiguration() {
126         return mWifiConfiguration;
127     }
128 
129     /**
130      * Set {@link WifiConfiguration}
131      */
setWifiConfiguration(WifiConfiguration wifiConfiguration)132     public void setWifiConfiguration(WifiConfiguration wifiConfiguration) {
133         this.mWifiConfiguration = wifiConfiguration;
134     }
135 
136     /**
137      * Get WifiSecurity category. The category value is defined in
138      * {@link com.android.tv.settings.library.network.AccessPoint}
139      */
getWifiSecurity()140     public int getWifiSecurity() {
141         return mWifiSecurity;
142     }
143 
144     /**
145      * Set WifiSecurity
146      *
147      * @param wifiSecurity WifiSecurity category defined in
148      *                     {@link com.android.tv.settings.library.network.AccessPoint}.
149      */
setWifiSecurity(int wifiSecurity)150     public void setWifiSecurity(int wifiSecurity) {
151         this.mWifiSecurity = wifiSecurity;
152     }
153 
154     /**
155      * Get the SSID of the connected network.
156      *
157      * @return the SSID.
158      */
getConnectedNetwork()159     public String getConnectedNetwork() {
160         return mConnectedNetwork;
161     }
162 
163     /**
164      * Set the SSID of the connected network.
165      *
166      * @param connectedNetwork SSID of the network.
167      */
setConnectedNetwork(String connectedNetwork)168     public void setConnectedNetwork(String connectedNetwork) {
169         mConnectedNetwork = connectedNetwork;
170     }
171 
172     /**
173      * Determine whether the password is hidden.
174      *
175      * @return True if hidden.
176      */
isPasswordHidden()177     public boolean isPasswordHidden() {
178         return this.mIsPasswordHidden;
179     }
180 
181     /**
182      * Set whether the password is hidden.
183      *
184      * @param hidden true if hidden.
185      */
setPasswordHidden(boolean hidden)186     public void setPasswordHidden(boolean hidden) {
187         this.mIsPasswordHidden = hidden;
188     }
189 
getConnectionFailedStatus()190     public ConnectionFailedStatus getConnectionFailedStatus() {
191         return mConnectionFailedStatus;
192     }
193 
setConnectionFailedStatus(ConnectionFailedStatus status)194     public void setConnectionFailedStatus(ConnectionFailedStatus status) {
195         mConnectionFailedStatus = status;
196     }
197 
getWifiEntry()198     public WifiEntry getWifiEntry() {
199         return wifiEntry;
200     }
201 
setWifiEntry(WifiEntry wifiEntry)202     public void setWifiEntry(WifiEntry wifiEntry) {
203         this.wifiEntry = wifiEntry;
204     }
205 
206     /**
207      * Initialize all the information.
208      */
init()209     public void init() {
210         mDataSummary = new HashMap<>();
211         mWifiConfiguration = new WifiConfiguration();
212         mWifiSecurity = 0;
213         mIsPasswordHidden = true;
214     }
215 
setVisible(@AGE int page, boolean visible)216     public void setVisible(@PAGE int page, boolean visible) {
217         mIsPageVisible.put(page, visible);
218     }
219 
isVisible(@AGE int page)220     public boolean isVisible(@PAGE int page) {
221         if (!mIsPageVisible.containsKey(page)) {
222             return true;
223         }
224         return mIsPageVisible.get(page);
225     }
226 
getEasyConnectNetworkId()227     public int getEasyConnectNetworkId() {
228         return mEasyConnectNetworkId;
229     }
230 
setEasyConnectNetworkId(int easyConnectNetworkId)231     public void setEasyConnectNetworkId(int easyConnectNetworkId) {
232         mEasyConnectNetworkId = easyConnectNetworkId;
233     }
234 
isAlreadyConnected()235     public boolean isAlreadyConnected() {
236         return mIsAlreadyConnected;
237     }
238 
setIsAlreadyConnected(boolean isAlreadyConnected)239     public void setIsAlreadyConnected(boolean isAlreadyConnected) {
240         mIsAlreadyConnected = isAlreadyConnected;
241     }
242 
243     public enum ConnectionFailedStatus {
244         AUTHENTICATION,
245         REJECTED,
246         TIMEOUT,
247         UNKNOWN,
248         EASY_CONNECT_FAILURE,
249     }
250 
251     @IntDef({
252             SELECT_WIFI,
253             PASSWORD,
254             SECURITY,
255             SSID,
256             EAP_METHOD,
257             PHASE_2_AUTHENTICATION,
258             CA_CERTIFICATE,
259             USER_CERT,
260             DOMAIN,
261             IDENTITY,
262             ANONYMOUS_IDENTITY,
263     })
264     @Retention(RetentionPolicy.SOURCE)
265     public @interface PAGE {
266     }
267 }
268