update flashlight handling

This commit is contained in:
tibbi 2018-05-28 10:54:10 +02:00
parent 95d0ca1fd0
commit bf82752748
2 changed files with 56 additions and 55 deletions

View File

@ -39,7 +39,6 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
private var mPreview: PreviewCameraOne? = null private var mPreview: PreviewCameraOne? = null
private var mPreviewUri: Uri? = null private var mPreviewUri: Uri? = null
private var mFlashlightState = FLASH_OFF
private var mIsInPhotoMode = false private var mIsInPhotoMode = false
private var mIsCameraAvailable = false private var mIsCameraAvailable = false
private var mIsVideoCaptureIntent = false private var mIsVideoCaptureIntent = false
@ -203,7 +202,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mIsInPhotoMode = true mIsInPhotoMode = true
mTimerHandler = Handler() mTimerHandler = Handler()
mFadeHandler = Handler() mFadeHandler = Handler()
mFlashlightState = if (config.turnFlashOffAtStartup) FLASH_OFF else config.flashlightState mPreview!!.setFlashlightState(if (config.turnFlashOffAtStartup) FLASH_OFF else config.flashlightState)
setupPreviewImage(true) setupPreviewImage(true)
} }
@ -236,7 +235,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
newIconId = R.drawable.ic_camera_rear newIconId = R.drawable.ic_camera_rear
} }
toggle_camera.setImageResource(newIconId) toggle_camera.setImageResource(newIconId)
disableFlash() mPreview?.setFlashlightState(FLASH_OFF)
hideTimer() hideTimer()
} else { } else {
toast(R.string.camera_switch_error) toast(R.string.camera_switch_error)
@ -255,37 +254,17 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
return return
} }
mFlashlightState = ++mFlashlightState % if (mIsInPhotoMode) 3 else 2 mPreview?.toggleFlashlight()
checkFlash()
} }
private fun checkFlash() { fun updateFlashlightState(state: Int) {
when (mFlashlightState) { config.flashlightState = state
FLASH_ON -> enableFlash() val flashDrawable = when (state) {
FLASH_AUTO -> autoFlash() FLASH_OFF -> R.drawable.ic_flash_off
else -> disableFlash() FLASH_ON -> R.drawable.ic_flash_on
else -> R.drawable.ic_flash_auto
} }
} toggle_flash.setImageResource(flashDrawable)
private fun disableFlash() {
mPreview?.disableFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_off)
mFlashlightState = FLASH_OFF
config.flashlightState = FLASH_OFF
}
private fun enableFlash() {
mPreview?.enableFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_on)
mFlashlightState = FLASH_ON
config.flashlightState = FLASH_ON
}
private fun autoFlash() {
mPreview?.setAutoFlash()
toggle_flash.setImageResource(R.drawable.ic_flash_auto)
mFlashlightState = FLASH_AUTO
config.flashlightState = FLASH_AUTO
} }
private fun shutterPressed() { private fun shutterPressed() {
@ -352,7 +331,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
if (mIsVideoCaptureIntent) if (mIsVideoCaptureIntent)
mPreview?.trySwitchToVideo() mPreview?.trySwitchToVideo()
disableFlash() mPreview?.setFlashlightState(FLASH_OFF)
hideTimer() hideTimer()
mIsInPhotoMode = !mIsInPhotoMode mIsInPhotoMode = !mIsInPhotoMode
showToggleCameraIfNeeded() showToggleCameraIfNeeded()
@ -389,8 +368,8 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
toggle_photo_video.setImageDrawable(resources.getDrawable(R.drawable.ic_camera)) toggle_photo_video.setImageDrawable(resources.getDrawable(R.drawable.ic_camera))
showToggleCameraIfNeeded() showToggleCameraIfNeeded()
shutter.setImageDrawable(resources.getDrawable(R.drawable.ic_video_rec)) shutter.setImageDrawable(resources.getDrawable(R.drawable.ic_video_rec))
checkFlash()
setupPreviewImage(false) setupPreviewImage(false)
mPreview?.checkFlashlight()
} }
private fun setupPreviewImage(isPhoto: Boolean) { private fun setupPreviewImage(isPhoto: Boolean) {
@ -474,7 +453,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
showToggleCameraIfNeeded() showToggleCameraIfNeeded()
if (mPreview?.setCamera(mCurrCameraId) == true) { if (mPreview?.setCamera(mCurrCameraId) == true) {
hideNavigationBarIcons() hideNavigationBarIcons()
checkFlash() mPreview?.checkFlashlight()
if (!mIsInPhotoMode) { if (!mIsInPhotoMode) {
initVideoButtons() initVideoButtons()
@ -540,7 +519,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
toggle_flash.beVisible() toggle_flash.beVisible()
} else { } else {
toggle_flash.beInvisible() toggle_flash.beInvisible()
disableFlash() mPreview?.setFlashlightState(FLASH_OFF)
} }
} }

View File

@ -19,8 +19,7 @@ import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.activities.MainActivity 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.Config import com.simplemobiletools.camera.helpers.*
import com.simplemobiletools.camera.helpers.PhotoProcessor
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -43,11 +42,12 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private var mRecorder: MediaRecorder? = null private var mRecorder: MediaRecorder? = null
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 mCurrVideoPath = "" private var mCurrVideoPath = ""
private var mCanTakePicture = false private var mCanTakePicture = false
private var mIsRecording = false private var mIsRecording = false
private var mIsVideoMode = false private var mIsInVideoMode = false
private var mIsSurfaceCreated = false private var mIsSurfaceCreated = false
private var mSwitchToVideoAsap = false private var mSwitchToVideoAsap = false
private var mSetupPreviewAfterMeasure = false private var mSetupPreviewAfterMeasure = false
@ -79,7 +79,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mSurfaceHolder.addCallback(this) mSurfaceHolder.addCallback(this)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
mCanTakePicture = false mCanTakePicture = false
mIsVideoMode = false mIsInVideoMode = false
mIsSurfaceCreated = false mIsSurfaceCreated = false
mSetupPreviewAfterMeasure = false mSetupPreviewAfterMeasure = false
mCurrVideoPath = "" mCurrVideoPath = ""
@ -136,7 +136,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
releaseCamera() releaseCamera()
mCamera = newCamera mCamera = newCamera
if (initCamera() && mIsVideoMode) { if (initCamera() && mIsInVideoMode) {
initRecorder() initRecorder()
} }
@ -201,7 +201,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
} }
var index = getResolutionIndex() var index = getResolutionIndex()
val resolutions = if (mIsVideoMode) { val resolutions = if (mIsInVideoMode) {
mParameters!!.supportedVideoSizes ?: mParameters!!.supportedPreviewSizes mParameters!!.supportedVideoSizes ?: mParameters!!.supportedPreviewSizes
} else { } else {
mParameters!!.supportedPictureSizes mParameters!!.supportedPictureSizes
@ -216,7 +216,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private fun getResolutionIndex(): Int { private fun getResolutionIndex(): Int {
val isBackCamera = mConfig.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK val isBackCamera = mConfig.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
return if (mIsVideoMode) { return if (mIsInVideoMode) {
if (isBackCamera) mConfig.backVideoResIndex else mConfig.frontVideoResIndex if (isBackCamera) mConfig.backVideoResIndex else mConfig.frontVideoResIndex
} else { } else {
if (isBackCamera) mConfig.backPhotoResIndex else mConfig.frontPhotoResIndex if (isBackCamera) mConfig.backPhotoResIndex else mConfig.frontPhotoResIndex
@ -454,7 +454,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private fun rescheduleAutofocus() { private fun rescheduleAutofocus() {
autoFocusHandler.removeCallbacksAndMessages(null) autoFocusHandler.removeCallbacksAndMessages(null)
autoFocusHandler.postDelayed({ autoFocusHandler.postDelayed({
if (!mIsVideoMode || !mIsRecording) { if (!mIsInVideoMode || !mIsRecording) {
focusArea(false, false) focusArea(false, false)
} }
}, REFOCUS_PERIOD) }, REFOCUS_PERIOD)
@ -495,7 +495,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) { override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
mIsSurfaceCreated = true mIsSurfaceCreated = true
if (mIsVideoMode) { if (mIsInVideoMode) {
initRecorder() initRecorder()
} }
} }
@ -561,7 +561,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
if (mSupportedPreviewSizes != null) { if (mSupportedPreviewSizes != null) {
// for simplicity lets assume that most displays are 16:9 and the remaining ones are 4:3 // for simplicity lets assume that most displays are 16:9 and the remaining ones are 4:3
// always set 16:9 for videos as many devices support 4:3 only in low quality // always set 16:9 for videos as many devices support 4:3 only in low quality
mPreviewSize = if (mIsSixteenToNine || mIsVideoMode) { mPreviewSize = if (mIsSixteenToNine || mIsInVideoMode) {
getOptimalPreviewSize(mSupportedPreviewSizes!!, mScreenSize.y, mScreenSize.x) getOptimalPreviewSize(mSupportedPreviewSizes!!, mScreenSize.y, mScreenSize.x)
} else { } else {
val newRatioHeight = (mScreenSize.x * (4.toDouble() / 3)).toInt() val newRatioHeight = (mScreenSize.x * (4.toDouble() / 3)).toInt()
@ -574,7 +574,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
if (mScreenSize.x > mPreviewSize!!.height) { if (mScreenSize.x > mPreviewSize!!.height) {
val ratio = mScreenSize.x.toFloat() / mPreviewSize!!.height val ratio = mScreenSize.x.toFloat() / mPreviewSize!!.height
lp.width = (mPreviewSize!!.height * ratio).toInt() lp.width = (mPreviewSize!!.height * ratio).toInt()
if (mIsSixteenToNine || mIsVideoMode) { if (mIsSixteenToNine || mIsInVideoMode) {
lp.height = mScreenSize.y lp.height = mScreenSize.y
} else { } else {
lp.height = (mPreviewSize!!.width * ratio).toInt() lp.height = (mPreviewSize!!.width * ratio).toInt()
@ -594,18 +594,40 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
} }
} }
fun enableFlash() { fun setFlashlightState(state: Int) {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH mFlashlightState = state
checkFlashlight()
}
fun toggleFlashlight() {
val newState = ++mFlashlightState % if (!mIsInVideoMode) 3 else 2
setFlashlightState(newState)
}
fun checkFlashlight() {
when (mFlashlightState) {
FLASH_OFF -> disableFlash()
FLASH_ON -> enableFlash()
FLASH_AUTO -> setAutoFlash()
}
mActivity?.updateFlashlightState(mFlashlightState)
}
private fun disableFlash() {
mFlashlightState = FLASH_OFF
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_OFF
updateCameraParameters() updateCameraParameters()
} }
fun disableFlash() { private fun enableFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF mFlashlightState = FLASH_ON
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_TORCH
updateCameraParameters() updateCameraParameters()
} }
fun setAutoFlash() { private fun setAutoFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF mFlashlightState = FLASH_AUTO
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_OFF
updateCameraParameters() updateCameraParameters()
Handler().postDelayed({ Handler().postDelayed({
@ -619,7 +641,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
stopRecording() stopRecording()
cleanupRecorder() cleanupRecorder()
mIsRecording = false mIsRecording = false
mIsVideoMode = false mIsInVideoMode = false
refreshPreview() refreshPreview()
} }
@ -633,7 +655,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mSwitchToVideoAsap = false mSwitchToVideoAsap = false
mIsRecording = false mIsRecording = false
mIsVideoMode = true mIsInVideoMode = true
mRecorder = MediaRecorder().apply { mRecorder = MediaRecorder().apply {
setCamera(mCamera) setCamera(mCamera)
setVideoSource(MediaRecorder.VideoSource.DEFAULT) setVideoSource(MediaRecorder.VideoSource.DEFAULT)
@ -745,7 +767,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
} }
fun deviceOrientationChanged() { fun deviceOrientationChanged() {
if (mIsVideoMode && !mIsRecording) { if (mIsInVideoMode && !mIsRecording) {
mRecorder = null mRecorder = null
initRecorder() initRecorder()
} }