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 not working scheduled reconnect #117

Merged
merged 1 commit into from
Jun 13, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.github.gotify.service;

import android.app.AlarmManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Handler;
import com.github.gotify.SSLSettings;
import com.github.gotify.Utils;
import com.github.gotify.api.Callback;
import com.github.gotify.api.CertUtils;
import com.github.gotify.client.model.Message;
import com.github.gotify.log.Log;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
Expand All @@ -17,8 +20,9 @@
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class WebSocketConnection {
class WebSocketConnection {
private final ConnectivityManager connectivityManager;
private final AlarmManager alarmManager;
private OkHttpClient client;

private final Handler reconnectHandler = new Handler();
Expand All @@ -41,8 +45,10 @@ public class WebSocketConnection {
String baseUrl,
SSLSettings settings,
String token,
ConnectivityManager connectivityManager) {
ConnectivityManager connectivityManager,
AlarmManager alarmManager) {
this.connectivityManager = connectivityManager;
this.alarmManager = alarmManager;
OkHttpClient.Builder builder =
new OkHttpClient.Builder()
.readTimeout(0, TimeUnit.MILLISECONDS)
Expand Down Expand Up @@ -119,14 +125,25 @@ public synchronized void close() {
}
}

public synchronized void scheduleReconnect(long millis) {
reconnectHandler.removeCallbacks(reconnectCallback);

Log.i(
"WebSocket: scheduling a restart in "
+ TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS)
+ " second(s)");
reconnectHandler.postDelayed(reconnectCallback, millis);
public synchronized void scheduleReconnect(long seconds) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.i(
"WebSocket: scheduling a restart in "
+ seconds
+ " second(s) (via alarm manager)");
final Calendar future = Calendar.getInstance();
future.add(Calendar.SECOND, (int) seconds);
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
future.getTimeInMillis(),
"reconnect-tag",
this::start,
null);
} else {
Log.i("WebSocket: scheduling a restart in " + seconds + " second(s)");
reconnectHandler.removeCallbacks(reconnectCallback);
reconnectHandler.postDelayed(reconnectCallback, TimeUnit.SECONDS.toMillis(seconds));
}
}

private class Listener extends WebSocketListener {
Expand Down Expand Up @@ -191,7 +208,7 @@ public void onFailure(WebSocket webSocket, Throwable t, Response response) {
int minutes = Math.min(errorCount * 2 - 1, 20);

onNetworkFailure.execute(minutes);
scheduleReconnect(TimeUnit.MINUTES.toMillis(minutes));
scheduleReconnect(TimeUnit.MINUTES.toSeconds(minutes));
}

super.onFailure(webSocket, t, response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.gotify.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
Expand Down Expand Up @@ -30,7 +31,6 @@
import com.github.gotify.messages.MessagesActivity;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class WebSocketService extends Service {
Expand Down Expand Up @@ -92,10 +92,15 @@ private void startPushService() {

ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

connection =
new WebSocketConnection(
settings.url(), settings.sslSettings(), settings.token(), cm)
settings.url(),
settings.sslSettings(),
settings.token(),
cm,
alarmManager)
.onOpen(this::onOpen)
.onClose(() -> foreground(getString(R.string.websocket_closed)))
.onBadRequest(this::onBadRequest)
Expand All @@ -121,7 +126,7 @@ private void doReconnect() {
return;
}

connection.scheduleReconnect(TimeUnit.SECONDS.toMillis(5));
connection.scheduleReconnect(5);
}

private void onBadRequest(String message) {
Expand Down