| /* |
| * Copyright (C) 2014 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.tv.settings.connectivity; |
| |
| import com.android.tv.settings.R; |
| import com.android.tv.settings.connectivity.setup.MessageWizardFragment; |
| import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment; |
| import com.android.tv.settings.util.ThemeHelper; |
| |
| import com.android.tv.settings.dialog.old.DialogActivity; |
| |
| import android.app.Fragment; |
| import android.app.FragmentTransaction; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.net.wifi.WifiManager; |
| import android.net.wifi.WifiManager.WpsCallback; |
| import android.net.wifi.WpsInfo; |
| import android.os.Bundle; |
| import android.os.Handler; |
| import android.os.Message; |
| |
| import java.util.ArrayList; |
| |
| public class WpsConnectionActivity extends DialogActivity |
| implements SelectFromListWizardFragment.Listener, TimedMessageWizardFragment.Listener { |
| |
| public static final String EXTRA_WPS_SETUP = "wps_setup"; |
| |
| public static Intent createIntent(Context context, int wpsSetup) { |
| return new Intent(context, WpsConnectionActivity.class).putExtra(EXTRA_WPS_SETUP, wpsSetup); |
| } |
| |
| private static final String WPS_FRAGMENT_TAG = "wps_fragment_tag"; |
| private static final String KEY_CANCEL = "cancel"; |
| private static final String KEY_RETRY = "retry"; |
| |
| private WifiManager mWifiManager; |
| private int mWpsSetup; |
| private boolean mWpsComplete; |
| private final WpsCallback mWpsCallback = new WpsCallback() { |
| @Override |
| public void onStarted(String pin) { |
| if (pin != null) { |
| displayFragment(createEnterPinFragment(pin), true); |
| } |
| } |
| |
| @Override |
| public void onSucceeded() { |
| mWpsComplete = true; |
| displayFragment(createSuccessFragment(), true); |
| } |
| |
| @Override |
| public void onFailed(int reason) { |
| mWpsComplete = true; |
| String errorMessage; |
| switch (reason) { |
| case WifiManager.WPS_OVERLAP_ERROR: |
| errorMessage = getString(R.string.wifi_wps_failed_overlap); |
| break; |
| case WifiManager.WPS_WEP_PROHIBITED: |
| errorMessage = getString(R.string.wifi_wps_failed_wep); |
| break; |
| case WifiManager.WPS_TKIP_ONLY_PROHIBITED: |
| errorMessage = getString(R.string.wifi_wps_failed_tkip); |
| break; |
| case WifiManager.IN_PROGRESS: |
| mWifiManager.cancelWps(null); |
| startWps(); |
| return; |
| case WifiManager.WPS_TIMED_OUT: |
| startWps(); |
| return; |
| default: |
| errorMessage = getString(R.string.wifi_wps_failed_generic); |
| break; |
| } |
| displayFragment(createErrorFragment(errorMessage), true); |
| } |
| }; |
| |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| setTheme(ThemeHelper.getThemeResource(getIntent())); |
| setLayoutProperties(R.layout.setup_auth_activity, R.id.description, R.id.action); |
| super.onCreate(savedInstanceState); |
| Bundle extras = getIntent().getExtras(); |
| mWpsSetup = getIntent().getIntExtra(EXTRA_WPS_SETUP, 0); |
| mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| } |
| |
| @Override |
| protected void onStart() { |
| super.onStart(); |
| startWps(); |
| } |
| |
| @Override |
| protected void onStop() { |
| super.onStop(); |
| if (!mWpsComplete) { |
| mWifiManager.cancelWps(null); |
| } |
| } |
| |
| @Override |
| public void onTimedMessageCompleted() { |
| setResult(RESULT_OK); |
| finish(); |
| } |
| |
| @Override |
| public void onListSelectionComplete(SelectFromListWizardFragment.ListItem listItem) { |
| startWps(); |
| } |
| |
| @Override |
| public void onListFocusChanged(SelectFromListWizardFragment.ListItem listItem) { |
| // Do nothing. |
| } |
| |
| private void startWps() { |
| Fragment currentFragment = getFragmentManager().findFragmentByTag(WPS_FRAGMENT_TAG); |
| if (!(currentFragment instanceof WpsScanningFragment)) { |
| displayFragment(createWpsScanningFragment(), true); |
| } |
| mWpsComplete = false; |
| WpsInfo wpsConfig = new WpsInfo(); |
| wpsConfig.setup = mWpsSetup; |
| if (!mWpsComplete) { |
| mWifiManager.startWps(wpsConfig, mWpsCallback); |
| } |
| } |
| |
| private Fragment createWpsScanningFragment() { |
| return WpsScanningFragment.newInstance(); |
| } |
| |
| private Fragment createEnterPinFragment(String pin) { |
| return WpsPinFragment.newInstance(pin); |
| } |
| |
| private Fragment createErrorFragment(String errorMessage) { |
| SelectFromListWizardFragment.ListItem retryListItem = |
| new SelectFromListWizardFragment.ListItem( |
| getString(R.string.wifi_wps_retry_scan).toUpperCase(), 0); |
| ArrayList<SelectFromListWizardFragment.ListItem> listItems = new ArrayList< |
| SelectFromListWizardFragment.ListItem>(); |
| listItems.add(retryListItem); |
| return SelectFromListWizardFragment.newInstance(errorMessage, null, listItems, |
| retryListItem); |
| } |
| |
| private Fragment createSuccessFragment() { |
| return TimedMessageWizardFragment.newInstance( |
| getString(R.string.wifi_setup_connection_success)); |
| } |
| |
| private void displayFragment(Fragment fragment, boolean forward) { |
| FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
| if (forward) { |
| transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); |
| } else { |
| transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); |
| } |
| transaction.replace(R.id.content, fragment, WPS_FRAGMENT_TAG).commit(); |
| } |
| } |