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 com.android.tv.settings.R; 20 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment; 21 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment.ListItem; 22 import com.android.tv.settings.form.FormPage; 23 import com.android.tv.settings.form.FormPageResultListener; 24 25 import android.app.Fragment; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.net.wifi.ScanResult; 29 import android.net.wifi.WifiConfiguration; 30 import android.net.wifi.WifiInfo; 31 import android.net.wifi.WifiManager; 32 import android.os.Bundle; 33 import android.text.TextUtils; 34 35 import java.net.InetAddress; 36 import java.util.ArrayList; 37 38 /** 39 * Add a wifi network where we already know the ssid/security; normal post-install settings. 40 */ 41 public class WifiConnectionActivity extends WifiMultiPagedFormActivity 42 implements ConnectToWifiFragment.Listener, TimedMessageWizardFragment.Listener { 43 44 private static final String EXTRA_WIFI_SSID = "wifi_ssid"; 45 private static final String EXTRA_WIFI_SECURITY_NAME = "wifi_security_name"; 46 private static final String EXTRA_WIFI_SCAN_RESULT = "scan_result"; 47 createIntent(Context context, String wifiSsid, WifiSecurity wifiSecurity)48 public static Intent createIntent(Context context, String wifiSsid, WifiSecurity wifiSecurity) { 49 return new Intent(context, WifiConnectionActivity.class) 50 .putExtra(EXTRA_WIFI_SSID, wifiSsid) 51 .putExtra(EXTRA_WIFI_SECURITY_NAME, wifiSecurity.name()); 52 } 53 createIntent(Context context, ScanResult result, WifiSecurity security)54 public static Intent createIntent(Context context, ScanResult result, WifiSecurity security) { 55 return new Intent(context, WifiConnectionActivity.class) 56 .putExtra(EXTRA_WIFI_SCAN_RESULT, result) 57 .putExtra(EXTRA_WIFI_SECURITY_NAME, security.name()); 58 } 59 60 private AdvancedWifiOptionsFlow mAdvancedWifiOptionsFlow; 61 private WifiConfiguration mConfiguration; 62 private WifiSecurity mWifiSecurity; 63 private FormPage mPasswordPage; 64 private FormPage mConnectPage; 65 private FormPage mSuccessPage; 66 67 @Override onCreate(Bundle savedInstanceState)68 protected void onCreate(Bundle savedInstanceState) { 69 mWifiSecurity = WifiSecurity.valueOf(getIntent().getStringExtra(EXTRA_WIFI_SECURITY_NAME)); 70 71 Bundle extras = getIntent().getExtras(); 72 if (extras.containsKey(EXTRA_WIFI_SCAN_RESULT)) { 73 mConfiguration = WifiConfigHelper.getConfigurationForNetwork( 74 this, (ScanResult) extras.getParcelable(EXTRA_WIFI_SCAN_RESULT)); 75 } else { 76 mConfiguration = WifiConfigHelper.getConfiguration( 77 this, getIntent().getStringExtra(EXTRA_WIFI_SSID), mWifiSecurity); 78 } 79 80 if (WifiConfigHelper.isNetworkSaved(mConfiguration)) { 81 addPage(WifiFormPageType.KNOWN_NETWORK); 82 } else { 83 addStartPage(); 84 } 85 super.onCreate(savedInstanceState); 86 } 87 88 @Override onConnectToWifiCompleted(int reason)89 public void onConnectToWifiCompleted(int reason) { 90 Bundle result = new Bundle(); 91 result.putString(FormPage.DATA_KEY_SUMMARY_STRING, Integer.toString(reason)); 92 onBundlePageResult(mConnectPage, result); 93 } 94 95 @Override onTimedMessageCompleted()96 public void onTimedMessageCompleted() { 97 Bundle result = new Bundle(); 98 result.putString(FormPage.DATA_KEY_SUMMARY_STRING, ""); 99 onBundlePageResult(mSuccessPage, result); 100 } 101 102 @Override onPageComplete(WifiFormPageType formPageType, FormPage formPage)103 protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) { 104 105 switch (formPageType) { 106 case KNOWN_NETWORK: 107 if (choiceChosen(formPage, R.string.wifi_connect)) { 108 addStartPage(); 109 } else if (choiceChosen(formPage, R.string.wifi_forget_network)) { 110 WifiConfigHelper.forgetConfiguration(this, mConfiguration); 111 setResult(RESULT_OK); 112 finish(); 113 } 114 break; 115 case ENTER_PASSWORD: 116 mPasswordPage = formPage; 117 String password = formPage.getDataSummary(); 118 setWifiConfigurationPassword(mConfiguration, mWifiSecurity, password); 119 optionsOrConnect(); 120 break; 121 case CONNECT: 122 switch (Integer.valueOf(formPage.getDataSummary())) { 123 case ConnectToWifiFragment.RESULT_REJECTED_BY_AP: 124 addPage(WifiFormPageType.CONNECT_REJECTED_BY_AP); 125 break; 126 case ConnectToWifiFragment.RESULT_UNKNOWN_ERROR: 127 addPage(WifiFormPageType.CONNECT_FAILED); 128 break; 129 case ConnectToWifiFragment.RESULT_TIMEOUT: 130 addPage(WifiFormPageType.CONNECT_TIMEOUT); 131 break; 132 case ConnectToWifiFragment.RESULT_BAD_AUTHENTICATION: 133 WifiConfigHelper.forgetConfiguration(this, mConfiguration); 134 addPage(WifiFormPageType.CONNECT_AUTHENTICATION_FAILURE); 135 break; 136 case ConnectToWifiFragment.RESULT_SUCCESS: 137 WifiConfigHelper.saveConfiguration(this, mConfiguration); 138 addPage(WifiFormPageType.SUCCESS); 139 break; 140 default: 141 break; 142 } 143 break; 144 case CONNECT_FAILED: 145 // Fall through 146 case CONNECT_TIMEOUT: 147 mAdvancedWifiOptionsFlow = new AdvancedWifiOptionsFlow(this, this, true, null); 148 // Fall through 149 case CONNECT_REJECTED_BY_AP: 150 if (choiceChosen(formPage, R.string.wifi_action_try_again)) { 151 clear(); 152 optionsOrConnect(); 153 } 154 break; 155 case CONNECT_AUTHENTICATION_FAILURE: 156 if (choiceChosen(formPage, R.string.wifi_action_try_again)) { 157 clear(); 158 if (mWifiSecurity.isOpen()) { 159 optionsOrConnect(); 160 } else { 161 addPage(WifiFormPageType.ENTER_PASSWORD); 162 } 163 } 164 break; 165 case SUCCESS: 166 break; 167 default: 168 if (mAdvancedWifiOptionsFlow != null) { 169 switch (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage)) { 170 case AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE: 171 connect(); 172 break; 173 case AdvancedWifiOptionsFlow.RESULT_UNKNOWN_PAGE: 174 case AdvancedWifiOptionsFlow.RESULT_PAGE_HANDLED: 175 default: 176 break; 177 } 178 } 179 break; 180 } 181 return true; 182 } 183 184 @Override displayPage(FormPage formPage, FormPageResultListener listener, boolean forward)185 protected void displayPage(FormPage formPage, FormPageResultListener listener, 186 boolean forward) { 187 WifiFormPageType formPageType = getFormPageType(formPage); 188 if (formPageType == WifiFormPageType.CONNECT) { 189 mConnectPage = formPage; 190 Fragment fragment = ConnectToWifiFragment.newInstance( 191 getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableSsid()), 192 true, mConfiguration); 193 displayFragment(fragment, forward); 194 } else if (formPageType == WifiFormPageType.SUCCESS) { 195 mSuccessPage = formPage; 196 Fragment fragment = TimedMessageWizardFragment.newInstance( 197 getString(formPageType.getTitleResourceId())); 198 displayFragment(fragment, forward); 199 } else { 200 displayPage(formPageType, mConfiguration.getPrintableSsid(), null, null, 201 getPreviousPage(formPageType), null, formPageType != WifiFormPageType.SUCCESS, 202 formPage, listener, forward, (mAdvancedWifiOptionsFlow != null) ? 203 mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType) : false); 204 } 205 } 206 getPreviousPage(WifiFormPageType formPageType)207 private FormPage getPreviousPage(WifiFormPageType formPageType) { 208 switch (formPageType) { 209 case ENTER_PASSWORD: 210 if (mPasswordPage != null) 211 mPasswordPage.clearData(); 212 return mPasswordPage; 213 default: 214 return (mAdvancedWifiOptionsFlow != null) ? mAdvancedWifiOptionsFlow 215 .getPreviousPage(formPageType) 216 : null; 217 } 218 } 219 addStartPage()220 private void addStartPage() { 221 /** 222 * WEP connections use the wepKeys for authentication. Other networks use preSharedKey. 223 * If the network isn't open or doesn't have its authentication info present, ask for it. 224 * Otherwise, go straight to connecting. 225 */ 226 if ((mWifiSecurity == WifiSecurity.WEP && TextUtils.isEmpty(mConfiguration.wepKeys[0])) 227 || (!mWifiSecurity.isOpen() && TextUtils.isEmpty(mConfiguration.preSharedKey))) { 228 addPage(WifiFormPageType.ENTER_PASSWORD); 229 } else { 230 connect(); 231 } 232 } 233 connect()234 private void connect() { 235 if (!WifiConfigHelper.isNetworkSaved(mConfiguration) && 236 mAdvancedWifiOptionsFlow != null) { 237 mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration); 238 } 239 addPage(WifiFormPageType.CONNECT); 240 } 241 optionsOrConnect()242 private void optionsOrConnect() { 243 if (mAdvancedWifiOptionsFlow != null) { 244 addPage(mAdvancedWifiOptionsFlow.getInitialPage()); 245 } else { 246 connect(); 247 } 248 } 249 } 250