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