Skip to content

Commit 87b678e

Browse files
committed
show/hide brightness seekbar from camera callback
1 parent 75eca9e commit 87b678e

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.simplemobiletools.commons.models.FAQItem
1919
import com.simplemobiletools.flashlight.BuildConfig
2020
import com.simplemobiletools.flashlight.R
2121
import com.simplemobiletools.flashlight.extensions.config
22+
import com.simplemobiletools.flashlight.helpers.CameraTorchListener
2223
import com.simplemobiletools.flashlight.helpers.MIN_BRIGHTNESS_LEVEL
2324
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
2425
import com.simplemobiletools.flashlight.models.Events
@@ -54,10 +55,6 @@ class MainActivity : SimpleActivity() {
5455

5556
flashlight_btn.setOnClickListener {
5657
mCameraImpl!!.toggleFlashlight()
57-
if (mCameraImpl?.supportsBrightnessControl() == true) {
58-
brightness_bar.beInvisibleIf(brightness_bar.isVisible)
59-
stroboscope_bar.beInvisible()
60-
}
6158
}
6259

6360
sos_btn.setOnClickListener {
@@ -184,7 +181,13 @@ class MainActivity : SimpleActivity() {
184181
}
185182

186183
private fun setupCameraImpl() {
187-
mCameraImpl = MyCameraImpl.newInstance(this)
184+
mCameraImpl = MyCameraImpl.newInstance(this, object : CameraTorchListener {
185+
override fun onTorchEnabled(isEnabled: Boolean) {
186+
if (mCameraImpl?.supportsBrightnessControl() == true) {
187+
brightness_bar.beVisibleIf(isEnabled)
188+
}
189+
}
190+
})
188191
if (config.turnFlashlightOn) {
189192
mCameraImpl!!.enableFlashlight()
190193
}
@@ -233,7 +236,6 @@ class MainActivity : SimpleActivity() {
233236
sos_btn.setTextColor(if (isSOSRunning) getProperPrimaryColor() else getContrastColor())
234237
} else if (mCameraImpl!!.toggleStroboscope()) {
235238
stroboscope_bar.beInvisibleIf(stroboscope_bar.isVisible())
236-
brightness_bar.beInvisible()
237239
changeIconColor(if (stroboscope_bar.isVisible()) getProperPrimaryColor() else getContrastColor(), stroboscope_btn)
238240
}
239241
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.simplemobiletools.flashlight.helpers
2+
3+
interface CameraTorchListener {
4+
fun onTorchEnabled(isEnabled:Boolean)
5+
}

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.flashlight.helpers
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.graphics.SurfaceTexture
56
import android.hardware.Camera
@@ -14,7 +15,7 @@ import com.simplemobiletools.flashlight.extensions.updateWidgets
1415
import com.simplemobiletools.flashlight.models.Events
1516
import org.greenrobot.eventbus.EventBus
1617

17-
class MyCameraImpl(val context: Context) {
18+
class MyCameraImpl private constructor(val context: Context, private val cameraTorchListener: CameraTorchListener?) {
1819
var stroboFrequency = 1000L
1920

2021
companion object {
@@ -40,7 +41,7 @@ class MyCameraImpl(val context: Context) {
4041
@Volatile
4142
private var isSOSRunning = false
4243

43-
fun newInstance(context: Context) = MyCameraImpl(context)
44+
fun newInstance(context: Context, cameraTorchListener: CameraTorchListener? = null) = MyCameraImpl(context, cameraTorchListener)
4445
}
4546

4647
init {
@@ -140,9 +141,10 @@ class MyCameraImpl(val context: Context) {
140141
}
141142
}
142143

144+
@SuppressLint("NewApi")
143145
private fun setupMarshmallowCamera() {
144146
if (marshmallowCamera == null) {
145-
marshmallowCamera = PostMarshmallowCamera(context)
147+
marshmallowCamera = PostMarshmallowCamera(context, cameraTorchListener)
146148
}
147149
}
148150

@@ -209,6 +211,7 @@ class MyCameraImpl(val context: Context) {
209211

210212
val mainRunnable = Runnable { stateChanged(true) }
211213
Handler(context.mainLooper).post(mainRunnable)
214+
marshmallowCamera?.initialize()
212215
}
213216

214217
private fun disableFlashlight() {
@@ -254,6 +257,7 @@ class MyCameraImpl(val context: Context) {
254257

255258
isFlashlightOn = false
256259
shouldStroboscopeStop = true
260+
marshmallowCamera?.cleanUp()
257261
}
258262

259263
private val stroboscope = Runnable {

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/PostMarshmallowCamera.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package com.simplemobiletools.flashlight.helpers
22

3-
import android.annotation.TargetApi
43
import android.content.Context
54
import android.hardware.camera2.CameraCharacteristics
65
import android.hardware.camera2.CameraManager
76
import android.os.Build
87
import android.os.Handler
8+
import androidx.annotation.RequiresApi
99
import com.simplemobiletools.commons.extensions.showErrorToast
1010
import com.simplemobiletools.commons.helpers.isTiramisuPlus
1111
import com.simplemobiletools.flashlight.extensions.config
1212
import com.simplemobiletools.flashlight.models.Events
1313
import org.greenrobot.eventbus.EventBus
1414

15-
internal class PostMarshmallowCamera constructor(val context: Context) {
15+
@RequiresApi(Build.VERSION_CODES.M)
16+
internal class PostMarshmallowCamera(
17+
private val context: Context,
18+
private val cameraTorchListener: CameraTorchListener? = null,
19+
) {
1620

1721
private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
1822
private var cameraId: String? = null
1923

24+
private val torchCallback = object : CameraManager.TorchCallback() {
25+
override fun onTorchModeChanged(cameraId: String, enabled: Boolean) {
26+
cameraTorchListener?.onTorchEnabled(enabled)
27+
}
28+
}
29+
2030
init {
2131
try {
2232
cameraId = manager.cameraIdList[0] ?: "0"
@@ -25,7 +35,6 @@ internal class PostMarshmallowCamera constructor(val context: Context) {
2535
}
2636
}
2737

28-
@TargetApi(Build.VERSION_CODES.M)
2938
fun toggleMarshmallowFlashlight(enable: Boolean) {
3039
try {
3140
manager.setTorchMode(cameraId!!, enable)
@@ -69,4 +78,12 @@ internal class PostMarshmallowCamera constructor(val context: Context) {
6978
}
7079
return brightnessLevel
7180
}
81+
82+
fun initialize() {
83+
manager.registerTorchCallback(torchCallback, Handler(context.mainLooper))
84+
}
85+
86+
fun cleanUp() {
87+
manager.unregisterTorchCallback(torchCallback)
88+
}
7289
}

0 commit comments

Comments
 (0)