-
-
Notifications
You must be signed in to change notification settings - Fork 178
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
tibbi
merged 5 commits into
SimpleMobileTools:master
from
KryptKode:feat/brightness-control
Nov 15, 2022
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cea57e9
Add torch brightness level support for Android 13+
KryptKode 75eca9e
set brightness level to maximum by default
KryptKode 87b678e
show/hide brightness seekbar from camera callback
KryptKode 98c0f05
cleanup flash implementation
KryptKode c975ab1
clear listeners after disabling flash for SOS and stroboscope
KryptKode File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraFlash.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){} | ||
} |
5 changes: 5 additions & 0 deletions
5
app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/CameraTorchListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/LollipopCameraFlash.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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 | ||
} | ||
} |
37 changes: 0 additions & 37 deletions
37
app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MarshmallowCamera.kt
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MarshmallowPlusCameraFlash.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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