Skip to content
Open
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
35 changes: 35 additions & 0 deletions app/src/main/java/org/fptn/vpn/services/CustomVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<FptnServerDto> fptnServerDtos = fptnServerRepository.getServersListFuture(false).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onClick() {
if (connectionState != null && connectionState.isActiveState()) {
CustomVpnService.startToDisconnect(this);
} else {
CustomVpnService.startToConnect(this, FptnServerDto.AUTO);
CustomVpnService.startToConnect(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down