OSDN Git Service

am 8815f032: Merge "Always set right auth_type value in apn."
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / WifiApSettings.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 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;
38
39 /*
40  * Displays preferences for Tethering.
41  */
42 public class WifiApSettings extends PreferenceActivity
43                             implements DialogInterface.OnClickListener {
44
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;
48
49     private static final int OPEN_INDEX = 0;
50     private static final int WPA_INDEX = 1;
51
52     private static final int DIALOG_AP_SETTINGS = 1;
53
54     private String[] mSecurityType;
55     private Preference mCreateNetwork;
56     private CheckBoxPreference mEnableWifiAp;
57
58     private WifiApDialog mDialog;
59     private WifiManager mWifiManager;
60     private WifiApEnabler mWifiApEnabler;
61     private WifiConfiguration mWifiConfig = null;
62
63     @Override
64     protected void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66
67         mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
68         mWifiConfig = mWifiManager.getWifiApConfiguration();
69         mSecurityType = getResources().getStringArray(R.array.wifi_ap_security);
70
71         addPreferencesFromResource(R.xml.wifi_ap_settings);
72
73         mCreateNetwork = findPreference(WIFI_AP_SSID_AND_SECURITY);
74         mEnableWifiAp = (CheckBoxPreference) findPreference(ENABLE_WIFI_AP);
75
76         mWifiApEnabler = new WifiApEnabler(this, mEnableWifiAp);
77
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]));
82         } else {
83             mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
84                                       mWifiConfig.SSID,
85                                       mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
86                                       mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));
87         }
88     }
89
90     @Override
91     protected Dialog onCreateDialog(int id) {
92         if (id == DIALOG_AP_SETTINGS) {
93             mDialog = new WifiApDialog(this, this, mWifiConfig);
94             return mDialog;
95         }
96         return null;
97     }
98
99     @Override
100     protected void onResume() {
101         super.onResume();
102         mWifiApEnabler.resume();
103     }
104
105     @Override
106     protected void onPause() {
107         super.onPause();
108         mWifiApEnabler.pause();
109     }
110
111     @Override
112     public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
113         if (preference == mCreateNetwork) {
114             showDialog(DIALOG_AP_SETTINGS);
115         }
116         return true;
117     }
118
119     public void onClick(DialogInterface dialogInterface, int button) {
120
121         if (button == DialogInterface.BUTTON_POSITIVE) {
122             mWifiConfig = mDialog.getConfig();
123             if (mWifiConfig != null) {
124                 /**
125                  * if soft AP is running, bring up with new config
126                  * else update the configuration alone
127                  */
128                 if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
129                     mWifiManager.setWifiApEnabled(mWifiConfig, true);
130                     /**
131                      * There is no tether notification on changing AP
132                      * configuration. Update status with new config.
133                      */
134                     mWifiApEnabler.updateConfigSummary(mWifiConfig);
135                 } else {
136                     mWifiManager.setWifiApConfiguration(mWifiConfig);
137                 }
138                 mCreateNetwork.setSummary(String.format(getString(CONFIG_SUBTEXT),
139                             mWifiConfig.SSID,
140                             mWifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK) ?
141                             mSecurityType[WPA_INDEX] : mSecurityType[OPEN_INDEX]));
142             }
143         }
144     }
145 }