• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static final int OPEN_INDEX = 0;
53     private static final int WPA_INDEX = 1;
54 
55     private View mView;
56     private TextView mSsid;
57     private int mSecurityType = AccessPoint.SECURITY_NONE;
58     private EditText mPassword;
59 
60     WifiConfiguration mWifiConfig;
61 
WifiApDialog(Context context, DialogInterface.OnClickListener listener, WifiConfiguration wifiConfig)62     public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
63             WifiConfiguration wifiConfig) {
64         super(context);
65         mListener = listener;
66         mWifiConfig = wifiConfig;
67         if (wifiConfig != null)
68           mSecurityType = AccessPoint.getSecurity(wifiConfig);
69     }
70 
getConfig()71     public WifiConfiguration getConfig() {
72 
73         WifiConfiguration config = new WifiConfiguration();
74 
75         /**
76          * TODO: SSID in WifiConfiguration for soft ap
77          * is being stored as a raw string without quotes.
78          * This is not the case on the client side. We need to
79          * make things consistent and clean it up
80          */
81         config.SSID = mSsid.getText().toString();
82 
83         switch (mSecurityType) {
84             case AccessPoint.SECURITY_NONE:
85                 config.allowedKeyManagement.set(KeyMgmt.NONE);
86                 return config;
87 
88             case AccessPoint.SECURITY_PSK:
89                 config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
90                 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
91                 if (mPassword.length() != 0) {
92                     String password = mPassword.getText().toString();
93                     config.preSharedKey = password;
94                 }
95                 return config;
96         }
97         return null;
98     }
99 
onCreate(Bundle savedInstanceState)100     protected void onCreate(Bundle savedInstanceState) {
101 
102         mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
103         Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
104 
105         setView(mView);
106         setInverseBackgroundForced(true);
107 
108         Context context = getContext();
109 
110         setTitle(R.string.wifi_tether_configure_ap_text);
111         mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
112         mSsid = (TextView) mView.findViewById(R.id.ssid);
113         mPassword = (EditText) mView.findViewById(R.id.password);
114 
115         setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
116         setButton(DialogInterface.BUTTON_NEGATIVE,
117         context.getString(R.string.wifi_cancel), mListener);
118 
119         if (mWifiConfig != null) {
120             mSsid.setText(mWifiConfig.SSID);
121             switch (mSecurityType) {
122               case AccessPoint.SECURITY_NONE:
123                   mSecurity.setSelection(OPEN_INDEX);
124                   break;
125               case AccessPoint.SECURITY_PSK:
126                   String str = mWifiConfig.preSharedKey;
127                   mPassword.setText(str);
128                   mSecurity.setSelection(WPA_INDEX);
129                   break;
130             }
131         }
132 
133         mSsid.addTextChangedListener(this);
134         mPassword.addTextChangedListener(this);
135         ((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
136         mSecurity.setOnItemSelectedListener(this);
137 
138         super.onCreate(savedInstanceState);
139 
140         showSecurityFields();
141         validate();
142     }
143 
validate()144     private void validate() {
145         if ((mSsid != null && mSsid.length() == 0) ||
146                    (mSecurityType == AccessPoint.SECURITY_PSK && mPassword.length() < 8)) {
147             getButton(BUTTON_SUBMIT).setEnabled(false);
148         } else {
149             getButton(BUTTON_SUBMIT).setEnabled(true);
150         }
151     }
152 
onClick(View view)153     public void onClick(View view) {
154         mPassword.setInputType(
155                 InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
156                 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
157                 InputType.TYPE_TEXT_VARIATION_PASSWORD));
158     }
159 
onTextChanged(CharSequence s, int start, int before, int count)160     public void onTextChanged(CharSequence s, int start, int before, int count) {
161     }
162 
beforeTextChanged(CharSequence s, int start, int count, int after)163     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
164     }
165 
afterTextChanged(Editable editable)166     public void afterTextChanged(Editable editable) {
167         validate();
168     }
169 
onItemSelected(AdapterView parent, View view, int position, long id)170     public void onItemSelected(AdapterView parent, View view, int position, long id) {
171         if(position == OPEN_INDEX)
172             mSecurityType = AccessPoint.SECURITY_NONE;
173         else
174             mSecurityType = AccessPoint.SECURITY_PSK;
175         showSecurityFields();
176         validate();
177     }
178 
onNothingSelected(AdapterView parent)179     public void onNothingSelected(AdapterView parent) {
180     }
181 
showSecurityFields()182     private void showSecurityFields() {
183         if (mSecurityType == AccessPoint.SECURITY_NONE) {
184             mView.findViewById(R.id.fields).setVisibility(View.GONE);
185             return;
186         }
187         mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
188     }
189 }
190