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 overridePendingTransition(R.anim.wps_activity_open_in, R.anim.wps_activity_open_out); 104 105 if (ThemeHelper.fromSetupWizard(getIntent())) { 106 setTitle(getResources().getString(R.string.wifi_wps_title)); 107 } 108 } 109 110 @Override onStart()111 protected void onStart() { 112 super.onStart(); 113 // Must be set before all other actions. 114 mActive = true; 115 116 startWps(); 117 } 118 119 @Override onStop()120 protected void onStop() { 121 super.onStop(); 122 mActive = false; 123 124 if (!mWpsComplete) { 125 mWifiManager.cancelWps(null); 126 } 127 } 128 129 @Override onTimedMessageCompleted()130 public void onTimedMessageCompleted() { 131 setResult(RESULT_OK); 132 finish(); 133 } 134 135 @Override onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem)136 public void onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem) { 137 startWps(); 138 } 139 140 @Override onListFocusChanged(SelectFromListWizardFragment.ListItem listItem)141 public void onListFocusChanged(SelectFromListWizardFragment.ListItem listItem) { 142 // Do nothing. 143 } 144 startWps()145 private void startWps() { 146 Fragment currentFragment = getFragmentManager().findFragmentByTag(WPS_FRAGMENT_TAG); 147 if (!(currentFragment instanceof WpsScanningFragment)) { 148 displayFragment(createWpsScanningFragment(), true); 149 } 150 mWpsComplete = false; 151 WpsInfo wpsConfig = new WpsInfo(); 152 wpsConfig.setup = WpsInfo.PBC; 153 mWifiManager.startWps(wpsConfig, mWpsCallback); 154 } 155 createWpsScanningFragment()156 private Fragment createWpsScanningFragment() { 157 return WpsScanningFragment.newInstance(); 158 } 159 createEnterPinFragment(String pin)160 private Fragment createEnterPinFragment(String pin) { 161 return WpsPinFragment.newInstance(pin); 162 } 163 createErrorFragment(String errorMessage)164 private Fragment createErrorFragment(String errorMessage) { 165 SelectFromListWizardFragment.ListItem retryListItem = 166 new SelectFromListWizardFragment.ListItem( 167 getString(R.string.wifi_wps_retry_scan), 0); 168 ArrayList<SelectFromListWizardFragment.ListItem> listItems = new ArrayList<>(); 169 listItems.add(retryListItem); 170 return SelectFromListWizardFragment.newInstance(errorMessage, null, listItems, 171 retryListItem); 172 } 173 createSuccessFragment()174 private Fragment createSuccessFragment() { 175 return TimedMessageWizardFragment.newInstance( 176 getString(R.string.wifi_setup_connection_success)); 177 } 178 displayFragment(Fragment fragment, boolean forward)179 private void displayFragment(Fragment fragment, boolean forward) { 180 FragmentTransaction transaction = getFragmentManager().beginTransaction(); 181 if (forward) { 182 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 183 } else { 184 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 185 } 186 transaction.replace(R.id.content, fragment, WPS_FRAGMENT_TAG).commit(); 187 } 188 } 189