FileDocCategorySizeDatePackage
ConnectSpecificProfilesActivity.javaAPI DocAndroid 1.5 API10497Wed May 06 22:42:48 BST 2009com.android.settings.bluetooth

ConnectSpecificProfilesActivity

public class ConnectSpecificProfilesActivity extends android.preference.PreferenceActivity implements LocalBluetoothDevice.Callback, Preference.OnPreferenceChangeListener
ConnectSpecificProfilesActivity presents the user with all of the profiles for a particular device, and allows him to choose which should be connected (or disconnected).

Fields Summary
private static final String
TAG
private static final String
KEY_ONLINE_MODE
private static final String
KEY_TITLE
private static final String
KEY_PROFILE_CONTAINER
public static final String
EXTRA_ADDRESS
private LocalBluetoothManager
mManager
private LocalBluetoothDevice
mDevice
private android.preference.PreferenceGroup
mProfileContainer
private android.preference.CheckBoxPreference
mOnlineModePreference
private boolean
mOnlineMode
The current mode of this activity and its checkboxes (either online mode or offline mode). In online mode, user interactions with the profile checkboxes will also toggle the profile's connectivity. In offline mode, they will not, and only the preferred state will be saved for the profile.
Constructors Summary
Methods Summary
private voidaddPreferencesForProfiles()

        for (Profile profile : mDevice.getProfiles()) {
            Preference pref = createProfilePreference(profile);
            mProfileContainer.addPreference(pref);
        }
    
private android.preference.CheckBoxPreferencecreateProfilePreference(com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile profile)
Creates a checkbox preference for the particular profile. The key will be the profile's name.

param
profile The profile for which the preference controls.
return
A preference that allows the user to choose whether this profile will be connected to.

        CheckBoxPreference pref = new CheckBoxPreference(this);
        pref.setKey(profile.toString());
        pref.setTitle(profile.localizedString);
        pref.setPersistent(false);
        pref.setOnPreferenceChangeListener(this);

        refreshProfilePreference(pref, profile);
        
        return pref;
    
private com.android.settings.bluetooth.LocalBluetoothProfileManager.ProfilegetProfileOf(android.preference.Preference pref)

        if (!(pref instanceof CheckBoxPreference)) return null;
        String key = pref.getKey();
        if (TextUtils.isEmpty(key)) return null;
        
        try {
            return Profile.valueOf(pref.getKey());
        } catch (IllegalArgumentException e) {
            return null;
        }
    
private static intgetProfileSummary(LocalBluetoothProfileManager profileManager, com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile profile, java.lang.String address, int connectionStatus, boolean onlineMode)

        if (!onlineMode || connectionStatus == SettingsBtStatus.CONNECTION_STATUS_DISCONNECTED) {
            return getProfileSummaryForSettingPreference(profile);
        } else {
            return profileManager.getSummary(address);
        }
    
private static final intgetProfileSummaryForSettingPreference(com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile profile)
Gets the summary that describes when checked, it will become a preferred profile.

param
profile The profile to get the summary for.
return
The summary.

        switch (profile) {
            case A2DP:
                return R.string.bluetooth_a2dp_profile_summary_use_for;
            case HEADSET:
                return R.string.bluetooth_headset_profile_summary_use_for;
            default:
                return 0;
        }
    
protected voidonCreate(android.os.Bundle savedInstanceState)

    
    
        
        super.onCreate(savedInstanceState);
        
        String address;
        if (savedInstanceState != null) {
            address = savedInstanceState.getString(EXTRA_ADDRESS);
        } else {
            Intent intent = getIntent();
            address = intent.getStringExtra(EXTRA_ADDRESS);
        }

        if (TextUtils.isEmpty(address)) {
            Log.w(TAG, "Activity started without address");
            finish();
        }
        
        mManager = LocalBluetoothManager.getInstance(this);
        mDevice = mManager.getLocalDeviceManager().findDevice(address);
        if (mDevice == null) {
            Log.w(TAG, "Device not found, cannot connect to it");
            finish();
        }

        addPreferencesFromResource(R.xml.bluetooth_device_advanced);
        mProfileContainer = (PreferenceGroup) findPreference(KEY_PROFILE_CONTAINER);
        
        // Set the title of the screen
        findPreference(KEY_TITLE).setTitle(
                getString(R.string.bluetooth_device_advanced_title, mDevice.getName()));

        // Listen for check/uncheck of the online mode checkbox
        mOnlineModePreference = (CheckBoxPreference) findPreference(KEY_ONLINE_MODE);
        mOnlineModePreference.setOnPreferenceChangeListener(this);
        
        // Add a preference for each profile
        addPreferencesForProfiles();
    
