10
10
import android .os .RemoteException ;
11
11
import android .preference .PreferenceManager ;
12
12
import android .util .Log ;
13
+
14
+ import androidx .annotation .NonNull ;
15
+
13
16
import com .google .common .base .Optional ;
14
17
import com .google .common .base .Strings ;
15
18
import com .google .common .collect .Iterables ;
16
19
import com .google .common .io .BaseEncoding ;
20
+ import com .google .common .util .concurrent .FutureCallback ;
21
+ import com .google .common .util .concurrent .Futures ;
22
+ import com .google .common .util .concurrent .ListenableFuture ;
23
+ import com .google .common .util .concurrent .MoreExecutors ;
24
+
17
25
import eu .siacs .conversations .Config ;
18
26
import eu .siacs .conversations .R ;
19
27
import eu .siacs .conversations .entities .Account ;
@@ -239,6 +247,9 @@ private void sendEndpoint(final Messenger messenger, String instance, final Unif
239
247
public boolean reconfigurePushDistributor () {
240
248
final boolean enabled = getTransport ().isPresent ();
241
249
setUnifiedPushDistributorEnabled (enabled );
250
+ if (!enabled ) {
251
+ unregisterCurrentPushTargets ();
252
+ }
242
253
return enabled ;
243
254
}
244
255
@@ -261,6 +272,43 @@ private void setUnifiedPushDistributorEnabled(final boolean enabled) {
261
272
}
262
273
}
263
274
275
+ private void unregisterCurrentPushTargets () {
276
+ final var future = deletePushTargets ();
277
+ Futures .addCallback (
278
+ future ,
279
+ new FutureCallback <>() {
280
+ @ Override
281
+ public void onSuccess (
282
+ final List <UnifiedPushDatabase .PushTarget > pushTargets ) {
283
+ broadcastUnregistered (pushTargets );
284
+ }
285
+
286
+ @ Override
287
+ public void onFailure (@ NonNull Throwable throwable ) {
288
+ Log .d (
289
+ Config .LOGTAG ,
290
+ "could not delete endpoints after UnifiedPushDistributor was disabled" );
291
+ }
292
+ },
293
+ MoreExecutors .directExecutor ());
294
+ }
295
+
296
+ private ListenableFuture <List <UnifiedPushDatabase .PushTarget >> deletePushTargets () {
297
+ return Futures .submit (() -> UnifiedPushDatabase .getInstance (service ).deletePushTargets (),SCHEDULER );
298
+ }
299
+
300
+ private void broadcastUnregistered (final List <UnifiedPushDatabase .PushTarget > pushTargets ) {
301
+ for (final UnifiedPushDatabase .PushTarget pushTarget : pushTargets ) {
302
+ Log .d (Config .LOGTAG ,"sending unregistered to " +pushTarget );
303
+ broadcastUnregistered (pushTarget );
304
+ }
305
+ }
306
+
307
+ private void broadcastUnregistered (final UnifiedPushDatabase .PushTarget pushTarget ) {
308
+ final var intent = unregisteredIntent (pushTarget );
309
+ service .sendBroadcast (intent );
310
+ }
311
+
264
312
public boolean processPushMessage (
265
313
final Account account , final Jid transport , final Element push ) {
266
314
final String instance = push .getAttribute ("instance" );
@@ -355,6 +403,7 @@ private void broadcastPushMessage(
355
403
updateIntent .putExtra ("token" , target .instance );
356
404
updateIntent .putExtra ("bytesMessage" , payload );
357
405
updateIntent .putExtra ("message" , new String (payload , StandardCharsets .UTF_8 ));
406
+ // TODO add distributor verification?
358
407
service .sendBroadcast (updateIntent );
359
408
}
360
409
@@ -379,6 +428,19 @@ private Intent endpointIntent(final String instance, final UnifiedPushDatabase.A
379
428
return intent ;
380
429
}
381
430
431
+ private Intent unregisteredIntent (final UnifiedPushDatabase .PushTarget pushTarget ) {
432
+ final Intent intent = new Intent (UnifiedPushDistributor .ACTION_UNREGISTERED );
433
+ intent .setPackage (pushTarget .application );
434
+ intent .putExtra ("token" , pushTarget .instance );
435
+ final var distributorVerificationIntent = new Intent ();
436
+ distributorVerificationIntent .setPackage (service .getPackageName ());
437
+ final var pendingIntent =
438
+ PendingIntent .getBroadcast (
439
+ service , 0 , distributorVerificationIntent , PendingIntent .FLAG_IMMUTABLE );
440
+ intent .putExtra ("distributor" , pendingIntent );
441
+ return intent ;
442
+ }
443
+
382
444
public void rebroadcastEndpoint (final Messenger messenger , final String instance , final Transport transport ) {
383
445
final UnifiedPushDatabase unifiedPushDatabase = UnifiedPushDatabase .getInstance (service );
384
446
final UnifiedPushDatabase .ApplicationEndpoint endpoint =
0 commit comments