OSDN Git Service

Disable tethering to enable Wifi
[android-x86/packages-apps-Settings.git] / src / com / android / settings / wifi / WifiEnabler.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 com.android.settings.WirelessSettings;
21
22 import android.content.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.net.NetworkInfo;
27 import android.net.wifi.SupplicantState;
28 import android.net.wifi.WifiInfo;
29 import android.net.wifi.WifiManager;
30 import android.preference.Preference;
31 import android.preference.CheckBoxPreference;
32 import android.provider.Settings;
33 import android.text.TextUtils;
34 import android.widget.Toast;
35
36 public class WifiEnabler implements Preference.OnPreferenceChangeListener {
37     private final Context mContext; 
38     private final CheckBoxPreference mCheckBox;
39     private final CharSequence mOriginalSummary;
40
41     private final WifiManager mWifiManager;
42     private final IntentFilter mIntentFilter;
43     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
44         @Override
45         public void onReceive(Context context, Intent intent) {
46             String action = intent.getAction();
47             if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
48                 handleWifiStateChanged(intent.getIntExtra(
49                         WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
50             } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
51                 handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
52                         intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
53             } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
54                 handleStateChanged(((NetworkInfo) intent.getParcelableExtra(
55                         WifiManager.EXTRA_NETWORK_INFO)).getDetailedState());
56             }
57         }
58     };
59
60     public WifiEnabler(Context context, CheckBoxPreference checkBox) {
61         mContext = context;
62         mCheckBox = checkBox;
63         mOriginalSummary = checkBox.getSummary();
64         checkBox.setPersistent(false);
65
66         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
67         mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
68         // The order matters! We really should not depend on this. :(
69         mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
70         mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
71     }
72
73     public void resume() {
74         // Wi-Fi state is sticky, so just let the receiver update UI
75         mContext.registerReceiver(mReceiver, mIntentFilter);
76         mCheckBox.setOnPreferenceChangeListener(this);
77     }
78     
79     public void pause() {
80         mContext.unregisterReceiver(mReceiver);
81         mCheckBox.setOnPreferenceChangeListener(null);
82     }
83     
84     public boolean onPreferenceChange(Preference preference, Object value) {
85         boolean enable = (Boolean) value;
86     
87         // Show toast message if Wi-Fi is not allowed in airplane mode
88         if (enable && !WirelessSettings
89                 .isRadioAllowed(mContext, Settings.System.RADIO_WIFI)) {
90             Toast.makeText(mContext, R.string.wifi_in_airplane_mode,
91                     Toast.LENGTH_SHORT).show();
92             return false;
93         }
94
95         /**
96          * Disable tethering if enabling Wifi
97          */
98         int wifiApState = mWifiManager.getWifiApState();
99         if (enable && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
100                 (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
101             mWifiManager.setWifiApEnabled(null, false);
102         }
103         if (mWifiManager.setWifiEnabled(enable)) {
104             mCheckBox.setEnabled(false);
105         } else {
106             mCheckBox.setSummary(R.string.wifi_error);
107         }
108
109         // Don't update UI to opposite state until we're sure
110         return false;
111     }
112     
113     private void handleWifiStateChanged(int state) {
114         switch (state) {
115             case WifiManager.WIFI_STATE_ENABLING:
116                 mCheckBox.setSummary(R.string.wifi_starting);
117                 mCheckBox.setEnabled(false);
118                 break;
119             case WifiManager.WIFI_STATE_ENABLED:
120                 mCheckBox.setChecked(true);
121                 mCheckBox.setSummary(null);
122                 mCheckBox.setEnabled(true);
123                 break;
124             case WifiManager.WIFI_STATE_DISABLING:
125                 mCheckBox.setSummary(R.string.wifi_stopping);
126                 mCheckBox.setEnabled(false);
127                 break;
128             case WifiManager.WIFI_STATE_DISABLED:
129                 mCheckBox.setChecked(false);
130                 mCheckBox.setSummary(mOriginalSummary);
131                 mCheckBox.setEnabled(true);
132                 break;
133             default:
134                 mCheckBox.setChecked(false);
135                 mCheckBox.setSummary(R.string.wifi_error);
136                 mCheckBox.setEnabled(true);
137         }
138     }
139
140     private void handleStateChanged(NetworkInfo.DetailedState state) {
141         // WifiInfo is valid if and only if Wi-Fi is enabled.
142         // Here we use the state of the check box as an optimization.
143         if (state != null && mCheckBox.isChecked()) {
144             WifiInfo info = mWifiManager.getConnectionInfo();
145             if (info != null) {
146                 mCheckBox.setSummary(Summary.get(mContext, info.getSSID(), state));
147             }
148         }
149     }
150 }