schedule another ping 30s after connectivity_change

This commit is contained in:
Daniel Gultsch 2019-01-27 20:54:45 +01:00
parent 8bc9f9a7c8
commit 0898783309
2 changed files with 153 additions and 126 deletions

View file

@ -64,6 +64,7 @@ public final class Config {
public static final int PING_TIMEOUT = 15;
public static final int SOCKET_TIMEOUT = 15;
public static final int CONNECT_TIMEOUT = 90;
public static final int POST_CONNECTIVITY_CHANGE_PING_INTERVAL = 30;
public static final int CONNECT_DISCO_TIMEOUT = 20;
public static final int MINI_GRACE_PERIOD = 750;

View file

@ -163,6 +163,7 @@ public class XmppConnectionService extends Service {
public static final String ACTION_IDLE_PING = "idle_ping";
public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh";
public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received";
private static final String ACTION_POST_CONNECTIVITY_CHANGE = "eu.siacs.conversations.POST_CONNECTIVITY_CHANGE";
private static final String SETTING_LAST_ACTIVITY_TS = "last_activity_timestamp";
@ -574,9 +575,14 @@ public class XmppConnectionService extends Service {
final String uuid = intent.getStringExtra("uuid");
switch (action) {
case ConnectivityManager.CONNECTIVITY_ACTION:
if (hasInternetConnection() && Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
if (hasInternetConnection()) {
if (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0) {
schedulePostConnectivityChange();
}
if (Config.RESET_ATTEMPT_COUNT_ON_NETWORK_CHANGE) {
resetAllAttemptCounts(true, false);
}
}
break;
case Intent.ACTION_SHUTDOWN:
logoutAndSave(true);
@ -688,7 +694,7 @@ public class XmppConnectionService extends Service {
}
synchronized (this) {
WakeLockHelper.acquire(wakeLock);
boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action);
boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
HashSet<Account> pingCandidates = new HashSet<>();
for (Account account : accounts) {
pingNow |= processAccountState(account,
@ -1186,6 +1192,26 @@ public class XmppConnectionService extends Service {
}
}
private void schedulePostConnectivityChange() {
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) {
return;
}
final long triggerAtMillis = SystemClock.elapsedRealtime() + (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL * 1000);
final Intent intent = new Intent(this, EventReceiver.class);
intent.setAction(ACTION_POST_CONNECTIVITY_CHANGE);
try {
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);
} else {
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);
}
} catch (RuntimeException e) {
Log.e(Config.LOGTAG, "unable to schedule alarm for post connectivity change", e);
}
}
public void scheduleWakeUpCall(int seconds, int requestCode) {
final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000;
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);