public voidonDeviceAttributesChanged(LocalBluetoothDevice device)

        refresh();
    
private voidonOnlineModeCheckedStateChanged(boolean checked)

        setOnlineMode(checked, true);
    
protected voidonPause()

        super.onPause();
        
        mDevice.unregisterCallback(this);
        mManager.setForegroundActivity(null);
    
public booleanonPreferenceChange(android.preference.Preference preference, java.lang.Object newValue)

        String key = preference.getKey();
        if (TextUtils.isEmpty(key) || newValue == null) return true;
        
        if (key.equals(KEY_ONLINE_MODE)) {
            onOnlineModeCheckedStateChanged((Boolean) newValue);
            
        } else {
            Profile profile = getProfileOf(preference);
            if (profile == null) return false;
            onProfileCheckedStateChanged(profile, (Boolean) newValue);
        }

        return true;
    
private voidonProfileCheckedStateChanged(com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile profile, boolean checked)

        if (mOnlineMode) {
            if (checked) {
                mDevice.connect(profile);
            } else {
                mDevice.disconnect(profile);
            }
        }
        
        LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                .getProfileManager(mManager, profile);
        profileManager.setPreferred(mDevice.getAddress(), checked);
    
protected voidonResume()

        super.onResume();
        
        mManager.setForegroundActivity(this);
        mDevice.registerCallback(this);

        refresh();
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        
        outState.putString(EXTRA_ADDRESS, mDevice.getAddress());
    
private voidrefresh()

        // We are in 'online mode' if we are connected, connecting, or disconnecting
        setOnlineMode(mDevice.isConnected() || mDevice.isBusy(), false);
        refreshProfiles();
    
private voidrefreshOnlineModePreference()

        mOnlineModePreference.setChecked(mOnlineMode);

        /* Gray out checkbox while connecting and disconnecting */
        mOnlineModePreference.setEnabled(!mDevice.isBusy());

        /**
         * If the device is online, show status. Otherwise, show a summary that
         * describes what the checkbox does.
         */
        mOnlineModePreference.setSummary(mOnlineMode ? mDevice.getSummary()
                : R.string.bluetooth_device_advanced_online_mode_summary);
    
private voidrefreshProfilePreference(android.preference.CheckBoxPreference profilePref, com.android.settings.bluetooth.LocalBluetoothProfileManager.Profile profile)

        String address = mDevice.getAddress();
        LocalBluetoothProfileManager profileManager = LocalBluetoothProfileManager
                .getProfileManager(mManager, profile);
        
        int connectionStatus = profileManager.getConnectionStatus(address);

        /* Gray out checkbox while connecting and disconnecting */
        profilePref.setEnabled(!mDevice.isBusy());

        profilePref.setSummary(getProfileSummary(profileManager, profile, address,
                connectionStatus, mOnlineMode));
        
        profilePref.setChecked(profileManager.isPreferred(address));
    
private voidrefreshProfiles()

        for (Profile profile : mDevice.getProfiles()) {
            CheckBoxPreference profilePref =
                    (CheckBoxPreference) findPreference(profile.toString());
            if (profilePref == null) {
                profilePref = createProfilePreference(profile);
                mProfileContainer.addPreference(profilePref);
            } else {
                refreshProfilePreference(profilePref, profile);
            }
        }
    
private voidsetOnlineMode(boolean onlineMode, boolean takeAction)
Switches between online/offline mode.

param
onlineMode Whether to be in online mode, or offline mode.
param
takeAction Whether to take action (i.e., connect or disconnect) based on the new online mode.

        mOnlineMode = onlineMode;
            
        if (takeAction) {
            if (onlineMode) {
                mDevice.connect();
            } else {
                mDevice.disconnect();
            }
        }
            
        refreshOnlineModePreference();