Add AP support for tethering
First phase of the AP support in UI and framework Bug: 2421638 Change-Id: I0b6c31be5fe79bd4d33c292464d271b054754e8d
This commit is contained in:
198
src/com/android/settings/wifi/WifiApDialog.java
Normal file
198
src/com/android/settings/wifi/WifiApDialog.java
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
|
||||
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.text.InputType;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
/**
|
||||
* Dialog to configure the SSID and security settings
|
||||
* for Access Point operation
|
||||
*/
|
||||
class WifiApDialog extends AlertDialog implements View.OnClickListener,
|
||||
TextWatcher, AdapterView.OnItemSelectedListener {
|
||||
|
||||
static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE;
|
||||
|
||||
private final DialogInterface.OnClickListener mListener;
|
||||
|
||||
private View mView;
|
||||
private TextView mSsid;
|
||||
private int mSecurityType = AccessPoint.SECURITY_NONE;
|
||||
private EditText mPassword;
|
||||
|
||||
WifiConfiguration mWifiConfig;
|
||||
|
||||
public WifiApDialog(Context context, DialogInterface.OnClickListener listener,
|
||||
WifiConfiguration wifiConfig) {
|
||||
super(context);
|
||||
mListener = listener;
|
||||
mWifiConfig = wifiConfig;
|
||||
if (wifiConfig != null)
|
||||
mSecurityType = AccessPoint.getSecurity(wifiConfig);
|
||||
}
|
||||
|
||||
public WifiConfiguration getConfig() {
|
||||
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
|
||||
config.SSID = mSsid.getText().toString();
|
||||
|
||||
switch (mSecurityType) {
|
||||
case AccessPoint.SECURITY_NONE:
|
||||
config.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||
return config;
|
||||
|
||||
case AccessPoint.SECURITY_WEP:
|
||||
config.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||
config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
|
||||
if (mPassword.length() != 0) {
|
||||
int length = mPassword.length();
|
||||
String password = mPassword.getText().toString();
|
||||
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
|
||||
if ((length == 10 || length == 26 || length == 58) &&
|
||||
password.matches("[0-9A-Fa-f]*")) {
|
||||
config.wepKeys[0] = password;
|
||||
} else {
|
||||
config.wepKeys[0] = '"' + password + '"';
|
||||
}
|
||||
}
|
||||
return config;
|
||||
|
||||
case AccessPoint.SECURITY_PSK:
|
||||
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
|
||||
config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
|
||||
if (mPassword.length() != 0) {
|
||||
String password = mPassword.getText().toString();
|
||||
if (password.matches("[0-9A-Fa-f]{64}")) {
|
||||
config.preSharedKey = password;
|
||||
} else {
|
||||
config.preSharedKey = '"' + password + '"';
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security));
|
||||
mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null);
|
||||
|
||||
setView(mView);
|
||||
setInverseBackgroundForced(true);
|
||||
|
||||
Context context = getContext();
|
||||
|
||||
setTitle(R.string.wifi_ap_configure_network);
|
||||
mView.findViewById(R.id.type).setVisibility(View.VISIBLE);
|
||||
mSsid = (TextView) mView.findViewById(R.id.ssid);
|
||||
mPassword = (EditText) mView.findViewById(R.id.password);
|
||||
|
||||
setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener);
|
||||
setButton(DialogInterface.BUTTON_NEGATIVE,
|
||||
context.getString(R.string.wifi_cancel), mListener);
|
||||
|
||||
if (mWifiConfig != null) {
|
||||
mSsid.setText(mWifiConfig.SSID);
|
||||
switch (mSecurityType) {
|
||||
case AccessPoint.SECURITY_WEP:
|
||||
mPassword.setText(mWifiConfig.wepKeys[0]);
|
||||
break;
|
||||
case AccessPoint.SECURITY_PSK:
|
||||
mPassword.setText(mWifiConfig.preSharedKey);
|
||||
break;
|
||||
}
|
||||
mSecurity.setSelection(mSecurityType);
|
||||
}
|
||||
|
||||
mSsid.addTextChangedListener(this);
|
||||
mPassword.addTextChangedListener(this);
|
||||
((CheckBox) mView.findViewById(R.id.show_password)).setOnClickListener(this);
|
||||
mSecurity.setOnItemSelectedListener(this);
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
showSecurityFields();
|
||||
validate();
|
||||
}
|
||||
|
||||
private void validate() {
|
||||
if ((mSsid != null && mSsid.length() == 0) ||
|
||||
(mSecurityType == AccessPoint.SECURITY_WEP && mPassword.length() == 0) ||
|
||||
(mSecurityType == AccessPoint.SECURITY_PSK && mPassword.length() < 8)) {
|
||||
getButton(BUTTON_SUBMIT).setEnabled(false);
|
||||
} else {
|
||||
getButton(BUTTON_SUBMIT).setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
mPassword.setInputType(
|
||||
InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ?
|
||||
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
|
||||
InputType.TYPE_TEXT_VARIATION_PASSWORD));
|
||||
}
|
||||
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
}
|
||||
|
||||
public void afterTextChanged(Editable editable) {
|
||||
validate();
|
||||
}
|
||||
|
||||
public void onItemSelected(AdapterView parent, View view, int position, long id) {
|
||||
mSecurityType = position;
|
||||
showSecurityFields();
|
||||
validate();
|
||||
}
|
||||
|
||||
public void onNothingSelected(AdapterView parent) {
|
||||
}
|
||||
|
||||
private void showSecurityFields() {
|
||||
if (mSecurityType == AccessPoint.SECURITY_NONE) {
|
||||
mView.findViewById(R.id.fields).setVisibility(View.GONE);
|
||||
return;
|
||||
}
|
||||
mView.findViewById(R.id.fields).setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user