Home | History | Annotate | Download | only in impl
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.hardware.camera2.impl;
     17 
     18 import android.os.Binder;
     19 import android.hardware.camera2.CameraCaptureSession;
     20 import android.hardware.camera2.CameraDevice;
     21 import android.hardware.camera2.CaptureFailure;
     22 import android.hardware.camera2.CaptureRequest;
     23 import android.hardware.camera2.CaptureResult;
     24 import android.hardware.camera2.TotalCaptureResult;
     25 import android.view.Surface;
     26 
     27 import java.util.concurrent.Executor;
     28 
     29 import static com.android.internal.util.Preconditions.*;
     30 
     31 /**
     32  * Proxy out invocations to the camera2 API callbacks into a {@link Dispatchable}.
     33  *
     34  * <p>Since abstract classes do not support Java's dynamic {@code Proxy}, we have to
     35  * to use our own proxy mechanism.</p>
     36  */
     37 public class CallbackProxies {
     38     public static class SessionStateCallbackProxy
     39             extends CameraCaptureSession.StateCallback {
     40         private final Executor mExecutor;
     41         private final CameraCaptureSession.StateCallback mCallback;
     42 
     43         public SessionStateCallbackProxy(Executor executor,
     44                 CameraCaptureSession.StateCallback callback) {
     45             mExecutor = checkNotNull(executor, "executor must not be null");
     46             mCallback = checkNotNull(callback, "callback must not be null");
     47         }
     48 
     49         @Override
     50         public void onConfigured(CameraCaptureSession session) {
     51             final long ident = Binder.clearCallingIdentity();
     52             try {
     53                 mExecutor.execute(() -> mCallback.onConfigured(session));
     54             } finally {
     55                 Binder.restoreCallingIdentity(ident);
     56             }
     57         }
     58 
     59 
     60         @Override
     61         public void onConfigureFailed(CameraCaptureSession session) {
     62             final long ident = Binder.clearCallingIdentity();
     63             try {
     64                 mExecutor.execute(() -> mCallback.onConfigureFailed(session));
     65             } finally {
     66                 Binder.restoreCallingIdentity(ident);
     67             }
     68         }
     69 
     70         @Override
     71         public void onReady(CameraCaptureSession session) {
     72             final long ident = Binder.clearCallingIdentity();
     73             try {
     74                 mExecutor.execute(() -> mCallback.onReady(session));
     75             } finally {
     76                 Binder.restoreCallingIdentity(ident);
     77             }
     78         }
     79 
     80         @Override
     81         public void onActive(CameraCaptureSession session) {
     82             final long ident = Binder.clearCallingIdentity();
     83             try {
     84                 mExecutor.execute(() -> mCallback.onActive(session));
     85             } finally {
     86                 Binder.restoreCallingIdentity(ident);
     87             }
     88         }
     89 
     90         @Override
     91         public void onCaptureQueueEmpty(CameraCaptureSession session) {
     92             final long ident = Binder.clearCallingIdentity();
     93             try {
     94                 mExecutor.execute(() -> mCallback.onCaptureQueueEmpty(session));
     95             } finally {
     96                 Binder.restoreCallingIdentity(ident);
     97             }
     98         }
     99 
    100         @Override
    101         public void onClosed(CameraCaptureSession session) {
    102             final long ident = Binder.clearCallingIdentity();
    103             try {
    104                 mExecutor.execute(() -> mCallback.onClosed(session));
    105             } finally {
    106                 Binder.restoreCallingIdentity(ident);
    107             }
    108         }
    109 
    110         @Override
    111         public void onSurfacePrepared(CameraCaptureSession session, Surface surface) {
    112             final long ident = Binder.clearCallingIdentity();
    113             try {
    114                 mExecutor.execute(() -> mCallback.onSurfacePrepared(session, surface));
    115             } finally {
    116                 Binder.restoreCallingIdentity(ident);
    117             }
    118         }
    119 
    120     }
    121 
    122     private CallbackProxies() {
    123         throw new AssertionError();
    124     }
    125 }
    126