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 mPreviewUri: Uri? = null
private var mFlashlightState = FLASH_OFF
private var mIsInPhotoMode = false
private var mIsCameraAvailable = false
private var mIsVideoCaptureIntent = false
@ -203,7 +202,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
mIsInPhotoMode = true
mTimerHandler = Handler()
mFadeHandler = Handler()
mFlashlightState = if (config.turnFlashOffAtStartup) FLASH_OFF else config.flashlightState
mPreview!!.setFlashlightState(if (config.turnFlashOffAtStartup) FLASH_OFF else config.flashlightState)
setupPreviewImage(true)
}
@ -236,7 +235,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
newIconId = R.drawable.ic_camera_rear
}
toggle_camera.setImageResource(newIconId)
disableFlash()
mPreview?.setFlashlightState(FLASH_OFF)
hideTimer()
} else {
toast(R.string.camera_switch_error)
@ -255,37 +254,17 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
return
}
mFlashlightState = ++mFlashlightState % if (mIsInPhotoMode) 3 else 2
checkFlash()
mPreview?.toggleFlashlight()
}
private fun checkFlash() {
when (mFlashlightState) {
FLASH_ON -> enableFlash()
FLASH_AUTO -> autoFlash()
else -> disableFlash()
fun updateFlashlightState(state: Int) {
config.flashlightState = 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
}
}
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
toggle_flash.setImageResource(flashDrawable)
}
private fun shutterPressed() {
@ -352,7 +331,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
if (mIsVideoCaptureIntent)
mPreview?.trySwitchToVideo()
disableFlash()
mPreview?.setFlashlightState(FLASH_OFF)
hideTimer()
mIsInPhotoMode = !mIsInPhotoMode
showToggleCameraIfNeeded()
@ -389,8 +368,8 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
toggle_photo_video.setImageDrawable(resources.getDrawable(R.drawable.ic_camera))
showToggleCameraIfNeeded()
shutter.setImageDrawable(resources.getDrawable(R.drawable.ic_video_rec))
checkFlash()
setupPreviewImage(false)
mPreview?.checkFlashlight()
}
private fun setupPreviewImage(isPhoto: Boolean) {
@ -474,7 +453,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
showToggleCameraIfNeeded()
if (mPreview?.setCamera(mCurrCameraId) == true) {
hideNavigationBarIcons()
checkFlash()
mPreview?.checkFlashlight()
if (!mIsInPhotoMode) {
initVideoButtons()
@ -540,7 +519,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
toggle_flash.beVisible()
} else {
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.dialogs.ChangeResolutionDialog
import com.simplemobiletools.camera.extensions.*
import com.simplemobiletools.camera.helpers.Config
import com.simplemobiletools.camera.helpers.PhotoProcessor
import com.simplemobiletools.camera.helpers.*
import com.simplemobiletools.commons.extensions.*
import java.io.File
import java.io.IOException
@ -43,11 +42,12 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private var mRecorder: MediaRecorder? = null
private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mZoomRatios = ArrayList<Int>()
private var mFlashlightState = FLASH_OFF
private var mCurrVideoPath = ""
private var mCanTakePicture = false
private var mIsRecording = false
private var mIsVideoMode = false
private var mIsInVideoMode = false
private var mIsSurfaceCreated = false
private var mSwitchToVideoAsap = false
private var mSetupPreviewAfterMeasure = false
@ -79,7 +79,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mSurfaceHolder.addCallback(this)
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
mCanTakePicture = false
mIsVideoMode = false
mIsInVideoMode = false
mIsSurfaceCreated = false
mSetupPreviewAfterMeasure = false
mCurrVideoPath = ""
@ -136,7 +136,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
releaseCamera()
mCamera = newCamera
if (initCamera() && mIsVideoMode) {
if (initCamera() && mIsInVideoMode) {
initRecorder()
}
@ -201,7 +201,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
}
var index = getResolutionIndex()
val resolutions = if (mIsVideoMode) {
val resolutions = if (mIsInVideoMode) {
mParameters!!.supportedVideoSizes ?: mParameters!!.supportedPreviewSizes
} else {
mParameters!!.supportedPictureSizes
@ -216,7 +216,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private fun getResolutionIndex(): Int {
val isBackCamera = mConfig.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK
return if (mIsVideoMode) {
return if (mIsInVideoMode) {
if (isBackCamera) mConfig.backVideoResIndex else mConfig.frontVideoResIndex
} else {
if (isBackCamera) mConfig.backPhotoResIndex else mConfig.frontPhotoResIndex
@ -454,7 +454,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
private fun rescheduleAutofocus() {
autoFocusHandler.removeCallbacksAndMessages(null)
autoFocusHandler.postDelayed({
if (!mIsVideoMode || !mIsRecording) {
if (!mIsInVideoMode || !mIsRecording) {
focusArea(false, false)
}
}, REFOCUS_PERIOD)
@ -495,7 +495,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
mIsSurfaceCreated = true
if (mIsVideoMode) {
if (mIsInVideoMode) {
initRecorder()
}
}
@ -561,7 +561,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
if (mSupportedPreviewSizes != null) {
// 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
mPreviewSize = if (mIsSixteenToNine || mIsVideoMode) {
mPreviewSize = if (mIsSixteenToNine || mIsInVideoMode) {
getOptimalPreviewSize(mSupportedPreviewSizes!!, mScreenSize.y, mScreenSize.x)
} else {
val newRatioHeight = (mScreenSize.x * (4.toDouble() / 3)).toInt()
@ -574,7 +574,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
if (mScreenSize.x > mPreviewSize!!.height) {
val ratio = mScreenSize.x.toFloat() / mPreviewSize!!.height
lp.width = (mPreviewSize!!.height * ratio).toInt()
if (mIsSixteenToNine || mIsVideoMode) {
if (mIsSixteenToNine || mIsInVideoMode) {
lp.height = mScreenSize.y
} else {
lp.height = (mPreviewSize!!.width * ratio).toInt()
@ -594,18 +594,40 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
}
}
fun enableFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH
fun setFlashlightState(state: Int) {
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()
}
fun disableFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
private fun enableFlash() {
mFlashlightState = FLASH_ON
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_TORCH
updateCameraParameters()
}
fun setAutoFlash() {
mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
private fun setAutoFlash() {
mFlashlightState = FLASH_AUTO
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_OFF
updateCameraParameters()
Handler().postDelayed({
@ -619,7 +641,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
stopRecording()
cleanupRecorder()
mIsRecording = false
mIsVideoMode = false
mIsInVideoMode = false
refreshPreview()
}
@ -633,7 +655,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
mSwitchToVideoAsap = false
mIsRecording = false
mIsVideoMode = true
mIsInVideoMode = true
mRecorder = MediaRecorder().apply {
setCamera(mCamera)
setVideoSource(MediaRecorder.VideoSource.DEFAULT)
@ -745,7 +767,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti
}
fun deviceOrientationChanged() {
if (mIsVideoMode && !mIsRecording) {
if (mIsInVideoMode && !mIsRecording) {
mRecorder = null
initRecorder()
}