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.net.wifi.WifiConfiguration;
     21 import android.os.Bundle;
     22 
     23 import com.android.tv.settings.R;
     24 import com.android.tv.settings.form.FormPage;
     25 import com.android.tv.settings.form.FormPageResultListener;
     26 
     27 /**
     28  * Manual-style add wifi network (the kind you'd use for adding a hidden or out-of-range network.)
     29  */
     30 public class AddWifiNetworkActivity extends WifiMultiPagedFormActivity
     31         implements ConnectToWifiFragment.Listener, TimedMessageWizardFragment.Listener {
     32 
     33     private AdvancedWifiOptionsFlow mAdvancedWifiOptionsFlow;
     34     private WifiConfiguration mConfiguration;
     35     private WifiSecurity mWifiSecurity;
     36     private FormPage mSsidPage;
     37     private FormPage mSecurityPage;
     38     private FormPage mPasswordPage;
     39     private FormPage mConnectPage;
     40     private FormPage mSuccessPage;
     41 
     42     @Override
     43     protected void onCreate(Bundle savedInstanceState) {
     44         mConfiguration = new WifiConfiguration();
     45         mConfiguration.hiddenSSID = true;
     46         addPage(WifiFormPageType.ENTER_SSID);
     47         super.onCreate(savedInstanceState);
     48     }
     49 
     50     @Override
     51     public void onConnectToWifiCompleted(int reason) {
     52         Bundle result = new Bundle();
     53         result.putString(FormPage.DATA_KEY_SUMMARY_STRING, Integer.toString(reason));
     54         onBundlePageResult(mConnectPage, result);
     55     }
     56 
     57     @Override
     58     public void onTimedMessageCompleted() {
     59         Bundle result = new Bundle();
     60         result.putString(FormPage.DATA_KEY_SUMMARY_STRING, "");
     61         onBundlePageResult(mSuccessPage, result);
     62     }
     63 
     64     @Override
     65     protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) {
     66 
     67         switch (formPageType) {
     68             case ENTER_SSID:
     69                 mSsidPage = formPage;
     70                 String ssid = formPage.getDataSummary();
     71                 WifiConfigHelper.setConfigSsid(mConfiguration, ssid);
     72                 addPage(WifiFormPageType.CHOOSE_SECURITY);
     73                 break;
     74             case CHOOSE_SECURITY:
     75                 mSecurityPage = formPage;
     76                 if (choiceChosen(formPage, R.string.wifi_security_type_none)) {
     77                     mWifiSecurity = WifiSecurity.NONE;
     78                 } else if (choiceChosen(formPage, R.string.wifi_security_type_wep)) {
     79                     mWifiSecurity = WifiSecurity.WEP;
     80                 } else if (choiceChosen(formPage, R.string.wifi_security_type_wpa)) {
     81                     mWifiSecurity = WifiSecurity.PSK;
     82                 } else if (choiceChosen(formPage, R.string.wifi_security_type_eap)) {
     83                     mWifiSecurity = WifiSecurity.EAP;
     84                 }
     85                 WifiConfigHelper.setConfigKeyManagementBySecurity(mConfiguration, mWifiSecurity);
     86                 if (mWifiSecurity == WifiSecurity.NONE) {
     87                     optionsOrConnect();
     88                 } else {
     89                     addPage(WifiFormPageType.ENTER_PASSWORD);
     90                 }
     91                 break;
     92             case ENTER_PASSWORD:
     93                 mPasswordPage = formPage;
     94                 String password = formPage.getDataSummary();
     95                 setWifiConfigurationPassword(mConfiguration, mWifiSecurity, password);
     96                 optionsOrConnect();
     97                 break;
     98             case CONNECT:
     99                 WifiConfigHelper.saveConfiguration(this, mConfiguration);
    100                 switch (Integer.valueOf(formPage.getDataSummary())) {
    101                     case ConnectToWifiFragment.RESULT_REJECTED_BY_AP:
    102                         addPage(WifiFormPageType.CONNECT_REJECTED_BY_AP);
    103                         break;
    104                     case ConnectToWifiFragment.RESULT_UNKNOWN_ERROR:
    105                         addPage(WifiFormPageType.CONNECT_FAILED);
    106                         break;
    107                     case ConnectToWifiFragment.RESULT_TIMEOUT:
    108                         addPage(WifiFormPageType.CONNECT_TIMEOUT);
    109                         break;
    110                     case ConnectToWifiFragment.RESULT_BAD_AUTHENTICATION:
    111                         addPage(WifiFormPageType.CONNECT_AUTHENTICATION_FAILURE);
    112                         break;
    113                     case ConnectToWifiFragment.RESULT_SUCCESS:
    114                         addPage(WifiFormPageType.SUCCESS);
    115                         break;
    116                     default:
    117                         break;
    118                 }
    119                 break;
    120             case CONNECT_FAILED:
    121                 // fall through
    122             case CONNECT_TIMEOUT:
    123                 mAdvancedWifiOptionsFlow = new AdvancedWifiOptionsFlow(this, this, true, null);
    124                 // fall through
    125             case CONNECT_REJECTED_BY_AP:
    126                 if (choiceChosen(formPage, R.string.wifi_action_try_again)) {
    127                     optionsOrConnect();
    128                 }
    129                 break;
    130             case CONNECT_AUTHENTICATION_FAILURE:
    131                 if (choiceChosen(formPage, R.string.wifi_action_try_again)) {
    132                     if (mAdvancedWifiOptionsFlow != null) {
    133                         addPage(mAdvancedWifiOptionsFlow.getInitialPage());
    134                     } else {
    135                         addPage(WifiFormPageType.ENTER_SSID);
    136                     }
    137                 }
    138                 break;
    139             default:
    140                 if (mAdvancedWifiOptionsFlow != null) {
    141                     switch (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage)) {
    142                         case AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE:
    143                             connect();
    144                             break;
    145                         case AdvancedWifiOptionsFlow.RESULT_UNKNOWN_PAGE:
    146                         case AdvancedWifiOptionsFlow.RESULT_PAGE_HANDLED:
    147                         default:
    148                             break;
    149                     }
    150                 }
    151                 break;
    152         }
    153         return true;
    154     }
    155 
    156     @Override
    157     protected void displayPage(FormPage formPage, FormPageResultListener listener, boolean forward) {
    158         WifiFormPageType formPageType = getFormPageType(formPage);
    159 
    160         if (formPageType == WifiFormPageType.CONNECT) {
    161             mConnectPage = formPage;
    162             Fragment fragment = ConnectToWifiFragment.newInstance(
    163                     getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableSsid()),
    164                     true, mConfiguration);
    165             displayFragment(fragment, forward);
    166         } else if (formPageType == WifiFormPageType.SUCCESS) {
    167             mSuccessPage = formPage;
    168             Fragment fragment = TimedMessageWizardFragment.newInstance(
    169                     getString(formPageType.getTitleResourceId()));
    170             displayFragment(fragment, forward);
    171         } else {
    172             displayPage(formPageType, mConfiguration.getPrintableSsid(), null, null,
    173                     getLastPage(formPageType), null, formPageType != WifiFormPageType.SUCCESS,
    174                     formPage, listener, forward, (mAdvancedWifiOptionsFlow != null) ?
    175                             mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType) : false);
    176         }
    177     }
    178 
    179     private FormPage getLastPage(WifiFormPageType formPageType) {
    180         switch (formPageType) {
    181             case CHOOSE_SECURITY:
    182                 return mSecurityPage;
    183             case ENTER_PASSWORD:
    184                 return mPasswordPage;
    185             case ENTER_SSID:
    186                 return mSsidPage;
    187             default:
    188                 return (mAdvancedWifiOptionsFlow != null) ? mAdvancedWifiOptionsFlow
    189                         .getPreviousPage(formPageType)
    190                         : null;
    191         }
    192     }
    193 
    194     private void connect() {
    195         if (mAdvancedWifiOptionsFlow != null) {
    196             mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration);
    197         }
    198         addPage(WifiFormPageType.CONNECT);
    199     }
    200 
    201     private void optionsOrConnect() {
    202         if (mAdvancedWifiOptionsFlow != null) {
    203             addPage(mAdvancedWifiOptionsFlow.getInitialPage());
    204         } else {
    205             connect();
    206         }
    207     }
    208 }
    209