From 75eca9e3f0d7f53a84613072db6605bef2941f03 Mon Sep 17 00:00:00 2001 From: darthpaul Date: Wed, 26 Oct 2022 14:16:27 +0100 Subject: [PATCH] set brightness level to maximum by default --- .../flashlight/activities/MainActivity.kt | 8 ++++---- .../flashlight/helpers/Constants.kt | 3 ++- .../flashlight/helpers/MyCameraImpl.kt | 10 ++++++++-- .../flashlight/helpers/PostMarshmallowCamera.kt | 17 +++++++++++++---- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt index 96ec87b..8c23fdf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -19,7 +19,7 @@ import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.flashlight.BuildConfig import com.simplemobiletools.flashlight.R import com.simplemobiletools.flashlight.extensions.config -import com.simplemobiletools.flashlight.helpers.DEFAULT_BRIGHTNESS_LEVEL +import com.simplemobiletools.flashlight.helpers.MIN_BRIGHTNESS_LEVEL import com.simplemobiletools.flashlight.helpers.MyCameraImpl import com.simplemobiletools.flashlight.models.Events import kotlinx.android.synthetic.main.activity_main.* @@ -218,10 +218,10 @@ class MainActivity : SimpleActivity() { } private fun setupBrightness() { - brightness_bar.max = mCameraImpl?.getMaximumBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL - brightness_bar.progress = config.brightnessLevel + brightness_bar.max = mCameraImpl?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL + brightness_bar.progress = mCameraImpl?.getCurrentBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL brightness_bar.onSeekBarChangeListener { level -> - val newLevel = level.coerceAtLeast(DEFAULT_BRIGHTNESS_LEVEL) + val newLevel = level.coerceAtLeast(MIN_BRIGHTNESS_LEVEL) mCameraImpl?.updateBrightnessLevel(newLevel) config.brightnessLevel = newLevel } diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt index ce944bd..1517a9c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/Constants.kt @@ -12,4 +12,5 @@ const val STROBOSCOPE_PROGRESS = "stroboscope_progress" const val FORCE_PORTRAIT_MODE = "force_portrait_mode" const val SOS = "sos" const val BRIGHTNESS_LEVEL = "brightness_level" -const val DEFAULT_BRIGHTNESS_LEVEL = 1 +const val MIN_BRIGHTNESS_LEVEL = 1 +const val DEFAULT_BRIGHTNESS_LEVEL = -1 diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt index f619a3a..a3a54a2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt @@ -344,8 +344,14 @@ class MyCameraImpl(val context: Context) { fun getMaximumBrightnessLevel(): Int { return if (isMarshmallow) { - marshmallowCamera?.getMaximumBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL - } else DEFAULT_BRIGHTNESS_LEVEL + marshmallowCamera?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL + } else MIN_BRIGHTNESS_LEVEL + } + + fun getCurrentBrightnessLevel(): Int { + return if (isMarshmallow) { + marshmallowCamera?.getCurrentBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL + } else MIN_BRIGHTNESS_LEVEL } fun supportsBrightnessControl(): Boolean { diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt index 2d02572..e63a0ba 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt @@ -30,7 +30,8 @@ internal class PostMarshmallowCamera constructor(val context: Context) { try { manager.setTorchMode(cameraId!!, enable) if (enable) { - changeTorchBrightness(context.config.brightnessLevel) + val brightnessLevel = getCurrentBrightnessLevel() + changeTorchBrightness(brightnessLevel) } } catch (e: Exception) { context.showErrorToast(e) @@ -50,14 +51,22 @@ internal class PostMarshmallowCamera constructor(val context: Context) { fun getMaximumBrightnessLevel(): Int { return if (isTiramisuPlus()) { val characteristics = manager.getCameraCharacteristics(cameraId!!) - characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL) ?: DEFAULT_BRIGHTNESS_LEVEL + characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL) ?: MIN_BRIGHTNESS_LEVEL } else { - DEFAULT_BRIGHTNESS_LEVEL + MIN_BRIGHTNESS_LEVEL } } fun supportsBrightnessControl(): Boolean { val maxBrightnessLevel = getMaximumBrightnessLevel() - return maxBrightnessLevel > DEFAULT_BRIGHTNESS_LEVEL + return maxBrightnessLevel > MIN_BRIGHTNESS_LEVEL + } + + fun getCurrentBrightnessLevel(): Int { + var brightnessLevel = context.config.brightnessLevel + if (brightnessLevel == DEFAULT_BRIGHTNESS_LEVEL) { + brightnessLevel = getMaximumBrightnessLevel() + } + return brightnessLevel } }