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

BluetoothDiscoverableEnabler

public class BluetoothDiscoverableEnabler extends Object implements Preference.OnPreferenceChangeListener
BluetoothDiscoverableEnabler is a helper to manage the "Discoverable" checkbox. It sets/unsets discoverability and keeps track of how much time until the the discoverability is automatically turned off.

Fields Summary
private static final String
TAG
private static final boolean
V
private static final String
SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT
private static final int
DISCOVERABLE_TIMEOUT
private static final String
SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP
private final android.content.Context
mContext
private final android.os.Handler
mUiHandler
private final android.preference.CheckBoxPreference
mCheckBoxPreference
private final LocalBluetoothManager
mLocalManager
private final android.content.BroadcastReceiver
mReceiver
private final Runnable
mUpdateCountdownSummaryRunnable
Constructors Summary
public BluetoothDiscoverableEnabler(android.content.Context context, android.preference.CheckBoxPreference checkBoxPreference)


         
        mContext = context;
        mUiHandler = new Handler();
        mCheckBoxPreference = checkBoxPreference;
        
        checkBoxPreference.setPersistent(false);
        
        mLocalManager = LocalBluetoothManager.getInstance(context);
        if (mLocalManager == null) {
            // Bluetooth not supported
            checkBoxPreference.setEnabled(false);
        }
    
Methods Summary
private intgetDiscoverableTimeout()

        int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1);
        if (timeout <= 0) {
            timeout = DISCOVERABLE_TIMEOUT;
        }
        
        return timeout;
    
private voidhandleModeChanged(int mode)

        if (V) {
            Log.v(TAG, "Got mode changed: " + mode);
        }
        
        if (mode == BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            mCheckBoxPreference.setChecked(true);
            updateCountdownSummary();
            
        } else {
            mCheckBoxPreference.setChecked(false);
        }
    
public booleanonPreferenceChange(android.preference.Preference preference, java.lang.Object value)

        if (V) {
            Log.v(TAG, "Preference changed to " + value);
        }
        
        // Turn on/off BT discoverability
        setEnabled((Boolean) value);
        
        return true;
    
public voidpause()

        if (mLocalManager == null) {
            return;
        }
        
        mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
        mCheckBoxPreference.setOnPreferenceChangeListener(null);
        mContext.unregisterReceiver(mReceiver);
    
private voidpersistDiscoverableEndTimestamp(long endTimestamp)

        SharedPreferences.Editor editor = mLocalManager.getSharedPreferences().edit();
        editor.putLong(SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
        editor.commit();
    
public voidresume()

        if (mLocalManager == null) {
            return;
        }

        IntentFilter filter = new IntentFilter(BluetoothIntent.SCAN_MODE_CHANGED_ACTION);
        mContext.registerReceiver(mReceiver, filter);
        mCheckBoxPreference.setOnPreferenceChangeListener(this);
        
        handleModeChanged(mLocalManager.getBluetoothManager().getScanMode());
    
private voidsetEnabled(boolean enable)

        BluetoothDevice manager = mLocalManager.getBluetoothManager();
        
        if (enable) {

            int timeout = getDiscoverableTimeout();
            manager.setDiscoverableTimeout(timeout);

            mCheckBoxPreference.setSummaryOn(
                    mContext.getResources().getString(R.string.bluetooth_is_discoverable, timeout));

            long endTimestamp = System.currentTimeMillis() + timeout * 1000;
            persistDiscoverableEndTimestamp(endTimestamp);
            
            manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
        } else {
            manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE);
        }
    
private voidupdateCountdownSummary()

        int mode = mLocalManager.getBluetoothManager().getScanMode();
        if (mode != BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
            return;
        }
            
        long currentTimestamp = System.currentTimeMillis();
        long endTimestamp = mLocalManager.getSharedPreferences().getLong(
                SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
            
        if (currentTimestamp > endTimestamp) {
            // We're still in discoverable mode, but maybe there isn't a timeout.
            mCheckBoxPreference.setSummaryOn(null);
            return;
        }
        
        String formattedTimeLeft = String.valueOf((endTimestamp - currentTimestamp) / 1000);
        
        mCheckBoxPreference.setSummaryOn(
                mContext.getResources().getString(R.string.bluetooth_is_discoverable,
                        formattedTimeLeft));
        
        synchronized (this) {
            mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
            mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
        }