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

WifiSettings

public class WifiSettings extends android.preference.PreferenceActivity implements DialogInterface.OnDismissListener, WifiLayer.Callback
Settings screen for WiFi. This will be launched from the main system settings.

Fields Summary
private static final String
TAG
private static final String
INSTANCE_KEY_DIALOG_BUNDLE
private android.app.Dialog
mDialog
private static final String
KEY_ADD_OTHER_NETWORK
private static final int
CONTEXT_MENU_ID_CONNECT
private static final int
CONTEXT_MENU_ID_FORGET
private static final int
CONTEXT_MENU_ID_CHANGE_PASSWORD
private static final int
MENU_ID_SCAN
private static final int
MENU_ID_ADVANCED
private static final String
KEY_WIFI_ENABLED
private static final String
KEY_OPEN_NETWORK_NOTIFICATIONS_ENABLED
private static final String
KEY_ACCESS_POINTS
private com.android.settings.ProgressCategory
mApCategory
private android.preference.CheckBoxPreference
mWifiEnabled
private WifiEnabler
mWifiEnabler
private android.preference.CheckBoxPreference
mOpenNetworkNotificationsEnabled
private android.preference.Preference
mAddOtherNetwork
private WeakHashMap
mAps
private WifiLayer
mWifiLayer
Constructors Summary
public WifiSettings()

        
    //============================
    // Activity lifecycle
    //============================
    
      
        mAps = new WeakHashMap<AccessPointState, AccessPointPreference>();
        mWifiLayer = new WifiLayer(this, this);
    
Methods Summary
private voidconnectToNetwork(AccessPointState state)
Decides what needs to happen to connect to a particular access point. If it is secured and doesn't already have a password, it will bring up a password box. Otherwise it will just connect.

        if (state.hasSecurity() && !state.hasPassword()) {
            showAccessPointDialog(state, AccessPointDialog.MODE_INFO);
        } else {
            mWifiLayer.connectToNetwork(state);
        }
    
private AccessPointStategetStateFromMenuInfo(android.view.ContextMenu.ContextMenuInfo menuInfo)

        if ((menuInfo == null) || !(menuInfo instanceof AdapterContextMenuInfo)) {
            return null;
        }
        
        AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) menuInfo;
        Preference pref = (Preference) getPreferenceScreen().getRootAdapter().getItem(
                adapterMenuInfo.position);
        if (pref == null || !(pref instanceof AccessPointPreference)) {
            return null;
        }

        return ((AccessPointPreference) pref).getAccessPointState();
    
public WifiLayergetWifiLayer()

        return mWifiLayer;
    
public voidonAccessPointSetChanged(AccessPointState ap, boolean added)


        AccessPointPreference pref = mAps.get(ap);
        
        if (WifiLayer.LOGV) {
            Log.v(TAG, "onAccessPointSetChanged with " + ap + " and "
                    + (added ? "added" : "removed") + ", found pref " + pref);
        }
        
        if (added) {
            
            if (pref == null) {
                pref = new AccessPointPreference(this, ap);
                mAps.put(ap, pref);
            } else {
                pref.setEnabled(true);
            }
            
            mApCategory.addPreference(pref);
            
        } else {

            mAps.remove(ap);
            
            if (pref != null) {
                mApCategory.removePreference(pref);
            }
            
        }
    
public voidonAccessPointsStateChanged(boolean enabled)

        if (enabled) {
            mApCategory.setEnabled(true);
        } else {
            mApCategory.removeAll();
            mAps.clear();
        }

        mAddOtherNetwork.setEnabled(enabled);
    
