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 android.app.Fragment; 20import android.app.FragmentTransaction; 21import android.content.Context; 22import android.net.wifi.WifiManager; 23import android.net.wifi.WifiManager.WpsCallback; 24import android.net.wifi.WpsInfo; 25import android.os.Bundle; 26 27import com.android.tv.settings.R; 28import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment; 29import com.android.tv.settings.dialog.old.DialogActivity; 30import com.android.tv.settings.util.ThemeHelper; 31 32import java.util.ArrayList; 33 34public class WpsConnectionActivity extends DialogActivity 35 implements SelectFromListWizardFragment.Listener, TimedMessageWizardFragment.Listener { 36 37 private static final String WPS_FRAGMENT_TAG = "wps_fragment_tag"; 38 39 private WifiManager mWifiManager; 40 private boolean mWpsComplete; 41 private boolean mActive; 42 43 private final WpsCallback mWpsCallback = new WpsCallback() { 44 @Override 45 public void onStarted(String pin) { 46 if (pin != null && mActive) { 47 displayFragment(createEnterPinFragment(pin), true); 48 } 49 } 50 51 @Override 52 public void onSucceeded() { 53 mWpsComplete = true; 54 55 if (!mActive) { 56 return; 57 } 58 59 displayFragment(createSuccessFragment(), true); 60 } 61 62 @Override 63 public void onFailed(int reason) { 64 mWpsComplete = true; 65 66 if (!mActive) { 67 return; 68 } 69 70 String errorMessage; 71 switch (reason) { 72 case WifiManager.WPS_OVERLAP_ERROR: 73 errorMessage = getString(R.string.wifi_wps_failed_overlap); 74 break; 75 case WifiManager.WPS_WEP_PROHIBITED: 76 errorMessage = getString(R.string.wifi_wps_failed_wep); 77 break; 78 case WifiManager.WPS_TKIP_ONLY_PROHIBITED: 79 errorMessage = getString(R.string.wifi_wps_failed_tkip); 80 break; 81 case WifiManager.IN_PROGRESS: 82 mWifiManager.cancelWps(null); 83 startWps(); 84 return; 85 case WifiManager.WPS_TIMED_OUT: 86 startWps(); 87 return; 88 default: 89 errorMessage = getString(R.string.wifi_wps_failed_generic); 90 break; 91 } 92 displayFragment(createErrorFragment(errorMessage), true); 93 } 94 }; 95 96 @Override 97 protected void onCreate(Bundle savedInstanceState) { 98 setTheme(ThemeHelper.getThemeResource(getIntent())); 99 setLayoutProperties(R.layout.setup_auth_activity, R.id.description, R.id.action); 100 super.onCreate(savedInstanceState); 101 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 102 } 103 104 @Override 105 protected void onStart() { 106 super.onStart(); 107 // Must be set before all other actions. 108 mActive = true; 109 110 startWps(); 111 } 112 113 @Override 114 protected void onStop() { 115 super.onStop(); 116 mActive = false; 117 118 if (!mWpsComplete) { 119 mWifiManager.cancelWps(null); 120 } 121 } 122 123 @Override 124 public void onTimedMessageCompleted() { 125 setResult(RESULT_OK); 126 finish(); 127 } 128 129 @Override 130 public void onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem) { 131 startWps(); 132 } 133 134 @Override 135 public void onListFocusChanged(SelectFromListWizardFragment.ListItem listItem) { 136 // Do nothing. 137 } 138 139 private void startWps() { 140 Fragment currentFragment = getFragmentManager().findFragmentByTag(WPS_FRAGMENT_TAG); 141 if (!(currentFragment instanceof WpsScanningFragment)) { 142 displayFragment(createWpsScanningFragment(), true); 143 } 144 mWpsComplete = false; 145 WpsInfo wpsConfig = new WpsInfo(); 146 wpsConfig.setup = WpsInfo.PBC; 147 mWifiManager.startWps(wpsConfig, mWpsCallback); 148 } 149 150 private Fragment createWpsScanningFragment() { 151 return WpsScanningFragment.newInstance(); 152 } 153 154 private Fragment createEnterPinFragment(String pin) { 155 return WpsPinFragment.newInstance(pin); 156 } 157 158 private Fragment createErrorFragment(String errorMessage) { 159 SelectFromListWizardFragment.ListItem retryListItem = 160 new SelectFromListWizardFragment.ListItem( 161 getString(R.string.wifi_wps_retry_scan), 0); 162 ArrayList<SelectFromListWizardFragment.ListItem> listItems = new ArrayList<>(); 163 listItems.add(retryListItem); 164 return SelectFromListWizardFragment.newInstance(errorMessage, null, listItems, 165 retryListItem); 166 } 167 168 private Fragment createSuccessFragment() { 169 return TimedMessageWizardFragment.newInstance( 170 getString(R.string.wifi_setup_connection_success)); 171 } 172 173 private void displayFragment(Fragment fragment, boolean forward) { 174 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 175 if (forward) { 176 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 177 } else { 178 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 179 } 180 transaction.replace(R.id.content, fragment, WPS_FRAGMENT_TAG).commit(); 181 } 182} 183