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