From 1618789e3f5e0b844916524da2229a3e08ceabfa Mon Sep 17 00:00:00 2001
From: Oscar Mira <valldrac@pm.me>
Date: Sun, 10 May 2020 01:56:34 +0200
Subject: [PATCH] Fix AudioPolicy device entries order to support
 SLOT_RANDOMIZE

Fix GrapheneOS/os_issue_tracker#137
---
 .../include/DeviceDescriptor.h                   | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/services/audiopolicy/common/managerdefinitions/include/DeviceDescriptor.h b/services/audiopolicy/common/managerdefinitions/include/DeviceDescriptor.h
index 33e506f1b..5f9f8b558 100644
--- a/services/audiopolicy/common/managerdefinitions/include/DeviceDescriptor.h
+++ b/services/audiopolicy/common/managerdefinitions/include/DeviceDescriptor.h
@@ -206,6 +206,22 @@ class DeviceVector : public SortedVector<sp<DeviceDescriptor> >
 
     void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const;
 
+protected:
+    // Sorts devices by type.
+    int do_compare(const void* lhs, const void* rhs) const override
+    {
+        const auto& l = *reinterpret_cast<const sp<DeviceDescriptor>*>(lhs);
+        const auto& r = *reinterpret_cast<const sp<DeviceDescriptor>*>(rhs);
+
+        audio_devices_t lt = l->type();
+        audio_devices_t rt = r->type();
+
+        if (lt == rt) {
+            return 0;
+        }
+        return lt > rt ? 1 : -1;
+    }
+
 private:
     void refreshTypes();
     audio_devices_t mDeviceTypes;
