Home | History | Annotate | Download | only in connectivity
      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 
     17 package com.android.tv.settings.connectivity;
     18 
     19 import android.app.Fragment;
     20 import android.content.Intent;
     21 import android.net.wifi.WifiConfiguration;
     22 import android.os.Bundle;
     23 
     24 import com.android.tv.settings.R;
     25 import com.android.tv.settings.connectivity.FormPageDisplayer.FormPageInfo;
     26 import com.android.tv.settings.connectivity.FormPageDisplayer.UserActivityListener;
     27 import com.android.tv.settings.connectivity.setup.PasswordInputWizardFragment;
     28 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment;
     29 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment.ListItem;
     30 import com.android.tv.settings.connectivity.setup.TextInputWizardFragment;
     31 import com.android.tv.settings.form.FormPage;
     32 import com.android.tv.settings.form.FormPageResultListener;
     33 import com.android.tv.settings.form.FormResultListener;
     34 import com.android.tv.settings.form.MultiPagedForm;
     35 
     36 import java.util.ArrayList;
     37 
     38 /**
     39  * Common functionality for wifi multipaged forms.
     40  */
     41 public abstract class WifiMultiPagedFormActivity extends MultiPagedForm
     42         implements TextInputWizardFragment.Listener, SelectFromListWizardFragment.Listener,
     43         AdvancedWifiOptionsFlow.PageHandler, PasswordInputWizardFragment.Listener {
     44 
     45     @Override
     46     protected abstract void displayPage(FormPage formPage, FormPageResultListener listener,
     47             boolean forward);
     48 
     49     protected abstract boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage);
     50 
     51     private FormPageDisplayer mFormPageDisplayer;
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         setLayoutProperties(R.layout.setup_auth_activity, R.id.description, R.id.action);
     56         mFormPageDisplayer = new FormPageDisplayer(this, getFragmentManager(), R.id.content);
     57         super.onCreate(savedInstanceState);
     58     }
     59 
     60     @Override
     61     public boolean onTextInputComplete(String text) {
     62         return mFormPageDisplayer.onTextInputComplete(text);
     63     }
     64 
     65     @Override
     66     public boolean onPasswordInputComplete(String text, boolean obfuscate) {
     67         return mFormPageDisplayer.onPasswordInputComplete(text, obfuscate);
     68     }
     69 
     70     @Override
     71     public void onListSelectionComplete(ListItem listItem) {
     72         mFormPageDisplayer.onListSelectionComplete(listItem);
     73     }
     74 
     75     @Override
     76     public void onListFocusChanged(ListItem listItem) {
     77         mFormPageDisplayer.onListFocusChanged(listItem);
     78     }
     79 
     80     @Override
     81     public void addPage(WifiFormPageType formPageType) {
     82         addPage(formPageType.create());
     83     }
     84 
     85     @Override
     86     public void removePage(FormPage formPage) {
     87         super.removePage(formPage);
     88     }
     89 
     90     @Override
     91     public boolean choiceChosen(FormPage formPage, int choiceResourceId) {
     92         return getString(choiceResourceId).equals(formPage.getDataSummary());
     93     }
     94 
     95     @Override
     96     protected void displayFormResults(ArrayList<FormPage> formPages, FormResultListener listener) {
     97         // Don't need to display anything, just exit.
     98         finish();
     99     }
    100 
    101     @Override
    102     protected void onComplete(ArrayList<FormPage> formPages) {
    103         // We should never reach this point.
    104     }
    105 
    106     @Override
    107     protected void onCancel(ArrayList<FormPage> formPages) {
    108         // We should never reach this point.
    109     }
    110 
    111     @Override
    112     protected boolean onPageComplete(FormPage formPage) {
    113         WifiFormPageType formPageType = getFormPageType(formPage);
    114 
    115         // Always clear future pages.
    116         clearAfter(formPage);
    117 
    118         // Always clear loading pages.
    119         if (formPageType.getDisplayType() == FormPageDisplayer.DISPLAY_TYPE_LOADING) {
    120             removePage(formPage);
    121         }
    122 
    123         return onPageComplete(formPageType, formPage);
    124     }
    125 
    126     protected Fragment displayPage(FormPageInfo formPageInfo, String titleArgument,
    127             String descriptionArgument,
    128             ArrayList<SelectFromListWizardFragment.ListItem> extraChoices,
    129             FormPage previousFormPage, UserActivityListener userActivityListener,
    130             boolean showProgress, FormPage currentFormPage,
    131             FormPageResultListener formPageResultListener, boolean forward, boolean emptyAllowed) {
    132         return mFormPageDisplayer.displayPage(formPageInfo, titleArgument, descriptionArgument,
    133                 extraChoices, previousFormPage, userActivityListener, showProgress, currentFormPage,
    134                 formPageResultListener, forward, emptyAllowed);
    135     }
    136 
    137     protected SelectFromListWizardFragment.ListItem getListItem(FormPage formPage) {
    138         return mFormPageDisplayer.getListItem(formPage);
    139     }
    140 
    141     protected void setWifiConfigurationPassword(
    142             WifiConfiguration wifiConfiguration, WifiSecurity wifiSecurity, String password) {
    143         if (wifiSecurity == WifiSecurity.WEP) {
    144             int length = password.length();
    145             // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
    146             if ((length == 10 || length == 26 || length == 58)
    147                     && password.matches("[0-9A-Fa-f]*")) {
    148                 wifiConfiguration.wepKeys[0] = password;
    149             } else {
    150                 wifiConfiguration.wepKeys[0] = '"' + password + '"';
    151             }
    152         } else {
    153             if (password.matches("[0-9A-Fa-f]{64}")) {
    154                 wifiConfiguration.preSharedKey = password;
    155             } else {
    156                 wifiConfiguration.preSharedKey = '"' + password + '"';
    157             }
    158         }
    159     }
    160 
    161     protected WifiFormPageType getFormPageType(FormPage formPage) {
    162         return WifiFormPageType.valueOf(formPage.getTitle());
    163     }
    164 
    165     protected void addPage(WifiFormPageType formPageType, Intent intent) {
    166         addPage(formPageType.create(intent));
    167     }
    168 
    169     protected void displayFragment(Fragment fragment, boolean forward) {
    170         mFormPageDisplayer.displayFragment(fragment, forward);
    171     }
    172 }
    173