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 com.android.settings.R;
20 import android.app.Dialog;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.preference.ListPreference;
27 import android.preference.Preference;
28 import android.preference.PreferenceActivity;
29 import android.preference.PreferenceScreen;
30 import android.preference.CheckBoxPreference;
31 import android.provider.Settings;
32 import android.util.Log;
33 import android.net.wifi.WifiConfiguration;
34 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
35 import android.net.wifi.WifiConfiguration.KeyMgmt;
36 import android.net.wifi.WifiManager;
37 import android.os.Bundle;
40 * Displays preferences for Tethering.
42 public class WifiApSettings extends PreferenceActivity
43 implements DialogInterface.OnClickListener {
45 private static final String WIFI_AP_SSID_AND_SECURITY = "wifi_ap_ssid_and_security";
46 private static final String ENABLE_WIFI_AP = "enable_wifi_ap";
47 private static final int CONFIG_SUBTEXT = R.string.wifi_tether_configure_subtext;
49 private static final int OPEN_INDEX = 0;
50 private static final int WPA_INDEX = 1;
52 private static final int DIALOG_AP_SETTINGS = 1;
54 private String[] mSecurityType;
55 private Preference mCreateNetwork;
56 private CheckBoxPreference mEnableWifiAp;
58 private WifiApDialog mDialog;
59 private WifiManager mWifiManager;
60 private WifiApEnabler mWifiApEnabler;
61 private WifiConfiguration mWifiConfig = null;
64 protected void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
67 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
68 mWifiConfig = mWifiManager.getWifiApConfiguration();
69 mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
71 addPreferencesFromResource(R.xml.wifi_ap_settings);
73 mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
74 mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
76 mWifiApEnabler = new WifiApEnabler(this, mEnableWifiAp);
78 if(mWifiConfig == null) {
79 String s = getString(com.android.internal.R.string.wifi_tether_configure_ssid_default);
80 mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
81 s, mSecurityType[OPEN_INDEX]));
83 mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
85 mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
86 mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));
91 protected Dialog onCreateDialog(int id) {
92 if (id == DIALOG_AP_SETTINGS) {
93 mDialog = new WifiApDialog(this, this, mWifiConfig);
100 protected void onResume() {
102 mWifiApEnabler.resume();
106 protected void onPause() {
108 mWifiApEnabler.pause();
112 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
113 if (preference == mCreateNetwork) {
114 showDialog(DIALOG_AP_SETTINGS);
119 public void onClick(DialogInterface dialogInterface, int button) {
121 if (button == DialogInterface.BUTTON_POSITIVE) {
122 mWifiConfig = mDialog.getConfig();
123 if (mWifiConfig != null) {
125 * if soft AP is running, bring up with new config
126 * else update the configuration alone
128 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
129 mWifiManager.setWifiApEnabled(mWifiConfig, true);
131 * There is no tether notification on changing AP
132 * configuration. Update status with new config.
134 mWifiApEnabler.updateConfigSummary(mWifiConfig);
136 mWifiManager.setWifiApConfiguration(mWifiConfig);
138 mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
140 mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
141 mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));