Skip to content

feat: implement pocket mode exclusions and active app checks - #563

Open
Mudit200408 wants to merge 7 commits into
sameerasw:developfrom
Mudit200408:feat-pocket-mode
Open

feat: implement pocket mode exclusions and active app checks#563
Mudit200408 wants to merge 7 commits into
sameerasw:developfrom
Mudit200408:feat-pocket-mode

Conversation

@Mudit200408

@Mudit200408 Mudit200408 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

No description provided.

- Improve the app detection and also filter system noise
thomasborgogno and others added 6 commits July 30, 2026 17:13
## Summary

Adds two new automation trigger types: **Bluetooth device
connected/disconnected** and **Wi-Fi network connected/disconnected**.
Users can now build automations that fire an action when the phone
connects to (or disconnects from) a specific paired Bluetooth device or
a specific Wi-Fi network — the same way existing triggers like Charger
Connected or Schedule work today.

## What's new

- **`Trigger.kt`**: 4 new sealed subtypes —
`BluetoothConnected`/`BluetoothDisconnected` (device address + name) and
`WifiConnected`/`WifiDisconnected` (SSID), all configurable via a
settings sheet like `Trigger.Schedule`.
- **`BluetoothModule`** (new): dynamic `BroadcastReceiver` on
`ACTION_ACL_CONNECTED`/`ACTION_ACL_DISCONNECTED`, filtered by MAC
address.
- **`WifiModule`** (new): `ConnectivityManager.NetworkCallback` on the
Wi-Fi transport, resolving the active SSID via `WifiManager` and firing
on connect/lost.
- **`AutomationManager`**: wires both modules into the existing module
lifecycle (same pattern as Power/Display/Time modules).
- **Editor UI**: two new trigger entries in `AutomationEditorActivity`,
each configurable through a bottom sheet:
- `BluetoothDeviceSelectionSheet` — pick from the phone's paired devices
(requests `BLUETOOTH_CONNECT` on Android 12+).
- `WifiNetworkSelectionSheet` — if the app has **Shizuku** (or root)
authorized, it runs `cmd wifi list-networks` under shell privileges and
lets the user pick straight from the device's saved networks; otherwise
it falls back to manual SSID entry with a "Use Current Network" helper
(requests `ACCESS_FINE_LOCATION`, needed by Android to read the current
SSID).
- **Manifest**: added `ACCESS_WIFI_STATE` (the Bluetooth/location
permissions needed were already present).

## Why

Requested enhancement to make automations react to connectivity context
(e.g. "turn on Wi-Fi calling when connected to home network", "mute when
a specific headset disconnects") without needing a schedule or manual
toggle.

## Test plan

- [ ] Create a Trigger automation on "Bluetooth Device
Connected"/"Disconnected" for a paired device and verify the linked
action fires on connect/disconnect.
- [ ] Create a Trigger automation on "Wi-Fi Network
Connected"/"Disconnected".
- [ ] Verify triggers still fire correctly after the app/service is
killed and restarted (foreground service reattachment).
…iver registration context in BluetoothModule
…selection sheets to use RoundedCardContainer
@sameerasw

Copy link
Copy Markdown
Owner

Hi! Can you take a look at these concerns and verify?

Potential Issues & Suggestions

1. IPC / Main Thread Blocking in isAppBypassedForPocketMode()

  • Location: ScreenOffAccessibilityService.kt
  • Issue: isAppBypassedForPocketMode() is called directly inside onSensorChanged() on every single proximity and light sensor event.
  • Detail: Inside isAppBypassedForPocketMode(), it opens SharedPreferences (getSharedPreferences(...)), checks UsageStatsManager.queryEvents(), calls rootInActiveWindow (Accessibility IPC), checks active media sessions via MediaSessionManager, and queries PackageManager.
  • Risk: Proximity/Light sensors can fire dozens of times per second. Executing multiple IPC calls, SharedPreferences reads, and accessibility window tree queries directly on the main thread inside onSensorChanged() will cause UI stutters, frame drops, or high CPU usage.
  • Recommendation: Cache isBypassed status or run app bypass checks only when sensor state transitions from unblocked to blocked, rather than on every raw sensor event update.

2. SharedPreferences Instantiation Overhead

  • Location: ScreenOffAccessibilityService.kt lines 397 & 431
  • Issue: getSharedPreferences("essentials_prefs", MODE_PRIVATE) is called repeatedly inside isAppBypassedForPocketMode(), getActivePackageName(), and onSensorChanged().
  • Recommendation: ScreenOffAccessibilityService already maintains shared preference state or listeners. Accessing prefs repeatedly inside high-frequency sensor callbacks should be replaced with cached properties or a single SharedPreferences reference.

3. hasActiveMediaSession() Context / Exception Edge Case

  • Location: ScreenOffAccessibilityService.kt
  • Issue: Checking active media sessions on certain OEM ROMs or restricted user profiles can throw SecurityException if notification access / media listener permission is revoked or restricted.
  • Recommendation: Wrap calls to hasActiveMediaSession() inside a try-catch block inside isAppBypassedForPocketMode() to prevent unexpected service crashes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

3 participants