Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ForegroundServiceDidNotStartInTimeException crash #10

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ public void toggleInProxy(Context context, int maxClients, int limitUpstreamByte
intent.putExtra(LIMIT_DOWNSTREAM_BYTES, limitDownstreamBytesPerSecond);
intent.putExtra(PROXY_PRIVATE_KEY, privateKey);

// Always start the service and let it handle the logic in onStartCommand
ContextCompat.startForegroundService(context, intent);
// Send the intent to the service to toggle the proxy
// and let the service handle the logic in onStartCommand
sendStartCommandToService(context, intent);
}

public void paramsChanged(Context context, int maxClients, int limitUpstreamBytesPerSecond,
Expand All @@ -123,8 +124,21 @@ public void paramsChanged(Context context, int maxClients, int limitUpstreamByte
intent.putExtra(LIMIT_DOWNSTREAM_BYTES, limitDownstreamBytesPerSecond);
intent.putExtra(PROXY_PRIVATE_KEY, privateKey);

// Start the service to handle the paramsChanged action
ContextCompat.startForegroundService(context, intent);
// Send the intent to the service to update the parameters
// and let the service handle the logic in onStartCommand
sendStartCommandToService(context, intent);
}

// Internal method to start the ConduitService with the provided intent
private void sendStartCommandToService(Context context, Intent intent) {
// Using startService instead of startForegroundService because the service might need to shut down
// quickly without ever showing a foreground notification. Calling startForegroundService implies
// that we must call startForeground() shortly after, but ConduitService handles different types of
// actions, and sometimes it might determine there's no need to keep running (e.g., shuts down immediately
// if there's no real work). By using startService, we avoid the requirement to show a notification
// if the service ends quickly. If it ends up starting the Psiphon tunnel or doing other long-running work,
// we'll upgrade it to a foreground service at that point.
context.startService(intent);
}

public void onStart(Context context) {
Expand Down