OSDN Git Service

am 66b5a58a: Merge "Fix init order so we have something to measure." into mnc-dev
[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 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;
37
38 import com.android.settings.R;
39
40 import android.util.Log;
41
42 /**
43  * Dialog to configure the SSID and security settings
44  * for Access Point operation
45  */
46 public class WifiApDialog extends AlertDialog implements View.OnClickListener,
47         TextWatcher, AdapterView.OnItemSelectedListener {
48
49     static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
50
51     private final DialogInterface.OnClickListener mListener;
52
53     public static final int OPEN_INDEX = 0;
54     public static final int WPA2_INDEX = 1;
55
56     private View mView;
57     private TextView mSsid;
58     private int mSecurityTypeIndex = OPEN_INDEX;
59     private EditText mPassword;
60     private int mBandIndex = OPEN_INDEX;
61
62     WifiConfiguration mWifiConfig;
63     WifiManager mWifiManager;
64     private Context mContext;
65
66     private static final String TAG = "WifiApDialog";
67
68     public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
69             WifiConfiguration wifiConfig) {
70         super(context);
71         mListener = listener;
72         mWifiConfig = wifiConfig;
73         if (wifiConfig != null) {
74             mSecurityTypeIndex = getSecurityTypeIndex(wifiConfig);
75         }
76         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
77         mContext =  context;
78     }
79
80     public static int getSecurityTypeIndex(WifiConfiguration wifiConfig) {
81         if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) {
82             return WPA2_INDEX;
83         }
84         return OPEN_INDEX;
85     }
86
87     public WifiConfiguration getConfig() {
88
89         WifiConfiguration config = new WifiConfiguration();
90
91         /**
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
96          */
97         config.SSID = mSsid.getText().toString();
98
99         config.apBand = mBandIndex;
100
101         switch (mSecurityTypeIndex) {
102             case OPEN_INDEX:
103                 config.allowedKeyManagement.set(KeyMgmt.NONE);
104                 return config;
105
106             case WPA2_INDEX:
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;
112                 }
113                 return config;
114         }
115         return null;
116     }
117
118     @Override
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);
124
125         setView(mView);
126         setInverseBackgroundForced(true);
127
128         Context context = getContext();
129
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);
134
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;
144         } else {
145             channelAdapter = ArrayAdapter.createFromResource(mContext,
146                     R.array.wifi_ap_band_config_full, android.R.layout.simple_spinner_item);
147         }
148
149         channelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
150
151         setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
152         setButton(DialogInterface.BUTTON_NEGATIVE,
153         context.getString(R.string.wifi_cancel), mListener);
154
155         if (mWifiConfig != null) {
156             mSsid.setText(mWifiConfig.SSID);
157             if (mWifiConfig.apBand == 0) {
158                mBandIndex = 0;
159             } else {
160                mBandIndex = 1;
161             }
162
163             mSecurity.setSelection(mSecurityTypeIndex);
164             if (mSecurityTypeIndex == WPA2_INDEX) {
165                 mPassword.setText(mWifiConfig.preSharedKey);
166             }
167         }
168
169         mChannel.setAdapter(channelAdapter);
170         mChannel.setOnItemSelectedListener(
171                 new AdapterView.OnItemSelectedListener() {
172                     boolean mInit = true;
173                     @Override
174                     public void onItemSelected(AdapterView<?> adapterView, View view, int position,
175                                                long id) {
176                         if (!mInit) {
177                             mBandIndex = position;
178                             mWifiConfig.apBand = mBandIndex;
179                             Log.i(TAG, "config on channelIndex : " + mBandIndex + " Band: " +
180                                     mWifiConfig.apBand);
181                         } else {
182                             mInit = false;
183                             mChannel.setSelection(mBandIndex);
184                         }
185
186                     }
187
188                     @Override
189                     public void onNothingSelected(AdapterView<?> adapterView) {
190
191                     }
192                 }
193         );
194
195         mSsid.addTextChangedListener(this);
196         mPassword.addTextChangedListener(this);
197         ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
198         mSecurity.setOnItemSelectedListener(this);
199
200         super.onCreate(savedInstanceState);
201
202         showSecurityFields();
203         validate();
204     }
205
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));
213     }
214
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);
220         } else {
221             getButton(BUTTON_SUBMIT).setEnabled(true);
222         }
223     }
224
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));
230     }
231
232     public void onTextChanged(CharSequence s, int start, int before, int count) {
233     }
234
235     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
236     }
237
238     public void afterTextChanged(Editable editable) {
239         validate();
240     }
241
242     @Override
243     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
244         mSecurityTypeIndex = position;
245         showSecurityFields();
246         validate();
247     }
248
249     @Override
250     public void onNothingSelected(AdapterView<?> parent) {
251     }
252
253     private void showSecurityFields() {
254         if (mSecurityTypeIndex == OPEN_INDEX) {
255             mView.findViewById(R.id.fields).setVisibility(View.GONE);
256             return;
257         }
258         mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
259     }
260 }