-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
LiveRecipient.java
275 lines (228 loc) · 10.1 KB
/
LiveRecipient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import com.annimon.stream.Stream;
import org.signal.core.util.ThreadUtil;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.conversation.colors.AvatarColor;
import org.thoughtcrime.securesms.database.CallLinkTable;
import org.thoughtcrime.securesms.database.DistributionListTables;
import org.thoughtcrime.securesms.database.GroupTable;
import org.thoughtcrime.securesms.database.model.GroupRecord;
import org.thoughtcrime.securesms.database.RecipientTable;
import org.thoughtcrime.securesms.database.SignalDatabase;
import org.thoughtcrime.securesms.database.model.DistributionListRecord;
import org.thoughtcrime.securesms.database.model.RecipientRecord;
import org.thoughtcrime.securesms.util.livedata.LiveDataUtil;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.subjects.BehaviorSubject;
public final class LiveRecipient {
private static final String TAG = Log.tag(LiveRecipient.class);
private final Context context;
private final MutableLiveData<Recipient> liveData;
private final LiveData<Recipient> observableLiveData;
private final LiveData<Recipient> observableLiveDataResolved;
private final Set<RecipientForeverObserver> observers;
private final Observer<Recipient> foreverObserver;
private final AtomicReference<Recipient> recipient;
private final RecipientTable recipientTable;
private final GroupTable groupDatabase;
private final DistributionListTables distributionListTables;
private final MutableLiveData<Object> refreshForceNotify;
private final BehaviorSubject<Recipient> subject;
LiveRecipient(@NonNull Context context, @NonNull Recipient defaultRecipient) {
this.context = context.getApplicationContext();
this.liveData = new MutableLiveData<>(defaultRecipient);
this.subject = BehaviorSubject.createDefault(defaultRecipient);
this.recipient = new AtomicReference<>(defaultRecipient);
this.recipientTable = SignalDatabase.recipients();
this.groupDatabase = SignalDatabase.groups();
this.distributionListTables = SignalDatabase.distributionLists();
this.observers = new CopyOnWriteArraySet<>();
this.foreverObserver = recipient -> {
ThreadUtil.postToMain(() -> {
for (RecipientForeverObserver o : observers) {
o.onRecipientChanged(recipient);
}
});
};
this.refreshForceNotify = new MutableLiveData<>(new Object());
this.observableLiveData = LiveDataUtil.combineLatest(LiveDataUtil.distinctUntilChanged(liveData, Recipient::hasSameContent),
refreshForceNotify,
(recipient, force) -> recipient);
this.observableLiveDataResolved = LiveDataUtil.filter(this.observableLiveData, r -> !r.isResolving());
}
public @NonNull RecipientId getId() {
return recipient.get().getId();
}
/**
* @return A recipient that may or may not be fully-resolved.
*/
public @NonNull Recipient get() {
return recipient.get();
}
/**
* @return An rx-flavored {@link Observable}.
*/
public @NonNull Observable<Recipient> observable() {
return subject;
}
/**
* Watch the recipient for changes. The callback will only be invoked if the provided lifecycle is
* in a valid state. No need to remove the observer. If you do wish to remove the observer (if,
* for instance, you wish to remove the listener before the end of the owner's lifecycle), you can
* use {@link #removeObservers(LifecycleOwner)}.
*/
public void observe(@NonNull LifecycleOwner owner, @NonNull Observer<Recipient> observer) {
ThreadUtil.postToMain(() -> observableLiveData.observe(owner, observer));
}
/**
* Removes all observers of this data registered for the given LifecycleOwner.
*/
public void removeObservers(@NonNull LifecycleOwner owner) {
ThreadUtil.runOnMain(() -> observableLiveData.removeObservers(owner));
}
/**
* Watch the recipient for changes. The callback could be invoked at any time. You MUST call
* {@link #removeForeverObserver(RecipientForeverObserver)} when finished. You should use
* {@link #observe(LifecycleOwner, Observer<Recipient>)} if possible, as it is lifecycle-safe.
*/
public void observeForever(@NonNull RecipientForeverObserver observer) {
ThreadUtil.postToMain(() -> {
if (observers.isEmpty()) {
observableLiveData.observeForever(foreverObserver);
}
observers.add(observer);
});
}
/**
* Unsubscribes the provided {@link RecipientForeverObserver} from future changes.
*/
public void removeForeverObserver(@NonNull RecipientForeverObserver observer) {
ThreadUtil.postToMain(() -> {
observers.remove(observer);
if (observers.isEmpty()) {
observableLiveData.removeObserver(foreverObserver);
}
});
}
/**
* @return A fully-resolved version of the recipient. May require reading from disk.
*/
@WorkerThread
public @NonNull Recipient resolve() {
Recipient current = recipient.get();
if (!current.isResolving() || current.getId().isUnknown()) {
return current;
}
if (ThreadUtil.isMainThread()) {
Log.w(TAG, "[Resolve][MAIN] " + getId(), new Throwable());
}
Recipient updated = fetchAndCacheRecipientFromDisk(getId());
set(updated);
return updated;
}
@WorkerThread
public void refresh() {
refresh(getId());
}
/**
* Forces a reload of the underlying recipient.
*/
@WorkerThread
public void refresh(@NonNull RecipientId id) {
if (!getId().equals(id)) {
Log.w(TAG, "Switching ID from " + getId() + " to " + id);
}
if (getId().isUnknown()) return;
if (ThreadUtil.isMainThread()) {
Log.w(TAG, "[Refresh][MAIN] " + id, new Throwable());
}
Recipient recipient = fetchAndCacheRecipientFromDisk(id);
set(recipient);
refreshForceNotify.postValue(new Object());
}
public @NonNull LiveData<Recipient> getLiveData() {
return observableLiveData;
}
public @NonNull LiveData<Recipient> getLiveDataResolved() {
return observableLiveDataResolved;
}
private @NonNull Recipient fetchAndCacheRecipientFromDisk(@NonNull RecipientId id) {
RecipientRecord record = recipientTable.getRecord(id);
RecipientDetails details;
if (record.getGroupId() != null) {
details = getGroupRecipientDetails(record);
} else if (record.getDistributionListId() != null) {
details = getDistributionListRecipientDetails(record);
} else if (record.getCallLinkRoomId() != null) {
details = getCallLinkRecipientDetails(record);
}else {
details = RecipientDetails.forIndividual(context, record);
}
Recipient recipient = new Recipient(record.getId(), details, true);
RecipientIdCache.INSTANCE.put(recipient);
return recipient;
}
@WorkerThread
private @NonNull RecipientDetails getGroupRecipientDetails(@NonNull RecipientRecord record) {
Optional<GroupRecord> groupRecord = groupDatabase.getGroup(record.getId());
if (groupRecord.isPresent()) {
String title = groupRecord.get().getTitle();
List<RecipientId> members = Stream.of(groupRecord.get().getMembers()).filterNot(RecipientId::isUnknown).toList();
Optional<Long> avatarId = Optional.empty();
if (groupRecord.get().hasAvatar()) {
avatarId = Optional.of(groupRecord.get().getAvatarId());
}
return new RecipientDetails(title, null, avatarId, false, false, record.getRegistered(), record, members, false, groupRecord.get().isActive(), null, groupRecord);
}
return new RecipientDetails(null, null, Optional.empty(), false, false, record.getRegistered(), record, null, false, false, null, Optional.empty());
}
@WorkerThread
private @NonNull RecipientDetails getDistributionListRecipientDetails(@NonNull RecipientRecord record) {
DistributionListRecord groupRecord = distributionListTables.getList(Objects.requireNonNull(record.getDistributionListId()));
// TODO [stories] We'll have to see what the perf is like for very large distribution lists. We may not be able to support fetching all the members.
if (groupRecord != null) {
String title = groupRecord.isUnknown() ? null : groupRecord.getName();
List<RecipientId> members = Stream.of(groupRecord.getMembers()).filterNot(RecipientId::isUnknown).toList();
return RecipientDetails.forDistributionList(title, members, record);
}
return RecipientDetails.forDistributionList(null, null, record);
}
@WorkerThread
private @NonNull RecipientDetails getCallLinkRecipientDetails(@NonNull RecipientRecord record) {
CallLinkTable.CallLink callLink = SignalDatabase.callLinks().getCallLinkByRoomId(Objects.requireNonNull(record.getCallLinkRoomId()));
if (callLink != null) {
String name = callLink.getState().getName();
return RecipientDetails.forCallLink(name, record, callLink.getAvatarColor());
}
return RecipientDetails.forCallLink(null, record, AvatarColor.UNKNOWN);
}
synchronized void set(@NonNull Recipient recipient) {
this.recipient.set(recipient);
this.liveData.postValue(recipient);
this.subject.onNext(recipient);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LiveRecipient that = (LiveRecipient) o;
return recipient.equals(that.recipient);
}
@Override
public int hashCode() {
return Objects.hash(recipient);
}
}