Skip to content

Commit 2a0072d

Browse files
Copilotignoramous
andcommitted
Add ACTION_USER_UNLOCKED support for Private Space VPN auto-start
Co-authored-by: ignoramous <852289+ignoramous@users.noreply.github.com>
1 parent d553856 commit 2a0072d

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

app/src/full/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
android:exported="false">
204204
<intent-filter>
205205
<action android:name="android.intent.action.USER_PRESENT" />
206+
<action android:name="android.intent.action.USER_UNLOCKED" />
206207
</intent-filter>
207208
</receiver>
208209

app/src/full/java/com/celzero/bravedns/receiver/UserPresentReceiver.kt

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,71 @@
1515
*/
1616
package com.celzero.bravedns.receiver
1717

18+
import Logger
1819
import Logger.LOG_TAG_UI
20+
import Logger.LOG_TAG_VPN
1921
import android.content.BroadcastReceiver
2022
import android.content.Context
2123
import android.content.Intent
24+
import android.net.VpnService
25+
import com.celzero.bravedns.service.PersistentState
2226
import com.celzero.bravedns.service.VpnController
27+
import org.koin.core.component.KoinComponent
28+
import org.koin.core.component.inject
2329

2430
/**
25-
* BroadcastReceiver to handle screen state changes (on/off) and user presence.
26-
* Informs the VpnController about these events.
31+
* BroadcastReceiver to handle user presence and user unlocking events.
32+
* Informs the VpnController about these events and handles VPN auto-start for Private Space unlock.
2733
*/
28-
class UserPresentReceiver : BroadcastReceiver() {
34+
class UserPresentReceiver : BroadcastReceiver(), KoinComponent {
35+
36+
val persistentState by inject<PersistentState>()
2937
override fun onReceive(context: Context, intent: Intent) {
30-
if (Intent.ACTION_SCREEN_OFF == intent.action) {
31-
VpnController.screenLock()
32-
Logger.v(LOG_TAG_UI, "user-present: action screen off, inform vpn service")
33-
} else if (Intent.ACTION_SCREEN_ON == intent.action || Intent.ACTION_USER_PRESENT == intent.action) {
34-
VpnController.screenUnlock()
35-
Logger.v(LOG_TAG_UI, "user-present: action screen on, inform vpn service")
38+
when (intent.action) {
39+
Intent.ACTION_USER_PRESENT -> {
40+
VpnController.screenUnlock()
41+
Logger.v(LOG_TAG_UI, "user-present: action user present, inform vpn service")
42+
}
43+
Intent.ACTION_USER_UNLOCKED -> {
44+
handleUserUnlocked(context)
45+
}
46+
else -> {
47+
Logger.v(LOG_TAG_UI, "user-present: unknown action ${intent.action}, skipping")
48+
}
49+
}
50+
}
51+
52+
private fun handleUserUnlocked(context: Context) {
53+
Logger.v(LOG_TAG_UI, "user-present: user unlocked (Private Space), inform vpn service")
54+
VpnController.screenUnlock()
55+
56+
// Auto-start VPN if auto-start is enabled and VPN was previously active
57+
// This handles Private Space unlock scenario where VPN should auto-start
58+
if (!persistentState.prefAutoStartBootUp) {
59+
Logger.v(LOG_TAG_VPN, "user-unlocked: auto start is not enabled, skipping auto-start")
60+
return
61+
}
62+
63+
// Check if VPN was activated and should be auto-started
64+
// Similar logic to BraveAutoStartReceiver but for user unlock events
65+
if (VpnController.state().activationRequested) {
66+
val prepareVpnIntent: Intent? =
67+
try {
68+
Logger.i(LOG_TAG_VPN, "user-unlocked: attempting to auto-start VPN for Private Space")
69+
VpnService.prepare(context)
70+
} catch (e: NullPointerException) {
71+
Logger.w(LOG_TAG_VPN, "user-unlocked: device does not support system-wide VPN mode")
72+
return
73+
}
74+
75+
if (prepareVpnIntent == null) {
76+
Logger.i(LOG_TAG_VPN, "user-unlocked: VPN is already prepared, invoking start")
77+
VpnController.start(context)
78+
} else {
79+
Logger.i(LOG_TAG_VPN, "user-unlocked: VPN needs preparation, cannot auto-start")
80+
}
3681
} else {
37-
Logger.v(LOG_TAG_UI, "user-present: unknown action ${intent.action}, skipping")
82+
Logger.v(LOG_TAG_VPN, "user-unlocked: VPN activation not requested, skipping auto-start")
3883
}
3984
}
4085
}

0 commit comments

Comments
 (0)