WifiEnabler.java revision d9e2c946af8cebeaf9fa81ca5657d3932ee56d1b
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
17package com.android.settings.wifi;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.net.NetworkInfo;
24import android.net.wifi.SupplicantState;
25import android.net.wifi.WifiInfo;
26import android.net.wifi.WifiManager;
27import android.os.Handler;
28import android.os.Message;
29import android.os.UserHandle;
30import android.os.UserManager;
31import android.provider.Settings;
32import android.widget.Switch;
33import android.widget.Toast;
34
35import com.android.internal.logging.MetricsProto.MetricsEvent;
36import com.android.settings.R;
37import com.android.settings.core.instrumentation.MetricsFeatureProvider;
38import com.android.settings.search.Index;
39import com.android.settings.widget.SwitchBar;
40import com.android.settingslib.RestrictedLockUtils;
41import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
42import com.android.settingslib.WirelessUtils;
43
44import java.util.concurrent.atomic.AtomicBoolean;
45
46public class WifiEnabler implements SwitchBar.OnSwitchChangeListener  {
47
48    private final SwitchBar mSwitchBar;
49    private final WifiManager mWifiManager;
50    private final MetricsFeatureProvider mMetricsFeatureProvider;
51
52    private Context mContext;
53    private boolean mListeningToOnSwitchChange = false;
54    private AtomicBoolean mConnected = new AtomicBoolean(false);
55
56
57    private boolean mStateMachineEvent;
58    private final IntentFilter mIntentFilter;
59    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
60        @Override
61        public void onReceive(Context context, Intent intent) {
62            String action = intent.getAction();
63            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
64                handleWifiStateChanged(intent.getIntExtra(
65                        WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
66            } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
67                if (!mConnected.get()) {
68                    handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState)
69                            intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE)));
70                }
71            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
72                NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
73                        WifiManager.EXTRA_NETWORK_INFO);
74                mConnected.set(info.isConnected());
75                handleStateChanged(info.getDetailedState());
76            }
77        }
78    };
79
80    private static final String EVENT_DATA_IS_WIFI_ON = "is_wifi_on";
81    private static final int EVENT_UPDATE_INDEX = 0;
82
83    private Handler mHandler = new Handler() {
84        @Override
85        public void handleMessage(Message msg) {
86            switch (msg.what) {
87                case EVENT_UPDATE_INDEX:
88                    final boolean isWiFiOn = msg.getData().getBoolean(EVENT_DATA_IS_WIFI_ON);
89                    Index.getInstance(mContext).updateFromClassNameResource(
90                            WifiSettings.class.getName(), true, isWiFiOn);
91                    break;
92            }
93        }
94    };
95
96    public WifiEnabler(Context context, SwitchBar switchBar,
97            MetricsFeatureProvider metricsFeatureProvider) {
98        mContext = context;
99        mSwitchBar = switchBar;
100        mMetricsFeatureProvider = metricsFeatureProvider;
101        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
102
103        mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
104        // The order matters! We really should not depend on this. :(
105        mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
106        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
107
108        setupSwitchBar();
109    }
110
111    public void setupSwitchBar() {
112        final int state = mWifiManager.getWifiState();
113        handleWifiStateChanged(state);
114        if (!mListeningToOnSwitchChange) {
115            mSwitchBar.addOnSwitchChangeListener(this);
116            mListeningToOnSwitchChange = true;
117        }
118        mSwitchBar.show();
119    }
120
121    public void teardownSwitchBar() {
122        if (mListeningToOnSwitchChange) {
123            mSwitchBar.removeOnSwitchChangeListener(this);
124            mListeningToOnSwitchChange = false;
125        }
126        mSwitchBar.hide();
127    }
128
129    public void resume(Context context) {
130        mContext = context;
131        // Wi-Fi state is sticky, so just let the receiver update UI
132        mContext.registerReceiver(mReceiver, mIntentFilter);
133        if (!mListeningToOnSwitchChange) {
134            mSwitchBar.addOnSwitchChangeListener(this);
135            mListeningToOnSwitchChange = true;
136        }
137    }
138
139    public void pause() {
140        mContext.unregisterReceiver(mReceiver);
141        if (mListeningToOnSwitchChange) {
142            mSwitchBar.removeOnSwitchChangeListener(this);
143            mListeningToOnSwitchChange = false;
144        }
145    }
146
147    private void handleWifiStateChanged(int state) {
148        // Clear any previous state
149        mSwitchBar.setDisabledByAdmin(null);
150
151        switch (state) {
152            case WifiManager.WIFI_STATE_ENABLING:
153                mSwitchBar.setEnabled(false);
154                break;
155            case WifiManager.WIFI_STATE_ENABLED:
156                setSwitchBarChecked(true);
157                mSwitchBar.setEnabled(true);
158                updateSearchIndex(true);
159                break;
160            case WifiManager.WIFI_STATE_DISABLING:
161                mSwitchBar.setEnabled(false);
162                break;
163            case WifiManager.WIFI_STATE_DISABLED:
164                setSwitchBarChecked(false);
165                mSwitchBar.setEnabled(true);
166                updateSearchIndex(false);
167                break;
168            default:
169                setSwitchBarChecked(false);
170                mSwitchBar.setEnabled(true);
171                updateSearchIndex(false);
172        }
173        if (mayDisableTethering(!mSwitchBar.isChecked())) {
174            if (RestrictedLockUtils.hasBaseUserRestriction(mContext,
175                    UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId())) {
176                mSwitchBar.setEnabled(false);
177            } else {
178                final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext,
179                    UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId());
180                mSwitchBar.setDisabledByAdmin(admin);
181            }
182        }
183    }
184
185    private void updateSearchIndex(boolean isWiFiOn) {
186        mHandler.removeMessages(EVENT_UPDATE_INDEX);
187
188        Message msg = new Message();
189        msg.what = EVENT_UPDATE_INDEX;
190        msg.getData().putBoolean(EVENT_DATA_IS_WIFI_ON, isWiFiOn);
191        mHandler.sendMessage(msg);
192    }
193
194    private void setSwitchBarChecked(boolean checked) {
195        mStateMachineEvent = true;
196        mSwitchBar.setChecked(checked);
197        mStateMachineEvent = false;
198    }
199
200    private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) {
201        // After the refactoring from a CheckBoxPreference to a Switch, this method is useless since
202        // there is nowhere to display a summary.
203        // This code is kept in case a future change re-introduces an associated text.
204        /*
205        // WifiInfo is valid if and only if Wi-Fi is enabled.
206        // Here we use the state of the switch as an optimization.
207        if (state != null && mSwitch.isChecked()) {
208            WifiInfo info = mWifiManager.getConnectionInfo();
209            if (info != null) {
210                //setSummary(Summary.get(mContext, info.getSSID(), state));
211            }
212        }
213        */
214    }
215
216    @Override
217    public void onSwitchChanged(Switch switchView, boolean isChecked) {
218        //Do nothing if called as a result of a state machine event
219        if (mStateMachineEvent) {
220            return;
221        }
222        // Show toast message if Wi-Fi is not allowed in airplane mode
223        if (isChecked && !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
224            Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
225            // Reset switch to off. No infinite check/listenenr loop.
226            mSwitchBar.setChecked(false);
227            return;
228        }
229
230        // Disable tethering if enabling Wifi
231        if (mayDisableTethering(isChecked)) {
232            mWifiManager.setWifiApEnabled(null, false);
233        }
234        if (isChecked) {
235            mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_WIFI_ON);
236        } else {
237            // Log if user was connected at the time of switching off.
238            mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_WIFI_OFF,
239                    mConnected.get());
240        }
241        if (!mWifiManager.setWifiEnabled(isChecked)) {
242            // Error
243            mSwitchBar.setEnabled(true);
244            Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
245        }
246    }
247
248    private boolean mayDisableTethering(boolean isChecked) {
249        final int wifiApState = mWifiManager.getWifiApState();
250        return isChecked && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
251            (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED));
252    }
253}
254