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