From 5c2e2b83b41327997ebb0502ef13710cd840a167 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 21 Sep 2017 20:40:36 +0200 Subject: [PATCH] improve the handling of third party intents --- .../com/simplemobiletools/camera/Preview.kt | 34 ++++++++++++++----- .../camera/activities/MainActivity.kt | 22 +++++++----- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt index 3261c867..0463f102 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt @@ -39,7 +39,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan private var mPreviewSize: Camera.Size? = null private var mParameters: Camera.Parameters? = null private var mRecorder: MediaRecorder? = null - private var mTargetUri: Uri? = null private var mScaleGestureDetector: ScaleGestureDetector? = null private var mZoomRatios: List? = null @@ -62,6 +61,8 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan private var autoFocusHandler = Handler() var isWaitingForTakePictureCallback = false + var mTargetUri: Uri? = null + var isImageCaptureIntent = false constructor(context: Context) : super(context) @@ -94,7 +95,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan if (mIsPreviewShown) { resumePreview() } else { - if (!mWasZooming) + if (!mWasZooming && !mIsPreviewShown) focusArea(false) mWasZooming = false @@ -214,10 +215,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan return null } - fun setTargetUri(uri: Uri) { - mTargetUri = uri - } - private fun initGestureDetector() { mScaleGestureDetector = ScaleGestureDetector(mActivity, object : ScaleGestureDetector.SimpleOnScaleGestureListener() { override fun onScale(detector: ScaleGestureDetector): Boolean { @@ -285,6 +282,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan mRotationAtCapture = MainActivity.mLastHandledOrientation mCamera!!.parameters = mParameters isWaitingForTakePictureCallback = true + mIsPreviewShown = true mCamera!!.takePicture(null, null, takePictureCallback) if (config.isSoundEnabled) { @@ -302,19 +300,37 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan private val takePictureCallback = Camera.PictureCallback { data, cam -> isWaitingForTakePictureCallback = false + if (!isImageCaptureIntent) { + handlePreview() + } + + if (isImageCaptureIntent) { + if (mTargetUri != null) { + storePhoto(data) + } else { + mActivity.finishActivity() + } + } else { + storePhoto(data) + } + } + + private fun storePhoto(data: ByteArray) { + PhotoProcessor(mActivity, mTargetUri, mCurrCameraId, mRotationAtCapture).execute(data) + } + + private fun handlePreview() { if (config.isShowPreviewEnabled) { - mIsPreviewShown = true if (!config.wasPhotoPreviewHintShown) { mActivity.toast(R.string.click_to_resume_preview) config.wasPhotoPreviewHintShown = true } } else { Handler().postDelayed({ + mIsPreviewShown = false resumePreview() }, PHOTO_PREVIEW_LENGTH) } - - PhotoProcessor(mActivity, mTargetUri, mCurrCameraId, mRotationAtCapture).execute(data) } private fun resumePreview() { diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt index f8b13db7..14f6aba1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt @@ -48,7 +48,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave private var mIsInPhotoMode = false private var mIsAskingPermissions = false private var mIsCameraAvailable = false - private var mIsImageCaptureIntent = false private var mIsVideoCaptureIntent = false private var mIsHardwareShutterHandled = false private var mCurrVideoRecTimer = 0 @@ -75,7 +74,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave mIsInPhotoMode = false mIsAskingPermissions = false mIsCameraAvailable = false - mIsImageCaptureIntent = false mIsVideoCaptureIntent = false mIsHardwareShutterHandled = false mCurrVideoRecTimer = 0 @@ -125,20 +123,22 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave } private fun handleIntent() { - if (intent?.action == MediaStore.ACTION_IMAGE_CAPTURE || intent?.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE) { - mIsImageCaptureIntent = true + if (isImageCaptureIntent()) { hideToggleModeAbout() - val output = intent.extras.get(MediaStore.EXTRA_OUTPUT) + val output = intent.extras?.get(MediaStore.EXTRA_OUTPUT) if (output != null && output is Uri) { - mPreview?.setTargetUri(output) + mPreview?.mTargetUri = output } } else if (intent?.action == MediaStore.ACTION_VIDEO_CAPTURE) { mIsVideoCaptureIntent = true hideToggleModeAbout() shutter.setImageDrawable(mRes.getDrawable(R.drawable.ic_video_rec)) } + mPreview?.isImageCaptureIntent = isImageCaptureIntent() } + private fun isImageCaptureIntent() = intent?.action == MediaStore.ACTION_IMAGE_CAPTURE || intent?.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE + private fun initializeCamera() { setContentView(R.layout.activity_main) initButtons() @@ -567,6 +567,11 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave return mIsCameraAvailable } + fun finishActivity() { + setResult(Activity.RESULT_OK) + finish() + } + override fun setFlashAvailable(available: Boolean) { if (available) { toggle_flash.beVisible() @@ -599,9 +604,8 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave setupPreviewImage(mIsInPhotoMode) } - if (mIsImageCaptureIntent) { - setResult(Activity.RESULT_OK) - finish() + if (isImageCaptureIntent()) { + finishActivity() } }