close tcp connection after 30s of inactivity when in push_mode

This commit is contained in:
Daniel Gultsch 2016-08-09 19:21:54 +02:00
parent 856029a611
commit 9bfdbc708e
3 changed files with 31 additions and 14 deletions

View file

@ -60,7 +60,10 @@ public final class Config {
public static final int CONNECT_DISCO_TIMEOUT = 20; public static final int CONNECT_DISCO_TIMEOUT = 20;
public static final int MINI_GRACE_PERIOD = 750; public static final int MINI_GRACE_PERIOD = 750;
public static final boolean CLOSE_TCP_WHEN_SWITCHING_TO_BACKGROUND = false; public static final boolean PUSH_MODE = false; //closes the tcp connection when going to background
//and after PING_MIN_INTERVAL of inactivity
//very experimental. only enable this if you want
//to around with GCM push
public static final int AVATAR_SIZE = 192; public static final int AVATAR_SIZE = 192;
public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP; public static final Bitmap.CompressFormat AVATAR_FORMAT = Bitmap.CompressFormat.WEBP;

View file

@ -3,7 +3,9 @@ package eu.siacs.conversations.services;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.ConnectivityManager;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.persistance.DatabaseBackend; import eu.siacs.conversations.persistance.DatabaseBackend;
public class EventReceiver extends BroadcastReceiver { public class EventReceiver extends BroadcastReceiver {
@ -16,8 +18,11 @@ public class EventReceiver extends BroadcastReceiver {
} else { } else {
mIntentForService.setAction("other"); mIntentForService.setAction("other");
} }
if (intent.getAction().equals("ui") final String action = intent.getAction();
|| DatabaseBackend.getInstance(context).hasEnabledAccounts()) { if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION) && Config.PUSH_MODE) {
return;
}
if (action.equals("ui") || DatabaseBackend.getInstance(context).hasEnabledAccounts()) {
context.startService(mIntentForService); context.startService(mIntentForService);
} }
} }

View file

@ -323,14 +323,15 @@ public class XmppConnectionService extends Service {
joinMuc(conversation); joinMuc(conversation);
} }
account.pendingConferenceJoins.clear(); account.pendingConferenceJoins.clear();
scheduleWakeUpCall(Config.PING_MAX_INTERVAL, account.getUuid().hashCode()); scheduleWakeUpCall(Config.PUSH_MODE ? Config.PING_MIN_INTERVAL : Config.PING_MAX_INTERVAL, account.getUuid().hashCode());
} else if (account.getStatus() == Account.State.OFFLINE) { } else if (account.getStatus() == Account.State.OFFLINE) {
resetSendingToWaiting(account); resetSendingToWaiting(account);
final boolean disabled = account.isOptionSet(Account.OPTION_DISABLED); final boolean disabled = account.isOptionSet(Account.OPTION_DISABLED);
final boolean pushMode = Config.CLOSE_TCP_WHEN_SWITCHING_TO_BACKGROUND final boolean listeners = checkListeners();
final boolean pushMode = Config.PUSH_MODE
&& mPushManagementService.available(account) && mPushManagementService.available(account)
&& checkListeners(); && listeners;
Log.d(Config.LOGTAG,account.getJid().toBareJid()+": push mode "+Boolean.toString(pushMode)); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": push mode="+Boolean.toString(pushMode)+" listeners="+Boolean.toString(listeners));
if (!disabled && !pushMode) { if (!disabled && !pushMode) {
int timeToReconnect = mRandom.nextInt(20) + 10; int timeToReconnect = mRandom.nextInt(20) + 10;
scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode()); scheduleWakeUpCall(timeToReconnect, account.getUuid().hashCode());
@ -582,7 +583,7 @@ public class XmppConnectionService extends Service {
break; break;
case ACTION_IDLE_PING: case ACTION_IDLE_PING:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !Config.CLOSE_TCP_WHEN_SWITCHING_TO_BACKGROUND) { && !Config.PUSH_MODE) {
scheduleNextIdlePing(); scheduleNextIdlePing();
} }
break; break;
@ -613,7 +614,7 @@ public class XmppConnectionService extends Service {
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
long lastReceived = account.getXmppConnection().getLastPacketReceived(); long lastReceived = account.getXmppConnection().getLastPacketReceived();
long lastSent = account.getXmppConnection().getLastPingSent(); long lastSent = account.getXmppConnection().getLastPingSent();
long pingInterval = "ui".equals(action) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000; long pingInterval = (Config.PUSH_MODE || "ui".equals(action)) ? Config.PING_MIN_INTERVAL * 1000 : Config.PING_MAX_INTERVAL * 1000;
long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime(); long msToNextPing = (Math.max(lastReceived, lastSent) + pingInterval) - SystemClock.elapsedRealtime();
long pingTimeoutIn = (lastSent + Config.PING_TIMEOUT * 1000) - SystemClock.elapsedRealtime(); long pingTimeoutIn = (lastSent + Config.PING_TIMEOUT * 1000) - SystemClock.elapsedRealtime();
if (lastSent > lastReceived) { if (lastSent > lastReceived) {
@ -661,10 +662,18 @@ public class XmppConnectionService extends Service {
} }
} }
if (pingNow) { if (pingNow) {
final boolean listeners = checkListeners();
for (Account account : pingCandidates) { for (Account account : pingCandidates) {
if (listeners
&& Config.PUSH_MODE
&& mPushManagementService.available(account)) {
account.getXmppConnection().waitForPush();
cancelWakeUpCall(account.getUuid().hashCode());
} else {
account.getXmppConnection().sendPing(); account.getXmppConnection().sendPing();
Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action="+action+")"); Log.d(Config.LOGTAG, account.getJid().toBareJid() + " send ping (action=" + action + ",listeners="+Boolean.toString(listeners)+")");
this.scheduleWakeUpCall(Config.PING_TIMEOUT, account.getUuid().hashCode()); scheduleWakeUpCall(Config.PING_TIMEOUT, account.getUuid().hashCode());
}
} }
} }
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
@ -801,7 +810,7 @@ public class XmppConnectionService extends Service {
updateUnreadCountBadge(); updateUnreadCountBadge();
toggleScreenEventReceiver(); toggleScreenEventReceiver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !Config.CLOSE_TCP_WHEN_SWITCHING_TO_BACKGROUND) { && !Config.PUSH_MODE) {
scheduleNextIdlePing(); scheduleNextIdlePing();
} }
} }
@ -1874,7 +1883,7 @@ public class XmppConnectionService extends Service {
if (connection.getFeatures().csi()) { if (connection.getFeatures().csi()) {
connection.sendInactive(); connection.sendInactive();
} }
if (Config.CLOSE_TCP_WHEN_SWITCHING_TO_BACKGROUND && mPushManagementService.available(account)) { if (Config.PUSH_MODE && mPushManagementService.available(account)) {
connection.waitForPush(); connection.waitForPush();
cancelWakeUpCall(account.getUuid().hashCode()); cancelWakeUpCall(account.getUuid().hashCode());
} }