From d4dd159b6ef55d35143cd268c168d3e3f6c8db2f Mon Sep 17 00:00:00 2001 From: Ido Ben-Hur Date: Wed, 13 Dec 2023 07:59:42 -0500 Subject: [PATCH] ParanoidSystemUI: BluetoothDialog: Sort devices by last connection time Same as settings does it. Offer a major UX improvement. Change-Id: I0ce229ed2baf9ee77f9f649fc2115e0670aed38a Signed-off-by: Adithya R --- .../qs/tiles/dialog/BluetoothDialog.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/co/aospa/systemui/qs/tiles/dialog/BluetoothDialog.java b/src/co/aospa/systemui/qs/tiles/dialog/BluetoothDialog.java index 996770d..c209c07 100644 --- a/src/co/aospa/systemui/qs/tiles/dialog/BluetoothDialog.java +++ b/src/co/aospa/systemui/qs/tiles/dialog/BluetoothDialog.java @@ -23,6 +23,7 @@ package co.aospa.systemui.qs.tiles.dialog; import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Intent; @@ -58,6 +59,8 @@ import com.android.systemui.statusbar.policy.BluetoothController; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; +import java.util.List; /** * Dialog for bluetooth @@ -244,7 +247,7 @@ public class BluetoothDialog extends SystemUIDialog implements Window.Callback { mDivider.setVisibility(showProgress ? View.GONE : View.VISIBLE); // devices - final Collection devices = mBluetoothController.getDevices(); + final Collection devices = getDevices(); if (!enabled || devices == null) { mBluetoothRecyclerView.setVisibility(View.GONE); mSeeAllLayout.setVisibility(View.GONE); @@ -266,4 +269,25 @@ public class BluetoothDialog extends SystemUIDialog implements Window.Callback { mAdapter.setActiveDevice(activeDevice); mSeeAllLayout.setVisibility(devices.size() > MAX_DEVICES_COUNT ? View.VISIBLE : View.GONE); } -} \ No newline at end of file + + private Collection getDevices() { + final Collection devices = mBluetoothController.getDevices(); + if (devices == null) return null; + if (devices.size() <= 1) return devices; + Collection sorted = new ArrayList<>(); + devices.stream().sorted(new BtDeviceComparator()).forEach(sorted::add); + return sorted; + } + + private static class BtDeviceComparator implements Comparator { + final List sortedDevices = + BluetoothAdapter.getDefaultAdapter().getMostRecentlyConnectedDevices(); + + @Override + public int compare(CachedBluetoothDevice o1, CachedBluetoothDevice o2) { + final int i1 = sortedDevices.indexOf(o1.getDevice()); + final int i2 = sortedDevices.indexOf(o2.getDevice()); + return i1 - i2; + } + } +}