2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.wifi;
19 import com.android.settings.R;
21 import android.app.AlertDialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.net.wifi.WifiConfiguration;
25 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
26 import android.net.wifi.WifiConfiguration.KeyMgmt;
27 import android.os.Bundle;
28 import android.text.Editable;
29 import android.text.InputType;
30 import android.text.TextWatcher;
31 import android.util.Log;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.AdapterView;
35 import android.widget.ArrayAdapter;
36 import android.widget.CheckBox;
37 import android.widget.EditText;
38 import android.widget.Spinner;
39 import android.widget.TextView;
42 * Dialog to configure the SSID and security settings
43 * for Access Point operation
45 class WifiApDialog extends AlertDialog implements View.OnClickListener,
46 TextWatcher, AdapterView.OnItemSelectedListener {
48 static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
50 private final DialogInterface.OnClickListener mListener;
53 private TextView mSsid;
54 private int mSecurityType = AccessPoint.SECURITY_NONE;
55 private EditText mPassword;
57 WifiConfiguration mWifiConfig;
59 public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
60 WifiConfiguration wifiConfig) {
63 mWifiConfig = wifiConfig;
64 if (wifiConfig != null)
65 mSecurityType = AccessPoint.getSecurity(wifiConfig);
68 public WifiConfiguration getConfig() {
70 WifiConfiguration config = new WifiConfiguration();
72 config.SSID = mSsid.getText().toString();
74 switch (mSecurityType) {
75 case AccessPoint.SECURITY_NONE:
76 config.allowedKeyManagement.set(KeyMgmt.NONE);
79 case AccessPoint.SECURITY_WEP:
80 config.allowedKeyManagement.set(KeyMgmt.NONE);
81 config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
82 if (mPassword.length() != 0) {
83 int length = mPassword.length();
84 String password = mPassword.getText().toString();
85 // WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
86 if ((length == 10 || length == 26 || length == 58) &&
87 password.matches("[0-9A-Fa-f]*")) {
88 config.wepKeys[0] = password;
90 config.wepKeys[0] = '"' + password + '"';
95 case AccessPoint.SECURITY_PSK:
96 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
97 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
98 if (mPassword.length() != 0) {
99 String password = mPassword.getText().toString();
100 if (password.matches("[0-9A-Fa-f]{64}")) {
101 config.preSharedKey = password;
103 config.preSharedKey = '"' + password + '"';
112 protected void onCreate(Bundle savedInstanceState) {
114 Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
115 mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
118 setInverseBackgroundForced(true);
120 Context context = getContext();
122 setTitle(R.string.wifi_tether_configure_ap_text);
123 mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
124 mSsid = (TextView) mView.findViewById(R.id.ssid);
125 mPassword = (EditText) mView.findViewById(R.id.password);
127 setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
128 setButton(DialogInterface.BUTTON_NEGATIVE,
129 context.getString(R.string.wifi_cancel), mListener);
131 if (mWifiConfig != null) {
132 mSsid.setText(mWifiConfig.SSID);
133 switch (mSecurityType) {
134 case AccessPoint.SECURITY_WEP:
135 mPassword.setText(mWifiConfig.wepKeys[0]);
137 case AccessPoint.SECURITY_PSK:
138 mPassword.setText(mWifiConfig.preSharedKey);
141 mSecurity.setSelection(mSecurityType);
144 mSsid.addTextChangedListener(this);
145 mPassword.addTextChangedListener(this);
146 ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
147 mSecurity.setOnItemSelectedListener(this);
149 super.onCreate(savedInstanceState);
151 showSecurityFields();
155 private void validate() {
156 if ((mSsid != null && mSsid.length() == 0) ||
157 (mSecurityType == AccessPoint.SECURITY_WEP && mPassword.length() == 0) ||
158 (mSecurityType == AccessPoint.SECURITY_PSK && mPassword.length() < 8)) {
159 getButton(BUTTON_SUBMIT).setEnabled(false);
161 getButton(BUTTON_SUBMIT).setEnabled(true);
165 public void onClick(View view) {
166 mPassword.setInputType(
167 InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
168 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
169 InputType.TYPE_TEXT_VARIATION_PASSWORD));
172 public void onTextChanged(CharSequence s, int start, int before, int count) {
175 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
178 public void afterTextChanged(Editable editable) {
182 public void onItemSelected(AdapterView parent, View view, int position, long id) {
183 mSecurityType = position;
184 showSecurityFields();
188 public void onNothingSelected(AdapterView parent) {
191 private void showSecurityFields() {
192 if (mSecurityType == AccessPoint.SECURITY_NONE) {
193 mView.findViewById(R.id.fields).setVisibility(View.GONE);
196 mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);