diff --git a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java index 28f7e0077..e49db54d3 100644 --- a/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java +++ b/src/main/java/eu/siacs/conversations/persistance/UnifiedPushDatabase.java @@ -50,14 +50,14 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { sqLiteDatabase.beginTransaction(); final Optional existingApplication; try (final Cursor cursor = - sqLiteDatabase.query( - "push", - new String[] {"application"}, - "instance=?", - new String[] {instance}, - null, - null, - null)) { + sqLiteDatabase.query( + "push", + new String[] {"application"}, + "instance=?", + new String[] {instance}, + null, + null, + null)) { if (cursor != null && cursor.moveToFirst()) { existingApplication = Optional.of(cursor.getString(0)); } else { @@ -84,17 +84,18 @@ 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(), - new String[] {account, transport}, - null, - null, - null)) { + sqLiteDatabase.query( + "push", + new String[] {"application", "instance"}, + "account <> ? OR transport <> ? OR expiration < " + expiration, + new String[] {account, transport}, + null, + null, + null)) { while (cursor != null && cursor.moveToNext()) { renewalBuilder.add( new PushTarget( @@ -109,14 +110,15 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { final String account, final String transport, final String instance) { final SQLiteDatabase sqLiteDatabase = getReadableDatabase(); try (final Cursor cursor = - sqLiteDatabase.query( - "push", - new String[] {"application", "endpoint"}, - "account = ? AND transport = ? AND instance = ? ", - new String[] {account, transport, instance}, - null, - null, - null)) { + sqLiteDatabase.query( + "push", + new String[] {"application", "endpoint"}, + "account = ? AND transport = ? AND instance = ? AND endpoint IS NOT NULL AND expiration >= " + + System.currentTimeMillis(), + new String[] {account, transport, instance}, + null, + null, + null)) { if (cursor != null && cursor.moveToFirst()) { return new ApplicationEndpoint( cursor.getString(cursor.getColumnIndexOrThrow("application")), @@ -140,14 +142,14 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { sqLiteDatabase.beginTransaction(); final String existingEndpoint; try (final Cursor cursor = - sqLiteDatabase.query( - "push", - new String[] {"endpoint"}, - "instance=?", - new String[] {instance}, - null, - null, - null)) { + sqLiteDatabase.query( + "push", + new String[] {"endpoint"}, + "instance=?", + new String[] {instance}, + null, + null, + null)) { if (cursor != null && cursor.moveToFirst()) { existingEndpoint = cursor.getString(0); } else { @@ -169,14 +171,14 @@ public class UnifiedPushDatabase extends SQLiteOpenHelper { final ImmutableList.Builder renewalBuilder = ImmutableList.builder(); final SQLiteDatabase sqLiteDatabase = getReadableDatabase(); try (final Cursor cursor = - sqLiteDatabase.query( - "push", - new String[] {"application", "instance"}, - "account = ?", - new String[] {account}, - null, - null, - null)) { + sqLiteDatabase.query( + "push", + new String[] {"application", "instance"}, + "account = ?", + new String[] {account}, + null, + null, + null)) { while (cursor != null && cursor.moveToNext()) { renewalBuilder.add( new PushTarget( 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 98ea59419..9f20928d5 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -419,6 +419,7 @@ public class XmppConnectionService extends Service { connectMultiModeConversations(account); syncDirtyContacts(account); + unifiedPushBroker.renewUnifiedPushEndpointsOnBind(account); } }; private boolean destroyed = false;