FileDocCategorySizeDatePackage
NetworkSetting.javaAPI DocAndroid 1.5 API15026Wed May 06 22:42:46 BST 2009com.android.phone

NetworkSetting

public class NetworkSetting extends android.preference.PreferenceActivity implements DialogInterface.OnCancelListener
"Networks" settings UI for the Phone app.

Fields Summary
private static final String
LOG_TAG
private static final boolean
DBG
private static final int
EVENT_NETWORK_SCAN_COMPLETED
private static final int
EVENT_NETWORK_SELECTION_DONE
private static final int
EVENT_AUTO_SELECT_DONE
private static final int
DIALOG_NETWORK_SELECTION
private static final int
DIALOG_NETWORK_LIST_LOAD
private static final int
DIALOG_NETWORK_AUTO_SELECT
private static final String
LIST_NETWORKS_KEY
private static final String
BUTTON_SRCH_NETWRKS_KEY
private static final String
BUTTON_AUTO_SELECT_KEY
private HashMap
mNetworkMap
com.android.internal.telephony.Phone
mPhone
String
mNetworkSelectMsg
message for network selection
private android.preference.PreferenceGroup
mNetworkList
private android.preference.Preference
mSearchButton
private android.preference.Preference
mAutoSelect
private android.os.Handler
mHandler
private INetworkQueryService
mNetworkQueryService
Local service interface
private android.content.ServiceConnection
mNetworkQueryServiceConnection
Service connection
private INetworkQueryServiceCallback
mCallback
This implementation of INetworkQueryServiceCallback is used to receive callback notifications from the network query service.
Constructors Summary
Methods Summary
private voidclearList()

        for (Preference p : mNetworkMap.keySet()) {
            mNetworkList.removePreference(p);
        }
        mNetworkMap.clear();
    
private voiddisplayEmptyNetworkList(boolean flag)

        mNetworkList.setTitle(flag ? R.string.empty_networks_list : R.string.label_available);
    
private voiddisplayNetworkQueryFailed(int error)

        String status = getResources().getString(R.string.network_query_error);

        NotificationMgr.getDefault().postTransientNotification(
                        NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);
    
private voiddisplayNetworkSelectionFailed(java.lang.Throwable ex)

        String status = getResources().getString(R.string.not_allowed);

        NotificationMgr.getDefault().postTransientNotification(
                        NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);
    
private voiddisplayNetworkSelectionSucceeded()

        String status = getResources().getString(R.string.registration_done);

        NotificationMgr.getDefault().postTransientNotification(
                        NotificationMgr.NETWORK_SELECTION_NOTIFICATION, status);

        mHandler.postDelayed(new Runnable() {
            public void run() {
                finish();
            }
        }, 3000);
    
private voiddisplayNetworkSeletionInProgress(java.lang.String networkStr)

        // TODO: use notification manager?
        mNetworkSelectMsg = getResources().getString(R.string.register_on_network, networkStr);

        showDialog(DIALOG_NETWORK_SELECTION);
    
public java.lang.StringgetNormalizedCarrierName(com.android.internal.telephony.gsm.NetworkInfo ni)

        if (ni != null) {
            return ni.getOperatorAlphaLong() + " (" + ni.getOperatorNumeric() + ")";
        }
        return null;
    
private voidloadNetworksList()

        if (DBG) log("load networks list...");
        showDialog(DIALOG_NETWORK_LIST_LOAD);

        // delegate query request to the service.
        try {
            mNetworkQueryService.startNetworkQuery(mCallback);
        } catch (RemoteException e) {
        }

        displayEmptyNetworkList(false);
    
private voidlog(java.lang.String msg)

        Log.d(LOG_TAG, "[NetworksList] " + msg);
    
private voidnetworksListLoaded(java.util.List result, int status)
networksListLoaded has been rewritten to take an array of NetworkInfo objects and a status field, instead of an AsyncResult. Otherwise, the functionality which takes the NetworkInfo array and creates a list of preferences from it, remains unchanged.

        if (DBG) log("networks list loaded");

        // update the state of the preferences.
        if (DBG) log("hideProgressPanel");
        dismissDialog(DIALOG_NETWORK_LIST_LOAD);
        getPreferenceScreen().setEnabled(true);
        clearList();

        if (status != NetworkQueryService.QUERY_OK) {
            if (DBG) log("error while querying available networks");
            displayNetworkQueryFailed(status);
            displayEmptyNetworkList(true);
        } else {
            if (result != null){
                displayEmptyNetworkList(false);

                // create a preference for each item in the list.
                // just use the operator name instead of the mildly
                // confusing mcc/mnc.
                for (NetworkInfo ni : result) {
                    Preference carrier = new Preference(this, null);
                    carrier.setTitle(ni.getOperatorAlphaLong());
                    carrier.setPersistent(false);
                    mNetworkList.addPreference(carrier);
                    mNetworkMap.put(carrier, ni);

                    if (DBG) log("  " + ni);
                }

            } else {
                displayEmptyNetworkList(true);
            }
        }
    
