From f25063aee3d8eeaf3767b3a4bf43e1895f2deb2d Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Wed, 29 Apr 2009 14:17:06 -0700 Subject: [PATCH] Fixed the Bluetooth On/Off Checkbox in settings so it is enabled/disable properly. In the BroadcastReceiver, pickup the new bluetooth status from the intent instead of calling back to LocalBluetoothManager. There could be race condition since LocalBluetoothManager itself gets the same info via the BroadcastReceiver mechanism. Also cleaned up logs. --- .../BluetoothDiscoverableEnabler.java | 10 ----- .../settings/bluetooth/BluetoothEnabler.java | 5 ++- .../bluetooth/LocalBluetoothDevice.java | 44 +++++++++++-------- .../bluetooth/LocalBluetoothManager.java | 4 +- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java index d458c5fdd0b..58fb5690aaf 100644 --- a/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothDiscoverableEnabler.java @@ -30,7 +30,6 @@ import android.os.Handler; import android.os.SystemProperties; import android.preference.Preference; import android.preference.CheckBoxPreference; -import android.util.Log; /** * BluetoothDiscoverableEnabler is a helper to manage the "Discoverable" @@ -39,7 +38,6 @@ import android.util.Log; */ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChangeListener { private static final String TAG = "BluetoothDiscoverableEnabler"; - private static final boolean V = LocalBluetoothManager.V; private static final String SYSTEM_PROPERTY_DISCOVERABLE_TIMEOUT = "debug.bt.discoverable_time"; @@ -109,10 +107,6 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan } public boolean onPreferenceChange(Preference preference, Object value) { - if (V) { - Log.v(TAG, "Preference changed to " + value); - } - // Turn on/off BT discoverability setEnabled((Boolean) value); @@ -155,10 +149,6 @@ public class BluetoothDiscoverableEnabler implements Preference.OnPreferenceChan } 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(); diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index 82961b86215..af2722f8575 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -19,6 +19,7 @@ package com.android.settings.bluetooth; import com.android.settings.R; import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothError; import android.bluetooth.BluetoothIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -48,7 +49,9 @@ public class BluetoothEnabler implements Preference.OnPreferenceChangeListener { private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - handleStateChanged(mLocalManager.getBluetoothState()); + int state = intent.getIntExtra(BluetoothIntent.BLUETOOTH_STATE, + BluetoothError.ERROR); + handleStateChanged(state); } }; diff --git a/src/com/android/settings/bluetooth/LocalBluetoothDevice.java b/src/com/android/settings/bluetooth/LocalBluetoothDevice.java index 86b1d69e3b3..5c797611552 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothDevice.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothDevice.java @@ -47,6 +47,8 @@ import java.util.List; */ public class LocalBluetoothDevice implements Comparable { private static final String TAG = "LocalBluetoothDevice"; + private static final boolean D = LocalBluetoothManager.D; + private static final boolean V = LocalBluetoothManager.V; private static final int CONTEXT_ITEM_CONNECT = Menu.FIRST + 1; private static final int CONTEXT_ITEM_DISCONNECT = Menu.FIRST + 2; @@ -124,18 +126,21 @@ public class LocalBluetoothDevice implements Comparable { private static LinkedList workQueue = new LinkedList(); private void queueCommand(BluetoothJob job) { - Log.d(TAG, workQueue.toString()); + if (D) { + Log.d(TAG, workQueue.toString()); + } synchronized (workQueue) { boolean processNow = pruneQueue(job); // Add job to queue - Log.d(TAG, "Adding: " + job.toString()); + if (D) { + Log.d(TAG, "Adding: " + job.toString()); + } workQueue.add(job); // if there's nothing pending from before, send the command to bt // framework immediately. if (workQueue.size() == 1 || processNow) { - Log.d(TAG, "workQueue.size() == 1 || TimeOut -> process command now"); // If the failed to process, just drop it from the queue. // There will be no callback to remove this from the queue. processCommands(); @@ -156,7 +161,9 @@ public class LocalBluetoothDevice implements Comparable { && existingJob.command == BluetoothCommand.CONNECT && existingJob.device.mAddress.equals(job.device.mAddress) && existingJob.profile == job.profile) { - Log.d(TAG, "Removed because of a pending disconnect. " + existingJob); + if (D) { + Log.d(TAG, "Removed because of a pending disconnect. " + existingJob); + } it.remove(); continue; } @@ -165,7 +172,6 @@ public class LocalBluetoothDevice implements Comparable { // Defensive Code: Remove any job that older than a preset time. // We never got a call back. It is better to have overlapping // calls than to get stuck. - Log.d(TAG, "Age:" + (now - existingJob.timeSent)); if (existingJob.timeSent != 0 && (now - existingJob.timeSent) >= MAX_WAIT_TIME_FOR_FRAMEWORK) { Log.w(TAG, "Timeout. Removing Job:" + existingJob.toString()); @@ -191,12 +197,13 @@ public class LocalBluetoothDevice implements Comparable { } if (successful) { - Log.d(TAG, "Command sent successfully:" + job.toString()); - } else { - Log.d(TAG, "Framework rejected command immediately:" + job.toString()); + if (D) { + Log.d(TAG, "Command sent successfully:" + job.toString()); + } + } else if (V) { + Log.v(TAG, "Framework rejected command immediately:" + job.toString()); } - - } else { + } else if (D) { Log.d(TAG, "Job already has a sent time. Skip. " + job.toString()); } @@ -204,19 +211,19 @@ public class LocalBluetoothDevice implements Comparable { } public void onProfileStateChanged() { - Log.d(TAG, "onProfileStateChanged:" + workQueue.toString()); + if (D) { + Log.d(TAG, "onProfileStateChanged:" + workQueue.toString()); + } BluetoothJob job = workQueue.peek(); if (job == null) { - Log.v(TAG, "Yikes, onProfileStateChanged called but job queue is empty. " - + "(Okay for device initiated actions and BluetoothA2dpService initiated " - + "Auto-connections)"); return; } else if (job.device.mAddress != mAddress) { // This can happen in 2 cases: 1) BT device initiated pairing and // 2) disconnects of one headset that's triggered by connects of // another. - Log.v(TAG, "onProfileStateChanged called. The addresses differ. this.mAddress=" - + mAddress + " workQueue.head=" + job.toString()); + if (D) { + Log.d(TAG, "mAddresses:" + mAddress + " != head:" + job.toString()); + } // Check to see if we need to remove the stale items from the queue if (!pruneQueue(null)) { @@ -225,7 +232,6 @@ public class LocalBluetoothDevice implements Comparable { } } else { // Remove the first item and process the next one - Log.d(TAG, "LocalBluetoothDevice.onProfileStateChanged() called. MAC addr matched"); workQueue.poll(); } @@ -240,7 +246,9 @@ public class LocalBluetoothDevice implements Comparable { * notification when it finishes processing a command */ private void processCommands() { - Log.d(TAG, "processCommands:" + workQueue.toString()); + if (D) { + Log.d(TAG, "processCommands:" + workQueue.toString()); + } Iterator it = workQueue.iterator(); while (it.hasNext()) { BluetoothJob job = it.next(); diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java index 273f79748e4..2e843386cc2 100644 --- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java +++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java @@ -30,6 +30,7 @@ import android.bluetooth.BluetoothIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.util.Config; import android.util.Log; import android.widget.Toast; @@ -40,7 +41,8 @@ import android.widget.Toast; */ public class LocalBluetoothManager { private static final String TAG = "LocalBluetoothManager"; - static final boolean V = true; + static final boolean V = Config.LOGV; + static final boolean D = Config.LOGD && false; private static final String SHARED_PREFERENCES_NAME = "bluetooth_settings";