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

Commit c15f7564 authored by Paul McLean's avatar Paul McLean Committed by Paul Mclean
Browse files

Fixing problems with 1st connect of USB audio device to Fugu.

Also added some additional logging functions as we are not done
looking at connect/disconnect issues.

Leaving in tact the multi-device connect/disconnect logic (neeeds to
be revisited)

Bug: 24906368
Change-Id: Iff91c51a9c7013dde56182059f3747e1d6cd727b
parent 87566087
Loading
Loading
Loading
Loading
+54 −1
Original line number Diff line number Diff line
@@ -94,6 +94,12 @@ public class AlsaCardsParser {
        public String textFormat() {
          return mCardName + " : " + mCardDescription;
        }

        public void log(int listIndex) {
            Slog.d(TAG, "" + listIndex +
                " [" + mCardNum + " " + mCardName + " : " + mCardDescription +
                " usb:" + mIsUsb);
        }
    }

    public AlsaCardsParser() {}
@@ -169,9 +175,41 @@ public class AlsaCardsParser {

    // return -1 if none found
    public int getDefaultUsbCard() {
        // save the current list of devices
        ArrayList<AlsaCardsParser.AlsaCardRecord> prevRecs = mCardRecords;
        if (DEBUG) {
            LogDevices("Previous Devices:", prevRecs);
        }

        // get the new list of devices
        scan();
        if (DEBUG) {
            LogDevices("Current Devices:", mCardRecords);
        }

        // Calculate the difference between the old and new device list
        ArrayList<AlsaCardRecord> newRecs = getNewCardRecords(prevRecs);
        if (DEBUG) {
            LogDevices("New Devices:", newRecs);
        }

        // Choose the most-recently added EXTERNAL card
        // Check recently added devices
        for (AlsaCardRecord rec : newRecs) {
            if (DEBUG) {
                Slog.d(TAG, rec.mCardName + " card:" + rec.mCardNum + " usb:" + rec.mIsUsb);
            }
            if (rec.mIsUsb) {
                // Found it
                return rec.mCardNum;
            }
        }

        // or return the first added EXTERNAL card?
        for (AlsaCardRecord rec : mCardRecords) {
        for (AlsaCardRecord rec : prevRecs) {
            if (DEBUG) {
                Slog.d(TAG, rec.mCardName + " card:" + rec.mCardNum + " usb:" + rec.mIsUsb);
            }
            if (rec.mIsUsb) {
                return rec.mCardNum;
            }
@@ -183,11 +221,17 @@ public class AlsaCardsParser {
    public int getDefaultCard() {
        // return an external card if possible
        int card = getDefaultUsbCard();
        if (DEBUG) {
            Slog.d(TAG, "getDefaultCard() default usb card:" + card);
        }

        if (card < 0 && getNumCardRecords() > 0) {
            // otherwise return the (internal) card with the highest number
            card = getCardRecordAt(getNumCardRecords() - 1).mCardNum;
        }
        if (DEBUG) {
            Slog.d(TAG, "  returns card:" + card);
        }
        return card;
    }

@@ -222,4 +266,13 @@ public class AlsaCardsParser {
            }
        }
    }

    static public void LogDevices(String caption, ArrayList<AlsaCardRecord> deviceList) {
        Slog.d(TAG, caption + " ----------------");
        int listIndex = 0;
        for (AlsaCardRecord device : deviceList) {
            device.log(listIndex++);
        }
        Slog.d(TAG, "----------------");
    }
}
+24 −21
Original line number Diff line number Diff line
@@ -212,6 +212,10 @@ public final class UsbAlsaManager {
    }

    private AlsaDevice waitForAlsaDevice(int card, int device, int type) {
        if (DEBUG) {
            Slog.e(TAG, "waitForAlsaDevice(c:" + card + " d:" + device + ")");
        }

        AlsaDevice testDevice = new AlsaDevice(type, card, device);

        // This value was empirically determined.
@@ -292,7 +296,8 @@ public final class UsbAlsaManager {
     */
    /* package */ UsbAudioDevice selectAudioCard(int card) {
        if (DEBUG) {
            Slog.d(TAG, "selectAudioCard() card:" + card);
            Slog.d(TAG, "selectAudioCard() card:" + card
                    + " isCardUsb(): " + mCardsParser.isCardUsb(card));
        }
        if (!mCardsParser.isCardUsb(card)) {
            // Don't. AudioPolicyManager has logic for falling back to internal devices.
@@ -304,6 +309,10 @@ public final class UsbAlsaManager {

        boolean hasPlayback = mDevicesParser.hasPlaybackDevices(card);
        boolean hasCapture = mDevicesParser.hasCaptureDevices(card);
        if (DEBUG) {
            Slog.d(TAG, "usb: hasPlayback:" + hasPlayback + " hasCapture:" + hasCapture);
        }

        int deviceClass =
            (mCardsParser.isCardUsb(card)
                ? UsbAudioDevice.kAudioDeviceClass_External
@@ -320,10 +329,6 @@ public final class UsbAlsaManager {
            return null;
        }

        if (DEBUG) {
            Slog.d(TAG, "usb: hasPlayback:" + hasPlayback + " hasCapture:" + hasCapture);
        }

        UsbAudioDevice audioDevice =
                new UsbAudioDevice(card, device, hasPlayback, hasCapture, deviceClass);
        AlsaCardsParser.AlsaCardRecord cardRecord = mCardsParser.getCardRecordFor(card);
@@ -339,7 +344,6 @@ public final class UsbAlsaManager {
        if (DEBUG) {
            Slog.d(TAG, "UsbAudioManager.selectDefaultDevice()");
        }
        mCardsParser.scan();
        return selectAudioCard(mCardsParser.getDefaultCard());
    }

@@ -361,27 +365,22 @@ public final class UsbAlsaManager {
                isAudioDevice = true;
            }
        }

        if (DEBUG) {
            Slog.d(TAG, "  isAudioDevice: " + isAudioDevice);
        }
        if (!isAudioDevice) {
            return;
        }

        ArrayList<AlsaCardsParser.AlsaCardRecord> prevScanRecs = mCardsParser.getScanRecords();
        mCardsParser.scan();

        int addedCard = -1;
        ArrayList<AlsaCardsParser.AlsaCardRecord>
            newScanRecs = mCardsParser.getNewCardRecords(prevScanRecs);
        if (newScanRecs.size() > 0) {
            // This is where we select the just connected device
            // NOTE - to switch to prefering the first-connected device, just always
            // take the else clause below.
            addedCard = newScanRecs.get(0).mCardNum;
        } else {
            addedCard = mCardsParser.getDefaultUsbCard();
        }
        int addedCard = mCardsParser.getDefaultUsbCard();

        // If the default isn't a USB device, let the existing "select internal mechanism"
        // handle the selection.
        if (DEBUG) {
            Slog.d(TAG, "  mCardsParser.isCardUsb(" + addedCard + ") = "
                        + mCardsParser.isCardUsb(addedCard));
        }
        if (mCardsParser.isCardUsb(addedCard)) {
            UsbAudioDevice audioDevice = selectAudioCard(addedCard);
            if (audioDevice != null) {
@@ -429,6 +428,10 @@ public final class UsbAlsaManager {
                }
            }
        }

        if (DEBUG) {
            Slog.d(TAG, "deviceAdded() - done");
        }
    }

    /* package */ void usbDeviceRemoved(UsbDevice usbDevice) {