diff options
author | Chris Wren <cwren@google.com> | 2011-02-01 15:09:13 -0800 |
---|---|---|
committer | Jaikumar Ganesh <jaikumar@google.com> | 2011-05-27 10:27:14 -0700 |
commit | cdd9d033b0f16c705433f009812e69bed977e1cf (patch) | |
tree | d936f43077d1062809a97a49f582d7f4a4f0c021 /src | |
parent | 7c284c2c62ce565dacfae641108a2a5033e78721 (diff) | |
download | packages_apps_settings-cdd9d033b0f16c705433f009812e69bed977e1cf.zip packages_apps_settings-cdd9d033b0f16c705433f009812e69bed977e1cf.tar.gz packages_apps_settings-cdd9d033b0f16c705433f009812e69bed977e1cf.tar.bz2 |
Allow users to change the length of BT disoverability. DO NOT MERGE
It can set to be two minues, five minutes, an hour, or forever discoverable.
The default is 120 secs.
This addresses the internal bug 2413429:
http://b/issue?id=2413429
and external issue 6348:
http://code.google.com/p/android/issues/detail?id=6348
Original Author: Chris Wren <crwen@google.com>
Modifications done by: Jaikumar Ganesh<jaikumar@google.com>
Change-Id: Ie12e56ac41aa01a161d263c7525b6e021d4eeb1f
Diffstat (limited to 'src')
4 files changed, 100 insertions, 29 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java index 37e48ff..7a4a1ff 100644 --- a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java @@ -26,8 +26,11 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Handler; import android.os.SystemProperties; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.CheckBoxPreference; +import android.provider.Settings; +import android.util.Log; /** * BluetoothDiscoverableEnabler is a helper to manage the "Discoverable" @@ -39,14 +42,28 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT = "debug.bt.discoverable_time"; - /* package */ static final int DEFAULT_DISCOVERABLE_TIMEOUT = 120; - /* package */ static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP = - "discoverable_end_timestamp"; + static final int DISCOVERABLE_TIMEOUT_TWO_MINUTES = 120; + static final int DISCOVERABLE_TIMEOUT_FIVE_MINUTES = 300; + static final int DISCOVERABLE_TIMEOUT_ONE_HOUR = 3600; + static final int DISCOVERABLE_TIMEOUT_NEVER = 0; + + static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP = + "discoverable_end_timestamp"; + + private static final String VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES = "twomin"; + private static final String VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES = "fivemin"; + private static final String VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR = "onehour"; + private static final String VALUE_DISCOVERABLE_TIMEOUT_NEVER = "never"; + + // no need for this timeout anymore since we have the listPreference default value + // leaving now temporary until requestpermissionactivity is modified.. + static final int DEFAULT_DISCOVERABLE_TIMEOUT = DISCOVERABLE_TIMEOUT_TWO_MINUTES; private final Context mContext; private final Handler mUiHandler; private final CheckBoxPreference mCheckBoxPreference; + private final ListPreference mTimeoutListPreference; private final LocalBluetoothManager mLocalManager; @@ -69,12 +86,16 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan } }; - public BluetoothDiscoverableEnabler(Context context, CheckBoxPreference checkBoxPreference) { + public BluetoothDiscoverableEnabler(Context context, + CheckBoxPreference checkBoxPreference, ListPreference timeoutListPreference) { mContext = context; mUiHandler = new Handler(); mCheckBoxPreference = checkBoxPreference; + mTimeoutListPreference = timeoutListPreference; checkBoxPreference.setPersistent(false); + // we actually want to persist this since can't infer from BT device state + mTimeoutListPreference.setPersistent(true); mLocalManager = LocalBluetoothManager.getInstance(context); if (mLocalManager == null) { @@ -91,7 +112,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); mContext.registerReceiver(mReceiver, filter); mCheckBoxPreference.setOnPreferenceChangeListener(this); - + mTimeoutListPreference.setOnPreferenceChangeListener(this); handleModeChanged(mLocalManager.getBluetoothAdapter().getScanMode()); } @@ -102,12 +123,18 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); mCheckBoxPreference.setOnPreferenceChangeListener(null); + mTimeoutListPreference.setOnPreferenceChangeListener(null); mContext.unregisterReceiver(mReceiver); } public boolean onPreferenceChange(Preference preference, Object value) { - // Turn on/off BT discoverability - setEnabled((Boolean) value); + if (preference == mCheckBoxPreference) { + // Turn on/off BT discoverability + setEnabled((Boolean) value); + } else if (preference == mTimeoutListPreference) { + mTimeoutListPreference.setValue((String) value); + setEnabled(true); + } return true; } @@ -116,26 +143,52 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan BluetoothAdapter manager = mLocalManager.getBluetoothAdapter(); 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; + long endTimestamp = System.currentTimeMillis() + timeout * 1000L; persistDiscoverableEndTimestamp(endTimestamp); - manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE); + updateCountdownSummary(); + + manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, timeout); } else { manager.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE); } } + private void updateTimerDisplay(int timeout) { + if (getDiscoverableTimeout() == DISCOVERABLE_TIMEOUT_NEVER) { + mCheckBoxPreference.setSummaryOn( + mContext.getResources() + .getString(R.string.bluetooth_is_discoverable_always)); + } else { + mCheckBoxPreference.setSummaryOn( + mContext.getResources() + .getString(R.string.bluetooth_is_discoverable, timeout)); + } + } + private int getDiscoverableTimeout() { int timeout = SystemProperties.getInt(SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT, -1); - if (timeout <= 0) { - timeout = DEFAULT_DISCOVERABLE_TIMEOUT; + if (timeout < 0) { + String timeoutValue = null; + if (mTimeoutListPreference != null && mTimeoutListPreference.getValue() != null) { + timeoutValue = mTimeoutListPreference.getValue().toString(); + } else { + mTimeoutListPreference.setValue(VALUE_DISCOVERABLE_TIMEOUT_TWO_MINUTES); + return DISCOVERABLE_TIMEOUT_TWO_MINUTES; + } + + if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_NEVER)) { + timeout = DISCOVERABLE_TIMEOUT_NEVER; + } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_ONE_HOUR)) { + timeout = DISCOVERABLE_TIMEOUT_ONE_HOUR; + } else if (timeoutValue.equals(VALUE_DISCOVERABLE_TIMEOUT_FIVE_MINUTES)) { + timeout = DISCOVERABLE_TIMEOUT_FIVE_MINUTES; + } else { + timeout = DISCOVERABLE_TIMEOUT_TWO_MINUTES; + } } return timeout; @@ -151,7 +204,6 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { mCheckBoxPreference.setChecked(true); updateCountdownSummary(); - } else { mCheckBoxPreference.setChecked(false); } @@ -173,11 +225,8 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan return; } - String formattedTimeLeft = String.valueOf((endTimestamp - currentTimestamp) / 1000); - - mCheckBoxPreference.setSummaryOn( - mContext.getResources().getString(R.string.bluetooth_is_discoverable, - formattedTimeLeft)); + int timeLeft = (int) ((endTimestamp - currentTimestamp) / 1000L); + updateTimerDisplay(timeLeft); synchronized (this) { mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index 1e73b2d..e897673 100644 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -31,9 +31,13 @@ import android.content.IntentFilter; import android.os.Bundle; import android.os.ParcelUuid; import android.preference.CheckBoxPreference; +import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; +import android.preference.PreferenceCategory; +import android.preference.PreferenceScreen; +import android.text.TextUtils; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; @@ -55,6 +59,7 @@ public class BluetoothSettings extends PreferenceActivity private static final String KEY_BT_CHECKBOX = "bt_checkbox"; private static final String KEY_BT_DISCOVERABLE = "bt_discoverable"; private static final String KEY_BT_DEVICE_LIST = "bt_device_list"; + private static final String KEY_BT_DISCOVERABLE_TIMEOUT = "bt_discoverable_timeout"; private static final String KEY_BT_NAME = "bt_name"; private static final String KEY_BT_SCAN = "bt_scan"; @@ -141,9 +146,9 @@ public class BluetoothSettings extends PreferenceActivity this, (CheckBoxPreference) findPreference(KEY_BT_CHECKBOX)); - mDiscoverableEnabler = new BluetoothDiscoverableEnabler( - this, - (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE)); + mDiscoverableEnabler = new BluetoothDiscoverableEnabler(this, + (CheckBoxPreference) findPreference(KEY_BT_DISCOVERABLE), + (ListPreference) findPreference(KEY_BT_DISCOVERABLE_TIMEOUT)); mNamePreference = (BluetoothNamePreference) findPreference(KEY_BT_NAME); diff --git a/src/com/android/settings/bluetooth/RequestPermissionActivity.java b/src/com/android/settings/bluetooth/RequestPermissionActivity.java index dd802f3..52cd439 100644 --- a/src/com/android/settings/bluetooth/RequestPermissionActivity.java +++ b/src/com/android/settings/bluetooth/RequestPermissionActivity.java @@ -157,7 +157,15 @@ public class RequestPermissionActivity extends Activity implements builder.setCancelable(false); } else { // Ask the user whether to turn on discovery mode or not - builder.setMessage(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); + // For lasting discoverable mode there is a different message + // TODO(): Revisit this when public APIs for discoverable timeout are introduced. + if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { + builder.setMessage( + getString(R.string.bluetooth_ask_enablement_and_lasting_discovery)); + } else { + builder.setMessage( + getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); + } builder.setPositiveButton(getString(R.string.yes), this); builder.setNegativeButton(getString(R.string.no), this); } @@ -243,9 +251,14 @@ public class RequestPermissionActivity extends Activity implements Log.e(TAG, "Timeout = " + mTimeout); - if (mTimeout > MAX_DISCOVERABLE_TIMEOUT) { - mTimeout = MAX_DISCOVERABLE_TIMEOUT; - } else if (mTimeout <= 0) { + // Right now assuming for simplicity that an app can pick any int value, + // and if equal to BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER + // it will be treated as a request for lasting discoverability. + // Alternatively, a check can be added here for enforcing the specific allowed values + // as listed in BluetoothDiscoverableEnabler. + // We need to make all these value public. + + if (mTimeout <= 0) { mTimeout = BluetoothDiscoverableEnabler.DEFAULT_DISCOVERABLE_TIMEOUT; } } else { diff --git a/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java b/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java index c869868..2657d91 100644 --- a/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java +++ b/src/com/android/settings/bluetooth/RequestPermissionHelperActivity.java @@ -75,7 +75,11 @@ public class RequestPermissionHelperActivity extends AlertActivity implements if (mEnableOnly) { tv.setText(getString(R.string.bluetooth_ask_enablement)); } else { - tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); + if (mTimeout == BluetoothDiscoverableEnabler.DISCOVERABLE_TIMEOUT_NEVER) { + tv.setText(getString(R.string.bluetooth_ask_enablement_and_lasting_discovery)); + } else { + tv.setText(getString(R.string.bluetooth_ask_enablement_and_discovery, mTimeout)); + } } p.mPositiveButtonText = getString(R.string.yes); |