Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions app/src/main/kotlin/com/cyb3rko/flashdim/Camera.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,12 @@ internal class Camera(activity: AppCompatActivity) {
cameraManager.setTorchMode(cameraId, enabled)
}

fun sendLightLevel(activity: AppCompatActivity, currentLevel: Int, level: Int) {
fun sendLightLevel(activity: AppCompatActivity, currentLevel: Int, level: Int, preactivate: Boolean = false) {
if (currentLevel != level) {
try {
if (preactivate && currentLevel <= 0) {
cameraManager.setTorchMode(cameraId, true)
}
cameraManager.turnOnTorchWithStrengthLevel(cameraId, level)
} catch (e: Exception) {
handleFlashlightException(e, activity)
Expand All @@ -75,7 +78,7 @@ internal class Camera(activity: AppCompatActivity) {
fun doesDeviceHaveFlash(packageManager: PackageManager): Boolean =
packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)

fun sendLightLevel(context: Context, level: Int, activate: Boolean) {
fun sendLightLevel(context: Context, level: Int, activate: Boolean, preactivate: Boolean = false) {
try {
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE)
as CameraManager
Expand All @@ -84,6 +87,9 @@ internal class Camera(activity: AppCompatActivity) {
if (level == -1) {
cameraManager.setTorchMode(cameraId, true)
} else {
if (preactivate) {
cameraManager.setTorchMode(cameraId, true)
}
cameraManager.turnOnTorchWithStrengthLevel(cameraId, level)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class MainActivity : AppCompatActivity() {
private var vibrateButtons = false
private var vibrateMorse = false
private var intentFlash = false
private var torchPreactivation = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -121,6 +122,7 @@ class MainActivity : AppCompatActivity() {

vibrateButtons = Safe.getBoolean(Safe.BUTTON_VIBRATION, true)
vibrateMorse = Safe.getBoolean(Safe.MORSE_VIBRATION, true)
torchPreactivation = Safe.getBoolean(Safe.TORCH_PREACTIVATION, false)
val systemVibrator = (getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager)
.defaultVibrator
Vibrator.initialize(systemVibrator)
Expand All @@ -130,6 +132,7 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
Safe.initialize(applicationContext)
torchPreactivation = Safe.getBoolean(Safe.TORCH_PREACTIVATION, false)
val restored = restoreLightLevelUi()
if (!restored) executeAppOpenFlash()
intentFlash = false
Expand All @@ -144,7 +147,7 @@ class MainActivity : AppCompatActivity() {
"DIMMER_MAX" -> maxLevel
else -> -1
}
camera.sendLightLevel(this, currentLevel, newLevel)
camera.sendLightLevel(this, currentLevel, newLevel, torchPreactivation)
updateLightLevelView(if (newLevel > 0) newLevel else 0)
binding.seekBar.setProgress(if (newLevel > 0) newLevel else 0)
currentLevel = newLevel
Expand All @@ -171,10 +174,10 @@ class MainActivity : AppCompatActivity() {
if (progress > 0) {
if (progress <= maxLevel) {
if (vibrateButtons) Vibrator.vibrateTick()
camera.sendLightLevel(this@MainActivity, currentLevel, progress)
camera.sendLightLevel(this@MainActivity, currentLevel, progress, torchPreactivation)
updateLightLevelView(progress)
} else {
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel)
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel, torchPreactivation)
updateLightLevelView(maxLevel)
}
currentLevel = progress
Expand Down Expand Up @@ -212,7 +215,7 @@ class MainActivity : AppCompatActivity() {
if (vibrateButtons) Vibrator.vibrateDoubleClick()
if (isDimAllowed()) {
updateLightLevelView(maxLevel)
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel)
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel, torchPreactivation)
currentLevel = maxLevel
seekBar.setProgress(maxLevel)
} else {
Expand All @@ -222,14 +225,14 @@ class MainActivity : AppCompatActivity() {
halfButton.setOnClickListener {
if (vibrateButtons) Vibrator.vibrateClick()
updateLightLevelView(maxLevel / 2)
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel / 2)
camera.sendLightLevel(this@MainActivity, currentLevel, maxLevel / 2, torchPreactivation)
currentLevel = maxLevel / 2
seekBar.setProgress(maxLevel / 2)
}
minButton.setOnClickListener {
if (vibrateButtons) Vibrator.vibrateClick()
updateLightLevelView(1)
camera.sendLightLevel(this@MainActivity, currentLevel, 1)
camera.sendLightLevel(this@MainActivity, currentLevel, 1, torchPreactivation)
currentLevel = 1
seekBar.setProgress(1)
}
Expand Down Expand Up @@ -323,7 +326,7 @@ class MainActivity : AppCompatActivity() {
private fun activateInitialFlash() {
if (maxLevel > 1) {
val level = Safe.getInt(Safe.PREFERRED_LEVEL, -1)
camera.sendLightLevel(this@MainActivity, currentLevel, level)
camera.sendLightLevel(this@MainActivity, currentLevel, level, torchPreactivation)
updateLightLevelView(level)
binding.seekBar.setProgress(level)
} else {
Expand Down Expand Up @@ -423,7 +426,7 @@ class MainActivity : AppCompatActivity() {
val handler = MorseHandler { letter, code, delay, on ->
if (on) {
if (maxLevel > 1) {
camera.sendLightLevel(this@MainActivity, -1, maxLevel)
camera.sendLightLevel(this@MainActivity, -1, maxLevel, torchPreactivation)
} else {
camera.setTorchMode(true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ class SettingsTile : TileService() {

private fun actAsToggle() {
var level = -1
var preactivate = false
try {
if (Safe.getBoolean(Safe.QUICK_SETTINGS_LINK, false)) {
level = Safe.getInt(Safe.PREFERRED_LEVEL, 1)
}
preactivate = Safe.getBoolean(Safe.TORCH_PREACTIVATION, false)
} catch (_: Exception) {
Log.e("FlashDim", "Safe operations failed in SettingsTile")
}
Expand All @@ -133,8 +135,8 @@ class SettingsTile : TileService() {
cameraManager?.let {
Log.d("FlashDim", "Toggling flashlight from SettingsTile (toggle)")
when (qsTile.state) {
Tile.STATE_INACTIVE -> sendFlashlightSignal(it, level, true)
Tile.STATE_ACTIVE -> sendFlashlightSignal(it, level, false)
Tile.STATE_INACTIVE -> sendFlashlightSignal(it, level, true, preactivate)
Tile.STATE_ACTIVE -> sendFlashlightSignal(it, level, false, preactivate)
}
}
} catch (e: Exception) {
Expand All @@ -157,6 +159,12 @@ class SettingsTile : TileService() {
stage = DIMMER_MIN
}
}
var preactivate = false
try {
preactivate = Safe.getBoolean(Safe.TORCH_PREACTIVATION, false)
} catch (_: Exception) {
Log.e("FlashDim", "Safe operations failed in SettingsTile")
}
description = stage.description()

val maxLevel = Safe.getInt(Safe.MAX_LEVEL, -1)
Expand All @@ -175,7 +183,7 @@ class SettingsTile : TileService() {
}
cameraManager?.let {
Log.d("FlashDim", "Dimming flashlight from SettingsTile (dimmer)")
sendFlashlightSignal(it, newLevel, newLevel != DIMMER_OFF)
sendFlashlightSignal(it, newLevel, newLevel != DIMMER_OFF, preactivate)
}
} catch (e: Exception) {
Log.e("FlashDim", "Camera access failed in SettingsTile (dimmer")
Expand All @@ -191,11 +199,14 @@ class SettingsTile : TileService() {
Safe.writeInt(Safe.QUICKTILE_DIM_STAGE, stage.next())
}

private fun sendFlashlightSignal(cameraManager: CameraManager, level: Int, activate: Boolean) {
private fun sendFlashlightSignal(cameraManager: CameraManager, level: Int, activate: Boolean, preactivate: Boolean) {
if (activate) {
if (level == -1) {
cameraManager.setTorchMode(cameraManager.cameraIdList[0], true)
} else {
if (preactivate && !enabled) {
cameraManager.setTorchMode(cameraManager.cameraIdList[0], true)
}
cameraManager.turnOnTorchWithStrengthLevel(cameraManager.cameraIdList[0], level)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class VolumeButtonService : AccessibilityService() {
Log.i("FlashDim Service", "Both volume buttons pressed")
val flashActive = Safe.getBoolean(Safe.FLASH_ACTIVE, false)
val flashLevel = if (!flashActive) getFlashLevel() else 0
Camera.sendLightLevel(applicationContext, flashLevel, !flashActive)
val preactivate = !flashActive && Safe.getBoolean(Safe.TORCH_PREACTIVATION, false)
Camera.sendLightLevel(applicationContext, flashLevel, !flashActive, preactivate)
}
} else {
volumeUpPressed = false
Expand Down
1 change: 1 addition & 0 deletions app/src/main/kotlin/com/cyb3rko/flashdim/utils/Safe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ internal object Safe {
const val QUICK_SETTINGS_LINK = "quick_settings_link"
const val REPORT_DIALOG_SHOWN = "${BuildConfig.VERSION_CODE}-report_dialog"
const val STARTUP_COUNTER = "startup_counter"
const val TORCH_PREACTIVATION = "torch_preactivation"
const val VOLUME_BUTTONS_LINK = "volume_buttons_link"

private lateinit var sharedPreferences: SharedPreferences
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ re <string name="dialog_links_github">Repository on GitHub</string>
<string name="preference_item_preferred_level_dialog_title">Set preferred level</string>
<string name="preference_item_preferred_level_dialog_message">New: %1$d\nCurrent: %2$d</string>
<string name="preference_item_preferred_level_dialog_negative_button">Cancel</string>
<string name="preference_item_torch_preactivation">Fix brightness control</string>
<string name="preference_item_torch_preactivation_summary">Turn on if your flashlight sometimes ignores brightness changes</string>
<string name="preference_category_quick_access">Quick access</string>
<string name="preference_item_quick_settings_simple">Quick settings toggle mode</string>
<string name="preference_item_quick_settings_simple_summary">Instead of dimming mode, allow the quick settings tile to only toggle the flash on and off</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
app:summary="@string/preference_item_pause_flash_summary"
app:icon="@drawable/ic_close" />

<SwitchPreferenceCompat
app:key="torch_preactivation"
app:defaultValue="false"
app:title="@string/preference_item_torch_preactivation"
app:summary="@string/preference_item_torch_preactivation_summary"
app:icon="@drawable/ic_level_scaled" />

</PreferenceCategory>

<PreferenceCategory
Expand Down