public booleanonContextItemSelected(android.view.MenuItem item)

        
        AccessPointState state = getStateFromMenuInfo(item.getMenuInfo());
        if (state == null) {
            return false;
        }

        switch (item.getItemId()) {
            
            case CONTEXT_MENU_ID_CONNECT:
                connectToNetwork(state);
                return true;
                
            case CONTEXT_MENU_ID_FORGET:
                mWifiLayer.forgetNetwork(state);
                return true;
                
            case CONTEXT_MENU_ID_CHANGE_PASSWORD:
                showAccessPointDialog(state, AccessPointDialog.MODE_CONFIGURE);
                return true;
                
            default:
                return false;
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)

        super.onCreate(savedInstanceState);

        onCreatePreferences();
        mWifiLayer.onCreate();

        onCreatedWifi();
        mWifiLayer.onCreatedCallback();
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, android.view.ContextMenu.ContextMenuInfo menuInfo)

        super.onCreateContextMenu(menu, v, menuInfo);
     
        AccessPointState state = getStateFromMenuInfo(menuInfo);
        if (state == null) {
            return;
        }
     
        menu.setHeaderTitle(state.getHumanReadableSsid());
        
        if (state.isConnectable()) {
            menu.add(0, CONTEXT_MENU_ID_CONNECT, 0, R.string.wifi_context_menu_connect);
        }
        
        if (state.isForgetable()) {
            menu.add(0, CONTEXT_MENU_ID_FORGET, 1, R.string.wifi_context_menu_forget);
            
            if (state.hasPassword()) {
                menu.add(0, CONTEXT_MENU_ID_CHANGE_PASSWORD, 2,
                        R.string.wifi_context_menu_change_password);
            }
        }
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);
        
        menu.add(0, MENU_ID_SCAN, 0, R.string.scan_wifi)
            .setIcon(R.drawable.ic_menu_scan_network);
        
        menu.add(0, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
            .setIcon(android.R.drawable.ic_menu_manage);
        
        return true;
    
private voidonCreatePreferences()
Shouldn't have any dependency on the wifi layer.

        addPreferencesFromResource(R.xml.wifi_settings);
        
        final PreferenceScreen preferenceScreen = getPreferenceScreen();
        
        mApCategory = (ProgressCategory) preferenceScreen.findPreference(KEY_ACCESS_POINTS);
        // We don't want the ordering to be the order preferences are added,
        // instead we want*:
        //   1) preferred, visible APs
        //   2) visible APs
        //   3) preferred, APs out of range
        //   * this ordering logic is in AccessPointPreference's compareTo
        mApCategory.setOrderingAsAdded(false);
        
        mWifiEnabled = (CheckBoxPreference) preferenceScreen.findPreference(KEY_WIFI_ENABLED);
        mWifiEnabler = new WifiEnabler(this, (WifiManager) getSystemService(WIFI_SERVICE),
                mWifiEnabled);
        
        mOpenNetworkNotificationsEnabled = (CheckBoxPreference) preferenceScreen
                .findPreference(KEY_OPEN_NETWORK_NOTIFICATIONS_ENABLED);
        mOpenNetworkNotificationsEnabled.setChecked(Settings.Secure.getInt(getContentResolver(),
            Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);        
        
        mAddOtherNetwork = preferenceScreen.findPreference(KEY_ADD_OTHER_NETWORK);
        
        registerForContextMenu(getListView());
    
private voidonCreatedWifi()

    
protected voidonDestroy()

        super.onDestroy();
        
        if (mDialog != null) {
            mDialog.dismiss();
        }
    
public voidonDismiss(android.content.DialogInterface dialog)
{@inheritDoc}

        if (dialog == mDialog) {
            mDialog = null;
        }
    
public voidonError(int messageResId)

        Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        super.onOptionsItemSelected(item);
        
        switch (item.getItemId()) {

            case MENU_ID_SCAN:
                mWifiLayer.attemptScan();
                return true;
                
            case MENU_ID_ADVANCED:
                Intent intent = new Intent(this, AdvancedSettings.class);
                startActivity(intent);
                return true;
                
            default:
                return false;
        }
    
protected voidonPause()

        super.onPause();
        mWifiLayer.onPause();
        mWifiEnabler.pause();
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)

        super.onPreferenceTreeClick(preferenceScreen, preference);

        if (preference == mAddOtherNetwork) {
            showAddOtherNetworkDialog();
        } else if (preference == mOpenNetworkNotificationsEnabled) {
            Settings.Secure.putInt(getContentResolver(),
                Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
                mOpenNetworkNotificationsEnabled.isChecked() ? 1 : 0);
        } else if (preference instanceof AccessPointPreference) {
            AccessPointState state = ((AccessPointPreference) preference).getAccessPointState();
            showAccessPointDialog(state, AccessPointDialog.MODE_INFO);
        }
        
        return false;
    
protected voidonRestoreInstanceState(android.os.Bundle state)

        super.onRestoreInstanceState(state);
        
        Bundle dialogBundle = state.getBundle(INSTANCE_KEY_DIALOG_BUNDLE);
        if (dialogBundle != null) {
            mDialog = new AccessPointDialog(this, mWifiLayer);
            mDialog.onRestoreInstanceState(dialogBundle);
            showDialog(mDialog);
        }
    
protected voidonResume()

        super.onResume();
        mWifiLayer.onResume();
        mWifiEnabler.resume();
    
public voidonRetryPassword(AccessPointState ap)


        if ((mDialog != null) && mDialog.isShowing()) {
            // If we're already showing a dialog, ignore this request
            return;
        }
        
        showAccessPointDialog(ap, AccessPointDialog.MODE_RETRY_PASSWORD);
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        
        if (mDialog != null) {
            Bundle dialogBundle = mDialog.onSaveInstanceState();
            outState.putBundle(INSTANCE_KEY_DIALOG_BUNDLE, dialogBundle);
        }
    
public voidonScanningStatusChanged(boolean started)

        mApCategory.setProgress(started);
    
public voidshowAccessPointDialog(AccessPointState state, int mode)

        AccessPointDialog dialog = new AccessPointDialog(this, mWifiLayer);
        dialog.setMode(mode);
        dialog.setState(state);
        showDialog(dialog);
    
private voidshowAddOtherNetworkDialog()

        AccessPointDialog dialog = new AccessPointDialog(this, mWifiLayer);
        dialog.setState(new AccessPointState(this));
        dialog.setMode(AccessPointDialog.MODE_CONFIGURE);
        dialog.setTitle(R.string.wifi_add_other_network);
        dialog.setAutoSecurityAllowed(false);
        showDialog(dialog);
    
private voidshowDialog(android.app.Dialog dialog)

        // Have only one dialog open at a time
        if (mDialog != null) {
            mDialog.dismiss();
        }
        
        mDialog = dialog;
        dialog.setOnDismissListener(this);
        if (dialog != null) {
            dialog.show();
        }