Add UVC support

This CL adds support for UVC GadgetFunction. UVC can be enabled/disabled
by setting the `ro.usb.uvc.enabled` property, and this CL makes sure
that the Settings app honors the property when showing USB functions to
the user.

Bug: 242344221
Test: Manually tested that the 'USB Webcam' option is present when the
      property is set, and removed when property is unset.
Change-Id: I5d1ff0a43d3c0c722bc9e03132a581da5c61bd76
This commit is contained in:
Avichal Rakesh
2023-01-19 14:21:02 -08:00
parent d540e02be8
commit e6731b05e1
5 changed files with 25 additions and 2 deletions

View File

@@ -8710,6 +8710,10 @@
select what the USB connection for this device should be used for. This choice select what the USB connection for this device should be used for. This choice
is for transferring photos via PTP. --> is for transferring photos via PTP. -->
<string name="usb_use_photo_transfers">PTP</string> <string name="usb_use_photo_transfers">PTP</string>
<!-- Title of one of the choices in a dialog (with title defined in usb_use) that lets the user
select what the USB connection for this device should be used for. This choice
is for using the device as a Webcam. -->
<string name="usb_use_uvc_webcam">Webcam</string>
<!-- Title of one of the choices in a dialog (with title defined in usb_use) that lets the user <!-- Title of one of the choices in a dialog (with title defined in usb_use) that lets the user
select what the USB connection for this device should be used for. This choice select what the USB connection for this device should be used for. This choice
is for transcoding the files that are transferred via MTP. --> is for transcoding the files that are transferred via MTP. -->
@@ -8775,6 +8779,8 @@
<string name="usb_summary_photo_transfers">PTP</string> <string name="usb_summary_photo_transfers">PTP</string>
<!-- Settings item summary for USB preference when set to entering MIDI mode [CHAR LIMIT=NONE] --> <!-- Settings item summary for USB preference when set to entering MIDI mode [CHAR LIMIT=NONE] -->
<string name="usb_summary_MIDI">MIDI</string> <string name="usb_summary_MIDI">MIDI</string>
<!-- Settings item summary for USB preference when set to entering UVC mode [CHAR LIMIT=NONE] -->
<string name="usb_summary_UVC">Webcam</string>
<!-- Settings item summary for USB preference when set to transferring files via MTP <!-- Settings item summary for USB preference when set to transferring files via MTP
and powering other device [CHAR LIMIT=NONE] --> and powering other device [CHAR LIMIT=NONE] -->
<string name="usb_summary_file_transfers_power">File transfer and supplying power</string> <string name="usb_summary_file_transfers_power">File transfer and supplying power</string>
@@ -8787,6 +8793,9 @@
<!-- Settings item summary for USB preference when set to entering MIDI mode <!-- Settings item summary for USB preference when set to entering MIDI mode
and powering other device [CHAR LIMIT=NONE] --> and powering other device [CHAR LIMIT=NONE] -->
<string name="usb_summary_MIDI_power">MIDI and supplying power</string> <string name="usb_summary_MIDI_power">MIDI and supplying power</string>
<!-- Settings item summary for USB preference when set to entering UVC mode
and powering other device [CHAR LIMIT=NONE] -->
<string name="usb_summary_UVC_power">Webcam and supplying power</string>
<!-- Settings item title for background check prefs [CHAR LIMIT=35] --> <!-- Settings item title for background check prefs [CHAR LIMIT=35] -->
<string name="background_check_pref">Background check</string> <string name="background_check_pref">Background check</string>

View File

@@ -129,6 +129,8 @@ public class ConnectedUsbDeviceUpdater {
return R.string.usb_summary_photo_transfers; return R.string.usb_summary_photo_transfers;
} else if (functions == UsbManager.FUNCTION_MIDI) { } else if (functions == UsbManager.FUNCTION_MIDI) {
return R.string.usb_summary_MIDI; return R.string.usb_summary_MIDI;
} else if (functions == UsbManager.FUNCTION_UVC) {
return R.string.usb_summary_UVC;
} else { } else {
return R.string.usb_summary_charging_only; return R.string.usb_summary_charging_only;
} }
@@ -141,6 +143,8 @@ public class ConnectedUsbDeviceUpdater {
return R.string.usb_summary_photo_transfers_power; return R.string.usb_summary_photo_transfers_power;
} else if (functions == UsbManager.FUNCTION_MIDI) { } else if (functions == UsbManager.FUNCTION_MIDI) {
return R.string.usb_summary_MIDI_power; return R.string.usb_summary_MIDI_power;
} else if (functions == UsbManager.FUNCTION_UVC) {
return R.string.usb_summary_UVC_power;
} else { } else {
return R.string.usb_summary_power_only; return R.string.usb_summary_power_only;
} }

View File

