Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add torch brightness level support for Android 13+ #174

Merged
merged 5 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.ShortcutInfo
import android.content.res.ColorStateList
import android.graphics.drawable.Icon
import android.graphics.drawable.LayerDrawable
import android.os.Bundle
Expand All @@ -19,6 +18,8 @@ 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
import kotlinx.android.synthetic.main.activity_main.*
Expand Down Expand Up @@ -179,10 +180,17 @@ class MainActivity : SimpleActivity() {
}

private fun setupCameraImpl() {
mCameraImpl = MyCameraImpl.newInstance(this)
mCameraImpl = MyCameraImpl.newInstance(this, object : CameraTorchListener {
override fun onTorchEnabled(isEnabled: Boolean) {
if (mCameraImpl!!.supportsBrightnessControl()) {
brightness_bar.beVisibleIf(isEnabled)
}
}
})
if (config.turnFlashlightOn) {
mCameraImpl!!.enableFlashlight()
}
setupBrightness()
}

private fun setupStroboscope() {
Expand Down Expand Up @@ -211,6 +219,16 @@ class MainActivity : SimpleActivity() {
}
}

private fun setupBrightness() {
brightness_bar.max = mCameraImpl?.getMaximumBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
brightness_bar.progress = mCameraImpl?.getCurrentBrightnessLevel() ?: MIN_BRIGHTNESS_LEVEL
brightness_bar.onSeekBarChangeListener { level ->
val newLevel = level.coerceAtLeast(MIN_BRIGHTNESS_LEVEL)
mCameraImpl?.updateBrightnessLevel(newLevel)
config.brightnessLevel = newLevel
}
}

private fun cameraPermissionGranted(isSOS: Boolean) {
if (isSOS) {
val isSOSRunning = mCameraImpl!!.toggleSOS()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.simplemobiletools.flashlight.helpers

interface CameraTorchListener {
fun onTorchEnabled(isEnabled:Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ class Config(context: Context) : BaseConfig(context) {
var forcePortraitMode: Boolean
get() = prefs.getBoolean(FORCE_PORTRAIT_MODE, true)
set(forcePortraitMode) = prefs.edit().putBoolean(FORCE_PORTRAIT_MODE, forcePortraitMode).apply()

var brightnessLevel: Int
get() = prefs.getInt(BRIGHTNESS_LEVEL, DEFAULT_BRIGHTNESS_LEVEL)
set(brightnessLevel) = prefs.edit().putInt(BRIGHTNESS_LEVEL, brightnessLevel).apply()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ const val STROBOSCOPE_FREQUENCY = "stroboscope_frequency"
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 MIN_BRIGHTNESS_LEVEL = 1
const val DEFAULT_BRIGHTNESS_LEVEL = -1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@file:Suppress("DEPRECATION")

package com.simplemobiletools.flashlight.helpers

import android.graphics.SurfaceTexture
import android.hardware.Camera

class LollipopCameraFlash : CameraFlash {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't test this on Android 5. I ensured that it is consistent with what was on the MyCameraImpl class

private var camera: Camera? = null
private var params: Camera.Parameters? = null

override fun toggleFlashlight(enable: Boolean) {
if (camera == null || params == null || camera!!.parameters == null) {
return
}
val flashMode = if (enable) Camera.Parameters.FLASH_MODE_ON else Camera.Parameters.FLASH_MODE_OFF
params!!.flashMode = flashMode
camera!!.parameters = params
if (enable) {
val dummy = SurfaceTexture(1)
camera!!.setPreviewTexture(dummy)
camera!!.startPreview()
}
}

override fun initialize() {
camera = Camera.open()
params = camera!!.parameters
params!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
camera!!.parameters = params
}

override fun release() {
camera!!.release()
camera = null
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
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

@RequiresApi(Build.VERSION_CODES.M)
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
}
}