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

Commit 1df2a1d7 authored by Juffin Alex Varghese's avatar Juffin Alex Varghese Committed by Linux Build Service Account
Browse files

Bluetooth: Remove out of range devices after ScanComplete

Change to remove device from UI when Bluetooth of remote
device is off or not discoverable anymore. Without this
change a device that discovered previously is available
in available devices list always, though the remote device
is not discoverable anymore. The current change updates
available devices list on scan complete removing the
devices that are available in cached devices and are not
discoverable anymore.

CRs-Fixed: 523374
Change-Id: Ia250d52e6fb7a69af242c961d33027f1e724ab25
(cherry picked from commit fee7b100ff7ac4eb7117ec1bc5c592d0c7adbbf8)
(cherry picked from commit 38d07978c7fb4f40af96b9fb96af09a64ce5bfff)
(cherry picked from commit 28a27af31eac45f4b3fe5932240dc437a37dda5c)
parent 1505f0fd
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -288,6 +288,14 @@ final class BluetoothEventManager {
                        cachedDevice.setVisible(false);
                        cachedDevice.setVisible(false);
                    }
                    }
                }
                }
                if (cachedDevice.isRemovable()) {
                    synchronized (mCallbacks) {
                        for (BluetoothCallback callback : mCallbacks) {
                            callback.onDeviceDeleted(cachedDevice);
                        }
                    }
                    mDeviceManager.onDeviceDeleted(cachedDevice);
                }
                int reason = intent.getIntExtra(BluetoothDevice.EXTRA_REASON,
                int reason = intent.getIntExtra(BluetoothDevice.EXTRA_REASON,
                        BluetoothDevice.ERROR);
                        BluetoothDevice.ERROR);


+13 −0
Original line number Original line Diff line number Diff line
@@ -64,6 +64,8 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {


    private boolean mVisible;
    private boolean mVisible;


    private boolean mDeviceRemove;

    private int mPhonebookPermissionChoice;
    private int mPhonebookPermissionChoice;


    private int mMessagePermissionChoice;
    private int mMessagePermissionChoice;
@@ -339,6 +341,7 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
                    if (Utils.D) {
                    if (Utils.D) {
                        Log.d(TAG, "Command sent successfully:REMOVE_BOND " + describe(null));
                        Log.d(TAG, "Command sent successfully:REMOVE_BOND " + describe(null));
                    }
                    }
                    setRemovable(true);
                } else if (Utils.V) {
                } else if (Utils.V) {
                    Log.v(TAG, "Framework rejected command immediately:REMOVE_BOND " +
                    Log.v(TAG, "Framework rejected command immediately:REMOVE_BOND " +
                            describe(null));
                            describe(null));
@@ -424,6 +427,11 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
        return mVisible;
        return mVisible;
    }
    }


    boolean isRemovable () {
        return mDeviceRemove;
   }


    void setVisible(boolean visible) {
    void setVisible(boolean visible) {
        if (mVisible != visible) {
        if (mVisible != visible) {
            mVisible = visible;
            mVisible = visible;
@@ -431,6 +439,11 @@ final class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice> {
        }
        }
    }
    }


    void setRemovable(boolean removable) {
        mDeviceRemove = removable;
    }


    int getBondState() {
    int getBondState() {
        return mDevice.getBondState();
        return mDevice.getBondState();
    }
    }
+16 −3
Original line number Original line Diff line number Diff line
@@ -111,13 +111,17 @@ final class CachedBluetoothDeviceManager {
    }
    }


    public synchronized void onScanningStateChanged(boolean started) {
    public synchronized void onScanningStateChanged(boolean started) {
        if (!started) return;

        // If starting a new scan, clear old visibility
        // If starting a new scan, clear old visibility
        // Iterate in reverse order since devices may be removed.
        // Iterate in reverse order since devices may be removed.
        for (int i = mCachedDevices.size() - 1; i >= 0; i--) {
        for (int i = mCachedDevices.size() - 1; i >= 0; i--) {
            CachedBluetoothDevice cachedDevice = mCachedDevices.get(i);
            CachedBluetoothDevice cachedDevice = mCachedDevices.get(i);
            if (started) {
                cachedDevice.setVisible(false);
                cachedDevice.setVisible(false);
            } else if (!started &&
                cachedDevice.getBondState() == BluetoothDevice.BOND_NONE &&
                cachedDevice.isRemovable()) {
                mCachedDevices.remove(cachedDevice);
            }
        }
        }
    }
    }


@@ -135,6 +139,15 @@ final class CachedBluetoothDeviceManager {
        }
        }
    }
    }


    public synchronized void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
        Log.d(TAG,"onDeviceDeleted");
        if (cachedDevice != null &&
            cachedDevice.getBondState() == BluetoothDevice.BOND_NONE &&
            cachedDevice.isRemovable()) {
            mCachedDevices.remove(cachedDevice);
        }
    }

    public synchronized void onBluetoothStateChanged(int bluetoothState) {
    public synchronized void onBluetoothStateChanged(int bluetoothState) {
        // When Bluetooth is turning off, we need to clear the non-bonded devices
        // When Bluetooth is turning off, we need to clear the non-bonded devices
        // Otherwise, they end up showing up on the next BT enable
        // Otherwise, they end up showing up on the next BT enable
+20 −0
Original line number Original line Diff line number Diff line
@@ -130,6 +130,23 @@ public abstract class DeviceListPreferenceFragment extends
        }
        }
    }
    }


    void removeOorDevices() {
        Collection<CachedBluetoothDevice> cachedDevices =
                mLocalManager.getCachedDeviceManager().getCachedDevicesCopy();
        for (CachedBluetoothDevice cachedDevice : cachedDevices) {
             if (cachedDevice.getBondState() == BluetoothDevice.BOND_NONE &&
                 !cachedDevice.isVisible()) {
                 Log.d(TAG, "Device Removed " + cachedDevice);
                 BluetoothDevicePreference preference = mDevicePreferenceMap.get(cachedDevice);
                 if (preference != null) {
                     mDeviceListGroup.removePreference(preference);
                 }
                 mDevicePreferenceMap.remove(cachedDevice);
                 cachedDevice.setRemovable(true);
             }
         }
    }

    @Override
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
            Preference preference) {
@@ -191,6 +208,9 @@ public abstract class DeviceListPreferenceFragment extends
    }
    }


    public void onScanningStateChanged(boolean started) {
    public void onScanningStateChanged(boolean started) {
        if (started == false) {
          removeOorDevices();
        }
        updateProgressUi(started);
        updateProgressUi(started);
    }
    }