FileDocCategorySizeDatePackage
WifiEnabler.javaAPI DocAndroid 1.5 API7294Wed May 06 22:42:48 BST 2009com.android.settings.wifi

WifiEnabler

public class WifiEnabler extends Object implements Preference.OnPreferenceChangeListener

Fields Summary
private static final boolean
LOCAL_LOGD
private static final String
TAG
private final android.content.Context
mContext
private final android.net.wifi.WifiManager
mWifiManager
private final android.preference.CheckBoxPreference
mWifiCheckBoxPref
private final CharSequence
mOriginalSummary
private final android.content.IntentFilter
mWifiStateFilter
private final android.content.BroadcastReceiver
mWifiStateReceiver
Constructors Summary
public WifiEnabler(android.content.Context context, android.net.wifi.WifiManager wifiManager, android.preference.CheckBoxPreference wifiCheckBoxPreference)

    
        
              
        mContext = context;
        mWifiCheckBoxPref = wifiCheckBoxPreference;
        mWifiManager = wifiManager;
        
        mOriginalSummary = wifiCheckBoxPreference.getSummary();
        wifiCheckBoxPreference.setPersistent(false);
        
        mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    
Methods Summary
private android.preference.PreferencegetDependencyPreference()

        String depKey = mWifiCheckBoxPref.getDependency();
        if (TextUtils.isEmpty(depKey)) {
            return null;
        }
        
        return mWifiCheckBoxPref.getPreferenceManager().findPreference(depKey);
    
private static java.lang.StringgetHumanReadableWifiState(int wifiState)

        switch (wifiState) {
            case WIFI_STATE_DISABLED:
                return "Disabled";
            case WIFI_STATE_DISABLING:
                return "Disabling";
            case WIFI_STATE_ENABLED:
                return "Enabled";
            case WIFI_STATE_ENABLING:
                return "Enabling";
            case WIFI_STATE_UNKNOWN:
                return "Unknown";
            default:
                return "Some other state!";    
        }
    
private voidhandleNetworkStateChanged(android.net.NetworkInfo networkInfo)


        if (LOCAL_LOGD) {
            Log.d(TAG, "Received network state changed to " + networkInfo);
        }
        
        if (mWifiManager.isWifiEnabled()) {
            String summary = WifiStatus.getStatus(mContext, 
                    mWifiManager.getConnectionInfo().getSSID(), networkInfo.getDetailedState());
            mWifiCheckBoxPref.setSummary(summary);
        }
    
private voidhandleWifiStateChanged(int wifiState, int previousWifiState)


        if (LOCAL_LOGD) {
            Log.d(TAG, "Received wifi state changed from "
                    + getHumanReadableWifiState(previousWifiState) + " to "
                    + getHumanReadableWifiState(wifiState));
        }
        
        if (wifiState == WIFI_STATE_DISABLED || wifiState == WIFI_STATE_ENABLED) {
            mWifiCheckBoxPref.setChecked(wifiState == WIFI_STATE_ENABLED);
            mWifiCheckBoxPref
                    .setSummary(wifiState == WIFI_STATE_DISABLED ? mOriginalSummary : null);
            
            mWifiCheckBoxPref.setEnabled(isEnabledByDependency());
            
        } else if (wifiState == WIFI_STATE_DISABLING || wifiState == WIFI_STATE_ENABLING) {
            mWifiCheckBoxPref.setSummary(wifiState == WIFI_STATE_ENABLING ? R.string.wifi_starting
                    : R.string.wifi_stopping);
            
        } else if (wifiState == WIFI_STATE_UNKNOWN) {
            int message = R.string.wifi_error;
            if (previousWifiState == WIFI_STATE_ENABLING) message = R.string.error_starting;
            else if (previousWifiState == WIFI_STATE_DISABLING) message = R.string.error_stopping;
            
            mWifiCheckBoxPref.setChecked(false);
            mWifiCheckBoxPref.setSummary(message);
            mWifiCheckBoxPref.setEnabled(true);
        }
    
private booleanisEnabledByDependency()

        Preference dep = getDependencyPreference();
        if (dep == null) {
            return true;
        }
        
        return !dep.shouldDisableDependents();
    
public booleanonPreferenceChange(android.preference.Preference preference, java.lang.Object value)

        // Turn on/off Wi-Fi
        setWifiEnabled((Boolean) value);
        
        // Don't update UI to opposite state until we're sure
        return false;
    
public voidpause()

        mContext.unregisterReceiver(mWifiStateReceiver);
        mWifiCheckBoxPref.setOnPreferenceChangeListener(null);
    
public voidresume()

        int state = mWifiManager.getWifiState();
        // This is the widget enabled state, not the preference toggled state
        mWifiCheckBoxPref.setEnabled(state == WIFI_STATE_ENABLED || state == WIFI_STATE_DISABLED
                || state == WIFI_STATE_UNKNOWN);

        mContext.registerReceiver(mWifiStateReceiver, mWifiStateFilter);
        mWifiCheckBoxPref.setOnPreferenceChangeListener(this);
    
private voidsetWifiEnabled(boolean enable)

        // Disable button
        mWifiCheckBoxPref.setEnabled(false);
        
        if (!mWifiManager.setWifiEnabled(enable)) {
            mWifiCheckBoxPref.setSummary(enable ? R.string.error_starting : R.string.error_stopping);
        }