show/hide brightness seekbar from camera callback
This commit is contained in:
parent
75eca9e3f0
commit
87b678e4bb
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.simplemobiletools.flashlight.helpers
|
||||
|
||||
interface CameraTorchListener {
|
||||
fun onTorchEnabled(isEnabled:Boolean)
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue