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

Commit 3fc9d788 authored by Brian Duddie's avatar Brian Duddie
Browse files

Improve debuggability of sensor listener exhaustion

Logcat may not be available, so determine the likely culprit and include
it directly in the exception message.

Bug: 412955375
Flag: EXEMPT log only update
Test: run with test code that intentionally triggers exhaustion
Change-Id: Ie24e8763eef76766fe1ed949cc77cc5eafa053fc
parent 01e50f35
Loading
Loading
Loading
Loading
+13 −4
Original line number Diff line number Diff line
@@ -257,15 +257,24 @@ public class SystemSensorManager extends SensorManager {
        }
        if (mSensorListeners.size() >= MAX_LISTENER_COUNT) {
            Log.e(TAG, "Too many sensor listeners! Dump:");
            Map<String, Integer> listenerCounts = new HashMap<>();
            synchronized (mSensorListeners) {
                int i = 0;
                for (SensorEventListener debugListener : mSensorListeners.keySet()) {
                    Log.e(TAG, "  " + ++i + ": " + debugListener.toString());
                }
            }
            throw new IllegalStateException("register failed, "
                + "the sensor listeners size has exceeded the maximum limit "
                + MAX_LISTENER_COUNT);
                    String listenerName = debugListener.toString();
                    Log.e(TAG, "  " + ++i + ": " + listenerName);
                    int index = listenerName.indexOf('@');
                    listenerName = index < 0 ? listenerName
                            : listenerName.substring(0, index);
                    listenerCounts.put(
                            listenerName, listenerCounts.getOrDefault(listenerName, 0) + 1);
                }
            }
            Map.Entry<String, Integer> maxEntry = listenerCounts.entrySet().stream()
                    .max(Map.Entry.comparingByValue()).get();
            throw new IllegalStateException("Too many sensor listeners (" + MAX_LISTENER_COUNT
                    + "). Most common: " + maxEntry.getKey() + " (" + maxEntry.getValue()
                    + ")");
        }

        // Invariants to preserve: