1/*
2 * Copyright (C) 2014 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
17package com.android.tv.settings.connectivity;
18
19import com.android.tv.settings.R;
20import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment;
21import com.android.tv.settings.connectivity.setup.TextInputWizardFragment;
22import com.android.tv.settings.form.FormPage;
23
24import android.content.Context;
25import android.content.Intent;
26
27import java.util.ArrayList;
28
29/**
30 * Wifi form pages.
31 */
32public enum WifiFormPageType implements FormPageDisplayer.FormPageInfo {
33    CHOOSE_NETWORK(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE, R.string.title_select_wifi_network,
34            0, new int[] { R.string.other_network },
35            new int[] { R.drawable.ic_wifi_add}),
36    ENTER_SSID(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_ssid, 0,
37            TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS),
38    CHOOSE_SECURITY(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE, R.string.security_type, 0,
39            new int[] { R.string.wifi_security_type_none, R.string.wifi_security_type_wep,
40            R.string.wifi_security_type_wpa, R.string.wifi_security_type_eap }),
41    ENTER_PASSWORD(FormPageDisplayer.DISPLAY_TYPE_PSK_INPUT,
42            R.string.wifi_setup_input_password, 0, 0),
43    CONNECT(FormPageDisplayer.DISPLAY_TYPE_LOADING, R.string.wifi_connecting, 0),
44    CONNECT_FAILED(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
45            R.string.title_wifi_could_not_connect, 0, new int[] { R.string.wifi_action_try_again,
46            R.string.wifi_action_view_available_networks }),
47    CONNECT_TIMEOUT(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
48            R.string.title_wifi_could_not_connect_timeout, 0, new int[] {
49            R.string.wifi_action_try_again, R.string.wifi_action_view_available_networks }),
50    CONNECT_AUTHENTICATION_FAILURE(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
51            R.string.title_wifi_could_not_connect_authentication_failure, 0, new int[] {
52            R.string.wifi_action_try_again, R.string.wifi_action_view_available_networks }),
53    CONNECT_REJECTED_BY_AP(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
54            R.string.title_wifi_could_not_connect_ap_reject, 0, new int[] {
55            R.string.wifi_action_try_again, R.string.wifi_action_view_available_networks }),
56    SAVE(FormPageDisplayer.DISPLAY_TYPE_LOADING, R.string.wifi_saving, 0),
57    SAVE_FAILED(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
58            R.string.title_wifi_could_not_save, 0, new int[] { R.string.wifi_action_ok }),
59    SAVE_SUCCESS(FormPageDisplayer.DISPLAY_TYPE_LOADING, R.string.wifi_setup_save_success, 0),
60    SUCCESS(FormPageDisplayer.DISPLAY_TYPE_LOADING, R.string.wifi_setup_connection_success, 0),
61    SUMMARY_CONNECTED_WIFI(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
62            R.string.wifi_summary_title_connected,
63            R.string.wifi_summary_description_connected_to_wifi_network, new int[] {
64            R.string.wifi_action_dont_change_network, R.string.wifi_action_change_network }),
65    SUMMARY_CONNECTED_NON_WIFI(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
66            R.string.wifi_summary_title_connected, 0, new int[] { R.string.wifi_action_ok }),
67    SUMMARY_NOT_CONNECTED(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
68            R.string.wifi_summary_title_not_connected, 0, new int[] { R.string.wifi_action_ok }),
69    ADVANCED_OPTIONS(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
70            R.string.title_wifi_advanced_options, 0, new int[] { R.string.wifi_action_advanced_no,
71            R.string.wifi_action_advanced_yes }),
72    PROXY_SETTINGS(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE, R.string.title_wifi_proxy_settings,
73            R.string.proxy_warning_limited_support, new int[] { R.string.wifi_action_proxy_none,
74            R.string.wifi_action_proxy_manual }),
75    PROXY_HOSTNAME(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_proxy_hostname,
76            R.string.proxy_hostname_description, TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
77            R.string.proxy_hostname_hint),
78    PROXY_PORT(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_proxy_port,
79            R.string.proxy_port_description, TextInputWizardFragment.INPUT_TYPE_NUMERIC,
80            R.string.proxy_port_hint),
81    PROXY_BYPASS(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_proxy_bypass,
82            R.string.proxy_exclusionlist_description,
83            TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
84            R.string.proxy_exclusionlist_hint),
85    IP_SETTINGS(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE, R.string.title_wifi_ip_settings, 0,
86            new int[] { R.string.wifi_action_dhcp, R.string.wifi_action_static }),
87    IP_ADDRESS(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_ip_address,
88            R.string.wifi_ip_address_description, TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
89            R.string.wifi_ip_address_hint),
90    GATEWAY(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_gateway,
91            R.string.wifi_gateway_description, TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
92            R.string.wifi_gateway_hint),
93    NETWORK_PREFIX_LENGTH(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT,
94            R.string.title_wifi_network_prefix_length,
95            R.string.wifi_network_prefix_length_description,
96            TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
97            R.string.wifi_network_prefix_length_hint),
98    DNS1(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_dns1,
99            R.string.wifi_dns1_description, TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
100            R.string.wifi_dns1_hint),
101    DNS2(FormPageDisplayer.DISPLAY_TYPE_TEXT_INPUT, R.string.title_wifi_dns2,
102            R.string.wifi_dns2_description, TextInputWizardFragment.INPUT_TYPE_NO_SUGGESTIONS,
103            R.string.wifi_dns2_hint),
104    PROXY_SETTINGS_INVALID(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
105            R.string.title_wifi_proxy_settings_invalid, 0, new int[] {
106            R.string.wifi_action_try_again }),
107    IP_SETTINGS_INVALID(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE,
108            R.string.title_wifi_ip_settings_invalid, 0, new int[] {
109            R.string.wifi_action_try_again }),
110    KNOWN_NETWORK(FormPageDisplayer.DISPLAY_TYPE_LIST_CHOICE, R.string.title_wifi_known_network, 0,
111            new int[] { R.string.wifi_connect, R.string.wifi_forget_network }),
112    WPS(FormPageDisplayer.DISPLAY_TYPE_LOADING, 0, 0);
113
114    private final int mDisplayType;
115    private final int mTitleResource;
116    private final int mDescriptionResource;
117    private final int mInputType;
118    private final int[] mDefaultListItemTitles;
119    private final int[] mDefaultListItemIcons;
120    private final int mDefaultPrefillResource;
121
122    WifiFormPageType(int displayType, int titleResource, int descriptionResource) {
123        this(displayType, titleResource, descriptionResource,
124                TextInputWizardFragment.INPUT_TYPE_NORMAL);
125    }
126
127    WifiFormPageType(int displayType, int titleResource, int descriptionResource,
128            int textType) {
129        this(displayType, titleResource, descriptionResource, textType, 0);
130    }
131
132    WifiFormPageType(int displayType, int titleResource, int descriptionResource,
133            int textType, int defaultPrefillResource) {
134        mDisplayType = displayType;
135        mTitleResource = titleResource;
136        mDescriptionResource = descriptionResource;
137        mInputType = textType;
138        mDefaultListItemTitles = null;
139        mDefaultListItemIcons = null;
140        mDefaultPrefillResource = defaultPrefillResource;
141    }
142
143    WifiFormPageType(int displayType, int titleResource, int descriptionResource,
144            int[] defaultListItemTitles) {
145        this(displayType, titleResource, descriptionResource, defaultListItemTitles, null);
146    }
147
148    WifiFormPageType(int displayType, int titleResource, int descriptionResource,
149            int[] defaultListItemTitles, int[] defaultListItemIcons) {
150        mDisplayType = displayType;
151        mTitleResource = titleResource;
152        mDescriptionResource = descriptionResource;
153        mInputType = TextInputWizardFragment.INPUT_TYPE_NORMAL;
154        mDefaultListItemTitles = defaultListItemTitles;
155        mDefaultListItemIcons = defaultListItemIcons;
156        if (mDefaultListItemTitles != null && mDefaultListItemIcons != null
157                && mDefaultListItemTitles.length != mDefaultListItemIcons.length) {
158            throw new IllegalArgumentException("Form page type " + name()
159                    + " had title and icon arrays that we'ren't the same length! "
160                    + "The title array had length " + mDefaultListItemTitles.length
161                    + " but the icon array had length " + mDefaultListItemIcons.length + "!");
162        }
163        mDefaultPrefillResource = 0;
164    }
165
166    @Override
167    public int getTitleResourceId() {
168        return mTitleResource;
169    }
170
171    @Override
172    public int getDescriptionResourceId() {
173        return mDescriptionResource;
174    }
175
176    @Override
177    public int getInputType() {
178        return mInputType;
179    }
180
181    @Override
182    public int getDisplayType() {
183        return mDisplayType;
184    }
185
186    @Override
187    public int getDefaultPrefillResourceId() {
188        return mDefaultPrefillResource;
189    }
190
191
192    @Override
193    public ArrayList<SelectFromListWizardFragment.ListItem> getChoices(
194            Context context, ArrayList<SelectFromListWizardFragment.ListItem> extraChoices) {
195        ArrayList<SelectFromListWizardFragment.ListItem> choices = new ArrayList<>();
196        if (extraChoices != null) {
197            choices.addAll(extraChoices);
198        }
199
200        if (mDefaultListItemTitles != null) {
201            // Find the largest priority of the items placed at the end of the list and place
202            // default items after.
203            int largestLastPriority = Integer.MIN_VALUE;
204            if (extraChoices != null) {
205                for (SelectFromListWizardFragment.ListItem item : extraChoices) {
206                    if (item.getPinnedPosition()
207                            == SelectFromListWizardFragment.PinnedListItem.LAST) {
208                        SelectFromListWizardFragment.PinnedListItem pinnedItem =
209                                (SelectFromListWizardFragment.PinnedListItem) item;
210                        largestLastPriority = java.lang.Math.max(
211                                largestLastPriority, pinnedItem.getPinnedPriority());
212                    }
213                }
214            }
215
216            for (int i = 0; i < mDefaultListItemTitles.length; i++) {
217                choices.add(new SelectFromListWizardFragment.PinnedListItem(
218                        context.getString(mDefaultListItemTitles[i]),
219                        mDefaultListItemIcons == null ? 0 : mDefaultListItemIcons[i],
220                        SelectFromListWizardFragment.PinnedListItem.LAST, i + largestLastPriority));
221            }
222        }
223        return choices;
224    }
225
226    public FormPage create() {
227        return FormPage.createTextInputForm(name());
228    }
229
230    public FormPage create(Intent intent) {
231        if (mDisplayType != FormPageDisplayer.DISPLAY_TYPE_LOADING) {
232            throw new IllegalArgumentException("Form page type " + name() + " had display type "
233                    + mDisplayType + " but " + FormPageDisplayer.DISPLAY_TYPE_LOADING
234                    + " expected!");
235        }
236        return FormPage.createIntentForm(name(), intent);
237    }
238}
239