removing a redundant interface

This commit is contained in:
tibbi 2022-12-20 18:58:59 +01:00
parent b9f180d9d2
commit b96e268a45
3 changed files with 88 additions and 103 deletions

View File

@ -1,12 +1,90 @@
package com.simplemobiletools.flashlight.helpers
interface CameraFlash {
fun initialize()
fun toggleFlashlight(enable: Boolean)
fun changeTorchBrightness(level: Int) {}
fun getMaximumBrightnessLevel(): Int = DEFAULT_BRIGHTNESS_LEVEL
fun supportsBrightnessControl(): Boolean = false
fun getCurrentBrightnessLevel(): Int = DEFAULT_BRIGHTNESS_LEVEL
fun unregisterListeners(){}
fun release(){}
import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Handler
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 CameraFlash(
private val context: Context,
private var 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"
} catch (e: Exception) {
context.showErrorToast(e)
}
}
fun toggleFlashlight(enable: Boolean) {
try {
if (supportsBrightnessControl() && enable) {
val brightnessLevel = getCurrentBrightnessLevel()
changeTorchBrightness(brightnessLevel)
} else {
manager.setTorchMode(cameraId!!, enable)
}
} catch (e: Exception) {
context.showErrorToast(e)
val mainRunnable = Runnable {
EventBus.getDefault().post(Events.CameraUnavailable())
}
Handler(context.mainLooper).post(mainRunnable)
}
}
fun changeTorchBrightness(level: Int) {
if (isTiramisuPlus()) {
manager.turnOnTorchWithStrengthLevel(cameraId!!, level)
}
}
fun getMaximumBrightnessLevel(): Int {
return if (isTiramisuPlus()) {
val characteristics = manager.getCameraCharacteristics(cameraId!!)
characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL) ?: MIN_BRIGHTNESS_LEVEL
} else {
MIN_BRIGHTNESS_LEVEL
}
}
fun supportsBrightnessControl(): Boolean {
val maxBrightnessLevel = getMaximumBrightnessLevel()
return maxBrightnessLevel > MIN_BRIGHTNESS_LEVEL
}
fun getCurrentBrightnessLevel(): Int {
var brightnessLevel = context.config.brightnessLevel
if (brightnessLevel == DEFAULT_BRIGHTNESS_LEVEL) {
brightnessLevel = getMaximumBrightnessLevel()
}
return brightnessLevel
}
fun initialize() {
manager.registerTorchCallback(torchCallback, Handler(context.mainLooper))
}
fun unregisterListeners() {
manager.unregisterTorchCallback(torchCallback)
}
fun release() {
cameraTorchListener = null
}
}

View File

@ -1,93 +0,0 @@
package com.simplemobiletools.flashlight.helpers
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 MarshmallowPlusCameraFlash(
private val context: Context,
private var cameraTorchListener: CameraTorchListener? = null,
) : CameraFlash {
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"
} catch (e: Exception) {
context.showErrorToast(e)
}
}
override fun toggleFlashlight(enable: Boolean) {
try {
if (supportsBrightnessControl() && enable) {
val brightnessLevel = getCurrentBrightnessLevel()
changeTorchBrightness(brightnessLevel)
} else {
manager.setTorchMode(cameraId!!, enable)
}
} catch (e: Exception) {
context.showErrorToast(e)
val mainRunnable = Runnable {
EventBus.getDefault().post(Events.CameraUnavailable())
}
Handler(context.mainLooper).post(mainRunnable)
}
}
override fun changeTorchBrightness(level: Int) {
if (isTiramisuPlus()) {
manager.turnOnTorchWithStrengthLevel(cameraId!!, level)
}
}
override fun getMaximumBrightnessLevel(): Int {
return if (isTiramisuPlus()) {
val characteristics = manager.getCameraCharacteristics(cameraId!!)
characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL) ?: MIN_BRIGHTNESS_LEVEL
} else {
MIN_BRIGHTNESS_LEVEL
}
}
override fun supportsBrightnessControl(): Boolean {
val maxBrightnessLevel = getMaximumBrightnessLevel()
return maxBrightnessLevel > MIN_BRIGHTNESS_LEVEL
}
override fun getCurrentBrightnessLevel(): Int {
var brightnessLevel = context.config.brightnessLevel
if (brightnessLevel == DEFAULT_BRIGHTNESS_LEVEL) {
brightnessLevel = getMaximumBrightnessLevel()
}
return brightnessLevel
}
override fun initialize() {
manager.registerTorchCallback(torchCallback, Handler(context.mainLooper))
}
override fun unregisterListeners() {
manager.unregisterTorchCallback(torchCallback)
}
override fun release() {
cameraTorchListener = null
}
}

View File

@ -132,7 +132,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
fun handleCameraSetup() {
try {
if (cameraFlash == null) {
cameraFlash = MarshmallowPlusCameraFlash(context, cameraTorchListener)
cameraFlash = CameraFlash(context, cameraTorchListener)
}
} catch (e: Exception) {
EventBus.getDefault().post(Events.CameraUnavailable())