Settings app changes for Bluez4.

This commit is contained in:
Jaikumar Ganesh
2009-05-05 22:26:56 -07:00
parent db08e6543a
commit 91b33f1462
3 changed files with 62 additions and 63 deletions

View File

@@ -38,20 +38,20 @@ import android.preference.CheckBoxPreference;
*/ */
public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChangeListener { public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChangeListener {
private static final String TAG = "BluetoothDiscoverableEnabler"; private static final String TAG = "BluetoothDiscoverableEnabler";
private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT = private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT =
"debug.bt.discoverable_time"; "debug.bt.discoverable_time";
private static final int DISCOVERABLE_TIMEOUT = 120; private static final int DISCOVERABLE_TIMEOUT = 120;
private static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP = private static final String SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP =
"discoverable_end_timestamp"; "discoverable_end_timestamp";
private final Context mContext; private final Context mContext;
private final Handler mUiHandler; private final Handler mUiHandler;
private final CheckBoxPreference mCheckBoxPreference; private final CheckBoxPreference mCheckBoxPreference;
private final LocalBluetoothManager mLocalManager; private final LocalBluetoothManager mLocalManager;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
@@ -66,17 +66,17 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
private final Runnable mUpdateCountdownSummaryRunnable = new Runnable() { private final Runnable mUpdateCountdownSummaryRunnable = new Runnable() {
public void run() { public void run() {
updateCountdownSummary(); updateCountdownSummary();
} }
}; };
public BluetoothDiscoverableEnabler(Context context, CheckBoxPreference checkBoxPreference) { public BluetoothDiscoverableEnabler(Context context, CheckBoxPreference checkBoxPreference) {
mContext = context; mContext = context;
mUiHandler = new Handler(); mUiHandler = new Handler();
mCheckBoxPreference = checkBoxPreference; mCheckBoxPreference = checkBoxPreference;
checkBoxPreference.setPersistent(false); checkBoxPreference.setPersistent(false);
mLocalManager = LocalBluetoothManager.getInstance(context); mLocalManager = LocalBluetoothManager.getInstance(context);
if (mLocalManager == null) { if (mLocalManager == null) {
// Bluetooth not supported // Bluetooth not supported
@@ -92,30 +92,30 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
IntentFilter filter = new IntentFilter(BluetoothIntent.SCAN_MODE_CHANGED_ACTION); IntentFilter filter = new IntentFilter(BluetoothIntent.SCAN_MODE_CHANGED_ACTION);
mContext.registerReceiver(mReceiver, filter); mContext.registerReceiver(mReceiver, filter);
mCheckBoxPreference.setOnPreferenceChangeListener(this); mCheckBoxPreference.setOnPreferenceChangeListener(this);
handleModeChanged(mLocalManager.getBluetoothManager().getScanMode()); handleModeChanged(mLocalManager.getBluetoothManager().getScanMode());
} }
public void pause() { public void pause() {
if (mLocalManager == null) { if (mLocalManager == null) {
return; return;
} }
mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
mCheckBoxPreference.setOnPreferenceChangeListener(null); mCheckBoxPreference.setOnPreferenceChangeListener(null);
mContext.unregisterReceiver(mReceiver); mContext.unregisterReceiver(mReceiver);
} }
public boolean onPreferenceChange(Preference preference, Object value) { public boolean onPreferenceChange(Preference preference, Object value) {
// Turn on/off BT discoverability // Turn on/off BT discoverability
setEnabled((Boolean) value); setEnabled((Boolean) value);
return true; return true;
} }
private void setEnabled(final boolean enable) { private void setEnabled(final boolean enable) {
BluetoothDevice manager = mLocalManager.getBluetoothManager(); BluetoothDevice manager = mLocalManager.getBluetoothManager();
if (enable) { if (enable) {
int timeout = getDiscoverableTimeout(); int timeout = getDiscoverableTimeout();
@@ -126,7 +126,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
long endTimestamp = System.currentTimeMillis() + timeout * 1000; long endTimestamp = System.currentTimeMillis() + timeout * 1000;
persistDiscoverableEndTimestamp(endTimestamp); persistDiscoverableEndTimestamp(endTimestamp);
manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE); manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
} else { } else {
manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE); manager.setScanMode(BluetoothDevice.SCAN_MODE_CONNECTABLE);
@@ -138,7 +138,7 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
if (timeout <= 0) { if (timeout <= 0) {
timeout = DISCOVERABLE_TIMEOUT; timeout = DISCOVERABLE_TIMEOUT;
} }
return timeout; return timeout;
} }
@@ -147,44 +147,44 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan
editor.putLong(SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp); editor.putLong(SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, endTimestamp);
editor.commit(); editor.commit();
} }
private void handleModeChanged(int mode) { private void handleModeChanged(int mode) {
if (mode == BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { if (mode == BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
mCheckBoxPreference.setChecked(true); mCheckBoxPreference.setChecked(true);
updateCountdownSummary(); updateCountdownSummary();
} else { } else {
mCheckBoxPreference.setChecked(false); mCheckBoxPreference.setChecked(false);
} }
} }
private void updateCountdownSummary() { private void updateCountdownSummary() {
int mode = mLocalManager.getBluetoothManager().getScanMode(); int mode = mLocalManager.getBluetoothManager().getScanMode();
if (mode != BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) { if (mode != BluetoothDevice.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
return; return;
} }
long currentTimestamp = System.currentTimeMillis(); long currentTimestamp = System.currentTimeMillis();
long endTimestamp = mLocalManager.getSharedPreferences().getLong( long endTimestamp = mLocalManager.getSharedPreferences().getLong(
SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0); SHARED_PREFERENCES_KEY_DISCOVERABLE_END_TIMESTAMP, 0);
if (currentTimestamp > endTimestamp) { if (currentTimestamp > endTimestamp) {
// We're still in discoverable mode, but maybe there isn't a timeout. // We're still in discoverable mode, but maybe there isn't a timeout.
mCheckBoxPreference.setSummaryOn(null); mCheckBoxPreference.setSummaryOn(null);
return; return;
} }
String formattedTimeLeft = String.valueOf((endTimestamp - currentTimestamp) / 1000); String formattedTimeLeft = String.valueOf((endTimestamp - currentTimestamp) / 1000);
mCheckBoxPreference.setSummaryOn( mCheckBoxPreference.setSummaryOn(
mContext.getResources().getString(R.string.bluetooth_is_discoverable, mContext.getResources().getString(R.string.bluetooth_is_discoverable,
formattedTimeLeft)); formattedTimeLeft));
synchronized (this) { synchronized (this) {
mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable); mUiHandler.removeCallbacks(mUpdateCountdownSummaryRunnable);
mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000); mUiHandler.postDelayed(mUpdateCountdownSummaryRunnable, 1000);
} }
} }
} }

View File

@@ -722,7 +722,6 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
break; break;
case CONTEXT_ITEM_UNPAIR: case CONTEXT_ITEM_UNPAIR:
mLocalManager.getBluetoothManager().disconnectRemoteDeviceAcl(mAddress);
unpair(); unpair();
break; break;

View File

@@ -43,14 +43,14 @@ public class LocalBluetoothManager {
private static final String TAG = "LocalBluetoothManager"; private static final String TAG = "LocalBluetoothManager";
static final boolean V = Config.LOGV; static final boolean V = Config.LOGV;
static final boolean D = Config.LOGD; static final boolean D = Config.LOGD;
private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings"; private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings";
private static LocalBluetoothManager INSTANCE; private static LocalBluetoothManager INSTANCE;
/** Used when obtaining a reference to the singleton instance. */ /** Used when obtaining a reference to the singleton instance. */
private static Object INSTANCE_LOCK = new Object(); private static Object INSTANCE_LOCK = new Object();
private boolean mInitialized; private boolean mInitialized;
private Context mContext; private Context mContext;
/** If a BT-related activity is in the foreground, this will be it. */ /** If a BT-related activity is in the foreground, this will be it. */
private Activity mForegroundActivity; private Activity mForegroundActivity;
@@ -61,24 +61,24 @@ public class LocalBluetoothManager {
private LocalBluetoothDeviceManager mLocalDeviceManager; private LocalBluetoothDeviceManager mLocalDeviceManager;
private BluetoothEventRedirector mEventRedirector; private BluetoothEventRedirector mEventRedirector;
private BluetoothA2dp mBluetoothA2dp; private BluetoothA2dp mBluetoothA2dp;
private int mState = BluetoothError.ERROR; private int mState = BluetoothError.ERROR;
private List<Callback> mCallbacks = new ArrayList<Callback>(); private List<Callback> mCallbacks = new ArrayList<Callback>();
private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins private static final int SCAN_EXPIRATION_MS = 5 * 60 * 1000; // 5 mins
private long mLastScan; private long mLastScan;
public static LocalBluetoothManager getInstance(Context context) { public static LocalBluetoothManager getInstance(Context context) {
synchronized (INSTANCE_LOCK) { synchronized (INSTANCE_LOCK) {
if (INSTANCE == null) { if (INSTANCE == null) {
INSTANCE = new LocalBluetoothManager(); INSTANCE = new LocalBluetoothManager();
} }
if (!INSTANCE.init(context)) { if (!INSTANCE.init(context)) {
return null; return null;
} }
return INSTANCE; return INSTANCE;
} }
} }
@@ -86,15 +86,15 @@ public class LocalBluetoothManager {
private boolean init(Context context) { private boolean init(Context context) {
if (mInitialized) return true; if (mInitialized) return true;
mInitialized = true; mInitialized = true;
// This will be around as long as this process is // This will be around as long as this process is
mContext = context.getApplicationContext(); mContext = context.getApplicationContext();
mManager = (BluetoothDevice) context.getSystemService(Context.BLUETOOTH_SERVICE); mManager = (BluetoothDevice) context.getSystemService(Context.BLUETOOTH_SERVICE);
if (mManager == null) { if (mManager == null) {
return false; return false;
} }
mLocalDeviceManager = new LocalBluetoothDeviceManager(this); mLocalDeviceManager = new LocalBluetoothDeviceManager(this);
mEventRedirector = new BluetoothEventRedirector(this); mEventRedirector = new BluetoothEventRedirector(this);
@@ -104,11 +104,11 @@ public class LocalBluetoothManager {
return true; return true;
} }
public BluetoothDevice getBluetoothManager() { public BluetoothDevice getBluetoothManager() {
return mManager; return mManager;
} }
public Context getContext() { public Context getContext() {
return mContext; return mContext;
} }
@@ -116,7 +116,7 @@ public class LocalBluetoothManager {
public Activity getForegroundActivity() { public Activity getForegroundActivity() {
return mForegroundActivity; return mForegroundActivity;
} }
public void setForegroundActivity(Activity activity) { public void setForegroundActivity(Activity activity) {
if (mErrorDialog != null) { if (mErrorDialog != null) {
mErrorDialog.dismiss(); mErrorDialog.dismiss();
@@ -124,31 +124,31 @@ public class LocalBluetoothManager {
} }
mForegroundActivity = activity; mForegroundActivity = activity;
} }
public SharedPreferences getSharedPreferences() { public SharedPreferences getSharedPreferences() {
return mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE); return mContext.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
} }
public LocalBluetoothDeviceManager getLocalDeviceManager() { public LocalBluetoothDeviceManager getLocalDeviceManager() {
return mLocalDeviceManager; return mLocalDeviceManager;
} }
List<Callback> getCallbacks() { List<Callback> getCallbacks() {
return mCallbacks; return mCallbacks;
} }
public void registerCallback(Callback callback) { public void registerCallback(Callback callback) {
synchronized (mCallbacks) { synchronized (mCallbacks) {
mCallbacks.add(callback); mCallbacks.add(callback);
} }
} }
public void unregisterCallback(Callback callback) { public void unregisterCallback(Callback callback) {
synchronized (mCallbacks) { synchronized (mCallbacks) {
mCallbacks.remove(callback); mCallbacks.remove(callback);
} }
} }
public void startScanning(boolean force) { public void startScanning(boolean force) {
if (mManager.isDiscovering()) { if (mManager.isDiscovering()) {
/* /*
@@ -156,7 +156,7 @@ public class LocalBluetoothManager {
* Note: we only call the callbacks, not the same path as if the * Note: we only call the callbacks, not the same path as if the
* scanning state had really changed (in that case the device * scanning state had really changed (in that case the device
* manager would clear its list of unpaired scanned devices). * manager would clear its list of unpaired scanned devices).
*/ */
dispatchScanningStateChanged(true); dispatchScanningStateChanged(true);
} else { } else {
if (!force) { if (!force) {
@@ -176,22 +176,22 @@ public class LocalBluetoothManager {
} }
} }
} }
if (mManager.startDiscovery(true)) { if (mManager.startDiscovery()) {
mLastScan = System.currentTimeMillis(); mLastScan = System.currentTimeMillis();
} }
} }
} }
public int getBluetoothState() { public int getBluetoothState() {
if (mState == BluetoothError.ERROR) { if (mState == BluetoothError.ERROR) {
syncBluetoothState(); syncBluetoothState();
} }
return mState; return mState;
} }
void setBluetoothStateInt(int state) { void setBluetoothStateInt(int state) {
mState = state; mState = state;
if (state == BluetoothDevice.BLUETOOTH_STATE_ON || if (state == BluetoothDevice.BLUETOOTH_STATE_ON ||
@@ -199,7 +199,7 @@ public class LocalBluetoothManager {
mLocalDeviceManager.onBluetoothStateChanged(state == BluetoothDevice.BLUETOOTH_STATE_ON); mLocalDeviceManager.onBluetoothStateChanged(state == BluetoothDevice.BLUETOOTH_STATE_ON);
} }
} }
private void syncBluetoothState() { private void syncBluetoothState() {
int bluetoothState; int bluetoothState;
@@ -218,7 +218,7 @@ public class LocalBluetoothManager {
boolean wasSetStateSuccessful = enabled boolean wasSetStateSuccessful = enabled
? mManager.enable() ? mManager.enable()
: mManager.disable(); : mManager.disable();
if (wasSetStateSuccessful) { if (wasSetStateSuccessful) {
setBluetoothStateInt(enabled setBluetoothStateInt(enabled
? BluetoothDevice.BLUETOOTH_STATE_TURNING_ON ? BluetoothDevice.BLUETOOTH_STATE_TURNING_ON
@@ -229,11 +229,11 @@ public class LocalBluetoothManager {
"setBluetoothEnabled call, manager didn't return success for enabled: " "setBluetoothEnabled call, manager didn't return success for enabled: "
+ enabled); + enabled);
} }
syncBluetoothState(); syncBluetoothState();
} }
} }
/** /**
* @param started True if scanning started, false if scanning finished. * @param started True if scanning started, false if scanning finished.
*/ */
@@ -242,7 +242,7 @@ public class LocalBluetoothManager {
mLocalDeviceManager.onScanningStateChanged(started); mLocalDeviceManager.onScanningStateChanged(started);
dispatchScanningStateChanged(started); dispatchScanningStateChanged(started);
} }
private void dispatchScanningStateChanged(boolean started) { private void dispatchScanningStateChanged(boolean started) {
synchronized (mCallbacks) { synchronized (mCallbacks) {
for (Callback callback : mCallbacks) { for (Callback callback : mCallbacks) {
@@ -267,7 +267,7 @@ public class LocalBluetoothManager {
.setPositiveButton(android.R.string.ok, null) .setPositiveButton(android.R.string.ok, null)
.show(); .show();
} else { } else {
// Fallback on a toast // Fallback on a toast
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
} }
} }
@@ -277,5 +277,5 @@ public class LocalBluetoothManager {
void onDeviceAdded(LocalBluetoothDevice device); void onDeviceAdded(LocalBluetoothDevice device);
void onDeviceDeleted(LocalBluetoothDevice device); void onDeviceDeleted(LocalBluetoothDevice device);
} }
} }