|
15 | 15 | */ |
16 | 16 | package com.celzero.bravedns.receiver |
17 | 17 |
|
| 18 | +import Logger |
18 | 19 | import Logger.LOG_TAG_UI |
| 20 | +import Logger.LOG_TAG_VPN |
19 | 21 | import android.content.BroadcastReceiver |
20 | 22 | import android.content.Context |
21 | 23 | import android.content.Intent |
| 24 | +import android.net.VpnService |
| 25 | +import com.celzero.bravedns.service.PersistentState |
22 | 26 | import com.celzero.bravedns.service.VpnController |
| 27 | +import org.koin.core.component.KoinComponent |
| 28 | +import org.koin.core.component.inject |
23 | 29 |
|
24 | 30 | /** |
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. |
27 | 33 | */ |
28 | | -class UserPresentReceiver : BroadcastReceiver() { |
| 34 | +class UserPresentReceiver : BroadcastReceiver(), KoinComponent { |
| 35 | + |
| 36 | + val persistentState by inject<PersistentState>() |
29 | 37 | 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 | + } |
36 | 81 | } 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") |
38 | 83 | } |
39 | 84 | } |
40 | 85 | } |
0 commit comments