This source file includes following definitions.
- onAutoFocusSuccess
- onAutoFocusFail
- onAutoFocusScan
- onAutoFocusInactive
- onCaptureCompleted
- resetState
- lockAutoFocus
- unlockAutoFocus
- setActiveAutoFocus
- setPassiveAutoFocus
- beginTraceAsync
- endTraceAsync
- updateCaptureRequest
package com.android.ex.camera2.pos;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CaptureResult.Key;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.util.Log;
import com.android.ex.camera2.utils.SysTrace;
public class AutoFocusStateMachine {
public interface AutoFocusStateListener {
void onAutoFocusSuccess(CaptureResult result, boolean locked);
void onAutoFocusFail(CaptureResult result, boolean locked);
void onAutoFocusScan(CaptureResult result);
void onAutoFocusInactive(CaptureResult result);
}
private static final String TAG = "AutoFocusStateMachine";
private static final boolean DEBUG_LOGGING = Log.isLoggable(TAG, Log.DEBUG);
private static final boolean VERBOSE_LOGGING = Log.isLoggable(TAG, Log.VERBOSE);
private static final int AF_UNINITIALIZED = -1;
private final AutoFocusStateListener mListener;
private int mLastAfState = AF_UNINITIALIZED;
private int mLastAfMode = AF_UNINITIALIZED;
private int mCurrentAfMode = AF_UNINITIALIZED;
private int mCurrentAfTrigger = AF_UNINITIALIZED;
private int mCurrentAfCookie = AF_UNINITIALIZED;
private String mCurrentAfTrace = "";
private int mLastAfCookie = 0;
public AutoFocusStateMachine(AutoFocusStateListener listener) {
if (listener == null) {
throw new IllegalArgumentException("listener should not be null");
}
mListener = listener;
}
public synchronized void onCaptureCompleted(CaptureResult result) {
if (result == null) {
Log.w(TAG, "onCaptureCompleted - missing result, skipping AF update");
return;
}
Key<Integer> keyAfState = CaptureResult.CONTROL_AF_STATE;
if (keyAfState == null) {
Log.e(TAG, "onCaptureCompleted - missing android.control.afState key, " +
"skipping AF update");
return;
}
Key<Integer> keyAfMode = CaptureResult.CONTROL_AF_MODE;
if (keyAfMode == null) {
Log.e(TAG, "onCaptureCompleted - missing android.control.afMode key, " +
"skipping AF update");
return;
}
Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
Integer afMode = result.get(CaptureResult.CONTROL_AF_MODE);
if (afState == null) {
Log.w(TAG, "onCaptureCompleted - missing android.control.afState !");
return;
} else if (afMode == null) {
Log.w(TAG, "onCaptureCompleted - missing android.control.afMode !");
return;
}
if (DEBUG_LOGGING) Log.d(TAG, "onCaptureCompleted - new AF mode = " + afMode +
" new AF state = " + afState);
if (mLastAfState == afState && afMode == mLastAfMode) {
return;
}
if (VERBOSE_LOGGING) Log.v(TAG, "onCaptureCompleted - new AF mode = " + afMode +
" new AF state = " + afState);
mLastAfState = afState;
mLastAfMode = afMode;
switch (afState) {
case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
mListener.onAutoFocusSuccess(result, true);
endTraceAsync();
break;
case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
mListener.onAutoFocusFail(result, true);
endTraceAsync();
break;
case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
mListener.onAutoFocusSuccess(result, false);
break;
case CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
mListener.onAutoFocusFail(result, false);
break;
case CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN:
mListener.onAutoFocusScan(result);
break;
case CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN:
mListener.onAutoFocusScan(result);
break;
case CaptureResult.CONTROL_AF_STATE_INACTIVE:
mListener.onAutoFocusInactive(result);
break;
}
}
public synchronized void resetState() {
if (VERBOSE_LOGGING) Log.v(TAG, "resetState - last state was " + mLastAfState);
mLastAfState = AF_UNINITIALIZED;
}
public synchronized void lockAutoFocus(CaptureRequest.Builder repeatingBuilder,
CaptureRequest.Builder requestBuilder) {
if (VERBOSE_LOGGING) Log.v(TAG, "lockAutoFocus");
if (mCurrentAfMode == AF_UNINITIALIZED) {
throw new IllegalStateException("AF mode was not enabled");
}
beginTraceAsync("AFSM_lockAutoFocus");
mCurrentAfTrigger = CaptureRequest.CONTROL_AF_TRIGGER_START;
repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
requestBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
repeatingBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
requestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_START);
}
public synchronized void unlockAutoFocus(CaptureRequest.Builder repeatingBuilder,
CaptureRequest.Builder requestBuilder) {
if (VERBOSE_LOGGING) Log.v(TAG, "unlockAutoFocus");
if (mCurrentAfMode == AF_UNINITIALIZED) {
throw new IllegalStateException("AF mode was not enabled");
}
mCurrentAfTrigger = CaptureRequest.CONTROL_AF_TRIGGER_CANCEL;
repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
requestBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
repeatingBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
requestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
}
public synchronized void setActiveAutoFocus(CaptureRequest.Builder repeatingBuilder,
CaptureRequest.Builder requestBuilder) {
if (VERBOSE_LOGGING) Log.v(TAG, "setActiveAutoFocus");
beginTraceAsync("AFSM_setActiveAutoFocus");
mCurrentAfMode = CaptureRequest.CONTROL_AF_MODE_AUTO;
repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
requestBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
repeatingBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_IDLE);
requestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CaptureRequest.CONTROL_AF_TRIGGER_START);
}
public synchronized void setPassiveAutoFocus(boolean picture,
CaptureRequest.Builder repeatingBuilder) {
if (VERBOSE_LOGGING) Log.v(TAG, "setPassiveAutoFocus - picture " + picture);
if (picture) {
mCurrentAfMode = CaptureResult.CONTROL_AF_MODE_CONTINUOUS_PICTURE;
} else {
mCurrentAfMode = CaptureResult.CONTROL_AF_MODE_CONTINUOUS_VIDEO;
}
repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
}
private synchronized void beginTraceAsync(String sectionName) {
if (mCurrentAfCookie != AF_UNINITIALIZED) {
SysTrace.endSectionAsync(mCurrentAfTrace, mCurrentAfCookie);
}
mLastAfCookie++;
mCurrentAfCookie = mLastAfCookie;
mCurrentAfTrace = sectionName;
SysTrace.beginSectionAsync(sectionName, mCurrentAfCookie);
}
private synchronized void endTraceAsync() {
if (mCurrentAfCookie == AF_UNINITIALIZED) {
Log.w(TAG, "endTraceAsync - no current trace active");
return;
}
SysTrace.endSectionAsync(mCurrentAfTrace, mCurrentAfCookie);
mCurrentAfCookie = AF_UNINITIALIZED;
}
public synchronized void updateCaptureRequest(CaptureRequest.Builder repeatingBuilder) {
if (repeatingBuilder == null) {
throw new IllegalArgumentException("repeatingBuilder shouldn't be null");
}
if (mCurrentAfMode == AF_UNINITIALIZED) {
throw new IllegalStateException("AF mode was not enabled");
}
repeatingBuilder.set(CaptureRequest.CONTROL_AF_MODE, mCurrentAfMode);
}
}