Skip to content
Merged
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -475,17 +475,32 @@ private void broadcastVpnConnectivityChange(TunnelStatus status) {
private void startLastSuccessfulTunnel() {
LOG.info("Received an auto-connect request, loading last successful tunnel.");
JSONObject tunnel = tunnelStore.load();

// Retrieve the server name early so we can pass it to startForegroundWithNotification.
// Fall back to an empty string when no tunnel is stored.
String serverName = "";
if (tunnel != null) {
serverName = tunnel.optString(TUNNEL_SERVER_NAME, "");
}

// Always call startForeground before any early return. Android 8+ requires that a service
// started via startForegroundService() calls startForeground() within 5 seconds, regardless
// of the code path taken. Requires android.permission.FOREGROUND_SERVICE since Android P.
startForegroundWithNotification(serverName);

if (tunnel == null) {
LOG.info("Last successful tunnel not found. User not connected at shutdown/install.");
tunnelStore.setTunnelStatus(TunnelStatus.DISCONNECTED);
QuickSettingsTileService.requestTileUpdate(this);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a good design. disconnecting the tunnel, updating the tile, stopping the foreground notification and stopself all feel part of the same lifecycle transition. We shouldn't have to repeat this sequence everywhere.

At a minimum we need perhaps a disconnect method that does all those actions. But it perhaps needs a deeper look into the apis to simplify and cleanup

stopSelf();
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
return;
}
if (VpnTunnelService.prepare(VpnTunnelService.this) != null) {
// We cannot prepare the VPN when running as a background service, as it requires UI.
LOG.warning("VPN not prepared, aborting auto-connect.");
tunnelStore.setTunnelStatus(TunnelStatus.DISCONNECTED);
QuickSettingsTileService.requestTileUpdate(this);
stopSelf();
return;
}
try {
Expand All @@ -494,9 +509,6 @@ private void startLastSuccessfulTunnel() {
tunnelConfig.name = tunnel.getString(TUNNEL_SERVER_NAME);
tunnelConfig.transportConfig = tunnel.getString(TUNNEL_CONFIG_KEY);

// Start the service in the foreground as per Android 8+ background service execution limits.
// Requires android.permission.FOREGROUND_SERVICE since Android P.
startForegroundWithNotification(tunnelConfig.name);
startTunnel(tunnelConfig, true);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Failed to retrieve JSON tunnel data", e);
Expand Down
Loading