Wait for network when websocket connection failed
This commit is contained in:
@@ -6,8 +6,11 @@ import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Color;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
@@ -25,6 +28,7 @@ import com.github.gotify.log.Log;
|
||||
import com.github.gotify.log.UncaughtExceptionHandler;
|
||||
import com.github.gotify.messages.MessagesActivity;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class WebSocketService extends Service {
|
||||
@@ -84,15 +88,45 @@ public class WebSocketService extends Service {
|
||||
missingMessageUtil.lastReceivedMessage(lastReceivedMessage::set);
|
||||
}
|
||||
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
connection =
|
||||
new WebSocketConnection(settings.url(), settings.sslSettings(), settings.token())
|
||||
new WebSocketConnection(
|
||||
settings.url(), settings.sslSettings(), settings.token(), cm)
|
||||
.onOpen(this::onOpen)
|
||||
.onClose(() -> foreground(getString(R.string.websocket_closed)))
|
||||
.onBadRequest(this::onBadRequest)
|
||||
.onFailure((min) -> foreground(getString(R.string.websocket_failed, min)))
|
||||
.onDisconnect(this::onDisconnect)
|
||||
.onMessage(this::onMessage)
|
||||
.onReconnected(this::notifyMissedNotifications)
|
||||
.start();
|
||||
|
||||
IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
|
||||
ReconnectListener receiver = new ReconnectListener(this::doReconnect);
|
||||
registerReceiver(receiver, intentFilter);
|
||||
}
|
||||
|
||||
private void onDisconnect() {
|
||||
foreground(getString(R.string.websocket_no_network));
|
||||
}
|
||||
|
||||
private void doReconnect() {
|
||||
if (connection == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
new Handler()
|
||||
.postDelayed(
|
||||
() -> new Thread(this::notifyAndStart).start(),
|
||||
TimeUnit.SECONDS.toMillis(5));
|
||||
}
|
||||
|
||||
private void notifyAndStart() {
|
||||
notifyMissedNotifications();
|
||||
connection.start();
|
||||
}
|
||||
|
||||
private void onBadRequest(String message) {
|
||||
|
||||
Reference in New Issue
Block a user