From 0bd4d8f34f2a19390bc8dcf7f524cd7ea9143d05 Mon Sep 17 00:00:00 2001 From: fil Date: Tue, 4 Nov 2025 01:25:13 +0700 Subject: [PATCH] Processing case: start vpn service from tileService with connecting to previously selected server --- .../fptn/vpn/services/CustomVpnService.java | 35 +++++++++++++++++++ .../vpn/services/tile/FptnTileService.java | 2 +- .../org/fptn/vpn/core/common/Constants.kt | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/fptn/vpn/services/CustomVpnService.java b/app/src/main/java/org/fptn/vpn/services/CustomVpnService.java index 1a4874f6..fda6509a 100644 --- a/app/src/main/java/org/fptn/vpn/services/CustomVpnService.java +++ b/app/src/main/java/org/fptn/vpn/services/CustomVpnService.java @@ -2,6 +2,7 @@ import static org.fptn.vpn.core.common.Constants.SELECTED_SERVER; import static org.fptn.vpn.core.common.Constants.SELECTED_SERVER_ID_AUTO; +import static org.fptn.vpn.core.common.Constants.START_FROM_TILE_AUTO; import android.annotation.SuppressLint; import android.app.AlarmManager; @@ -139,6 +140,17 @@ public static void startToConnect(Context context, FptnServerDto fptnServerDto) context.startService(intent); } + public static void startToConnect(Context context) { + Intent intent = new Intent(context, CustomVpnService.class); + intent.setAction(ACTION_CONNECT); + // Now it method called only from FptnTileService + intent.putExtra(SELECTED_SERVER, START_FROM_TILE_AUTO); + + // If started service not become foreground - will be exception ANR - after 30 seconds approx. + //context.startForegroundService(intent); + context.startService(intent); + } + public static void startToDisconnect(Context context) { Intent intent = new Intent(context, CustomVpnService.class); intent.setAction(ACTION_DISCONNECT); @@ -221,6 +233,29 @@ public int onStartCommand(Intent intent, int flags, int startId) { setConnectionState(ConnectionState.CONNECTING, null); int serverId = intent.getIntExtra(SELECTED_SERVER, SELECTED_SERVER_ID_AUTO); + + // Process startService from TileService + if (serverId == START_FROM_TILE_AUTO) { + Log.i(TAG, "onStartCommand: start from tileService"); + // If reset selected server enabled - process like average auto + if (SharedPrefUtils.getResetSelectedServerEnabled(this)) { + Log.i(TAG, "onStartCommand: start from tileService. Reset selected server enabled"); + serverId = SELECTED_SERVER_ID_AUTO; + } else { + // But if reset disabled, try to get previously selected server from DB + FptnServerDto server = fptnServerRepository.getSelected().get(); + if (server != null) { + // if found + Log.i(TAG, "onStartCommand: start from tileService. Previously selected server found: " + server); + serverId = server.id; + } else { + // if not found - auto + Log.i(TAG, "onStartCommand: start from tileService. Previously selected server not found: select auto"); + serverId = SELECTED_SERVER_ID_AUTO; + } + } + } + if (serverId == SELECTED_SERVER_ID_AUTO) { try { List fptnServerDtos = fptnServerRepository.getServersListFuture(false).get(); diff --git a/app/src/main/java/org/fptn/vpn/services/tile/FptnTileService.java b/app/src/main/java/org/fptn/vpn/services/tile/FptnTileService.java index fceff289..c03adb33 100644 --- a/app/src/main/java/org/fptn/vpn/services/tile/FptnTileService.java +++ b/app/src/main/java/org/fptn/vpn/services/tile/FptnTileService.java @@ -52,7 +52,7 @@ public void onClick() { if (connectionState != null && connectionState.isActiveState()) { CustomVpnService.startToDisconnect(this); } else { - CustomVpnService.startToConnect(this, FptnServerDto.AUTO); + CustomVpnService.startToConnect(this); } } diff --git a/core/common/src/main/kotlin/org/fptn/vpn/core/common/Constants.kt b/core/common/src/main/kotlin/org/fptn/vpn/core/common/Constants.kt index 38cdcdbe..6b861939 100644 --- a/core/common/src/main/kotlin/org/fptn/vpn/core/common/Constants.kt +++ b/core/common/src/main/kotlin/org/fptn/vpn/core/common/Constants.kt @@ -4,6 +4,7 @@ object Constants { const val QUICK_SETTINGS_TILE_REQUESTED_SHARED_PREF_KEY: String = "QUICK_SETTINGS_TILE_REQUESTED_SHARED_PREF_KEY" const val SELECTED_SERVER: String = "fptn.selected.server" const val SELECTED_SERVER_ID_AUTO: Int = -1 + const val START_FROM_TILE_AUTO: Int = -1000 // NOTIFICATIONS CONSTANTS const val MAIN_NOTIFICATION_CHANNEL_ID = "fptnvpn-notification-main"