Skip to content

Commit cea57e9

Browse files
committed
Add torch brightness level support for Android 13+
1 parent dc66f8b commit cea57e9

File tree

6 files changed

+85
-4
lines changed

6 files changed

+85
-4
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import android.annotation.SuppressLint
44
import android.content.Intent
55
import android.content.pm.ActivityInfo
66
import android.content.pm.ShortcutInfo
7-
import android.content.res.ColorStateList
87
import android.graphics.drawable.Icon
98
import android.graphics.drawable.LayerDrawable
109
import android.os.Bundle
1110
import android.view.WindowManager
1211
import android.widget.ImageView
12+
import androidx.core.view.isVisible
1313
import com.simplemobiletools.commons.extensions.*
1414
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
1515
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
@@ -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.DEFAULT_BRIGHTNESS_LEVEL
2223
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
2324
import com.simplemobiletools.flashlight.models.Events
2425
import kotlinx.android.synthetic.main.activity_main.*
@@ -53,6 +54,10 @@ class MainActivity : SimpleActivity() {
5354

5455
flashlight_btn.setOnClickListener {
5556
mCameraImpl!!.toggleFlashlight()
57+
if (mCameraImpl?.supportsBrightnessControl() == true) {
58+
brightness_bar.beInvisibleIf(brightness_bar.isVisible)
59+
stroboscope_bar.beInvisible()
60+
}
5661
}
5762

5863
sos_btn.setOnClickListener {
@@ -183,6 +188,7 @@ class MainActivity : SimpleActivity() {
183188
if (config.turnFlashlightOn) {
184189
mCameraImpl!!.enableFlashlight()
185190
}
191+
setupBrightness()
186192
}
187193

188194
private fun setupStroboscope() {
@@ -211,12 +217,23 @@ class MainActivity : SimpleActivity() {
211217
}
212218
}
213219

220+
private fun setupBrightness() {
221+
brightness_bar.max = mCameraImpl?.getMaximumBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
222+
brightness_bar.progress = config.brightnessLevel
223+
brightness_bar.onSeekBarChangeListener { level ->
224+
val newLevel = level.coerceAtLeast(DEFAULT_BRIGHTNESS_LEVEL)
225+
mCameraImpl?.updateBrightnessLevel(newLevel)
226+
config.brightnessLevel = newLevel
227+
}
228+
}
229+
214230
private fun cameraPermissionGranted(isSOS: Boolean) {
215231
if (isSOS) {
216232
val isSOSRunning = mCameraImpl!!.toggleSOS()
217233
sos_btn.setTextColor(if (isSOSRunning) getProperPrimaryColor() else getContrastColor())
218234
} else if (mCameraImpl!!.toggleStroboscope()) {
219235
stroboscope_bar.beInvisibleIf(stroboscope_bar.isVisible())
236+
brightness_bar.beInvisible()
220237
changeIconColor(if (stroboscope_bar.isVisible()) getProperPrimaryColor() else getContrastColor(), stroboscope_btn)
221238
}
222239
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ class Config(context: Context) : BaseConfig(context) {
4040
var forcePortraitMode: Boolean
4141
get() = prefs.getBoolean(FORCE_PORTRAIT_MODE, true)
4242
set(forcePortraitMode) = prefs.edit().putBoolean(FORCE_PORTRAIT_MODE, forcePortraitMode).apply()
43+
44+
var brightnessLevel: Int
45+
get() = prefs.getInt(BRIGHTNESS_LEVEL, DEFAULT_BRIGHTNESS_LEVEL)
46+
set(brightnessLevel) = prefs.edit().putInt(BRIGHTNESS_LEVEL, brightnessLevel).apply()
4347
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ const val STROBOSCOPE_FREQUENCY = "stroboscope_frequency"
1111
const val STROBOSCOPE_PROGRESS = "stroboscope_progress"
1212
const val FORCE_PORTRAIT_MODE = "force_portrait_mode"
1313
const val SOS = "sos"
14+
const val BRIGHTNESS_LEVEL = "brightness_level"
15+
const val DEFAULT_BRIGHTNESS_LEVEL = 1

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class MyCameraImpl(val context: Context) {
2929
private var shouldEnableSOS = false
3030
private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
3131

32-
private var marshmallowCamera: MarshmallowCamera? = null
32+
private var marshmallowCamera: PostMarshmallowCamera? = null
3333

3434
@Volatile
3535
private var shouldStroboscopeStop = false
@@ -142,7 +142,7 @@ class MyCameraImpl(val context: Context) {
142142

143143
private fun setupMarshmallowCamera() {
144144
if (marshmallowCamera == null) {
145-
marshmallowCamera = MarshmallowCamera(context)
145+
marshmallowCamera = PostMarshmallowCamera(context)
146146
}
147147
}
148148

@@ -341,4 +341,22 @@ class MyCameraImpl(val context: Context) {
341341
}
342342
}
343343
}
344+
345+
fun getMaximumBrightnessLevel(): Int {
346+
return if (isMarshmallow) {
347+
marshmallowCamera?.getMaximumBrightnessLevel() ?: DEFAULT_BRIGHTNESS_LEVEL
348+
} else DEFAULT_BRIGHTNESS_LEVEL
349+
}
350+
351+
fun supportsBrightnessControl(): Boolean {
352+
return if (isMarshmallow) {
353+
marshmallowCamera?.supportsBrightnessControl() ?: false
354+
} else false
355+
}
356+
357+
fun updateBrightnessLevel(level: Int) {
358+
if (isMarshmallow) {
359+
marshmallowCamera?.changeTorchBrightness(level)
360+
}
361+
}
344362
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ package com.simplemobiletools.flashlight.helpers
22

33
import android.annotation.TargetApi
44
import android.content.Context
5+
import android.hardware.camera2.CameraCharacteristics
56
import android.hardware.camera2.CameraManager
67
import android.os.Build
78
import android.os.Handler
89
import com.simplemobiletools.commons.extensions.showErrorToast
10+
import com.simplemobiletools.commons.helpers.isTiramisuPlus
11+
import com.simplemobiletools.flashlight.extensions.config
912
import com.simplemobiletools.flashlight.models.Events
1013
import org.greenrobot.eventbus.EventBus
1114

12-
internal class MarshmallowCamera constructor(val context: Context) {
15+
internal class PostMarshmallowCamera constructor(val context: Context) {
1316

1417
private val manager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
1518
private var cameraId: String? = null
@@ -26,6 +29,9 @@ internal class MarshmallowCamera constructor(val context: Context) {
2629
fun toggleMarshmallowFlashlight(enable: Boolean) {
2730
try {
2831
manager.setTorchMode(cameraId!!, enable)
32+
if (enable) {
33+
changeTorchBrightness(context.config.brightnessLevel)
34+
}
2935
} catch (e: Exception) {
3036
context.showErrorToast(e)
3137
val mainRunnable = Runnable {
@@ -34,4 +40,24 @@ internal class MarshmallowCamera constructor(val context: Context) {
3440
Handler(context.mainLooper).post(mainRunnable)
3541
}
3642
}
43+
44+
fun changeTorchBrightness(level: Int) {
45+
if (isTiramisuPlus()) {
46+
manager.turnOnTorchWithStrengthLevel(cameraId!!, level)
47+
}
48+
}
49+
50+
fun getMaximumBrightnessLevel(): Int {
51+
return if (isTiramisuPlus()) {
52+
val characteristics = manager.getCameraCharacteristics(cameraId!!)
53+
characteristics.get(CameraCharacteristics.FLASH_INFO_STRENGTH_MAXIMUM_LEVEL) ?: DEFAULT_BRIGHTNESS_LEVEL
54+
} else {
55+
DEFAULT_BRIGHTNESS_LEVEL
56+
}
57+
}
58+
59+
fun supportsBrightnessControl(): Boolean {
60+
val maxBrightnessLevel = getMaximumBrightnessLevel()
61+
return maxBrightnessLevel > DEFAULT_BRIGHTNESS_LEVEL
62+
}
3763
}

app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@
105105
app:layout_constraintStart_toStartOf="parent"
106106
app:layout_constraintTop_toBottomOf="@+id/stroboscope_btn" />
107107

108+
<com.simplemobiletools.commons.views.MySeekBar
109+
android:id="@+id/brightness_bar"
110+
android:layout_width="@dimen/seekbar_width"
111+
android:layout_height="wrap_content"
112+
android:layout_margin="@dimen/activity_margin"
113+
android:paddingTop="@dimen/medium_margin"
114+
android:paddingBottom="@dimen/medium_margin"
115+
android:visibility="invisible"
116+
app:layout_constraintBottom_toBottomOf="parent"
117+
app:layout_constraintEnd_toEndOf="parent"
118+
app:layout_constraintHorizontal_bias="0.5"
119+
app:layout_constraintStart_toStartOf="parent"
120+
app:layout_constraintTop_toBottomOf="@+id/stroboscope_btn" />
121+
108122
</androidx.constraintlayout.widget.ConstraintLayout>
109123
</androidx.core.widget.NestedScrollView>
110124
</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)