Loading core/java/android/hardware/usb/UsbManager.java +7 −2 Original line number Diff line number Diff line Loading @@ -954,7 +954,10 @@ public class UsbManager { /** * Returns whether the given functions are valid inputs to UsbManager. * Currently the empty functions or any of MTP, PTP, RNDIS, MIDI are accepted. * Currently the empty functions or any of MTP, PTP, RNDIS, MIDI, NCM are accepted. * * Only one function may be set at a time, except for RNDIS and NCM, which can be set together * because from a user perspective they are the same function (tethering). * * @return Whether the mask is settable. * Loading @@ -962,7 +965,9 @@ public class UsbManager { */ public static boolean areSettableFunctions(long functions) { return functions == FUNCTION_NONE || ((~SETTABLE_FUNCTIONS & functions) == 0 && Long.bitCount(functions) == 1); || ((~SETTABLE_FUNCTIONS & functions) == 0 && ((Long.bitCount(functions) == 1) || (functions == (FUNCTION_RNDIS | FUNCTION_NCM)))); } /** Loading tests/UsbTests/src/com/android/server/usb/UsbManagerNoPermTest.java +35 −2 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.server.usb; import static org.junit.Assert.assertEquals; import android.content.Context; import android.hardware.usb.UsbManager; Loading @@ -23,12 +25,12 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.server.usblib.UsbManagerTestLib; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import com.android.server.usblib.UsbManagerTestLib; /** * Unit tests for {@link android.hardware.usb.UsbManager}. * Note: NOT claimed MANAGE_USB permission in Manifest Loading Loading @@ -78,4 +80,35 @@ public class UsbManagerNoPermTest { public void testUsbApi_SetCurrentFunctions_OnSecurityException() throws Exception { mUsbManagerTestLib.testSetCurrentFunctionsEx(UsbManager.FUNCTION_NONE); } public void assertSettableFunctions(boolean settable, long functions) { assertEquals( "areSettableFunctions(" + UsbManager.usbFunctionsToString(functions) + "):", settable, UsbManager.areSettableFunctions(functions)); } /** * Tests the behaviour of the static areSettableFunctions method. This method performs no IPCs * and requires no permissions. */ @Test public void testUsbManager_AreSettableFunctions() { // NONE is settable. assertSettableFunctions(true, UsbManager.FUNCTION_NONE); // MTP, PTP, RNDIS, MIDI, NCM are all settable by themselves. assertSettableFunctions(true, UsbManager.FUNCTION_MTP); assertSettableFunctions(true, UsbManager.FUNCTION_PTP); assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS); assertSettableFunctions(true, UsbManager.FUNCTION_MIDI); assertSettableFunctions(true, UsbManager.FUNCTION_NCM); // Setting two functions at the same time is not allowed... assertSettableFunctions(false, UsbManager.FUNCTION_MTP | UsbManager.FUNCTION_PTP); assertSettableFunctions(false, UsbManager.FUNCTION_PTP | UsbManager.FUNCTION_RNDIS); assertSettableFunctions(false, UsbManager.FUNCTION_MIDI | UsbManager.FUNCTION_NCM); // ... except in the special case of RNDIS and NCM. assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS | UsbManager.FUNCTION_NCM); } } Loading
core/java/android/hardware/usb/UsbManager.java +7 −2 Original line number Diff line number Diff line Loading @@ -954,7 +954,10 @@ public class UsbManager { /** * Returns whether the given functions are valid inputs to UsbManager. * Currently the empty functions or any of MTP, PTP, RNDIS, MIDI are accepted. * Currently the empty functions or any of MTP, PTP, RNDIS, MIDI, NCM are accepted. * * Only one function may be set at a time, except for RNDIS and NCM, which can be set together * because from a user perspective they are the same function (tethering). * * @return Whether the mask is settable. * Loading @@ -962,7 +965,9 @@ public class UsbManager { */ public static boolean areSettableFunctions(long functions) { return functions == FUNCTION_NONE || ((~SETTABLE_FUNCTIONS & functions) == 0 && Long.bitCount(functions) == 1); || ((~SETTABLE_FUNCTIONS & functions) == 0 && ((Long.bitCount(functions) == 1) || (functions == (FUNCTION_RNDIS | FUNCTION_NCM)))); } /** Loading
tests/UsbTests/src/com/android/server/usb/UsbManagerNoPermTest.java +35 −2 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package com.android.server.usb; import static org.junit.Assert.assertEquals; import android.content.Context; import android.hardware.usb.UsbManager; Loading @@ -23,12 +25,12 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; import com.android.server.usblib.UsbManagerTestLib; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import com.android.server.usblib.UsbManagerTestLib; /** * Unit tests for {@link android.hardware.usb.UsbManager}. * Note: NOT claimed MANAGE_USB permission in Manifest Loading Loading @@ -78,4 +80,35 @@ public class UsbManagerNoPermTest { public void testUsbApi_SetCurrentFunctions_OnSecurityException() throws Exception { mUsbManagerTestLib.testSetCurrentFunctionsEx(UsbManager.FUNCTION_NONE); } public void assertSettableFunctions(boolean settable, long functions) { assertEquals( "areSettableFunctions(" + UsbManager.usbFunctionsToString(functions) + "):", settable, UsbManager.areSettableFunctions(functions)); } /** * Tests the behaviour of the static areSettableFunctions method. This method performs no IPCs * and requires no permissions. */ @Test public void testUsbManager_AreSettableFunctions() { // NONE is settable. assertSettableFunctions(true, UsbManager.FUNCTION_NONE); // MTP, PTP, RNDIS, MIDI, NCM are all settable by themselves. assertSettableFunctions(true, UsbManager.FUNCTION_MTP); assertSettableFunctions(true, UsbManager.FUNCTION_PTP); assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS); assertSettableFunctions(true, UsbManager.FUNCTION_MIDI); assertSettableFunctions(true, UsbManager.FUNCTION_NCM); // Setting two functions at the same time is not allowed... assertSettableFunctions(false, UsbManager.FUNCTION_MTP | UsbManager.FUNCTION_PTP); assertSettableFunctions(false, UsbManager.FUNCTION_PTP | UsbManager.FUNCTION_RNDIS); assertSettableFunctions(false, UsbManager.FUNCTION_MIDI | UsbManager.FUNCTION_NCM); // ... except in the special case of RNDIS and NCM. assertSettableFunctions(true, UsbManager.FUNCTION_RNDIS | UsbManager.FUNCTION_NCM); } }