// CameraActivity.java
package com.aspirs.focal;
import android.hardware.Camera;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MotionEvent;
import android.os.Handler;
import com.aspirs.focal.ui.ShutterButton;
import com.aspirs.focal.CameraManager;
import com.aspirs.focal.SnapShotManager;
public class CameraActivity extends Activity implements CameraManager.CameraReadyListener {
private boolean mPaused;
private boolean mUserWantsExposureRing;
private boolean mIsShutterLongClicked = false;
private boolean mIsFocusing = false;
public final static int CAMERA_MODE_PHOTO = 1;
private static int mCameraMode = CAMERA_MODE_PHOTO;
private CameraManager mCamManager;
private SnapshotManager mSnapshotManager;
private Handler mHandler;
private CameraPreviewListener mCamPreviewListener;
private ShutterButton mShutterButton;
private MainSnapshotListener mSnapshotListener;
/**
* Event: Activity created
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mPaused = false;
getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
// Setup the camera hardware and preview
setupCamera();
// Setup shutter button
mShutterButton = (ShutterButton) findViewById(R.id.btn_shutter);
MainShutterClickListener shutterClickListener = new MainShutterClickListener();
mShutterButton.setOnClickListener(shutterClickListener);
mShutterButton.setOnTouchListener(shutterClickListener);
mShutterButton.setOnLongClickListener(shutterClickListener);
}
@Override
public void onCameraReady() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Profiler.getDefault().start("OnCameraReady");
Camera.Parameters params = mCamManager.getParameters();
if (params == null) {
// Are we too fast? Let's try again.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
onCameraReady();
}
}, 20);
return;
}
Camera.Size picSize = params.getPictureSize();
Camera.Size sz = Util.getOptimalPreviewSize(CameraActivity.this, params.getSupportedPreviewSizes(),
((float) picSize.width / (float) picSize.height));
if (sz == null) {
Log.e(TAG, "No preview size!! Something terribly wrong with camera!");
return;
}
if (mSnapshotManager == null) {
mSnapshotManager = new SnapshotManager(mCamManager, CameraActivity.this);
mSnapshotListener = new MainSnapshotListener();
mSnapshotManager.addListener(mSnapshotListener);
}
}
});
}
protected void setupCamera() {
// Setup the Camera hardware and preview
mCamManager = new CameraManager(this);
((CameraApplication)getApplication()).setCameraManager(mCamManager);
setGLRenderer(mCamManager.getRenderer());
mCamPreviewListener = new CameraPreviewListener();
mCamManager.setPreviewPauseListener(mCamPreviewListener);
mCamManager.setCameraReadyListener(this);
mCamManager.open(Camera.CameraInfo.CAMERA_FACING_BACK); // the initial facing is back.
}
/**
* Listener that is called when the preview pauses or resumes
*/
private class CameraPreviewListener implements CameraManager.PreviewPauseListener {
@Override
public void onPreviewPause() {
// XXX: Do a little animation
}
@Override
public void onPreviewResume() {
// XXX: Do a little animation
}
}
/**
* When the shutter button is pressed
*/
public class MainShutterClickListener implements OnClickListener, View.OnLongClickListener, View.OnTouchListener {
@Override
public void onClick(View v) {
if (mSnapshotManager == null) return;
mSnapshotManager.queueSnapshot(true, 0);
}
@Override
public boolean onLongClick(View view) {
mIsShutterLongClicked = true;
return true;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// If we long-press the shutter button and no capture transformer handles it, we
// will just have nothing happening. We register the long click event in here, and
// trigger a snapshot once it's released.
if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP && mIsShutterLongClicked) {
mIsShutterLongClicked = false;
onClick(view);
}
return view.onTouchEvent(motionEvent);
}
}
/**
* Snapshot listener for when snapshots are taken, in SnapshotManager
*/
private class MainSnapshotListener implements SnapshotManager.SnapshotListener {
@Override
public void onSnapshotShutter(final SnapshotManager.SnapshotInfo info) {
// Unlock camera auto settings
mCamManager.setLockSetup(false);
mCamManager.setStabilization(false);
}
}
/**
* Focus listener to animate the focus HUD ring from FocusManager events
*/
private class MainFocusListener implements FocusManager.FocusListener {
@Override
public void onFocusStart(final boolean smallAdjust) {
mIsFocusing = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
mFocusHudRing.animateWorking(smallAdjust ? 200 : 1500);
}
});
}
@Override
public void onFocusReturns(final boolean smallAdjust, final boolean success) {
mIsFocusing = false;
runOnUiThread(new Runnable() {
@Override
public void run() {
mFocusHudRing.animatePressUp();
if (!smallAdjust) {
mFocusHudRing.setFocusImage(success);
} else {
mFocusHudRing.setFocusImage(true);
}
}
});
}
}
}