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

Commit 08bff3b9 authored by Mike Lockwood's avatar Mike Lockwood
Browse files

USB: Add functions for querying if a USB function is supported and enabled.



Change-Id: I5fc24960e1e01c27e892acc2e76c12ddf8c654cb
Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 9846bc6a
Loading
Loading
Loading
Loading
+30 −0
Original line number Diff line number Diff line
@@ -17,6 +17,10 @@

package android.hardware;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Class for accessing USB state information.
 * @hide
@@ -114,4 +118,30 @@ public class Usb {
     * Used in extras for the {@link #ACTION_USB_CONNECTED} broadcast
     */
    public static final String USB_FUNCTION_DISABLED = "disabled";

    private static File getFunctionEnableFile(String function) {
        return new File("/sys/class/usb_composite/" + function + "/enable");
    }

    /**
     * Returns true if the specified USB function is supported by the kernel.
     * Note that a USB function maybe supported but disabled.
     */
    public static boolean isFunctionSupported(String function) {
        return getFunctionEnableFile(function).exists();
    }

    /**
     * Returns true if the specified USB function is currently enabled.
     */
    public static boolean isFunctionEnabled(String function) {
        try {
            FileInputStream stream = new FileInputStream(getFunctionEnableFile(function));
            boolean enabled = (stream.read() == '1');
            stream.close();
            return enabled;
        } catch (IOException e) {
            return false;
        }
    }
}