From 87b678e4bbff0cacb77700fbd68a84eab723046e Mon Sep 17 00:00:00 2001 From: darthpaul Date: Wed, 9 Nov 2022 09:36:08 +0000 Subject: [PATCH] show/hide brightness seekbar from camera callback --- .../flashlight/activities/MainActivity.kt | 14 ++++++----- .../flashlight/helpers/CameraTorchListener.kt | 5 ++++ .../flashlight/helpers/MyCameraImpl.kt | 10 +++++--- .../helpers/PostMarshmallowCamera.kt | 23 ++++++++++++++++--- 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraTorchListener.kt 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 8c23fdf..8d3c9ff 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt @@ -19,6 +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.CameraTorchListener import com.simplemobiletools.flashlight.helpers.MIN_BRIGHTNESS_LEVEL import com.simplemobiletools.flashlight.helpers.MyCameraImpl import com.simplemobiletools.flashlight.models.Events @@ -54,10 +55,6 @@ class MainActivity : SimpleActivity() { flashlight_btn.setOnClickListener { mCameraImpl!!.toggleFlashlight() - if (mCameraImpl?.supportsBrightnessControl() == true) { - brightness_bar.beInvisibleIf(brightness_bar.isVisible) - stroboscope_bar.beInvisible() - } } sos_btn.setOnClickListener { @@ -184,7 +181,13 @@ class MainActivity : SimpleActivity() { } private fun setupCameraImpl() { - mCameraImpl = MyCameraImpl.newInstance(this) + mCameraImpl = MyCameraImpl.newInstance(this, object : CameraTorchListener { + override fun onTorchEnabled(isEnabled: Boolean) { + if (mCameraImpl?.supportsBrightnessControl() == true) { + brightness_bar.beVisibleIf(isEnabled) + } + } + }) if (config.turnFlashlightOn) { mCameraImpl!!.enableFlashlight() } @@ -233,7 +236,6 @@ class MainActivity : SimpleActivity() { sos_btn.setTextColor(if (isSOSRunning) getProperPrimaryColor() else getContrastColor()) } else if (mCameraImpl!!.toggleStroboscope()) { stroboscope_bar.beInvisibleIf(stroboscope_bar.isVisible()) - brightness_bar.beInvisible() changeIconColor(if (stroboscope_bar.isVisible()) getProperPrimaryColor() else getContrastColor(), stroboscope_btn) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraTorchListener.kt b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraTorchListener.kt new file mode 100644 index 0000000..481db07 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraTorchListener.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.flashlight.helpers + +interface CameraTorchListener { + fun onTorchEnabled(isEnabled:Boolean) +} 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 a3a54a2..d9b5e52 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt @@ -1,5 +1,6 @@ package com.simplemobiletools.flashlight.helpers +import android.annotation.SuppressLint import android.content.Context import android.graphics.SurfaceTexture import android.hardware.Camera @@ -14,7 +15,7 @@ import com.simplemobiletools.flashlight.extensions.updateWidgets import com.simplemobiletools.flashlight.models.Events import org.greenrobot.eventbus.EventBus -class MyCameraImpl(val context: Context) { +class MyCameraImpl private constructor(val context: Context, private val cameraTorchListener: CameraTorchListener?) { var stroboFrequency = 1000L companion object { @@ -40,7 +41,7 @@ class MyCameraImpl(val context: Context) { @Volatile private var isSOSRunning = false - fun newInstance(context: Context) = MyCameraImpl(context) + fun newInstance(context: Context, cameraTorchListener: CameraTorchListener? = null) = MyCameraImpl(context, cameraTorchListener) } init { @@ -140,9 +141,10 @@ class MyCameraImpl(val context: Context) { } } + @SuppressLint("NewApi") private fun setupMarshmallowCamera() { if (marshmallowCamera == null) { - marshmallowCamera = PostMarshmallowCamera(context) + marshmallowCamera = PostMarshmallowCamera(context, cameraTorchListener) } } @@ -209,6 +211,7 @@ class MyCameraImpl(val context: Context) { val mainRunnable = Runnable { stateChanged(true) } Handler(context.mainLooper).post(mainRunnable) + marshmallowCamera?.initialize() } private fun disableFlashlight() { @@ -254,6 +257,7 @@ class MyCameraImpl(val context: Context) { isFlashlightOn = false shouldStroboscopeStop = true + marshmallowCamera?.cleanUp() } private val stroboscope = Runnable { 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 e63a0ba..8ec920c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt +++ b/app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt @@ -1,22 +1,32 @@ package com.simplemobiletools.flashlight.helpers -import android.annotation.TargetApi import android.content.Context import android.hardware.camera2.CameraCharacteristics import android.hardware.camera2.CameraManager import android.os.Build import android.os.Handler +import androidx.annotation.RequiresApi import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.helpers.isTiramisuPlus import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.models.Events import org.greenrobot.eventbus.EventBus -internal class PostMarshmallowCamera constructor(val context: Context) { +@RequiresApi(Build.VERSION_CODES.M) +internal class PostMarshmallowCamera( + private val context: Context, + private val cameraTorchListener: CameraTorchListener? = null, +) { private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager private var cameraId: String? = null + private val torchCallback = object : CameraManager.TorchCallback() { + override fun onTorchModeChanged(cameraId: String, enabled: Boolean) { + cameraTorchListener?.onTorchEnabled(enabled) + } + } + init { try { cameraId = manager.cameraIdList[0] ?: "0" @@ -25,7 +35,6 @@ internal class PostMarshmallowCamera constructor(val context: Context) { } } - @TargetApi(Build.VERSION_CODES.M) fun toggleMarshmallowFlashlight(enable: Boolean) { try { manager.setTorchMode(cameraId!!, enable) @@ -69,4 +78,12 @@ internal class PostMarshmallowCamera constructor(val context: Context) { } return brightnessLevel } + + fun initialize() { + manager.registerTorchCallback(torchCallback, Handler(context.mainLooper)) + } + + fun cleanUp() { + manager.unregisterTorchCallback(torchCallback) + } }