From 604c5b9e7613dfc04cf773c74e84048fc16bc6c4 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 26 Mar 2017 18:24:51 +0200 Subject: [PATCH] fix #26, add automatic flash --- .../com/simplemobiletools/camera/Config.kt | 6 ++-- .../com/simplemobiletools/camera/Constants.kt | 6 +++- .../com/simplemobiletools/camera/Preview.kt | 12 ++++---- .../camera/activities/MainActivity.kt | 29 ++++++++++++------- 4 files changed, 33 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt index d2959c2f..fe08cce8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Config.kt @@ -34,9 +34,9 @@ class Config(context: Context) : BaseConfig(context) { get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK) set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply() - var lastFlashlightState: Boolean - get() = prefs.getBoolean(LAST_FLASHLIGHT_STATE, false) - set(enabled) = prefs.edit().putBoolean(LAST_FLASHLIGHT_STATE, enabled).apply() + var flashlightState: Int + get() = prefs.getInt(FLASHLIGHT_STATE, FLASH_OFF) + set(state) = prefs.edit().putInt(FLASHLIGHT_STATE, state).apply() var backPhotoResIndex: Int get() = prefs.getInt(BACK_PHOTO_RESOLUTION_INDEX, -1) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt index e39c3896..7b7f118c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Constants.kt @@ -9,8 +9,12 @@ val SAVE_PHOTOS = "save_photos" val SHOW_PREVIEW = "show_preview" val SOUND = "sound" val LAST_USED_CAMERA = "last_used_camera" -val LAST_FLASHLIGHT_STATE = "last_flashlight_state" +val FLASHLIGHT_STATE = "flashlight_state" val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index" val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index" val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index" val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index" + +val FLASH_OFF = 0 +val FLASH_ON = 1 +val FLASH_AUTO = 2 diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt index 6c011f4f..18e66358 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/Preview.kt @@ -6,6 +6,7 @@ import android.graphics.Rect import android.hardware.Camera import android.media.* import android.net.Uri +import android.os.Build import android.os.Environment import android.os.Handler import android.util.Log @@ -44,7 +45,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan private var mCurrVideoPath = "" private var mCanTakePicture = false - private var mIsFlashEnabled = false private var mIsRecording = false private var mIsVideoMode = false private var mIsSurfaceCreated = false @@ -68,7 +68,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan mSurfaceHolder.addCallback(this) mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS) mCanTakePicture = false - mIsFlashEnabled = false mIsVideoMode = false mIsSurfaceCreated = false mSetupPreviewAfterMeasure = false @@ -267,7 +266,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan } } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { mCamera!!.enableShutterSound(false) } @@ -465,15 +464,18 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan fun enableFlash() { mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_TORCH mCamera!!.parameters = mParameters - mIsFlashEnabled = true } fun disableFlash() { - mIsFlashEnabled = false mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_OFF mCamera!!.parameters = mParameters } + fun autoFlash() { + mParameters!!.flashMode = Camera.Parameters.FLASH_MODE_AUTO + mCamera!!.parameters = mParameters + } + fun initPhotoMode() { stopRecording() cleanupRecorder() diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt index 6364b22e..7a70c4c1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt @@ -44,7 +44,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho private var mPreview: Preview? = null private var mPreviewUri: Uri? = null - private var mIsFlashEnabled = false + private var mFlashlightState = FLASH_OFF private var mIsInPhotoMode = false private var mIsAskingPermissions = false private var mIsCameraAvailable = false @@ -140,7 +140,7 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho mIsInPhotoMode = true mTimerHandler = Handler() mFadeHandler = Handler() - mIsFlashEnabled = config.lastFlashlightState + mFlashlightState = config.flashlightState setupPreviewImage(true) } @@ -228,30 +228,37 @@ class MainActivity : SimpleActivity(), SensorEventListener, PreviewListener, Pho return } - mIsFlashEnabled = !mIsFlashEnabled + mFlashlightState = ++mFlashlightState % 3 checkFlash() } private fun checkFlash() { - if (mIsFlashEnabled) { - enableFlash() - } else { - disableFlash() + when (mFlashlightState) { + FLASH_ON -> enableFlash() + FLASH_AUTO -> autoFlash() + else -> disableFlash() } } private fun disableFlash() { mPreview?.disableFlash() toggle_flash.setImageResource(R.drawable.ic_flash_off) - mIsFlashEnabled = false - config.lastFlashlightState = mIsFlashEnabled + mFlashlightState = FLASH_OFF + config.flashlightState = FLASH_OFF } private fun enableFlash() { mPreview?.enableFlash() toggle_flash.setImageResource(R.drawable.ic_flash_on) - mIsFlashEnabled = true - config.lastFlashlightState = mIsFlashEnabled + mFlashlightState = FLASH_ON + config.flashlightState = FLASH_ON + } + + private fun autoFlash() { + mPreview?.autoFlash() + toggle_flash.setImageResource(R.drawable.ic_flash_auto) + mFlashlightState = FLASH_AUTO + config.flashlightState = FLASH_AUTO } private fun shutterPressed() {