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

Commit 186b9e44 authored by Robert Wu's avatar Robert Wu
Browse files

USB MIDI: Reset counter after unique codes full

Some tests continously plug and unplug MIDI devices.
After 1000 operations, there is an infinite while loop.

This CL clears up the hashset if it is nearly full.

Bug: 234688233
Test: Plug and unplug MIDI devices
Change-Id: Ie0c6359c07694e3f6cc4a3011fd39a525314ab94
parent 9ff3c6b6
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@ public class UsbHostManager {
    private final HashMap<String, ArrayList<UsbDirectMidiDevice>>
            mMidiDevices = new HashMap<String, ArrayList<UsbDirectMidiDevice>>();
    private final HashSet<String> mMidiUniqueCodes = new HashSet<String>();
    private static final int MAX_UNIQUE_CODE_GENERATION_ATTEMPTS = 10;
    private final Random mRandom = new Random();
    private final boolean mHasMidiFeature;

@@ -645,11 +646,18 @@ public class UsbHostManager {
    // Generate a 3 digit code.
    private String generateNewUsbDeviceIdentifier() {
        String code;
        int numberOfAttempts = 0;
        do {
            if (numberOfAttempts > MAX_UNIQUE_CODE_GENERATION_ATTEMPTS) {
                Slog.w(TAG, "MIDI unique code array resetting");
                mMidiUniqueCodes.clear();
                numberOfAttempts = 0;
            }
            code = "";
            for (int i = 0; i < 3; i++) {
                code += mRandom.nextInt(10);
            }
            numberOfAttempts++;
        } while (mMidiUniqueCodes.contains(code));
        mMidiUniqueCodes.add(code);
        return code;