Skip to content
Merged
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 @@ -1159,12 +1159,14 @@ class SettingsRepository(private val context: Context) {

if (fixed <= 0f && min <= 0f && peak <= 0f) {
com.sameerasw.essentials.utils.RefreshRateUtils.resetRefreshRate(
context,
shouldRestoreInfinityPeakOnRefreshRateReset()
)
} else if (mode == com.sameerasw.essentials.utils.RefreshRateUtils.MODE_RANGE && min > 0f && peak > 0f) {
com.sameerasw.essentials.utils.RefreshRateUtils.applyRangeRefreshRate(min, peak)
com.sameerasw.essentials.utils.RefreshRateUtils.applyRangeRefreshRate(context, min, peak)
} else if (fixed > 0f || peak > 0f) {
com.sameerasw.essentials.utils.RefreshRateUtils.applyFixedRefreshRate(
context,
if (fixed > 0f) fixed else peak
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class VolumeLongPressDetector(
val events = _events.asSharedFlow()

private val isRunning = AtomicBoolean(false)
val isListening: Boolean
get() = isRunning.get()
private var detectionJob: Job? = null
private val reader = InputEventReader(volumeDevicePath)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import com.sameerasw.essentials.input.InputDeviceScanner
import com.sameerasw.essentials.input.VolumeLongPressDetector
import com.sameerasw.essentials.input.VolumePressEvent
import com.sameerasw.essentials.shizuku.ShizukuPermissionHelper
import rikka.shizuku.Shizuku
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand All @@ -31,6 +33,16 @@ class InputEventListenerService : Service() {
private lateinit var shizukuHelper: ShizukuPermissionHelper
private var isTorchOn = false

private val binderReceivedListener = Shizuku.OnBinderReceivedListener {
Log.d("InputEventListener", "Shizuku binder received, attempting to start listening")
startListening()
}

private val binderDeadListener = Shizuku.OnBinderDeadListener {
Log.w("InputEventListener", "Shizuku binder died, stopping listener")
stopListening()
}

override fun onCreate() {
super.onCreate()
shizukuHelper = ShizukuPermissionHelper(this)
Expand Down Expand Up @@ -73,6 +85,9 @@ class InputEventListenerService : Service() {
startForeground(NOTIFICATION_ID, notification)
}

Shizuku.addBinderReceivedListener(binderReceivedListener)
Shizuku.addBinderDeadListener(binderDeadListener)

scope?.launch {
delay(500)
startListening()
Expand All @@ -81,37 +96,35 @@ class InputEventListenerService : Service() {

private fun startListening() {
scope?.launch {
stopListening()

if (!com.sameerasw.essentials.utils.ShellUtils.hasPermission(this@InputEventListenerService)) {
Log.e("InputEventListener", "Shell permission not granted")
return@launch
}

val devices = withContext(Dispatchers.IO) {
// find device
InputDeviceScanner().scanForVolumeDevices()
}

if (devices.isEmpty()) {
Log.e("InputEventListener", "No devices found")
// Clear prefs if no device found
getSharedPreferences("essentials_prefs", MODE_PRIVATE)
.edit().remove("shizuku_detected_device_path").apply()
return@launch
}

// picked first item
// IMPROVEMENT: Iterate and try to find the one that actually emits keys
val devicePath = devices.first().path
Log.d("InputEventListener", "Listening on device: $devicePath")

// Save detected device to prefs for UI
getSharedPreferences("essentials_prefs", MODE_PRIVATE)
.edit().putString("shizuku_detected_device_path", devicePath).apply()

detector = VolumeLongPressDetector(devicePath, 500)
val activeDetector = VolumeLongPressDetector(devicePath, 500)
detector = activeDetector

launch {
detector?.events?.collect { event ->
activeDetector.events.collect { event ->
val powerManager =
getSystemService(POWER_SERVICE) as android.os.PowerManager
val isScreenInteractive = try {
Expand All @@ -121,7 +134,6 @@ class InputEventListenerService : Service() {
}
val isAod = isAodShowing()

// Only process volume events if screen is fully off (not interactive AND not AOD)
if (isScreenInteractive || isAod) {
return@collect
}
Expand All @@ -144,7 +156,6 @@ class InputEventListenerService : Service() {
val isGlobalEnabled =
prefs.getBoolean("flashlight_global_enabled", false)

// Only if adjustment is enabled
if (isAdjustEnabled || isGlobalEnabled) {
val action =
if (event.direction == com.sameerasw.essentials.input.VolumeDirection.UP)
Expand Down Expand Up @@ -207,10 +218,15 @@ class InputEventListenerService : Service() {
}
}

detector?.startListening(this)
activeDetector.startListening(this)
}
}

private fun stopListening() {
detector?.stopListening()
detector = null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// Reinforce foreground
val notification = NotificationCompat.Builder(this, "service_channel")
Expand All @@ -231,7 +247,9 @@ class InputEventListenerService : Service() {
}

override fun onDestroy() {
detector?.stopListening()
Shizuku.removeBinderReceivedListener(binderReceivedListener)
Shizuku.removeBinderDeadListener(binderDeadListener)
stopListening()
scope?.cancel()
super.onDestroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,23 @@ class NotificationListener : NotificationListenerService() {
return false
}

private fun isSilentNotification(sbn: StatusBarNotification): Boolean {
try {
val rankingMap = currentRanking ?: return false
val ranking = Ranking()
if (rankingMap.getRanking(sbn.key, ranking)) {
return ranking.importance < android.app.NotificationManager.IMPORTANCE_DEFAULT
}
} catch (_: Exception) {
}
return false
}

private fun populateActiveUnreadNotifications() {
unreadNotifications.clear()
try {
activeNotifications?.forEach { sbn ->
if (!sbn.isOngoing && sbn.packageName != packageName && !isMediaNotification(sbn)) {
if (!sbn.isOngoing && sbn.packageName != packageName && !isMediaNotification(sbn) && !isSilentNotification(sbn)) {
unreadNotifications[sbn.key] = sbn.packageName
}
}
Expand Down Expand Up @@ -674,7 +686,7 @@ class NotificationListener : NotificationListenerService() {
val pm = getSystemService(Context.POWER_SERVICE) as PowerManager
val isReallyLocked = isScreenLocked || !pm.isInteractive || com.sameerasw.essentials.services.dreams.AmbientDreamService.isDreaming

if (isReallyLocked && !sbn.isOngoing && sbn.packageName != packageName && !isMediaNotification(sbn)) {
if (isReallyLocked && !sbn.isOngoing && sbn.packageName != packageName && !isMediaNotification(sbn) && !isSilentNotification(sbn)) {
unreadNotifications[sbn.key] = sbn.packageName
// Trigger refresh if something is playing
try {
Expand Down
Loading