Home | History | Annotate | Download | only in wifi
      1 /*
      2  * Copyright (C) 2010 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.settings.wifi;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.net.wifi.WifiConfiguration;
     23 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
     24 import android.net.wifi.WifiConfiguration.KeyMgmt;
     25 import android.os.Bundle;
     26 import android.text.Editable;
     27 import android.text.InputType;
     28 import android.text.TextWatcher;
     29 import android.view.View;
     30 import android.widget.AdapterView;
     31 import android.widget.CheckBox;
     32 import android.widget.EditText;
     33 import android.widget.Spinner;
     34 import android.widget.TextView;
     35 
     36 import com.android.settings.R;
     37 
     38 /**
     39  * Dialog to configure the SSID and security settings
     40  * for Access Point operation
     41  */
     42 public class WifiApDialog extends AlertDialog implements View.OnClickListener,
     43         TextWatcher, AdapterView.OnItemSelectedListener {
     44 
     45     static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
     46 
     47     private final DialogInterface.OnClickListener mListener;
     48 
     49     public static final int OPEN_INDEX = 0;
     50     public static final int WPA_INDEX = 1;
     51     public static final int WPA2_INDEX = 2;
     52 
     53     private View mView;
     54     private TextView mSsid;
     55     private int mSecurityTypeIndex = OPEN_INDEX;
     56     private EditText mPassword;
     57 
     58     WifiConfiguration mWifiConfig;
     59 
     60     public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
     61             WifiConfiguration wifiConfig) {
     62         super(context);
     63         mListener = listener;
     64         mWifiConfig = wifiConfig;
     65         if (wifiConfig != null) {
     66             mSecurityTypeIndex = getSecurityTypeIndex(wifiConfig);
     67         }
     68     }
     69 
     70     public static int getSecurityTypeIndex(WifiConfiguration wifiConfig) {
     71         if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
     72             return WPA_INDEX;
     73         } else if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) {
     74             return WPA2_INDEX;
     75         }
     76         return OPEN_INDEX;
     77     }
     78 
     79     public WifiConfiguration getConfig() {
     80 
     81         WifiConfiguration config = new WifiConfiguration();
     82 
     83         /**
     84          * TODO: SSID in WifiConfiguration for soft ap
     85          * is being stored as a raw string without quotes.
     86          * This is not the case on the client side. We need to
     87          * make things consistent and clean it up
     88          */
     89         config.SSID = mSsid.getText().toString();
     90 
     91         switch (mSecurityTypeIndex) {
     92             case OPEN_INDEX:
     93                 config.allowedKeyManagement.set(KeyMgmt.NONE);
     94                 return config;
     95 
     96             case WPA_INDEX:
     97                 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
     98                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
     99                 if (mPassword.length() != 0) {
    100                     String password = mPassword.getText().toString();
    101                     config.preSharedKey = password;
    102                 }
    103                 return config;
    104 
    105             case WPA2_INDEX:
    106                 config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
    107                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
    108                 if (mPassword.length() != 0) {
    109                     String password = mPassword.getText().toString();
    110                     config.preSharedKey = password;
    111                 }
    112                 return config;
    113         }
    114         return null;
    115     }
    116 
    117     @Override
    118     protected void onCreate(Bundle savedInstanceState) {
    119 
    120         mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
    121         Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
    122 
    123         setView(mView);
    124         setInverseBackgroundForced(true);
    125 
    126         Context context = getContext();
    127 
    128         setTitle(R.string.wifi_tether_configure_ap_text);
    129         mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
    130         mSsid = (TextView) mView.findViewById(R.id.ssid);
    131         mPassword = (EditText) mView.findViewById(R.id.password);
    132 
    133         setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
    134         setButton(DialogInterface.BUTTON_NEGATIVE,
    135         context.getString(R.string.wifi_cancel), mListener);
    136 
    137         if (mWifiConfig != null) {
    138             mSsid.setText(mWifiConfig.SSID);
    139             mSecurity.setSelection(mSecurityTypeIndex);
    140             if (mSecurityTypeIndex == WPA_INDEX ||
    141                     mSecurityTypeIndex == WPA2_INDEX) {
    142                   mPassword.setText(mWifiConfig.preSharedKey);
    143             }
    144         }
    145 
    146         mSsid.addTextChangedListener(this);
    147         mPassword.addTextChangedListener(this);
    148         ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
    149         mSecurity.setOnItemSelectedListener(this);
    150 
    151         super.onCreate(savedInstanceState);
    152 
    153         showSecurityFields();
    154         validate();
    155     }
    156 
    157     private void validate() {
    158         if ((mSsid != null && mSsid.length() == 0) ||
    159                    (((mSecurityTypeIndex == WPA_INDEX) || (mSecurityTypeIndex == WPA2_INDEX))&&
    160                         mPassword.length() < 8)) {
    161             getButton(BUTTON_SUBMIT).setEnabled(false);
    162         } else {
    163             getButton(BUTTON_SUBMIT).setEnabled(true);
    164         }
    165     }
    166 
    167     public void onClick(View view) {
    168         mPassword.setInputType(
    169                 InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
    170                 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
    171                 InputType.TYPE_TEXT_VARIATION_PASSWORD));
    172     }
    173 
    174     public void onTextChanged(CharSequence s, int start, int before, int count) {
    175     }
    176 
    177     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    178     }
    179 
    180     public void afterTextChanged(Editable editable) {
    181         validate();
    182     }
    183 
    184     @Override
    185     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    186         mSecurityTypeIndex = position;
    187         showSecurityFields();
    188         validate();
    189     }
    190 
    191     @Override
    192     public void onNothingSelected(AdapterView<?> parent) {
    193     }
    194 
    195     private void showSecurityFields() {
    196         if (mSecurityTypeIndex == OPEN_INDEX) {
    197             mView.findViewById(R.id.fields).setVisibility(View.GONE);
    198             return;
    199         }
    200         mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
    201     }
    202 }
    203