mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-04-28 00:48:55 +02:00
removing a redundant interface
This commit is contained in:
parent
b9f180d9d2
commit
b96e268a45
@ -1,12 +1,90 @@
|
|||||||
package com.simplemobiletools.flashlight.helpers
|
package com.simplemobiletools.flashlight.helpers
|
||||||
|
|
||||||
interface CameraFlash {
|
import android.content.Context
|
||||||
fun initialize()
|
import android.hardware.camera2.CameraCharacteristics
|
||||||
fun toggleFlashlight(enable: Boolean)
|
import android.hardware.camera2.CameraManager
|
||||||
fun changeTorchBrightness(level: Int) {}
|
import android.os.Handler
|
||||||
fun getMaximumBrightnessLevel(): Int = DEFAULT_BRIGHTNESS_LEVEL
|
import com.simplemobiletools.commons.extensions.showErrorToast
|
||||||
fun supportsBrightnessControl(): Boolean = false
|
import com.simplemobiletools.commons.helpers.isTiramisuPlus
|
||||||
fun getCurrentBrightnessLevel(): Int = DEFAULT_BRIGHTNESS_LEVEL
|
import com.simplemobiletools.flashlight.extensions.config
|
||||||
fun unregisterListeners(){}
|
import com.simplemobiletools.flashlight.models.Events
|
||||||
fun release(){}
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
@ -132,7 +132,7 @@ class MyCameraImpl private constructor(val context: Context, private var cameraT
|
|||||||
fun handleCameraSetup() {
|
fun handleCameraSetup() {
|
||||||
try {
|
try {
|
||||||
if (cameraFlash == null) {
|
if (cameraFlash == null) {
|
||||||
cameraFlash = MarshmallowPlusCameraFlash(context, cameraTorchListener)
|
cameraFlash = CameraFlash(context, cameraTorchListener)
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
EventBus.getDefault().post(Events.CameraUnavailable())
|
EventBus.getDefault().post(Events.CameraUnavailable())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user