diff --git a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java index 9b17406f7..e49db54d3 100644 --- a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java +++ b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java @@ -84,13 +84,14 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { public List getRenewals(final String account, final String transport) { final ImmutableList.Builder renewalBuilder = ImmutableList.builder(); + // TODO use a date somewhat in the future to account for period renewal triggers + final long expiration = System.currentTimeMillis(); final SQLiteDatabase sqLiteDatabase = getReadableDatabase(); try (final Cursor cursor = sqLiteDatabase.query( "push", new String[] {"application", "instance"}, - "account <> ? OR transport <> ? OR expiration < " - + System.currentTimeMillis(), + "account <> ? OR transport <> ? OR expiration < " + expiration, new String[] {account, transport}, null, null, @@ -112,7 +113,8 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { sqLiteDatabase.query( "push", new String[] {"application", "endpoint"}, - "account = ? AND transport = ? AND instance = ? ", + "account = ? AND transport = ? AND instance = ? AND endpoint IS NOT NULL AND expiration >= " + + System.currentTimeMillis(), new String[] {account, transport, instance}, null, null, diff --git a/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java b/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java index 101a09fc3..6e675cc27 100644 --- a/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java +++ b/src/main/java/eu/siacs/conversations/services/UnifiedPushBroker.java @@ -34,10 +34,28 @@ public class UnifiedPushBroker { this.service = xmppConnectionService; } + public void renewUnifiedPushEndpointsOnBind(final Account account) { + final Optional transport = getTransport(); + if (transport.isPresent()) { + final Account transportAccount = transport.get().account; + if (transportAccount != null && transportAccount.getUuid().equals(account.getUuid())) { + Log.d( + Config.LOGTAG, + account.getJid().asBareJid() + ": trigger endpoint renewal on bind"); + renewUnifiedEndpoint(transport.get()); + } + } + } + public Optional renewUnifiedPushEndpoints() { final Optional transportOptional = getTransport(); if (transportOptional.isPresent()) { - renewUnifiedEndpoint(transportOptional.get()); + final Transport transport = transportOptional.get(); + if (transport.account.isEnabled()) { + renewUnifiedEndpoint(transportOptional.get()); + } else { + Log.d(Config.LOGTAG, "skipping UnifiedPush endpoint renewal. Account is disabled"); + } } else { Log.d(Config.LOGTAG, "skipping UnifiedPush endpoint renewal. No transport selected"); } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 22e7b38e5..d3722053f 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -381,6 +381,7 @@ public class XmppConnectionService extends Service { connectMultiModeConversations(account); syncDirtyContacts(account); + unifiedPushBroker.renewUnifiedPushEndpointsOnBind(account); } }; private final AtomicLong mLastExpiryRun = new AtomicLong(0);