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 */ 16package android.hardware.camera2.impl; 17 18import android.hardware.camera2.CameraCaptureSession; 19import android.hardware.camera2.CameraDevice; 20import android.hardware.camera2.CaptureFailure; 21import android.hardware.camera2.CaptureRequest; 22import android.hardware.camera2.CaptureResult; 23import android.hardware.camera2.TotalCaptureResult; 24import android.hardware.camera2.dispatch.Dispatchable; 25import android.hardware.camera2.dispatch.MethodNameInvoker; 26import android.view.Surface; 27 28import static com.android.internal.util.Preconditions.*; 29 30/** 31 * Proxy out invocations to the camera2 API callbacks into a {@link Dispatchable}. 32 * 33 * <p>Since abstract classes do not support Java's dynamic {@code Proxy}, we have to 34 * to use our own proxy mechanism.</p> 35 */ 36public class CallbackProxies { 37 38 // TODO: replace with codegen 39 40 public static class DeviceStateCallbackProxy extends CameraDeviceImpl.StateCallbackKK { 41 private final MethodNameInvoker<CameraDeviceImpl.StateCallbackKK> mProxy; 42 43 public DeviceStateCallbackProxy( 44 Dispatchable<CameraDeviceImpl.StateCallbackKK> dispatchTarget) { 45 dispatchTarget = checkNotNull(dispatchTarget, "dispatchTarget must not be null"); 46 mProxy = new MethodNameInvoker<>(dispatchTarget, CameraDeviceImpl.StateCallbackKK.class); 47 } 48 49 @Override 50 public void onOpened(CameraDevice camera) { 51 mProxy.invoke("onOpened", camera); 52 } 53 54 @Override 55 public void onDisconnected(CameraDevice camera) { 56 mProxy.invoke("onDisconnected", camera); 57 } 58 59 @Override 60 public void onError(CameraDevice camera, int error) { 61 mProxy.invoke("onError", camera, error); 62 } 63 64 @Override 65 public void onUnconfigured(CameraDevice camera) { 66 mProxy.invoke("onUnconfigured", camera); 67 } 68 69 @Override 70 public void onActive(CameraDevice camera) { 71 mProxy.invoke("onActive", camera); 72 } 73 74 @Override 75 public void onBusy(CameraDevice camera) { 76 mProxy.invoke("onBusy", camera); 77 } 78 79 @Override 80 public void onClosed(CameraDevice camera) { 81 mProxy.invoke("onClosed", camera); 82 } 83 84 @Override 85 public void onIdle(CameraDevice camera) { 86 mProxy.invoke("onIdle", camera); 87 } 88 } 89 90 @SuppressWarnings("deprecation") 91 public static class DeviceCaptureCallbackProxy extends CameraDeviceImpl.CaptureCallback { 92 private final MethodNameInvoker<CameraDeviceImpl.CaptureCallback> mProxy; 93 94 public DeviceCaptureCallbackProxy( 95 Dispatchable<CameraDeviceImpl.CaptureCallback> dispatchTarget) { 96 dispatchTarget = checkNotNull(dispatchTarget, "dispatchTarget must not be null"); 97 mProxy = new MethodNameInvoker<>(dispatchTarget, CameraDeviceImpl.CaptureCallback.class); 98 } 99 100 @Override 101 public void onCaptureStarted(CameraDevice camera, 102 CaptureRequest request, long timestamp, long frameNumber) { 103 mProxy.invoke("onCaptureStarted", camera, request, timestamp, frameNumber); 104 } 105 106 @Override 107 public void onCapturePartial(CameraDevice camera, 108 CaptureRequest request, CaptureResult result) { 109 mProxy.invoke("onCapturePartial", camera, request, result); 110 } 111 112 @Override 113 public void onCaptureProgressed(CameraDevice camera, 114 CaptureRequest request, CaptureResult partialResult) { 115 mProxy.invoke("onCaptureProgressed", camera, request, partialResult); 116 } 117 118 @Override 119 public void onCaptureCompleted(CameraDevice camera, 120 CaptureRequest request, TotalCaptureResult result) { 121 mProxy.invoke("onCaptureCompleted", camera, request, result); 122 } 123 124 @Override 125 public void onCaptureFailed(CameraDevice camera, 126 CaptureRequest request, CaptureFailure failure) { 127 mProxy.invoke("onCaptureFailed", camera, request, failure); 128 } 129 130 @Override 131 public void onCaptureSequenceCompleted(CameraDevice camera, 132 int sequenceId, long frameNumber) { 133 mProxy.invoke("onCaptureSequenceCompleted", camera, sequenceId, frameNumber); 134 } 135 136 @Override 137 public void onCaptureSequenceAborted(CameraDevice camera, 138 int sequenceId) { 139 mProxy.invoke("onCaptureSequenceAborted", camera, sequenceId); 140 } 141 } 142 143 public static class SessionStateCallbackProxy 144 extends CameraCaptureSession.StateCallback { 145 private final MethodNameInvoker<CameraCaptureSession.StateCallback> mProxy; 146 147 public SessionStateCallbackProxy( 148 Dispatchable<CameraCaptureSession.StateCallback> dispatchTarget) { 149 dispatchTarget = checkNotNull(dispatchTarget, "dispatchTarget must not be null"); 150 mProxy = new MethodNameInvoker<>(dispatchTarget, 151 CameraCaptureSession.StateCallback.class); 152 } 153 154 @Override 155 public void onConfigured(CameraCaptureSession session) { 156 mProxy.invoke("onConfigured", session); 157 } 158 159 160 @Override 161 public void onConfigureFailed(CameraCaptureSession session) { 162 mProxy.invoke("onConfigureFailed", session); 163 } 164 165 @Override 166 public void onReady(CameraCaptureSession session) { 167 mProxy.invoke("onReady", session); 168 } 169 170 @Override 171 public void onActive(CameraCaptureSession session) { 172 mProxy.invoke("onActive", session); 173 } 174 175 @Override 176 public void onClosed(CameraCaptureSession session) { 177 mProxy.invoke("onClosed", session); 178 } 179 180 @Override 181 public void onSurfacePrepared(CameraCaptureSession session, Surface surface) { 182 mProxy.invoke("onSurfacePrepared", session, surface); 183 } 184 185 } 186 187 private CallbackProxies() { 188 throw new AssertionError(); 189 } 190} 191