1 /* 2 * Copyright (C) 2015 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 17 package com.android.camera.one.v2.core; 18 19 import android.hardware.camera2.CaptureResult; 20 import android.hardware.camera2.TotalCaptureResult; 21 22 import com.android.camera.async.Updatable; 23 import com.android.camera.one.v2.camera2proxy.AndroidCaptureResultProxy; 24 import com.android.camera.one.v2.camera2proxy.AndroidTotalCaptureResultProxy; 25 import com.android.camera.one.v2.camera2proxy.CaptureResultProxy; 26 import com.android.camera.one.v2.camera2proxy.TotalCaptureResultProxy; 27 28 import java.util.Collection; 29 30 /** 31 * Static factories for simple {@link ResponseListener}s. 32 */ 33 public final class ResponseListeners { 34 /** 35 * Base class for {@link ResponseListener}s which wrap another callback. 36 * <p> 37 * Forwards hashCode and equals (and no other methods) to the callback. This 38 * enables us to maintain sets of listeners without duplicates. Subclasses 39 * should override the appropriate {@link ResponseListener} methods to 40 * forward to mDelegate as needed. 41 */ 42 private static abstract class ResponseListenerBase<T> extends ResponseListener { 43 private final Updatable<T> mDelegate; 44 45 private ResponseListenerBase(Updatable<T> delegate) { 46 mDelegate = delegate; 47 } 48 49 @Override 50 public int hashCode() { 51 return mDelegate.hashCode(); 52 } 53 54 @Override 55 public boolean equals(Object other) { 56 return mDelegate.equals(other); 57 } 58 } 59 60 private ResponseListeners() { 61 } 62 63 /** 64 * @param callback A thread-safe callback to receive the final metadata for 65 * each frame. Metadata will always be received in-order. 66 */ 67 public static ResponseListener forFinalMetadata( 68 final Updatable<TotalCaptureResultProxy> callback) { 69 return new ResponseListenerBase<TotalCaptureResultProxy>(callback) { 70 @Override 71 public void onCompleted(TotalCaptureResult result) { 72 callback.update(new AndroidTotalCaptureResultProxy(result)); 73 } 74 }; 75 } 76 77 /** 78 * @param callback A thread-safe callback to receive partial and final 79 * metadata for each frame. Metadata may be received 80 * out-of-order. 81 */ 82 public static ResponseListener forPartialMetadata(final Updatable<CaptureResultProxy> callback) { 83 return new ResponseListenerBase<CaptureResultProxy>(callback) { 84 @Override 85 public void onProgressed(CaptureResult partialResult) { 86 callback.update(new AndroidCaptureResultProxy(partialResult)); 87 } 88 89 @Override 90 public void onCompleted(TotalCaptureResult result) { 91 callback.update(new AndroidTotalCaptureResultProxy(result)); 92 } 93 }; 94 } 95 96 /** 97 * @param callback A thread-safe callback to receive the timestamp of the 98 * expose time for each frame. Timestamps will be received 99 * in-order. 100 */ 101 public static ResponseListener forTimestamps(final Updatable<Long> callback) { 102 return new ResponseListenerBase<Long>(callback) { 103 @Override 104 public void onStarted(long timestamp) { 105 callback.update(timestamp); 106 } 107 }; 108 } 109 110 /** 111 * @param callback A thread-safe callback to be invoked as soon as each 112 * frame is exposed by the device. 113 */ 114 public static ResponseListener forFrameExposure(final Updatable<Void> callback) { 115 return new ResponseListenerBase<Void>(callback) { 116 @Override 117 @SuppressWarnings("ConstantConditions") 118 public void onStarted(long timestamp) { 119 callback.update(null); 120 } 121 }; 122 } 123 124 /** 125 * Combines multiple {@link ResponseListener}s. 126 */ 127 public static ResponseListener forListeners(ResponseListener... listeners) { 128 return new ResponseListenerBroadcaster(listeners); 129 } 130 131 /** 132 * Combines multiple {@link ResponseListener}s. 133 */ 134 public static ResponseListener forListeners(Collection<ResponseListener> listeners) { 135 return new ResponseListenerBroadcaster(listeners); 136 } 137 } 138