public voidonCancel(android.content.DialogInterface dialog)

        // request that the service stop the query with this callback object.
        try {
            mNetworkQueryService.stopNetworkQuery(mCallback);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
        finish();
    
protected voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        addPreferencesFromResource(R.xml.carrier_select);

        mPhone = PhoneApp.getInstance().phone;

        mNetworkList = (PreferenceGroup) getPreferenceScreen().findPreference(LIST_NETWORKS_KEY);
        mNetworkMap = new HashMap<Preference, NetworkInfo>();

        mSearchButton = getPreferenceScreen().findPreference(BUTTON_SRCH_NETWRKS_KEY);
        mAutoSelect = getPreferenceScreen().findPreference(BUTTON_AUTO_SELECT_KEY);

        // Start the Network Query service, and bind it.
        // The OS knows to start he service only once and keep the instance around (so
        // long as startService is called) until a stopservice request is made.  Since
        // we want this service to just stay in the background until it is killed, we
        // don't bother stopping it from our end.
        startService (new Intent(this, NetworkQueryService.class));
        bindService (new Intent(this, NetworkQueryService.class), mNetworkQueryServiceConnection,
                Context.BIND_AUTO_CREATE);
    
protected android.app.DialogonCreateDialog(int id)


        if ((id == DIALOG_NETWORK_SELECTION) || (id == DIALOG_NETWORK_LIST_LOAD) ||
                (id == DIALOG_NETWORK_AUTO_SELECT)) {
            ProgressDialog dialog = new ProgressDialog(this);
            switch (id) {
                case DIALOG_NETWORK_SELECTION:
                    // It would be more efficient to reuse this dialog by moving
                    // this setMessage() into onPreparedDialog() and NOT use
                    // removeDialog().  However, this is not possible since the
                    // message is rendered only 2 times in the ProgressDialog -
                    // after show() and before onCreate.
                    dialog.setMessage(mNetworkSelectMsg);
                    dialog.setCancelable(false);
                    dialog.setIndeterminate(true);
                    break;
                case DIALOG_NETWORK_AUTO_SELECT:
                    dialog.setMessage(getResources().getString(R.string.register_automatically));
                    dialog.setCancelable(false);
                    dialog.setIndeterminate(true);
                    break;
                case DIALOG_NETWORK_LIST_LOAD:
                default:
                    // reinstate the cancelablity of the dialog.
                    dialog.setMessage(getResources().getString(R.string.load_networks_progress));
                    dialog.setCancelable(true);
                    dialog.setOnCancelListener(this);
                    break;
            }
            return dialog;
        }
        return null;
    
protected voidonDestroy()
Override onDestroy() to unbind the query service, avoiding service leak exceptions.

        // unbind the service.
        unbindService(mNetworkQueryServiceConnection);

        super.onDestroy();
    
public booleanonPreferenceTreeClick(android.preference.PreferenceScreen preferenceScreen, android.preference.Preference preference)


    
          
        boolean handled = false;

        if (preference == mSearchButton) {
            loadNetworksList();
            handled = true;
        } else if (preference == mAutoSelect) {
            selectNetworkAutomatic();
            handled = true;
        } else {
            Preference selectedCarrier = preference;

            String networkStr = selectedCarrier.getTitle().toString();
            if (DBG) log("selected network: " + networkStr);

            Message msg = mHandler.obtainMessage(EVENT_NETWORK_SELECTION_DONE);
            mPhone.selectNetworkManually(mNetworkMap.get(selectedCarrier), msg);

            displayNetworkSeletionInProgress(networkStr);

            handled = true;
        }

        return handled;
    
protected voidonPrepareDialog(int id, android.app.Dialog dialog)

        if ((id == DIALOG_NETWORK_SELECTION) || (id == DIALOG_NETWORK_LIST_LOAD) ||
                (id == DIALOG_NETWORK_AUTO_SELECT)) {
            // when the dialogs come up, we'll need to indicate that
            // we're in a busy state to dissallow further input.
            getPreferenceScreen().setEnabled(false);
        }
    
private voidselectNetworkAutomatic()

        if (DBG) log("select network automatically...");
        showDialog(DIALOG_NETWORK_AUTO_SELECT);

        Message msg = mHandler.obtainMessage(EVENT_AUTO_SELECT_DONE);
        mPhone.setNetworkSelectionModeAutomatic(msg);