From 50c129b118b5993fcb3acf28abf4a3bd8e4226fe Mon Sep 17 00:00:00 2001 From: malikakash Date: Fri, 28 Jul 2023 07:07:08 +0000 Subject: [PATCH] Implement CameraIdRemapping in CameraManager - Add remapCameraIds() to CameraManager. - We also take care of updating the CameraIdRemapping state in the CameraService every time we lose connection to the camera service. That should take care of maintaining the active configuration state in the camera service. Security Bug: b/288901406 (For clearance related to the effort and the permission design) Bug: 286287541 Test: manual test. Change-Id: I74bea71462aefe7332375812a3219117a6d1506e --- .../hardware/camera2/CameraManager.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/core/java/android/hardware/camera2/CameraManager.java b/core/java/android/hardware/camera2/CameraManager.java index c2fe0800812f9..c80124c4c2ec3 100644 --- a/core/java/android/hardware/camera2/CameraManager.java +++ b/core/java/android/hardware/camera2/CameraManager.java @@ -31,6 +31,7 @@ import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Point; import android.hardware.CameraExtensionSessionStats; +import android.hardware.CameraIdRemapping; import android.hardware.CameraStatus; import android.hardware.ICameraService; import android.hardware.ICameraServiceListener; @@ -1729,6 +1730,17 @@ public final class CameraManager { } } + /** + * Remaps Camera Ids in the CameraService. + * + * @hide + */ + @RequiresPermission(android.Manifest.permission.CAMERA_INJECT_EXTERNAL_CAMERA) + public void remapCameraIds(@NonNull CameraIdRemapping cameraIdRemapping) + throws CameraAccessException, SecurityException, IllegalArgumentException { + CameraManagerGlobal.get().remapCameraIds(cameraIdRemapping); + } + /** * Reports {@link CameraExtensionSessionStats} to the {@link ICameraService} to be logged for * currently active session. Validation is done downstream. @@ -1802,6 +1814,13 @@ public final class CameraManager { private final Object mLock = new Object(); + /** + * The active CameraIdRemapping. This will be used to refresh the cameraIdRemapping state + * in the CameraService every time we connect to it, including when the CameraService + * Binder dies and we reconnect to it. + */ + @Nullable private CameraIdRemapping mActiveCameraIdRemapping; + // Access only through getCameraService to deal with binder death private ICameraService mCameraService; private boolean mHasOpenCloseListenerPermission = false; @@ -1944,6 +1963,41 @@ public final class CameraManager { } catch (RemoteException e) { // Camera service died in all probability } + + if (mActiveCameraIdRemapping != null) { + try { + cameraService.remapCameraIds(mActiveCameraIdRemapping); + } catch (ServiceSpecificException e) { + // Unexpected failure, ignore and continue. + Log.e(TAG, "Unable to remap camera Ids in the camera service"); + } catch (RemoteException e) { + // Camera service died in all probability + } + } + } + + /** Updates the cameraIdRemapping state in the CameraService. */ + public void remapCameraIds(@NonNull CameraIdRemapping cameraIdRemapping) + throws CameraAccessException, SecurityException { + synchronized (mLock) { + ICameraService cameraService = getCameraService(); + if (cameraService == null) { + throw new CameraAccessException( + CameraAccessException.CAMERA_DISCONNECTED, + "Camera service is currently unavailable."); + } + + try { + cameraService.remapCameraIds(cameraIdRemapping); + mActiveCameraIdRemapping = cameraIdRemapping; + } catch (ServiceSpecificException e) { + throwAsPublicException(e); + } catch (RemoteException e) { + throw new CameraAccessException( + CameraAccessException.CAMERA_DISCONNECTED, + "Camera service is currently unavailable."); + } + } } private String[] extractCameraIdListLocked() {