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

Commit e2378b10 authored by StevenHarperUK's avatar StevenHarperUK Committed by Gerrit Code Review
Browse files

Add Dock USB Audio Support for Samsung Car / Desk Docks 1 of 2

Samsung docks have a non-sensing USB adio port.
Samsung get around this by having a separate Setting to route the Audio
The Changes made will only add the extra Observer if the Kernel supports the endpoint

  /sys/class/switch/dock/state

A new BroadcastReciever (SettingsChangedReceiver) is used to watch out for users changing the Preference

The choice is made in the GalaxyS2Settings.apk (see 2 of 2)

  device/samsung/i9100/DeviceSettings/src/com/cyanogenmod/settings/device/DockFragmentActivity.java

After OnBootComplete and when the checkbox is changed, an Intent is triggered to this class.

When a Dock Event is caught if it is one the Samsung endpoint "dock" then if the users preference is checked,
then the event is allowed to go on and route the audio.  If it is not checked then it is ignored.

Change-Id: Id43af5a13d04e96645ccc49292518107aa4af98e
parent 7acd1c45
Loading
Loading
Loading
Loading
+53 −12
Original line number Diff line number Diff line
@@ -107,6 +107,14 @@ class WiredAccessoryObserver extends UEventObserver {
            Slog.w(TAG, "This kernel does not have usb audio support");
        }

        // Monitor Samsung USB audio
        uei = new UEventInfo("dock", BIT_USB_HEADSET_DGTL, BIT_USB_HEADSET_ANLG);
        if (uei.checkSwitchExists()) {
            retVal.add(uei);
        } else {
            Slog.w(TAG, "This kernel does not have samsung usb dock audio support");
        }

        // Monitor HDMI
        //
        // If the kernel has support for the "hdmi_audio" switch, use that.  It will be signalled
@@ -135,6 +143,7 @@ class WiredAccessoryObserver extends UEventObserver {
    private int mHeadsetState;
    private int mPrevHeadsetState;
    private String mHeadsetName;
    private boolean dockAudioEnabled = false;

    private final Context mContext;
    private final WakeLock mWakeLock;  // held while there is a pending route change
@@ -148,16 +157,39 @@ class WiredAccessoryObserver extends UEventObserver {
        mWakeLock.setReferenceCounted(false);
        mAudioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

        File f = new File("/sys/class/switch/dock/state");
        if (f!=null && f.exists()) {
            // Listen out for changes to the Dock Audio Settings
            context.registerReceiver(new SettingsChangedReceiver(),
            new IntentFilter("com.cyanogenmod.settings.SamsungDock"), null, null);
        }
        context.registerReceiver(new BootCompletedReceiver(),
            new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
    }

    private final class SettingsChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Slog.e(TAG, "Recieved a Settings Changed Action " + action);
            if (action.equals("com.cyanogenmod.settings.SamsungDock")) {
                String data = intent.getStringExtra("data");
                Slog.e(TAG, "Recieved a Dock Audio change " + data);
                if (data != null && data.equals("1")) {
                    dockAudioEnabled = true;
                } else {
                    dockAudioEnabled = false;
                }
            }
        }
    }

    private final class BootCompletedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // At any given time accessories could be inserted
        // one on the board, one on the dock and one on HDMI:
        // observe three UEVENTs
            // one on the board, one on the dock, one on the samsung dock and one on HDMI:
            // observe all UEVENTs that have a valid switch supported by the Kernel
            init();  // set initial status
            for (int i = 0; i < uEventInfo.size(); ++i) {
                UEventInfo uei = uEventInfo.get(i);
@@ -173,6 +205,16 @@ class WiredAccessoryObserver extends UEventObserver {
        try {
            String devPath = event.get("DEVPATH");
            String name = event.get("SWITCH_NAME");
            if (name.equals("dock")) {
                // Samsung USB Audio Jack is non-sensing - so must be enabled manually
                // The choice is made in the GalaxyS2Settings.apk
                // device/samsung/i9100/DeviceSettings/src/com/cyanogenmod/settings/device/DockFragmentActivity.java
                // This sends an Intent to this class
                if (!dockAudioEnabled) {
                    Slog.e(TAG, "Ignoring dock event as Audio routing disabled " + event);
                    return;
                }
            }
            int state = Integer.parseInt(event.get("SWITCH_STATE"));
            updateState(devPath, name, state);
        } catch (NumberFormatException e) {
@@ -180,8 +222,7 @@ class WiredAccessoryObserver extends UEventObserver {
        }
    }

    private synchronized final void updateState(String devPath, String name, int state)
    {
    private synchronized final void updateState(String devPath, String name, int state) {
        for (int i = 0; i < uEventInfo.size(); ++i) {
            UEventInfo uei = uEventInfo.get(i);
            if (devPath.equals(uei.getDevPath())) {