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 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.net.wifi.WifiManager;
26 import android.os.Bundle;
27 import android.text.Editable;
28 import android.text.InputType;
29 import android.text.TextWatcher;
30 import android.view.View;
31 import android.widget.AdapterView;
32 import android.widget.ArrayAdapter;
33 import android.widget.CheckBox;
34 import android.widget.EditText;
35 import android.widget.Spinner;
36 import android.widget.TextView;
38 import com.android.settings.R;
40 import android.util.Log;
43 * Dialog to configure the SSID and security settings
44 * for Access Point operation
46 public class WifiApDialog extends AlertDialog implements View.OnClickListener,
47 TextWatcher, AdapterView.OnItemSelectedListener {
49 static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
51 private final DialogInterface.OnClickListener mListener;
53 public static final int OPEN_INDEX = 0;
54 public static final int WPA2_INDEX = 1;
57 private TextView mSsid;
58 private int mSecurityTypeIndex = OPEN_INDEX;
59 private EditText mPassword;
60 private int mBandIndex = OPEN_INDEX;
62 WifiConfiguration mWifiConfig;
63 WifiManager mWifiManager;
64 private Context mContext;
66 private static final String TAG = "WifiApDialog";
68 public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
69 WifiConfiguration wifiConfig) {
72 mWifiConfig = wifiConfig;
73 if (wifiConfig != null) {
74 mSecurityTypeIndex = getSecurityTypeIndex(wifiConfig);
76 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
80 public static int getSecurityTypeIndex(WifiConfiguration wifiConfig) {
81 if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) {
87 public WifiConfiguration getConfig() {
89 WifiConfiguration config = new WifiConfiguration();
92 * TODO: SSID in WifiConfiguration for soft ap
93 * is being stored as a raw string without quotes.
94 * This is not the case on the client side. We need to
95 * make things consistent and clean it up
97 config.SSID = mSsid.getText().toString();
99 config.apBand = mBandIndex;
101 switch (mSecurityTypeIndex) {
103 config.allowedKeyManagement.set(KeyMgmt.NONE);
107 config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK);
108 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
109 if (mPassword.length() != 0) {
110 String password = mPassword.getText().toString();
111 config.preSharedKey = password;
119 protected void onCreate(Bundle savedInstanceState) {
120 boolean mInit = true;
121 mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
122 Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
123 final Spinner mChannel = (Spinner) mView.findViewById(R.id.choose_channel);
126 setInverseBackgroundForced(true);
128 Context context = getContext();
130 setTitle(R.string.wifi_tether_configure_ap_text);
131 mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
132 mSsid = (TextView) mView.findViewById(R.id.ssid);
133 mPassword = (EditText) mView.findViewById(R.id.password);
135 ArrayAdapter <CharSequence> channelAdapter;
136 String countryCode = mWifiManager.getCountryCode();
137 if (!mWifiManager.isDualBandSupported() || countryCode == null) {
138 //If no country code, 5GHz AP is forbidden
139 Log.i(TAG,(!mWifiManager.isDualBandSupported() ? "Device do not support 5GHz " :"")
140 + (countryCode == null ? " NO country code" :"") + " forbid 5GHz");
141 channelAdapter = ArrayAdapter.createFromResource(mContext,
142 R.array.wifi_ap_band_config_2G_only, android.R.layout.simple_spinner_item);
143 mWifiConfig.apBand = 0;
145 channelAdapter = ArrayAdapter.createFromResource(mContext,
146 R.array.wifi_ap_band_config_full, android.R.layout.simple_spinner_item);
149 channelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
151 setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
152 setButton(DialogInterface.BUTTON_NEGATIVE,
153 context.getString(R.string.wifi_cancel), mListener);
155 if (mWifiConfig != null) {
156 mSsid.setText(mWifiConfig.SSID);
157 if (mWifiConfig.apBand == 0) {
163 mSecurity.setSelection(mSecurityTypeIndex);
164 if (mSecurityTypeIndex == WPA2_INDEX) {
165 mPassword.setText(mWifiConfig.preSharedKey);
169 mChannel.setAdapter(channelAdapter);
170 mChannel.setOnItemSelectedListener(
171 new AdapterView.OnItemSelectedListener() {
172 boolean mInit = true;
174 public void onItemSelected(AdapterView<?> adapterView, View view, int position,
177 mBandIndex = position;
178 mWifiConfig.apBand = mBandIndex;
179 Log.i(TAG, "config on channelIndex : " + mBandIndex + " Band: " +
183 mChannel.setSelection(mBandIndex);
189 public void onNothingSelected(AdapterView<?> adapterView) {
195 mSsid.addTextChangedListener(this);
196 mPassword.addTextChangedListener(this);
197 ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
198 mSecurity.setOnItemSelectedListener(this);
200 super.onCreate(savedInstanceState);
202 showSecurityFields();
206 public void onRestoreInstanceState(Bundle savedInstanceState) {
207 super.onRestoreInstanceState(savedInstanceState);
208 mPassword.setInputType(
209 InputType.TYPE_CLASS_TEXT |
210 (((CheckBox) mView.findViewById(R.id.show_password)).isChecked() ?
211 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
212 InputType.TYPE_TEXT_VARIATION_PASSWORD));
215 private void validate() {
216 if ((mSsid != null && mSsid.length() == 0) ||
217 ((mSecurityTypeIndex == WPA2_INDEX)&&
218 mPassword.length() < 8)) {
219 getButton(BUTTON_SUBMIT).setEnabled(false);
221 getButton(BUTTON_SUBMIT).setEnabled(true);
225 public void onClick(View view) {
226 mPassword.setInputType(
227 InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
228 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
229 InputType.TYPE_TEXT_VARIATION_PASSWORD));
232 public void onTextChanged(CharSequence s, int start, int before, int count) {
235 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
238 public void afterTextChanged(Editable editable) {
243 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
244 mSecurityTypeIndex = position;
245 showSecurityFields();
250 public void onNothingSelected(AdapterView<?> parent) {
253 private void showSecurityFields() {
254 if (mSecurityTypeIndex == OPEN_INDEX) {
255 mView.findViewById(R.id.fields).setVisibility(View.GONE);
258 mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);