improve the handling of third party intents

This commit is contained in:
tibbi 2017-09-21 20:40:36 +02:00
parent 6d9fbc4504
commit 5c2e2b83b4
2 changed files with 38 additions and 18 deletions

View File

@ -39,7 +39,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private var mPreviewSize: Camera.Size? = null private var mPreviewSize: Camera.Size? = null
private var mParameters: Camera.Parameters? = null private var mParameters: Camera.Parameters? = null
private var mRecorder: MediaRecorder? = null private var mRecorder: MediaRecorder? = null
private var mTargetUri: Uri? = null
private var mScaleGestureDetector: ScaleGestureDetector? = null private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mZoomRatios: List<Int>? = null private var mZoomRatios: List<Int>? = null
@ -62,6 +61,8 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private var autoFocusHandler = Handler() private var autoFocusHandler = Handler()
var isWaitingForTakePictureCallback = false var isWaitingForTakePictureCallback = false
var mTargetUri: Uri? = null
var isImageCaptureIntent = false
constructor(context: Context) : super(context) constructor(context: Context) : super(context)
@ -94,7 +95,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (mIsPreviewShown) { if (mIsPreviewShown) {
resumePreview() resumePreview()
} else { } else {
if (!mWasZooming) if (!mWasZooming && !mIsPreviewShown)
focusArea(false) focusArea(false)
mWasZooming = false mWasZooming = false
@ -214,10 +215,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
return null return null
} }
fun setTargetUri(uri: Uri) {
mTargetUri = uri
}
private fun initGestureDetector() { private fun initGestureDetector() {
mScaleGestureDetector = ScaleGestureDetector(mActivity, object : ScaleGestureDetector.SimpleOnScaleGestureListener() { mScaleGestureDetector = ScaleGestureDetector(mActivity, object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean { override fun onScale(detector: ScaleGestureDetector): Boolean {
@ -285,6 +282,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mRotationAtCapture = MainActivity.mLastHandledOrientation mRotationAtCapture = MainActivity.mLastHandledOrientation
mCamera!!.parameters = mParameters mCamera!!.parameters = mParameters
isWaitingForTakePictureCallback = true isWaitingForTakePictureCallback = true
mIsPreviewShown = true
mCamera!!.takePicture(null, null, takePictureCallback) mCamera!!.takePicture(null, null, takePictureCallback)
if (config.isSoundEnabled) { if (config.isSoundEnabled) {
@ -302,19 +300,37 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private val takePictureCallback = Camera.PictureCallback { data, cam -> private val takePictureCallback = Camera.PictureCallback { data, cam ->
isWaitingForTakePictureCallback = false 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) { if (config.isShowPreviewEnabled) {
mIsPreviewShown = true
if (!config.wasPhotoPreviewHintShown) { if (!config.wasPhotoPreviewHintShown) {
mActivity.toast(R.string.click_to_resume_preview) mActivity.toast(R.string.click_to_resume_preview)
config.wasPhotoPreviewHintShown = true config.wasPhotoPreviewHintShown = true
} }
} else { } else {
Handler().postDelayed({ Handler().postDelayed({
mIsPreviewShown = false
resumePreview() resumePreview()
}, PHOTO_PREVIEW_LENGTH) }, PHOTO_PREVIEW_LENGTH)
} }
PhotoProcessor(mActivity, mTargetUri, mCurrCameraId, mRotationAtCapture).execute(data)
} }
private fun resumePreview() { private fun resumePreview() {

View File

@ -48,7 +48,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private var mIsInPhotoMode = false private var mIsInPhotoMode = false
private var mIsAskingPermissions = false private var mIsAskingPermissions = false
private var mIsCameraAvailable = false private var mIsCameraAvailable = false
private var mIsImageCaptureIntent = false
private var mIsVideoCaptureIntent = false private var mIsVideoCaptureIntent = false
private var mIsHardwareShutterHandled = false private var mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0 private var mCurrVideoRecTimer = 0
@ -75,7 +74,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mIsInPhotoMode = false mIsInPhotoMode = false
mIsAskingPermissions = false mIsAskingPermissions = false
mIsCameraAvailable = false mIsCameraAvailable = false
mIsImageCaptureIntent = false
mIsVideoCaptureIntent = false mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false mIsHardwareShutterHandled = false
mCurrVideoRecTimer = 0 mCurrVideoRecTimer = 0
@ -125,20 +123,22 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
} }
private fun handleIntent() { private fun handleIntent() {
if (intent?.action == MediaStore.ACTION_IMAGE_CAPTURE || intent?.action == MediaStore.ACTION_IMAGE_CAPTURE_SECURE) { if (isImageCaptureIntent()) {
mIsImageCaptureIntent = true
hideToggleModeAbout() hideToggleModeAbout()
val output = intent.extras.get(MediaStore.EXTRA_OUTPUT) val output = intent.extras?.get(MediaStore.EXTRA_OUTPUT)
if (output != null && output is Uri) { if (output != null && output is Uri) {
mPreview?.setTargetUri(output) mPreview?.mTargetUri = output
} }
} else if (intent?.action == MediaStore.ACTION_VIDEO_CAPTURE) { } else if (intent?.action == MediaStore.ACTION_VIDEO_CAPTURE) {
mIsVideoCaptureIntent = true mIsVideoCaptureIntent = true
hideToggleModeAbout() hideToggleModeAbout()
shutter.setImageDrawable(mRes.getDrawable(R.drawable.ic_video_rec)) 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() { private fun initializeCamera() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
initButtons() initButtons()
@ -567,6 +567,11 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
return mIsCameraAvailable return mIsCameraAvailable
} }
fun finishActivity() {
setResult(Activity.RESULT_OK)
finish()
}
override fun setFlashAvailable(available: Boolean) { override fun setFlashAvailable(available: Boolean) {
if (available) { if (available) {
toggle_flash.beVisible() toggle_flash.beVisible()
@ -599,9 +604,8 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
setupPreviewImage(mIsInPhotoMode) setupPreviewImage(mIsInPhotoMode)
} }
if (mIsImageCaptureIntent) { if (isImageCaptureIntent()) {
setResult(Activity.RESULT_OK) finishActivity()
finish()
} }
} }