FileDocCategorySizeDatePackage
BlockingStateCallback.javaAPI DocAndroid 5.1 API7024Thu Mar 12 22:22:48 GMT 2015com.android.ex.camera2.blocking

BlockingStateCallback

public class BlockingStateCallback extends CameraDevice.StateCallback
A camera device listener that implements blocking operations on state changes.

Provides wait calls that block until the next unobserved state of the requested type arrives. Unobserved states are states that have occurred since the last wait, or that will be received from the camera device in the future.

Pass-through all StateCallback changes to the proxy.

Fields Summary
private static final String
TAG
private static final boolean
VERBOSE
private final CameraDevice.StateCallback
mProxy
private final Object
mLock
private boolean
mWaiting
private final LinkedBlockingQueue
mRecentStates
private static final String[]
mStateNames
public static final int
STATE_UNINITIALIZED
Device has not reported any state yet
public static final int
STATE_OPENED
Device is in the first-opened state (transitory)
public static final int
STATE_CLOSED
Device is closed
public static final int
STATE_DISCONNECTED
Device is disconnected
public static final int
STATE_ERROR
Device has encountered a fatal error
private static int
NUM_STATES
Total number of reachable states
Constructors Summary
public BlockingStateCallback()


      
        mProxy = null;
    
public BlockingStateCallback(CameraDevice.StateCallback listener)

        mProxy = listener;
    
Methods Summary
public static voidappendStates(java.lang.StringBuilder s, java.util.Collection states)
Append all states to string

        boolean start = true;
        for (Integer state: states) {
            if (!start) s.append(" ");
            s.append(stateToString(state));
            start = false;
        }
    
public voidonClosed(android.hardware.camera2.CameraDevice camera)

        if (mProxy != null) mProxy.onClosed(camera);
        setCurrentState(STATE_CLOSED);
    
public voidonDisconnected(android.hardware.camera2.CameraDevice camera)

        if (mProxy != null) mProxy.onDisconnected(camera);
        setCurrentState(STATE_DISCONNECTED);
    
public voidonError(android.hardware.camera2.CameraDevice camera, int error)

        if (mProxy != null) mProxy.onError(camera, error);
        setCurrentState(STATE_ERROR);
    
public voidonOpened(android.hardware.camera2.CameraDevice camera)

        if (mProxy != null) mProxy.onOpened(camera);
        setCurrentState(STATE_OPENED);
    
private voidsetCurrentState(int state)


        
        if (VERBOSE) Log.v(TAG, "Camera device state now " + stateToString(state));
        try {
            mRecentStates.put(state);
        } catch(InterruptedException e) {
            throw new RuntimeException("Unable to set device state", e);
        }
    
public static java.lang.StringstateToString(int state)
Convert state integer to a String

        return mStateNames[state + 1];
    
public intwaitForAnyOfStates(java.util.Collection states, long timeout)
Wait until the one of the desired states is observed, checking all state transitions since the last state that was waited on.

Note: Only one waiter allowed at a time!

param
states Set of desired states to observe a transition to.
param
timeout how long to wait in milliseconds
return
the state reached
throws
TimeoutRuntimeException if none of the states is observed before timeout.

        synchronized(mLock) {
            if (mWaiting) throw new IllegalStateException("Only one waiter allowed at a time");
            mWaiting = true;
        }
        if (VERBOSE) {
            StringBuilder s = new StringBuilder("Waiting for state(s) ");
            appendStates(s, states);
            Log.v(TAG, s.toString());
        }

        Integer nextState = null;
        long timeoutLeft = timeout;
        long startMs = SystemClock.elapsedRealtime();
        try {
            while ((nextState = mRecentStates.poll(timeoutLeft, TimeUnit.MILLISECONDS))
                    != null) {
                if (VERBOSE) {
                    Log.v(TAG, "  Saw transition to " + stateToString(nextState));
                }
                if (states.contains(nextState)) break;
                long endMs = SystemClock.elapsedRealtime();
                timeoutLeft -= (endMs - startMs);
                startMs = endMs;
            }
        } catch (InterruptedException e) {
            throw new UnsupportedOperationException("Does not support interrupts on waits", e);
        }

        synchronized(mLock) {
            mWaiting = false;
        }

        if (!states.contains(nextState)) {
            StringBuilder s = new StringBuilder("Timed out after ");
            s.append(timeout);
            s.append(" ms waiting for state(s) ");
            appendStates(s, states);

            throw new TimeoutRuntimeException(s.toString());
        }

        return nextState;
    
public voidwaitForState(int state, long timeout)
Wait until the desired state is observed, checking all state transitions since the last state that was waited on.

Note: Only one waiter allowed at a time!

param
desired state to observe a transition to
param
timeout how long to wait in milliseconds
throws
TimeoutRuntimeException if the desired state is not observed before timeout.

        Integer[] stateArray = { state };

        waitForAnyOfStates(Arrays.asList(stateArray), timeout);