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

Commit d4d2c4a3 authored by Emilio López's avatar Emilio López
Browse files

framework: base: support legacy usb mass storage

This commit reintroduces support for the legacy usb mass storage
system. When the newer system is not supported, the framework
now checks if the old system is available. If present, it is used
to support usb mass storage mode.

Change-Id: I0dccc593636fd311accc561b5dfd21689336c907
parent faea1156
Loading
Loading
Loading
Loading
+73 −39
Original line number Diff line number Diff line
@@ -64,12 +64,16 @@ public class UsbService extends IUsbManager.Stub {
            "DEVPATH=/devices/virtual/switch/usb_configuration";
    private static final String USB_FUNCTIONS_MATCH =
            "DEVPATH=/devices/virtual/usb_composite/";
    private static final String USB_LEGACY_MATCH =
            "DEVPATH=/devices/virtual/switch/usb_mass_storage";
    private static final String USB_CONNECTED_PATH =
            "/sys/class/switch/usb_connected/state";
    private static final String USB_CONFIGURATION_PATH =
            "/sys/class/switch/usb_configuration/state";
    private static final String USB_COMPOSITE_CLASS_PATH =
            "/sys/class/usb_composite";
    private static final String USB_LEGACY_PATH =
            "/sys/class/switch/usb_mass_storage/state";

    private static final int MSG_UPDATE_STATE = 0;
    private static final int MSG_FUNCTION_ENABLED = 1;
@@ -83,6 +87,7 @@ public class UsbService extends IUsbManager.Stub {
    // current connected and configuration state
    private int mConnected;
    private int mConfiguration;
    private boolean mLegacy = false;

    // last broadcasted connected and configuration state
    private int mLastConnected = -1;
@@ -154,6 +159,17 @@ public class UsbService extends IUsbManager.Stub {
                String state = event.get("SWITCH_STATE");
                if (name != null && state != null) {
                    try {
                        if (mLegacy) {
                            int intState = (state == "offline" ? 0 : 1);
                            if ("usb_mass_storage".equals(name)) {
                                mConnected = intState;
                                // trigger an Intent broadcast
                                if (mSystemReady) {
                                    // debounce disconnects to avoid problems b$
                                    update(mConnected == 0);
                                }
                            }
                        } else {
                            int intState = Integer.parseInt(state);
                            if ("usb_connected".equals(name)) {
                                mConnected = intState;
@@ -169,6 +185,7 @@ public class UsbService extends IUsbManager.Stub {
                                    update(mConnected == 0);
                                }
                            }
                        }
                    } catch (NumberFormatException e) {
                        Slog.e(TAG, "Could not parse switch state from event " + event);
                    }
@@ -237,6 +254,19 @@ public class UsbService extends IUsbManager.Stub {

        } catch (FileNotFoundException e) {
            Slog.i(TAG, "This kernel does not have USB configuration switch support");
            Slog.i(TAG, "Trying legacy USB configuration switch support");
            try {
                FileReader file = new FileReader(USB_LEGACY_PATH);
                int len = file.read(buffer, 0, 1024);
                file.close();
                mConnected = ((new String(buffer, 0, len)).trim() == "offline" ? 0 : 1);
                mLegacy = true;

            } catch (FileNotFoundException f) {
                Slog.i(TAG, "This kernel does not have legacy USB configuration switch support");
            } catch (Exception f) {
                Slog.e(TAG, "" , f);
            }
        } catch (Exception e) {
            Slog.e(TAG, "" , e);
        }
@@ -245,6 +275,9 @@ public class UsbService extends IUsbManager.Stub {
            return;
        }

        if (mLegacy) {
            mEnabledFunctions.add(UsbManager.USB_FUNCTION_MASS_STORAGE);
        } else {
            // Read initial list of enabled and disabled functions (device mode)
            try {
                File[] files = new File(USB_COMPOSITE_CLASS_PATH).listFiles();
@@ -275,10 +308,11 @@ public class UsbService extends IUsbManager.Stub {
            } catch (Exception e) {
                Slog.e(TAG, "" , e);
            }
        }

        // handle the case where an accessory switched the driver to accessory mode
        // before the framework finished booting
        if (inAccessoryMode) {
        if (inAccessoryMode && !mLegacy) {
            readCurrentAccessoryLocked();

            // FIXME - if we booted in accessory mode, then we have no way to figure out