do not store mCurrCameraId in the activity

This commit is contained in:
tibbi 2018-05-28 11:29:25 +02:00
parent bf82752748
commit b8db893c1c
2 changed files with 36 additions and 29 deletions

View File

@ -44,7 +44,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private var mIsVideoCaptureIntent = false private var mIsVideoCaptureIntent = false
private var mIsHardwareShutterHandled = false private var mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0 private var mCurrVideoRecTimer = 0
private var mCurrCameraId = 0
var mLastHandledOrientation = 0 var mLastHandledOrientation = 0
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -115,7 +114,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mIsVideoCaptureIntent = false mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false mIsHardwareShutterHandled = false
mCurrVideoRecTimer = 0 mCurrVideoRecTimer = 0
mCurrCameraId = 0
mLastHandledOrientation = 0 mLastHandledOrientation = 0
mCameraImpl = getMyCamera() mCameraImpl = getMyCamera()
@ -191,10 +189,9 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
(btn_holder.layoutParams as RelativeLayout.LayoutParams).setMargins(0, 0, 0, (navBarHeight + resources.getDimension(R.dimen.activity_margin)).toInt()) (btn_holder.layoutParams as RelativeLayout.LayoutParams).setMargins(0, 0, 0, (navBarHeight + resources.getDimension(R.dimen.activity_margin)).toInt())
mCurrCameraId = config.lastUsedCamera
mPreview = PreviewCameraOne(this, camera_surface_view, this) mPreview = PreviewCameraOne(this, camera_surface_view, this)
view_holder.addView(mPreview) view_holder.addView(mPreview)
toggle_camera.setImageResource(if (mCurrCameraId == mCameraImpl.getBackCameraId()) R.drawable.ic_camera_front else R.drawable.ic_camera_rear) toggle_camera.setImageResource(if (config.lastUsedCamera == mCameraImpl.getBackCameraId()) R.drawable.ic_camera_front else R.drawable.ic_camera_rear)
mFocusCircleView = FocusCircleView(applicationContext) mFocusCircleView = FocusCircleView(applicationContext)
view_holder.addView(mFocusCircleView) view_holder.addView(mFocusCircleView)
@ -221,25 +218,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
return return
} }
mCurrCameraId = if (mCurrCameraId == mCameraImpl.getBackCameraId()) { mPreview!!.toggleCamera()
mCameraImpl.getFrontCameraId()
} else {
mCameraImpl.getBackCameraId()
}
config.lastUsedCamera = mCurrCameraId
var newIconId = R.drawable.ic_camera_front
mPreview?.releaseCamera()
if (mPreview?.setCamera(mCurrCameraId) == true) {
if (mCurrCameraId == mCameraImpl.getFrontCameraId()) {
newIconId = R.drawable.ic_camera_rear
}
toggle_camera.setImageResource(newIconId)
mPreview?.setFlashlightState(FLASH_OFF)
hideTimer()
} else {
toast(R.string.camera_switch_error)
}
} }
private fun showLastMediaPreview() { private fun showLastMediaPreview() {
@ -267,6 +246,10 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
toggle_flash.setImageResource(flashDrawable) toggle_flash.setImageResource(flashDrawable)
} }
fun updateCameraIcon(showFront: Boolean) {
toggle_camera.setImageResource(if (showFront) R.drawable.ic_camera_front else R.drawable.ic_camera_rear)
}
private fun shutterPressed() { private fun shutterPressed() {
if (checkCameraAvailable()) { if (checkCameraAvailable()) {
handleShutter() handleShutter()
@ -428,7 +411,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
} }
private fun hideTimer() { fun hideTimer() {
video_rec_curr_timer.text = 0.getFormattedDuration() video_rec_curr_timer.text = 0.getFormattedDuration()
video_rec_curr_timer.beGone() video_rec_curr_timer.beGone()
mCurrVideoRecTimer = 0 mCurrVideoRecTimer = 0
@ -451,7 +434,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private fun resumeCameraItems() { private fun resumeCameraItems() {
showToggleCameraIfNeeded() showToggleCameraIfNeeded()
if (mPreview?.setCamera(mCurrCameraId) == true) { if (mPreview?.setCamera() == true) {
hideNavigationBarIcons() hideNavigationBarIcons()
mPreview?.checkFlashlight() mPreview?.checkFlashlight()

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.camera.views package com.simplemobiletools.camera.views
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.annotation.TargetApi
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.graphics.Point import android.graphics.Point
@ -20,7 +21,9 @@ import com.simplemobiletools.camera.activities.MainActivity
import com.simplemobiletools.camera.dialogs.ChangeResolutionDialog import com.simplemobiletools.camera.dialogs.ChangeResolutionDialog
import com.simplemobiletools.camera.extensions.* import com.simplemobiletools.camera.extensions.*
import com.simplemobiletools.camera.helpers.* import com.simplemobiletools.camera.helpers.*
import com.simplemobiletools.camera.implementations.MyCameraOneImpl
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.isJellyBean1Plus
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.* import java.util.*
@ -43,6 +46,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private var mScaleGestureDetector: ScaleGestureDetector? = null private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mZoomRatios = ArrayList<Int>() private var mZoomRatios = ArrayList<Int>()
private var mFlashlightState = FLASH_OFF private var mFlashlightState = FLASH_OFF
private var mCameraImpl: MyCameraOneImpl? = null
private var mCurrVideoPath = "" private var mCurrVideoPath = ""
private var mCanTakePicture = false private var mCanTakePicture = false
@ -62,8 +66,8 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private var mRotationAtCapture = 0 private var mRotationAtCapture = 0
private var mIsFocusingBeforeCapture = false private var mIsFocusingBeforeCapture = false
private var autoFocusHandler = Handler() private var autoFocusHandler = Handler()
private var mActivity: MainActivity? = null
var mActivity: MainActivity? = null
var isWaitingForTakePictureCallback = false var isWaitingForTakePictureCallback = false
var mTargetUri: Uri? = null var mTargetUri: Uri? = null
var isImageCaptureIntent = false var isImageCaptureIntent = false
@ -78,6 +82,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mSurfaceHolder = mSurfaceView.holder mSurfaceHolder = mSurfaceView.holder
mSurfaceHolder.addCallback(this) mSurfaceHolder.addCallback(this)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
mCameraImpl = MyCameraOneImpl(activity.applicationContext)
mCanTakePicture = false mCanTakePicture = false
mIsInVideoMode = false mIsInVideoMode = false
mIsSurfaceCreated = false mIsSurfaceCreated = false
@ -118,7 +123,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
} }
} }
fun setCamera(cameraId: Int): Boolean { fun setCamera(cameraId: Int = mCurrCameraId): Boolean {
mCurrCameraId = cameraId mCurrCameraId = cameraId
val newCamera: Camera val newCamera: Camera
try { try {
@ -187,6 +192,24 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
return true return true
} }
fun toggleCamera() {
mCurrCameraId = if (mCurrCameraId == mCameraImpl!!.getBackCameraId()) {
mCameraImpl!!.getFrontCameraId()
} else {
mCameraImpl!!.getBackCameraId()
}
mConfig.lastUsedCamera = mCurrCameraId
releaseCamera()
if (setCamera(mCurrCameraId)) {
setFlashlightState(FLASH_OFF)
mActivity?.updateCameraIcon(mCurrCameraId == mCameraImpl!!.getFrontCameraId())
mActivity?.hideTimer()
} else {
mActivity?.toast(R.string.camera_switch_error)
}
}
private fun refreshPreview() { private fun refreshPreview() {
mIsSixteenToNine = getSelectedResolution().isSixteenToNine() mIsSixteenToNine = getSelectedResolution().isSixteenToNine()
mSetupPreviewAfterMeasure = true mSetupPreviewAfterMeasure = true
@ -290,6 +313,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
} }
} }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private fun takePicture() { private fun takePicture() {
if (mCanTakePicture) { if (mCanTakePicture) {
val selectedResolution = getSelectedResolution() val selectedResolution = getSelectedResolution()
@ -299,7 +323,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mActivity!!.toast(R.string.setting_resolution_failed) mActivity!!.toast(R.string.setting_resolution_failed)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (isJellyBean1Plus()) {
mCamera!!.enableShutterSound(false) mCamera!!.enableShutterSound(false)
} }