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

Commit 0e971f32 authored by Emilian Peev's avatar Emilian Peev
Browse files

Camera: Add support for camera access permission callback

Camera availability callbacks must support client
notifications about access priority changes. Such
data can be useful for any client that is actively
waiting for access to a particular camera device.

Bug: 121379978
Test: Camera CTS, successful docs build.
Change-Id: I423960c3a5bc5a2e156ccf3e8623c26d49350131
parent dbd71543
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -16794,6 +16794,7 @@ package android.hardware.camera2 {
  public abstract static class CameraManager.AvailabilityCallback {
    ctor public CameraManager.AvailabilityCallback();
    method public void onCameraAccessPrioritiesChanged();
    method public void onCameraAvailable(@NonNull String);
    method public void onCameraUnavailable(@NonNull String);
  }
+54 −0
Original line number Diff line number Diff line
@@ -678,6 +678,31 @@ public final class CameraManager {
        public void onCameraUnavailable(@NonNull String cameraId) {
            // default empty implementation
        }

        /**
         * Notify registered clients about a change in the camera access priorities.
         *
         * <p>Notification that camera access priorities have changed and the camera may
         * now be openable. An application that was previously denied camera access due to
         * a higher-priority user already using the camera, or that was disconnected from an
         * active camera session due to a higher-priority user trying to open the camera,
         * should try to open the camera again if it still wants to use it.  Note that
         * multiple applications may receive this callback at the same time, and only one of
         * them will succeed in opening the camera in practice, depending on exact access
         * priority levels and timing. This method is useful in cases where multiple
         * applications may be in the resumed state at the same time, and the user switches
         * focus between them, or if the current camera-using application moves between
         * full-screen and Picture-in-Picture (PiP) states. In such cases, the camera
         * available/unavailable callbacks will not be invoked, but another application may
         * now have higher priority for camera access than the current camera-using
         * application.</p>
         *
         * <p>The default implementation of this method does nothing.</p>
         *
         */
        public void onCameraAccessPrioritiesChanged() {
            // default empty implementation
        }
    }

    /**
@@ -1098,6 +1123,22 @@ public final class CameraManager {
            }
        }

        private void postSingleAccessPriorityChangeUpdate(final AvailabilityCallback callback,
                final Executor executor) {
            final long ident = Binder.clearCallingIdentity();
            try {
                executor.execute(
                    new Runnable() {
                        @Override
                        public void run() {
                            callback.onCameraAccessPrioritiesChanged();
                        }
                    });
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
        }

        private void postSingleUpdate(final AvailabilityCallback callback, final Executor executor,
                final String id, final int status) {
            if (isAvailable(status)) {
@@ -1347,6 +1388,19 @@ public final class CameraManager {
            }
        }

        @Override
        public void onCameraAccessPrioritiesChanged() {
            synchronized (mLock) {
                final int callbackCount = mCallbackMap.size();
                for (int i = 0; i < callbackCount; i++) {
                    Executor executor = mCallbackMap.valueAt(i);
                    final AvailabilityCallback callback = mCallbackMap.keyAt(i);

                    postSingleAccessPriorityChangeUpdate(callback, executor);
                }
            }
        }

        /**
         * Try to connect to camera service after some delay if any client registered camera
         * availability callback or torch status callback.
+4 −0
Original line number Diff line number Diff line
@@ -309,6 +309,10 @@ public class CameraBinderTest extends AndroidTestCase {
            Log.v(TAG, String.format("Camera %s has torch status changed to 0x%x",
                    cameraId, status));
        }
        @Override
        public void onCameraAccessPrioritiesChanged() {
            Log.v(TAG, "Camera access permission change");
        }
    }

    /**