2013-05-30 12:39:56 -07:00
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
import android.annotation.SuppressLint;
|
2018-02-01 19:22:48 -08:00
|
|
|
import android.content.BroadcastReceiver;
|
2013-05-30 12:39:56 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.AsyncTask;
|
2016-10-10 11:13:37 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2016-12-25 15:27:59 -08:00
|
|
|
import android.support.v4.app.NotificationManagerCompat;
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2017-09-15 22:38:53 -07:00
|
|
|
import com.annimon.stream.Collectors;
|
|
|
|
import com.annimon.stream.Stream;
|
|
|
|
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2017-09-15 22:38:53 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2013-05-30 12:39:56 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2016-10-10 11:13:37 -07:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.ExpirationInfo;
|
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
|
|
|
import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob;
|
2017-09-15 22:38:53 -07:00
|
|
|
import org.thoughtcrime.securesms.jobs.SendReadReceiptJob;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2019-10-30 09:59:11 +11:00
|
|
|
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
|
2016-10-10 11:13:37 -07:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2019-10-31 11:36:52 +11:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2020-02-11 09:38:05 +11:00
|
|
|
import org.whispersystems.signalservice.loki.api.LokiFileServerAPI;
|
2016-02-19 17:07:41 -08:00
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-09-15 22:38:53 -07:00
|
|
|
import java.util.Map;
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2019-10-07 10:25:14 +11:00
|
|
|
import kotlin.Unit;
|
2019-10-31 11:36:52 +11:00
|
|
|
import kotlin.contracts.Returns;
|
2019-10-07 10:25:14 +11:00
|
|
|
|
2018-02-01 19:22:48 -08:00
|
|
|
public class MarkReadReceiver extends BroadcastReceiver {
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2016-12-25 15:27:59 -08:00
|
|
|
private static final String TAG = MarkReadReceiver.class.getSimpleName();
|
2020-03-12 09:52:42 +11:00
|
|
|
public static final String CLEAR_ACTION = "network.loki.securesms.notifications.CLEAR";
|
2016-12-25 15:27:59 -08:00
|
|
|
public static final String THREAD_IDS_EXTRA = "thread_ids";
|
|
|
|
public static final String NOTIFICATION_ID_EXTRA = "notification_id";
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
2013-05-30 12:39:56 -07:00
|
|
|
@Override
|
2018-02-01 19:22:48 -08:00
|
|
|
public void onReceive(final Context context, Intent intent) {
|
2015-06-13 20:23:30 -07:00
|
|
|
if (!CLEAR_ACTION.equals(intent.getAction()))
|
2013-05-30 12:39:56 -07:00
|
|
|
return;
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
final long[] threadIds = intent.getLongArrayExtra(THREAD_IDS_EXTRA);
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
if (threadIds != null) {
|
2016-12-25 15:27:59 -08:00
|
|
|
NotificationManagerCompat.from(context).cancel(intent.getIntExtra(NOTIFICATION_ID_EXTRA, -1));
|
2013-05-30 12:39:56 -07:00
|
|
|
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
2016-10-10 11:13:37 -07:00
|
|
|
List<MarkedMessageInfo> messageIdsCollection = new LinkedList<>();
|
2016-02-19 17:07:41 -08:00
|
|
|
|
2014-01-18 18:25:51 -08:00
|
|
|
for (long threadId : threadIds) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Marking as read: " + threadId);
|
2017-02-22 15:05:35 -08:00
|
|
|
List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setRead(threadId, true);
|
2016-02-19 17:07:41 -08:00
|
|
|
messageIdsCollection.addAll(messageIds);
|
2014-01-18 18:25:51 -08:00
|
|
|
}
|
2013-05-30 12:39:56 -07:00
|
|
|
|
2016-10-10 11:13:37 -07:00
|
|
|
process(context, messageIdsCollection);
|
2016-02-19 17:07:41 -08:00
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
MessageNotifier.updateNotification(context);
|
2016-02-19 17:07:41 -08:00
|
|
|
|
2013-05-30 12:39:56 -07:00
|
|
|
return null;
|
|
|
|
}
|
2017-10-23 13:03:32 -07:00
|
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2013-05-30 12:39:56 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-10 11:13:37 -07:00
|
|
|
|
|
|
|
public static void process(@NonNull Context context, @NonNull List<MarkedMessageInfo> markedReadMessages) {
|
|
|
|
if (markedReadMessages.isEmpty()) return;
|
|
|
|
|
|
|
|
List<SyncMessageId> syncMessageIds = new LinkedList<>();
|
|
|
|
|
|
|
|
for (MarkedMessageInfo messageInfo : markedReadMessages) {
|
|
|
|
scheduleDeletion(context, messageInfo.getExpirationInfo());
|
2019-10-23 15:29:56 +11:00
|
|
|
if (!messageInfo.getSyncMessageId().getAddress().isGroup()) {
|
|
|
|
syncMessageIds.add(messageInfo.getSyncMessageId());
|
|
|
|
}
|
2016-10-10 11:13:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
2019-03-28 08:56:35 -07:00
|
|
|
.add(new MultiDeviceReadUpdateJob(syncMessageIds));
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
Map<Address, List<SyncMessageId>> addressMap = Stream.of(markedReadMessages)
|
|
|
|
.map(MarkedMessageInfo::getSyncMessageId)
|
|
|
|
.collect(Collectors.groupingBy(SyncMessageId::getAddress));
|
|
|
|
|
|
|
|
for (Address address : addressMap.keySet()) {
|
|
|
|
List<Long> timestamps = Stream.of(addressMap.get(address)).map(SyncMessageId::getTimetamp).toList();
|
2019-10-31 11:36:52 +11:00
|
|
|
MultiDeviceUtilities.getAllDevicePublicKeysWithFriendStatus(context, address.serialize()).success(devices -> {
|
|
|
|
for (Map.Entry<String, Boolean> entry : devices.entrySet()) {
|
|
|
|
String device = entry.getKey();
|
|
|
|
boolean isFriend = entry.getValue();
|
|
|
|
// Loki - This also prevents read receipts from being sent in group chats as they don't maintain a friend request status
|
|
|
|
if (isFriend) {
|
|
|
|
Util.runOnMain(() -> ApplicationContext.getInstance(context).getJobManager().add(new SendReadReceiptJob(Address.fromSerialized(device), timestamps)));
|
|
|
|
}
|
2019-10-07 10:25:14 +11:00
|
|
|
}
|
2019-10-31 11:36:52 +11:00
|
|
|
return Unit.INSTANCE;
|
|
|
|
});
|
2017-09-15 22:38:53 -07:00
|
|
|
}
|
2016-10-10 11:13:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void scheduleDeletion(Context context, ExpirationInfo expirationInfo) {
|
|
|
|
if (expirationInfo.getExpiresIn() > 0 && expirationInfo.getExpireStarted() <= 0) {
|
|
|
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
|
|
|
|
if (expirationInfo.isMms()) DatabaseFactory.getMmsDatabase(context).markExpireStarted(expirationInfo.getId());
|
|
|
|
else DatabaseFactory.getSmsDatabase(context).markExpireStarted(expirationInfo.getId());
|
|
|
|
|
|
|
|
expirationManager.scheduleDeletion(expirationInfo.getId(), expirationInfo.isMms(), expirationInfo.getExpiresIn());
|
|
|
|
}
|
|
|
|
}
|
2013-05-30 12:39:56 -07:00
|
|
|
}
|