@@ -53,6 +53,7 @@ public class UsbBackend {
private final boolean mTetheringRestrictedBySystem; private final boolean mTetheringRestrictedBySystem;
private final boolean mMidiSupported; private final boolean mMidiSupported;
private final boolean mTetheringSupported; private final boolean mTetheringSupported;
private final boolean mUVCEnabled;
private final boolean mIsAdminUser; private final boolean mIsAdminUser;
private UsbManager mUsbManager; private UsbManager mUsbManager;
@@ -74,12 +75,12 @@ public class UsbBackend {
mFileTransferRestrictedBySystem = isUsbFileTransferRestrictedBySystem(userManager); mFileTransferRestrictedBySystem = isUsbFileTransferRestrictedBySystem(userManager);
mTetheringRestricted = isUsbTetheringRestricted(userManager); mTetheringRestricted = isUsbTetheringRestricted(userManager);
mTetheringRestrictedBySystem = isUsbTetheringRestrictedBySystem(userManager); mTetheringRestrictedBySystem = isUsbTetheringRestrictedBySystem(userManager);
mUVCEnabled = isUvcEnabled();
mIsAdminUser = userManager.isAdminUser(); mIsAdminUser = userManager.isAdminUser();
mMidiSupported = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI); mMidiSupported = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
final TetheringManager tm = context.getSystemService(TetheringManager.class); final TetheringManager tm = context.getSystemService(TetheringManager.class);
mTetheringSupported = tm.isTetheringSupported(); mTetheringSupported = tm.isTetheringSupported();
updatePorts(); updatePorts();
} }
@@ -200,6 +201,10 @@ public class UsbBackend {
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId())); UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
} }
private static boolean isUvcEnabled() {
return UsbManager.isUvcSupportEnabled();
}
private boolean areFunctionDisallowed(long functions) { private boolean areFunctionDisallowed(long functions) {
return (mFileTransferRestricted && ((functions & UsbManager.FUNCTION_MTP) != 0 return (mFileTransferRestricted && ((functions & UsbManager.FUNCTION_MTP) != 0
|| (functions & UsbManager.FUNCTION_PTP) != 0)) || (functions & UsbManager.FUNCTION_PTP) != 0))
@@ -209,7 +214,8 @@ public class UsbBackend {
private boolean areFunctionsDisallowedBySystem(long functions) { private boolean areFunctionsDisallowedBySystem(long functions) {
return (mFileTransferRestrictedBySystem && ((functions & UsbManager.FUNCTION_MTP) != 0 return (mFileTransferRestrictedBySystem && ((functions & UsbManager.FUNCTION_MTP) != 0
|| (functions & UsbManager.FUNCTION_PTP) != 0)) || (functions & UsbManager.FUNCTION_PTP) != 0))
|| (mTetheringRestrictedBySystem && ((functions & UsbManager.FUNCTION_RNDIS) != 0)); || (mTetheringRestrictedBySystem && ((functions & UsbManager.FUNCTION_RNDIS) != 0))
|| (!mUVCEnabled && ((functions & UsbManager.FUNCTION_UVC) != 0));
} }
@VisibleForTesting @VisibleForTesting

View File

@@ -87,6 +87,9 @@ public class UsbConnectionBroadcastReceiver extends BroadcastReceiver implements
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_NCM)) { if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_NCM)) {
functions |= UsbManager.FUNCTION_NCM; functions |= UsbManager.FUNCTION_NCM;
} }
if (intent.getExtras().getBoolean(UsbManager.USB_FUNCTION_UVC)) {
functions |= UsbManager.FUNCTION_UVC;
}
mFunctions = functions; mFunctions = functions;
mDataRole = mUsbBackend.getDataRole(); mDataRole = mUsbBackend.getDataRole();
mPowerRole = mUsbBackend.getPowerRole(); mPowerRole = mUsbBackend.getPowerRole();

View File

@@ -52,6 +52,7 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
FUNCTIONS_MAP.put(UsbManager.FUNCTION_RNDIS, R.string.usb_use_tethering); FUNCTIONS_MAP.put(UsbManager.FUNCTION_RNDIS, R.string.usb_use_tethering);
FUNCTIONS_MAP.put(UsbManager.FUNCTION_MIDI, R.string.usb_use_MIDI); FUNCTIONS_MAP.put(UsbManager.FUNCTION_MIDI, R.string.usb_use_MIDI);
FUNCTIONS_MAP.put(UsbManager.FUNCTION_PTP, R.string.usb_use_photo_transfers); FUNCTIONS_MAP.put(UsbManager.FUNCTION_PTP, R.string.usb_use_photo_transfers);
FUNCTIONS_MAP.put(UsbManager.FUNCTION_UVC, R.string.usb_use_uvc_webcam);
FUNCTIONS_MAP.put(UsbManager.FUNCTION_NONE, R.string.usb_use_charging_only); FUNCTIONS_MAP.put(UsbManager.FUNCTION_NONE, R.string.usb_use_charging_only);
} }