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