OSDN Git Service

485e54efe85f07ade970005248f8a686fd52a080
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / WifiApDialog.java
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 com.android.settings.R;
20
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;
40
41 /**
42  * Dialog to configure the SSID and security settings
43  * for Access Point operation
44  */
45 class WifiApDialog extends AlertDialog implements View.OnClickListener,
46         TextWatcher, AdapterView.OnItemSelectedListener {
47
48     static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
49
50     private final DialogInterface.OnClickListener mListener;
51
52     private View mView;
53     private TextView mSsid;
54     private int mSecurityType = AccessPoint.SECURITY_NONE;
55     private EditText mPassword;
56
57     WifiConfiguration mWifiConfig;
58
59     public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
60             WifiConfiguration wifiConfig) {
61         super(context);
62         mListener = listener;
63         mWifiConfig = wifiConfig;
64         if (wifiConfig != null)
65           mSecurityType = AccessPoint.getSecurity(wifiConfig);
66     }
67
68     public WifiConfiguration getConfig() {
69
70         WifiConfiguration config = new WifiConfiguration();
71
72         config.SSID = mSsid.getText().toString();
73
74         switch (mSecurityType) {
75             case AccessPoint.SECURITY_NONE:
76                 config.allowedKeyManagement.set(KeyMgmt.NONE);
77                 return config;
78
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;
89                     } else {
90                         config.wepKeys[0] = '"' + password + '"';
91                     }
92                 }
93                 return config;
94
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;
102                     } else {
103                         config.preSharedKey = '"' + password + '"';
104                     }
105                 }
106                 return config;
107         }
108         return null;
109     }
110
111     @Override
112     protected void onCreate(Bundle savedInstanceState) {
113
114         Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
115         mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
116
117         setView(mView);
118         setInverseBackgroundForced(true);
119
120         Context context = getContext();
121
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);
126
127         setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
128         setButton(DialogInterface.BUTTON_NEGATIVE,
129         context.getString(R.string.wifi_cancel), mListener);
130
131         if (mWifiConfig != null) {
132             mSsid.setText(mWifiConfig.SSID);
133             switch (mSecurityType) {
134               case AccessPoint.SECURITY_WEP:
135                   mPassword.setText(mWifiConfig.wepKeys[0]);
136                   break;
137               case AccessPoint.SECURITY_PSK:
138                   mPassword.setText(mWifiConfig.preSharedKey);
139                   break;
140             }
141             mSecurity.setSelection(mSecurityType);
142         }
143
144         mSsid.addTextChangedListener(this);
145         mPassword.addTextChangedListener(this);
146         ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
147         mSecurity.setOnItemSelectedListener(this);
148
149         super.onCreate(savedInstanceState);
150
151         showSecurityFields();
152         validate();
153     }
154
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);
160         } else {
161             getButton(BUTTON_SUBMIT).setEnabled(true);
162         }
163     }
164
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));
170     }
171
172     public void onTextChanged(CharSequence s, int start, int before, int count) {
173     }
174
175     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
176     }
177
178     public void afterTextChanged(Editable editable) {
179         validate();
180     }
181
182     public void onItemSelected(AdapterView parent, View view, int position, long id) {
183         mSecurityType = position;
184         showSecurityFields();
185         validate();
186     }
187
188     public void onNothingSelected(AdapterView parent) {
189     }
190
191     private void showSecurityFields() {
192         if (mSecurityType == AccessPoint.SECURITY_NONE) {
193             mView.findViewById(R.id.fields).setVisibility(View.GONE);
194             return;
195         }
196         mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
197     }
198 }