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

Commit 6ed727e3 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

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

parents f827a0cf 08bff3b9
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;
        }
    }
}