import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
+import android.content.ComponentName;
import android.content.Intent;
+import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
+import android.os.IBinder;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.LayoutInflater;
// The degrees of the device rotated clockwise from its natural orientation.
private int mLastRawOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
+ private MediaSaveService mMediaSaveService;
+ private ServiceConnection mConnection = new ServiceConnection() {
+ @Override
+ public void onServiceConnected(ComponentName className, IBinder b) {
+ mMediaSaveService = ((MediaSaveService.LocalBinder) b).getService();
+ mCurrentModule.onMediaSaveServiceConnected(mMediaSaveService);
+ }
+ @Override
+ public void onServiceDisconnected(ComponentName className) {
+ mMediaSaveService = null;
+ }};
+
private static final String TAG = "CAM_activity";
private static final int[] DRAW_IDS = {
mCurrentModule.init(this, mFrame, true);
mSwitcher.setCurrentIndex(mCurrentModuleIndex);
mOrientationListener = new MyOrientationEventListener(this);
+ bindMediaSaveService();
}
public void init() {
mSwitcher.setCurrentIndex(mCurrentModuleIndex);
}
+ @Override
+ public void onDestroy() {
+ unbindMediaSaveService();
+ super.onDestroy();
+ }
+
private class MyOrientationEventListener
extends OrientationEventListener {
public MyOrientationEventListener(Context context) {
}
openModule(mCurrentModule, canReuse);
mCurrentModule.onOrientationChanged(mLastRawOrientation);
+ if (mMediaSaveService != null) {
+ mCurrentModule.onMediaSaveServiceConnected(mMediaSaveService);
+ }
getCameraScreenNail().setAlpha(0f);
getCameraScreenNail().setOnFrameDrawnOneShot(mOnFrameDrawn);
}
mCurrentModule.onResumeAfterSuper();
}
+ private void bindMediaSaveService() {
+ Intent intent = new Intent(this, MediaSaveService.class);
+ startService(intent); // start service before binding it so the
+ // service won't be killed if we unbind it.
+ bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
+ }
+
+ private void unbindMediaSaveService() {
+ mMediaSaveService.setListener(null);
+ unbindService(mConnection);
+ }
+
@Override
protected void onFullScreenChanged(boolean full) {
if (full) {
public CameraScreenNail getCameraScreenNail() {
return (CameraScreenNail) mCameraScreenNail;
}
+
+ public MediaSaveService getMediaSaveService() {
+ return mMediaSaveService;
+ }
}
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
-import android.content.ComponentName;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
-import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
-import android.content.ServiceConnection;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.ConditionVariable;
import android.os.Handler;
-import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
// A view group that contains all the small indicators.
private View mOnScreenIndicators;
- // We use MediaSaveService to do the work of saving images in background. This
- // reduces the shot-to-shot time.
- private MediaSaveService mMediaSaveService;
- private ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName className, IBinder b) {
- mMediaSaveService = ((MediaSaveService.LocalBinder) b).getService();
- mMediaSaveService.setListener(PhotoModule.this);
- }
- @Override
- public void onServiceDisconnected(ComponentName className) {
- mMediaSaveService = null;
- }};
// We use a queue to generated names of the images to be used later
// when the image is ready to be saved.
private NamedImages mNamedImages;
mShutterButton = mActivity.getShutterButton();
mShutterButton.setImageResource(R.drawable.btn_new_shutter);
mShutterButton.setOnShutterButtonListener(this);
- if (mMediaSaveService != null) {
- if (mMediaSaveService.isQueueFull()) {
- mShutterButton.enableTouch(false);
- } else {
- mShutterButton.enableTouch(true);
- }
- }
mShutterButton.setVisibility(View.VISIBLE);
+ MediaSaveService s = mActivity.getMediaSaveService();
+ // We set the listener only when both service and shutterbutton
+ // are initialized.
+ if (s != null) {
+ s.setListener(this);
+ }
mNamedImages = new NamedImages();
mPreferences, mContentResolver);
mLocationManager.recordLocation(recordLocation);
+ MediaSaveService s = mActivity.getMediaSaveService();
+ if (s != null) {
+ s.setListener(this);
+ }
mNamedImages = new NamedImages();
initializeZoom();
keepMediaProviderInstance();
Log.e(TAG, "Unbalanced name/data pair");
} else {
if (date == -1) date = mCaptureStartTime;
- mMediaSaveService.addImage(jpegData, title, date, mLocation, width, height,
+ mActivity.getMediaSaveService().addImage(
+ jpegData, title, date, mLocation, width, height,
orientation, mOnMediaSavedListener, mContentResolver);
}
} else {
// If we are already in the middle of taking a snapshot or the image save request
// is full then ignore.
if (mCameraDevice == null || mCameraState == SNAPSHOT_IN_PROGRESS
- || mCameraState == SWITCHING_CAMERA || mMediaSaveService.isQueueFull()) {
+ || mCameraState == SWITCHING_CAMERA
+ || mActivity.getMediaSaveService().isQueueFull()) {
return false;
}
mCaptureStartTime = System.currentTimeMillis();
mCameraStartUpThread.start();
}
- bindMediaSaveService();
// If first time initialization is not finished, put it in the
// message queue.
if (!mFirstTimeInitialized) {
PopupManager.getInstance(mActivity).notifyShowPopup(null);
}
- private void bindMediaSaveService() {
- Intent intent = new Intent(mActivity, MediaSaveService.class);
- mActivity.startService(intent); // start service before binding it so the
- // service won't be killed if we unbind it.
- mActivity.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
- }
-
- private void unbindMediaSaveService() {
- mMediaSaveService.setListener(null);
- mActivity.unbindService(mConnection);
- }
-
void waitCameraStartUpThread() {
try {
if (mCameraStartUpThread != null) {
mSurfaceTexture = null;
}
resetScreenOn();
- unbindMediaSaveService();
// Clear UI.
collapseCameraControls();
mPendingSwitchCameraId = -1;
if (mFocusManager != null) mFocusManager.removeMessages();
+ MediaSaveService s = mActivity.getMediaSaveService();
+ if (s != null) {
+ s.setListener(null);
+ }
}
private void initializeControlByIntent() {
public void onQueueFull() {
if (mShutterButton != null) mShutterButton.enableTouch(false);
}
+
+ @Override
+ public void onMediaSaveServiceConnected(MediaSaveService s) {
+ // We set the listener only when both service and shutterbutton
+ // are initialized.
+ if (mShutterButton != null) s.setListener(this);
+ }
}
private boolean mRestoreFlash; // This is used to check if we need to restore the flash
// status when going back from gallery.
+ private MediaSaveService.OnMediaSavedListener mOnMediaSavedListener =
+ new MediaSaveService.OnMediaSavedListener() {
+ @Override
+ public void onMediaSaved(Uri uri) {
+ if (uri != null) {
+ Util.broadcastNewPicture(mActivity, uri);
+ }
+ }
+ };
+
+
protected class CameraOpenThread extends Thread {
@Override
public void run() {
mShutterButton.setImageResource(R.drawable.btn_new_shutter_video);
mShutterButton.setOnShutterButtonListener(this);
mShutterButton.requestFocus();
+ mShutterButton.enableTouch(true);
// Disable the shutter button if effects are ON since it might take
// a little more time for the effects preview to be ready. We do not
return;
}
- if (mPaused || mSnapshotInProgress || effectsActive()) {
+ MediaSaveService s = mActivity.getMediaSaveService();
+ if (mPaused || mSnapshotInProgress || effectsActive() || s == null || s.isQueueFull()) {
return;
}
String title = Util.createJpegName(dateTaken);
int orientation = Exif.getOrientation(data);
Size s = mParameters.getPictureSize();
- Uri uri = Storage.addImage(mContentResolver, title, dateTaken, loc, orientation, data,
- s.width, s.height);
- if (uri != null) {
- Util.broadcastNewPicture(mActivity, uri);
- }
+ mActivity.getMediaSaveService().addImage(
+ data, title, dateTaken, loc, s.width, s.height,
+ orientation, mOnMediaSavedListener, mContentResolver);
}
private boolean resetEffect() {
mPieRenderer.hide();
}
}
+
+ @Override
+ public void onMediaSaveServiceConnected(MediaSaveService s) {
+ // do nothing.
+ }
}