This source file includes following definitions.
- getStateWaiter
- waitAndGetSession
- onActive
- onClosed
- onConfigured
- onConfigureFailed
- onReady
- setSession
- cancel
- isCancelled
- isDone
- get
- get
package com.android.ex.camera2.blocking;
import android.hardware.camera2.CameraCaptureSession;
import android.os.ConditionVariable;
import android.util.Log;
import com.android.ex.camera2.exceptions.TimeoutRuntimeException;
import com.android.ex.camera2.utils.StateChangeListener;
import com.android.ex.camera2.utils.StateWaiter;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class BlockingSessionCallback extends CameraCaptureSession.StateCallback {
public static final int SESSION_CONFIGURED = 0;
public static final int SESSION_CONFIGURE_FAILED = 1;
public static final int SESSION_READY = 2;
public static final int SESSION_ACTIVE = 3;
public static final int SESSION_CLOSED = 4;
private static final int NUM_STATES = 5;
private static final String TAG = "BlockingSessionCallback";
private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
private final CameraCaptureSession.StateCallback mProxy;
private final SessionFuture mSessionFuture = new SessionFuture();
private final StateWaiter mStateWaiter = new StateWaiter(sStateNames);
private final StateChangeListener mStateChangeListener = mStateWaiter.getListener();
private static final String[] sStateNames = {
"SESSION_CONFIGURED",
"SESSION_CONFIGURE_FAILED",
"SESSION_READY",
"SESSION_ACTIVE",
"SESSION_CLOSED"
};
public BlockingSessionCallback() {
mProxy = null;
}
public BlockingSessionCallback(CameraCaptureSession.StateCallback listener) {
if (listener == null) {
throw new NullPointerException("listener must not be null");
}
mProxy = listener;
}
public StateWaiter getStateWaiter() {
return mStateWaiter;
}
public CameraCaptureSession waitAndGetSession(long timeoutMs) {
try {
return mSessionFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
throw new TimeoutRuntimeException(
String.format("Failed to get session after %s milliseconds", timeoutMs), e);
}
}
@Override
public void onActive(CameraCaptureSession session) {
mSessionFuture.setSession(session);
if (mProxy != null) mProxy.onActive(session);
mStateChangeListener.onStateChanged(SESSION_ACTIVE);
}
@Override
public void onClosed(CameraCaptureSession session) {
mSessionFuture.setSession(session);
if (mProxy != null) mProxy.onClosed(session);
mStateChangeListener.onStateChanged(SESSION_CLOSED);
}
@Override
public void onConfigured(CameraCaptureSession session) {
mSessionFuture.setSession(session);
if (mProxy != null) {
mProxy.onConfigured(session);
}
mStateChangeListener.onStateChanged(SESSION_CONFIGURED);
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
mSessionFuture.setSession(session);
if (mProxy != null) {
mProxy.onConfigureFailed(session);
}
mStateChangeListener.onStateChanged(SESSION_CONFIGURE_FAILED);
}
@Override
public void onReady(CameraCaptureSession session) {
mSessionFuture.setSession(session);
if (mProxy != null) {
mProxy.onReady(session);
}
mStateChangeListener.onStateChanged(SESSION_READY);
}
private static class SessionFuture implements Future<CameraCaptureSession> {
private volatile CameraCaptureSession mSession;
ConditionVariable mCondVar = new ConditionVariable(false);
public void setSession(CameraCaptureSession session) {
mSession = session;
mCondVar.open();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return false;
}
@Override
public boolean isCancelled() {
return false;
}
@Override
public boolean isDone() {
return mSession != null;
}
@Override
public CameraCaptureSession get() {
mCondVar.block();
return mSession;
}
@Override
public CameraCaptureSession get(long timeout, TimeUnit unit) throws TimeoutException {
long timeoutMs = unit.convert(timeout, TimeUnit.MILLISECONDS);
if (!mCondVar.block(timeoutMs)) {
throw new TimeoutException(
"Failed to receive session after " + timeout + " " + unit);
}
if (mSession == null) {
throw new AssertionError();
}
return mSession;
}
}
}