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
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class SettingsRepository(private val context: Context) {
const val KEY_FLASHLIGHT_PULSE_ENABLED = "flashlight_pulse_enabled"
const val KEY_FLASHLIGHT_PULSE_FACEDOWN_ONLY = "flashlight_pulse_facedown_only"
const val KEY_FLASHLIGHT_PULSE_MAX_INTENSITY = "flashlight_pulse_max_intensity"
const val KEY_FLASHLIGHT_POCKET_TURN_OFF_ENABLED = "flashlight_pocket_turn_off_enabled"

const val KEY_SCREEN_LOCKED_SECURITY_ENABLED = "screen_locked_security_enabled"
const val KEY_HIDE_SYSTEM_ICONS = "hide_system_icons"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@ object FeatureRegistry {
R.string.search_remap_flashlight_desc,
"flashlight_toggle",
R.array.keywords_flashlight
),
SearchSetting(
R.string.search_flashlight_pocket_title,
R.string.search_flashlight_pocket_desc,
"flashlight_options",
R.array.keywords_pocket_detection
)
),
parentFeatureId = "Input",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
FreezeManager.freezeAll(this)
}

// Pocket Detection
private val pocketFlashlightHandler = Handler(Looper.getMainLooper())
private val pocketFlashlightRunnable = Runnable {
val prefs = getSharedPreferences("essentials_prefs", MODE_PRIVATE)
val pocketTurnOffEnabled = prefs.getBoolean("flashlight_pocket_turn_off_enabled", false)
// Re-check at fire time — guards against external torch-off between scheduling and firing
if (pocketTurnOffEnabled && flashlightHandler.isProximityBlocked && flashlightHandler.isTorchOn) {
flashlightHandler.toggleFlashlight()
}
}

private fun schedulePocketFlashlightTurnOff() {
pocketFlashlightHandler.removeCallbacks(pocketFlashlightRunnable)
pocketFlashlightHandler.postDelayed(pocketFlashlightRunnable, 1500L)
}

private fun cancelPocketFlashlightTurnOff() {
pocketFlashlightHandler.removeCallbacks(pocketFlashlightRunnable)
}

private val preferenceChangeListener =
android.content.SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key == "circle_to_search_gesture_enabled" ||
Expand Down Expand Up @@ -220,6 +240,7 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
omniGestureOverlayHandler.removeOverlay()
statusBarIconHandler.unregister()
stopInputEventListener()
cancelPocketFlashlightTurnOff()
serviceScope.cancel()
getSharedPreferences("essentials_prefs", MODE_PRIVATE)
.unregisterOnSharedPreferenceChangeListener(preferenceChangeListener)
Expand All @@ -244,6 +265,15 @@ class ScreenOffAccessibilityService : AccessibilityService(), SensorEventListene
val isBlocked = distance < maxRange && distance < 5f

flashlightHandler.isProximityBlocked = isBlocked

val prefs = getSharedPreferences("essentials_prefs", MODE_PRIVATE)
val pocketTurnOffEnabled = prefs.getBoolean("flashlight_pocket_turn_off_enabled", false)

if (pocketTurnOffEnabled && isBlocked && flashlightHandler.isTorchOn) {
schedulePocketFlashlightTurnOff()
} else {
cancelPocketFlashlightTurnOff()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@ fun ButtonRemapSettingsUI(
)
}
)

IconToggleItem(
iconRes = R.drawable.rounded_front_hand_24,
title = stringResource(R.string.flashlight_pocket_title),
description = stringResource(R.string.flashlight_pocket_desc),
isChecked = viewModel.isFlashlightPocketTurnOffEnabled.value,
onCheckedChange = {
viewModel.setFlashlightPocketTurnOffEnabled(it, context)
}
)
}

Button(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class MainViewModel : ViewModel() {
val isFlashlightPulseFacedownOnly = mutableStateOf(true)
val isFlashlightPulseUseLightingApps = mutableStateOf(true)
val flashlightPulseMaxIntensity = mutableFloatStateOf(0.5f)
val isFlashlightPocketTurnOffEnabled = mutableStateOf(false)
val isLocationPermissionGranted = mutableStateOf(false)
val isBackgroundLocationPermissionGranted = mutableStateOf(false)
val isFullScreenIntentPermissionGranted = mutableStateOf(false)
Expand Down Expand Up @@ -595,6 +596,10 @@ class MainViewModel : ViewModel() {
settingsRepository.getFloat(key, 0.5f)
}

SettingsRepository.KEY_FLASHLIGHT_POCKET_TURN_OFF_ENABLED -> {
isFlashlightPocketTurnOffEnabled.value = settingsRepository.getBoolean(key)
}

SettingsRepository.KEY_CIRCLE_TO_SEARCH_GESTURE_ENABLED -> {
isCircleToSearchGestureEnabled.value = settingsRepository.getBoolean(key)
}
Expand Down Expand Up @@ -1152,6 +1157,8 @@ class MainViewModel : ViewModel() {
SettingsRepository.KEY_FLASHLIGHT_PULSE_MAX_INTENSITY,
0.5f
)
isFlashlightPocketTurnOffEnabled.value =
settingsRepository.getBoolean(SettingsRepository.KEY_FLASHLIGHT_POCKET_TURN_OFF_ENABLED)
isPitchBlackThemeEnabled.value =
settingsRepository.getBoolean(SettingsRepository.KEY_PITCH_BLACK_THEME_ENABLED)

Expand Down Expand Up @@ -3313,6 +3320,14 @@ class MainViewModel : ViewModel() {
)
}

fun setFlashlightPocketTurnOffEnabled(enabled: Boolean, context: Context) {
isFlashlightPocketTurnOffEnabled.value = enabled
settingsRepository.putBoolean(
SettingsRepository.KEY_FLASHLIGHT_POCKET_TURN_OFF_ENABLED,
enabled
)
}

fun setFlashlightFadeEnabled(enabled: Boolean, context: Context) {
isFlashlightFadeEnabled.value = enabled
settingsRepository.putBoolean(SettingsRepository.KEY_FLASHLIGHT_FADE_ENABLED, enabled)
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
<string name="settings_section_other">Other</string>
<string name="flashlight_always_off_title">Always turn off flashlight</string>
<string name="flashlight_always_off_desc">Even while display is on</string>
<string name="flashlight_pocket_title">Pocket detection</string>
<string name="flashlight_pocket_desc">Turn off flashlight when phone is in your pocket</string>
<string name="content_desc_settings">Settings</string>

<!-- Caffeinate / Permissions -->
Expand Down Expand Up @@ -584,6 +586,8 @@
<string name="search_remap_haptic_desc">Vibration feedback when remapped button is pressed</string>
<string name="search_remap_flashlight_title">Flashlight toggle</string>
<string name="search_remap_flashlight_desc">Toggle flashlight with volume buttons</string>
<string name="search_flashlight_pocket_title">Flashlight pocket detection</string>
<string name="search_flashlight_pocket_desc">Auto-turn off flashlight when placed in pocket</string>
<string name="search_night_light_enable_title">Enable Dynamic Night Light</string>
<string name="search_night_light_enable_desc">Master switch for dynamic night light</string>
<string name="search_app_lock_enable_title">Enable app lock</string>
Expand Down Expand Up @@ -951,6 +955,13 @@
<item>pulse</item>
<item>notification</item>
</string-array>
<string-array name="keywords_pocket_detection">
<item>pocket</item>
<item>proximity</item>
<item>sensor</item>
<item>torch</item>
<item>auto</item>
</string-array>
<string-array name="keywords_qs_stay_awake">
<item>awake</item>
<item>developer</item>
Expand Down