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 <gh0strider.2k18.reborn@gmail.com>
This commit is contained in:
Ido Ben-Hur 2023-12-13 07:59:42 -05:00 committed by Adithya R
parent 06337b1a7a
commit d4dd159b6e
1 changed files with 26 additions and 2 deletions

View File

@ -23,6 +23,7 @@
package co.aospa.systemui.qs.tiles.dialog; package co.aospa.systemui.qs.tiles.dialog;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -58,6 +59,8 @@ import com.android.systemui.statusbar.policy.BluetoothController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.List;
/** /**
* Dialog for bluetooth * Dialog for bluetooth
@ -244,7 +247,7 @@ public class BluetoothDialog extends SystemUIDialog implements Window.Callback {
mDivider.setVisibility(showProgress ? View.GONE : View.VISIBLE); mDivider.setVisibility(showProgress ? View.GONE : View.VISIBLE);
// devices // devices
final Collection<CachedBluetoothDevice> devices = mBluetoothController.getDevices(); final Collection<CachedBluetoothDevice> devices = getDevices();
if (!enabled || devices == null) { if (!enabled || devices == null) {
mBluetoothRecyclerView.setVisibility(View.GONE); mBluetoothRecyclerView.setVisibility(View.GONE);
mSeeAllLayout.setVisibility(View.GONE); mSeeAllLayout.setVisibility(View.GONE);
@ -266,4 +269,25 @@ public class BluetoothDialog extends SystemUIDialog implements Window.Callback {
mAdapter.setActiveDevice(activeDevice); mAdapter.setActiveDevice(activeDevice);
mSeeAllLayout.setVisibility(devices.size() > MAX_DEVICES_COUNT ? View.VISIBLE : View.GONE); mSeeAllLayout.setVisibility(devices.size() > MAX_DEVICES_COUNT ? View.VISIBLE : View.GONE);
} }
private Collection<CachedBluetoothDevice> getDevices() {
final Collection<CachedBluetoothDevice> devices = mBluetoothController.getDevices();
if (devices == null) return null;
if (devices.size() <= 1) return devices;
Collection<CachedBluetoothDevice> sorted = new ArrayList<>();
devices.stream().sorted(new BtDeviceComparator()).forEach(sorted::add);
return sorted;
}
private static class BtDeviceComparator implements Comparator<CachedBluetoothDevice> {
final List<BluetoothDevice> 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;
}
}
} }