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 |
Methods Summary |
---|
private int | getDiscoverableTimeout()
int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1);
if (timeout <= 0) {
timeout = DISCOVERABLE_TIMEOUT;
}
return timeout;
|
private void | handleModeChanged(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 boolean | onPreferenceChange(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 void | pause()
if (mLocalManager == null) {
return;
}
mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
mCheckBoxPreference.setOnPreferenceChangeListener(null);
mContext.unregisterReceiver(mReceiver);
|
private void | persistDiscoverableEndTimestamp(long endTimestamp)
SharedPreferences.Editor editor = mLocalManager.getSharedPreferences().edit();
editor.putLong(SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
editor.commit();
|
public void | resume()
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 void | setEnabled(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 void | updateCountdownSummary()
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);
}
|