Wait for network when websocket connection failed
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package com.github.gotify.service;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import com.github.gotify.log.Log;
|
||||
|
||||
public class ReconnectListener extends BroadcastReceiver {
|
||||
|
||||
private Runnable reconnected;
|
||||
|
||||
ReconnectListener(Runnable reconnected) {
|
||||
this.reconnected = reconnected;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
NetworkInfo network = cm.getActiveNetworkInfo();
|
||||
|
||||
if (network != null && network.isConnected()) {
|
||||
Log.i("Network reconnected");
|
||||
reconnected.run();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.github.gotify.service;
|
||||
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Handler;
|
||||
import com.github.gotify.SSLSettings;
|
||||
import com.github.gotify.Utils;
|
||||
@@ -16,6 +18,7 @@ import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
|
||||
public class WebSocketConnection {
|
||||
private final ConnectivityManager connectivityManager;
|
||||
private OkHttpClient client;
|
||||
|
||||
private final Handler handler = new Handler();
|
||||
@@ -31,8 +34,14 @@ public class WebSocketConnection {
|
||||
private OnFailureCallback onFailure;
|
||||
private Runnable onReconnected;
|
||||
private boolean isClosed;
|
||||
private Runnable onDisconnect;
|
||||
|
||||
WebSocketConnection(String baseUrl, SSLSettings settings, String token) {
|
||||
WebSocketConnection(
|
||||
String baseUrl,
|
||||
SSLSettings settings,
|
||||
String token,
|
||||
ConnectivityManager connectivityManager) {
|
||||
this.connectivityManager = connectivityManager;
|
||||
OkHttpClient.Builder builder =
|
||||
new OkHttpClient.Builder()
|
||||
.readTimeout(0, TimeUnit.MILLISECONDS)
|
||||
@@ -66,6 +75,11 @@ public class WebSocketConnection {
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized WebSocketConnection onDisconnect(Runnable onDisconnect) {
|
||||
this.onDisconnect = onDisconnect;
|
||||
return this;
|
||||
}
|
||||
|
||||
synchronized WebSocketConnection onFailure(OnFailureCallback onFailure) {
|
||||
this.onFailure = onFailure;
|
||||
return this;
|
||||
@@ -153,6 +167,13 @@ public class WebSocketConnection {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkInfo network = connectivityManager.getActiveNetworkInfo();
|
||||
if (network == null || !network.isConnected()) {
|
||||
Log.i("WebSocket: Network not connected");
|
||||
onDisconnect.run();
|
||||
return;
|
||||
}
|
||||
|
||||
int minutes = Math.min(errorCount * 2 + 1, 20);
|
||||
|
||||
Log.i("WebSocket: trying to restart in " + minutes + " minute(s)");
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -57,4 +57,5 @@
|
||||
<string name="warning">Warning</string>
|
||||
<string name="http_warning">Using http is insecure and it\'s recommend to use https instead. Use your favorite search engine to get more information about this topic.</string>
|
||||
<string name="i_understand">I Understand</string>
|
||||
<string name="websocket_no_network">Waiting for network</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user