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.app.FragmentTransaction; 21 import android.content.Context; 22 import android.net.wifi.WifiManager; 23 import android.net.wifi.WifiManager.WpsCallback; 24 import android.net.wifi.WpsInfo; 25 import android.os.Bundle; 26 27 import com.android.tv.settings.R; 28 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment; 29 import com.android.tv.settings.dialog.old.DialogActivity; 30 import com.android.tv.settings.util.ThemeHelper; 31 32 import java.util.ArrayList; 33 34 public 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 onCreate(Bundle savedInstanceState)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 onStart()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 onStop()114 protected void onStop() { 115 super.onStop(); 116 mActive = false; 117 118 if (!mWpsComplete) { 119 mWifiManager.cancelWps(null); 120 } 121 } 122 123 @Override onTimedMessageCompleted()124 public void onTimedMessageCompleted() { 125 setResult(RESULT_OK); 126 finish(); 127 } 128 129 @Override onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem)130 public void onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem) { 131 startWps(); 132 } 133 134 @Override onListFocusChanged(SelectFromListWizardFragment.ListItem listItem)135 public void onListFocusChanged(SelectFromListWizardFragment.ListItem listItem) { 136 // Do nothing. 137 } 138 startWps()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 createWpsScanningFragment()150 private Fragment createWpsScanningFragment() { 151 return WpsScanningFragment.newInstance(); 152 } 153 createEnterPinFragment(String pin)154 private Fragment createEnterPinFragment(String pin) { 155 return WpsPinFragment.newInstance(pin); 156 } 157 createErrorFragment(String errorMessage)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 createSuccessFragment()168 private Fragment createSuccessFragment() { 169 return TimedMessageWizardFragment.newInstance( 170 getString(R.string.wifi_setup_connection_success)); 171 } 172 displayFragment(Fragment fragment, boolean forward)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