Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f25063ae authored by Michael Chan's avatar Michael Chan
Browse files

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.
parent d842cee7
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -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();
+4 −1
Original line number Diff line number Diff line
@@ -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);
        }
    };

+26 −18
Original line number Diff line number Diff line
@@ -47,6 +47,8 @@ import java.util.List;
 */
public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
    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<LocalBluetoothDevice> {
    private static LinkedList<BluetoothJob> workQueue = new LinkedList<BluetoothJob>();

    private void queueCommand(BluetoothJob job) {
        if (D) {
            Log.d(TAG, workQueue.toString());
        }
        synchronized (workQueue) {
            boolean processNow = pruneQueue(job);

            // Add job to queue
            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<LocalBluetoothDevice> {
                        && existingJob.command == BluetoothCommand.CONNECT
                        && existingJob.device.mAddress.equals(job.device.mAddress)
                        && existingJob.profile == job.profile) {
                    if (D) {
                        Log.d(TAG, "Removed because of a pending disconnect. " + existingJob);
                    }
                    it.remove();
                    continue;
                }
@@ -165,7 +172,6 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
            // 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<LocalBluetoothDevice> {
            }

            if (successful) {
                if (D) {
                    Log.d(TAG, "Command sent successfully:" + job.toString());
            } else {
                Log.d(TAG, "Framework rejected command immediately:" + job.toString());
                }
            
        } else {
            } else if (V) {
                Log.v(TAG, "Framework rejected command immediately:" + job.toString());
            }
        } else if (D) {
            Log.d(TAG, "Job already has a sent time. Skip. " + job.toString());
        }

@@ -204,19 +211,19 @@ public class LocalBluetoothDevice implements Comparable<LocalBluetoothDevice> {
    }

    public void onProfileStateChanged() {
        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<LocalBluetoothDevice> {
            }
        } 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<LocalBluetoothDevice> {
     *    notification when it finishes processing a command
     */
    private void processCommands() {
        if (D) {
            Log.d(TAG, "processCommands:" + workQueue.toString());
        }
        Iterator<BluetoothJob> it = workQueue.iterator();
        while (it.hasNext()) {
            BluetoothJob job = it.next();
+3 −1
Original line number Diff line number Diff line
@@ -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";