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 mIsHardwareShutterHandled = false
private var mCurrVideoRecTimer = 0
private var mCurrCameraId = 0
var mLastHandledOrientation = 0
override fun onCreate(savedInstanceState: Bundle?) {
@ -115,7 +114,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mIsVideoCaptureIntent = false
mIsHardwareShutterHandled = false
mCurrVideoRecTimer = 0
mCurrCameraId = 0
mLastHandledOrientation = 0
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())
mCurrCameraId = config.lastUsedCamera
mPreview = PreviewCameraOne(this, camera_surface_view, this)
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)
view_holder.addView(mFocusCircleView)
@ -221,25 +218,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
return
}
mCurrCameraId = if (mCurrCameraId == mCameraImpl.getBackCameraId()) {
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)
}
mPreview!!.toggleCamera()
}
private fun showLastMediaPreview() {
@ -259,7 +238,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
fun updateFlashlightState(state: Int) {
config.flashlightState = state
val flashDrawable = when (state) {
val flashDrawable = when (state) {
FLASH_OFF -> R.drawable.ic_flash_off
FLASH_ON -> R.drawable.ic_flash_on
else -> R.drawable.ic_flash_auto
@ -267,6 +246,10 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
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() {
if (checkCameraAvailable()) {
handleShutter()
@ -428,7 +411,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
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.beGone()
mCurrVideoRecTimer = 0
@ -451,7 +434,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private fun resumeCameraItems() {
showToggleCameraIfNeeded()
if (mPreview?.setCamera(mCurrCameraId) == true) {
if (mPreview?.setCamera() == true) {
hideNavigationBarIcons()
mPreview?.checkFlashlight()

View File

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