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.os.Bundle; 26 import android.text.Editable; 27 import android.text.InputType; 28 import android.text.TextWatcher; 29 import android.view.View; 30 import android.widget.AdapterView; 31 import android.widget.CheckBox; 32 import android.widget.EditText; 33 import android.widget.Spinner; 34 import android.widget.TextView; 35 36 import com.android.settings.R; 37 38 /** 39 * Dialog to configure the SSID and security settings 40 * for Access Point operation 41 */ 42 public class WifiApDialog extends AlertDialog implements View.OnClickListener, 43 TextWatcher, AdapterView.OnItemSelectedListener { 44 45 static final int BUTTON_SUBMIT = DialogInterface.BUTTON_POSITIVE; 46 47 private final DialogInterface.OnClickListener mListener; 48 49 public static final int OPEN_INDEX = 0; 50 public static final int WPA2_INDEX = 1; 51 52 private View mView; 53 private TextView mSsid; 54 private int mSecurityTypeIndex = OPEN_INDEX; 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 mSecurityTypeIndex = getSecurityTypeIndex(wifiConfig); 66 } 67 } 68 69 public static int getSecurityTypeIndex(WifiConfiguration wifiConfig) { 70 if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA2_PSK)) { 71 return WPA2_INDEX; 72 } 73 return OPEN_INDEX; 74 } 75 76 public WifiConfiguration getConfig() { 77 78 WifiConfiguration config = new WifiConfiguration(); 79 80 /** 81 * TODO: SSID in WifiConfiguration for soft ap 82 * is being stored as a raw string without quotes. 83 * This is not the case on the client side. We need to 84 * make things consistent and clean it up 85 */ 86 config.SSID = mSsid.getText().toString(); 87 88 switch (mSecurityTypeIndex) { 89 case OPEN_INDEX: 90 config.allowedKeyManagement.set(KeyMgmt.NONE); 91 return config; 92 93 case WPA2_INDEX: 94 config.allowedKeyManagement.set(KeyMgmt.WPA2_PSK); 95 config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN); 96 if (mPassword.length() != 0) { 97 String password = mPassword.getText().toString(); 98 config.preSharedKey = password; 99 } 100 return config; 101 } 102 return null; 103 } 104 105 @Override 106 protected void onCreate(Bundle savedInstanceState) { 107 108 mView = getLayoutInflater().inflate(R.layout.wifi_ap_dialog, null); 109 Spinner mSecurity = ((Spinner) mView.findViewById(R.id.security)); 110 111 setView(mView); 112 setInverseBackgroundForced(true); 113 114 Context context = getContext(); 115 116 setTitle(R.string.wifi_tether_configure_ap_text); 117 mView.findViewById(R.id.type).setVisibility(View.VISIBLE); 118 mSsid = (TextView) mView.findViewById(R.id.ssid); 119 mPassword = (EditText) mView.findViewById(R.id.password); 120 121 setButton(BUTTON_SUBMIT, context.getString(R.string.wifi_save), mListener); 122 setButton(DialogInterface.BUTTON_NEGATIVE, 123 context.getString(R.string.wifi_cancel), mListener); 124 125 if (mWifiConfig != null) { 126 mSsid.setText(mWifiConfig.SSID); 127 mSecurity.setSelection(mSecurityTypeIndex); 128 if (mSecurityTypeIndex == WPA2_INDEX) { 129 mPassword.setText(mWifiConfig.preSharedKey); 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 144 private void validate() { 145 if ((mSsid != null && mSsid.length() == 0) || 146 ((mSecurityTypeIndex == WPA2_INDEX)&& 147 mPassword.length() < 8)) { 148 getButton(BUTTON_SUBMIT).setEnabled(false); 149 } else { 150 getButton(BUTTON_SUBMIT).setEnabled(true); 151 } 152 } 153 154 public void onClick(View view) { 155 mPassword.setInputType( 156 InputType.TYPE_CLASS_TEXT | (((CheckBox) view).isChecked() ? 157 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : 158 InputType.TYPE_TEXT_VARIATION_PASSWORD)); 159 } 160 161 public void onTextChanged(CharSequence s, int start, int before, int count) { 162 } 163 164 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 165 } 166 167 public void afterTextChanged(Editable editable) { 168 validate(); 169 } 170 171 @Override 172 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 173 mSecurityTypeIndex = position; 174 showSecurityFields(); 175 validate(); 176 } 177 178 @Override 179 public void onNothingSelected(AdapterView<?> parent) { 180 } 181 182 private void showSecurityFields() { 183 if (mSecurityTypeIndex == OPEN_INDEX) { 184 mView.findViewById(R.id.fields).setVisibility(View.GONE); 185 return; 186 } 187 mView.findViewById(R.id.fields).setVisibility(View.VISIBLE); 188 } 189 } 190