WifiEnabler.java revision b90452f3d26201ea6a231f2150204241e66cd3fb
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 com.android.settings.R;
20import com.android.settings.WirelessSettings;
21
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.net.NetworkInfo;
27import android.net.wifi.WifiManager;
28import android.preference.Preference;
29import android.preference.CheckBoxPreference;
30import android.provider.Settings;
31import android.text.TextUtils;
32import android.widget.Toast;
33
34public class WifiEnabler implements Preference.OnPreferenceChangeListener {
35    private final Context mContext;
36    private final CheckBoxPreference mCheckBox;
37    private final CharSequence mOriginalSummary;
38
39    private final WifiManager mWifiManager;
40    private final IntentFilter mIntentFilter;
41    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
42        @Override
43        public void onReceive(Context context, Intent intent) {
44            String action = intent.getAction();
45            if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
46                handleWifiStateChanged(intent.getIntExtra(
47                        WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
48            } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
49                handleNetworkStateChanged((NetworkInfo)
50                        intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO));
51            }
52        }
53    };
54
55    public WifiEnabler(Context context, CheckBoxPreference checkBox) {
56        this(context, (WifiManager) context.getSystemService(Context.WIFI_SERVICE),
57                checkBox);
58    }
59
60    public WifiEnabler(Context context, WifiManager wifiManager,
61            CheckBoxPreference checkBox) {
62        mContext = context;
63        mCheckBox = checkBox;
64        mWifiManager = wifiManager;
65        mOriginalSummary = checkBox.getSummary();
66        checkBox.setPersistent(false);
67
68        mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
69        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
70    }
71
72    public void resume() {
73        // Wi-Fi state is sticky, so just let the receiver update UI
74        mContext.registerReceiver(mReceiver, mIntentFilter);
75        mCheckBox.setOnPreferenceChangeListener(this);
76    }
77
78    public void pause() {
79        mContext.unregisterReceiver(mReceiver);
80        mCheckBox.setOnPreferenceChangeListener(null);
81    }
82
83    public boolean onPreferenceChange(Preference preference, Object value) {
84        boolean enable = (Boolean) value;
85
86        // Show toast message if Wi-Fi is not allowed in airplane mode
87        if (enable && !WirelessSettings
88                .isRadioAllowed(mContext, Settings.System.RADIO_WIFI)) {
89            Toast.makeText(mContext, R.string.wifi_in_airplane_mode,
90                    Toast.LENGTH_SHORT).show();
91            return false;
92        }
93
94        if (mWifiManager.setWifiEnabled(enable)) {
95            mCheckBox.setEnabled(false);
96        } else {
97            mCheckBox.setSummary(R.string.wifi_error);
98        }
99
100        // Don't update UI to opposite state until we're sure
101        return false;
102    }
103
104    private void handleWifiStateChanged(int state) {
105        switch (state) {
106            case WifiManager.WIFI_STATE_ENABLING:
107                mCheckBox.setSummary(R.string.wifi_starting);
108                mCheckBox.setEnabled(false);
109                break;
110            case WifiManager.WIFI_STATE_ENABLED:
111                mCheckBox.setChecked(true);
112                mCheckBox.setSummary(null);
113                mCheckBox.setEnabled(true);
114                break;
115            case WifiManager.WIFI_STATE_DISABLING:
116                mCheckBox.setSummary(R.string.wifi_stopping);
117                mCheckBox.setEnabled(false);
118                break;
119            case WifiManager.WIFI_STATE_DISABLED:
120                mCheckBox.setChecked(false);
121                mCheckBox.setSummary(mOriginalSummary);
122                mCheckBox.setEnabled(true);
123                break;
124            default:
125                mCheckBox.setChecked(false);
126                mCheckBox.setSummary(R.string.wifi_error);
127                mCheckBox.setEnabled(true);
128        }
129    }
130
131    private void handleNetworkStateChanged(NetworkInfo networkInfo) {
132        if (mWifiManager.isWifiEnabled()) {
133            String summary = WifiStatus.getStatus(mContext,
134                    mWifiManager.getConnectionInfo().getSSID(), networkInfo.getDetailedState());
135            mCheckBox.setSummary(summary);
136        }
137    }
138}
139