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
This commit is contained in:
Robert Wu
2022-06-03 22:18:42 +00:00
parent 9ff3c6b64c
commit 186b9e447b

View File

@@ -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;