mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-22 18:00:02 +01:00
Introduce SignalDatabase as the main database entrypoint.
This commit is contained in:
@@ -13,11 +13,11 @@ import com.annimon.stream.Stream;
|
||||
|
||||
import org.signal.core.util.ThreadUtil;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientSettings;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.util.livedata.LiveDataUtil;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
|
||||
@@ -46,8 +46,8 @@ public final class LiveRecipient {
|
||||
this.context = context.getApplicationContext();
|
||||
this.liveData = new MutableLiveData<>(defaultRecipient);
|
||||
this.recipient = new AtomicReference<>(defaultRecipient);
|
||||
this.recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
||||
this.groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
||||
this.recipientDatabase = SignalDatabase.recipients();
|
||||
this.groupDatabase = SignalDatabase.groups();
|
||||
this.observers = new CopyOnWriteArraySet<>();
|
||||
this.foreverObserver = recipient -> {
|
||||
ThreadUtil.postToMain(() -> {
|
||||
|
||||
@@ -11,18 +11,16 @@ import net.zetetic.database.sqlcipher.SQLiteDatabase;
|
||||
|
||||
import org.signal.core.util.ThreadUtil;
|
||||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.signal.core.util.concurrent.TracingExecutorService;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.MissingRecipientException;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
||||
import org.thoughtcrime.securesms.util.CursorUtil;
|
||||
import org.thoughtcrime.securesms.util.LRUCache;
|
||||
import org.thoughtcrime.securesms.util.Stopwatch;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.concurrent.FilteredExecutor;
|
||||
import org.whispersystems.signalservice.api.push.ACI;
|
||||
|
||||
@@ -55,12 +53,12 @@ public final class LiveRecipientCache {
|
||||
@SuppressLint("UseSparseArrays")
|
||||
public LiveRecipientCache(@NonNull Context context) {
|
||||
this.context = context.getApplicationContext();
|
||||
this.recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
|
||||
this.recipientDatabase = SignalDatabase.recipients();
|
||||
this.recipients = new LRUCache<>(CACHE_MAX);
|
||||
this.warmedUp = new AtomicBoolean(false);
|
||||
this.localRecipientId = new AtomicReference<>(null);
|
||||
this.unknown = new LiveRecipient(context, Recipient.UNKNOWN);
|
||||
this.db = DatabaseFactory.getInstance(context).getRawDatabase();
|
||||
this.db = SignalDatabase.getRawDatabase();
|
||||
this.resolveExecutor = ThreadUtil.trace(new FilteredExecutor(SignalExecutors.BOUNDED, () -> !db.inTransaction()));
|
||||
}
|
||||
|
||||
@@ -187,7 +185,7 @@ public final class LiveRecipientCache {
|
||||
Stopwatch stopwatch = new Stopwatch("recipient-warm-up");
|
||||
|
||||
SignalExecutors.BOUNDED.execute(() -> {
|
||||
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
||||
ThreadDatabase threadDatabase = SignalDatabase.threads();
|
||||
List<Recipient> recipients = new ArrayList<>();
|
||||
|
||||
try (ThreadDatabase.Reader reader = threadDatabase.readerFor(threadDatabase.getRecentConversationList(THREAD_CACHE_WARM_MAX, false, false))) {
|
||||
@@ -206,7 +204,7 @@ public final class LiveRecipientCache {
|
||||
stopwatch.split("thread");
|
||||
|
||||
if (SignalStore.registrationValues().isRegistrationComplete()) {
|
||||
try (Cursor cursor = DatabaseFactory.getRecipientDatabase(context).getNonGroupContacts(false)) {
|
||||
try (Cursor cursor = SignalDatabase.recipients().getNonGroupContacts(false)) {
|
||||
int count = 0;
|
||||
while (cursor != null && cursor.moveToNext() && count < CONTACT_CACHE_WARM_MAX) {
|
||||
RecipientId id = RecipientId.from(CursorUtil.requireLong(cursor, RecipientDatabase.ID));
|
||||
|
||||
@@ -27,12 +27,12 @@ import org.thoughtcrime.securesms.contacts.avatars.TransparentContactPhoto;
|
||||
import org.thoughtcrime.securesms.conversation.colors.AvatarColor;
|
||||
import org.thoughtcrime.securesms.conversation.colors.ChatColors;
|
||||
import org.thoughtcrime.securesms.conversation.colors.ChatColorsPalette;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.MentionSetting;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.UnidentifiedAccessMode;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.VibrateState;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.model.databaseprotos.RecipientExtras;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
@@ -165,7 +165,7 @@ public class Recipient {
|
||||
@WorkerThread
|
||||
public static @NonNull Recipient externalUsername(@NonNull Context context, @NonNull ACI aci, @NonNull String username) {
|
||||
Recipient recipient = externalPush(context, aci, null, false);
|
||||
DatabaseFactory.getRecipientDatabase(context).setUsername(recipient.getId(), username);
|
||||
SignalDatabase.recipients().setUsername(recipient.getId(), username);
|
||||
return recipient;
|
||||
}
|
||||
|
||||
@@ -225,7 +225,7 @@ public class Recipient {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
|
||||
RecipientDatabase db = SignalDatabase.recipients();
|
||||
RecipientId recipientId = db.getAndPossiblyMerge(aci, e164, highTrust);
|
||||
|
||||
Recipient resolved = resolved(recipientId);
|
||||
@@ -249,7 +249,7 @@ public class Recipient {
|
||||
*/
|
||||
@WorkerThread
|
||||
public static @NonNull Recipient externalContact(@NonNull Context context, @NonNull String identifier) {
|
||||
RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
|
||||
RecipientDatabase db = SignalDatabase.recipients();
|
||||
RecipientId id = null;
|
||||
|
||||
if (UuidUtil.isUuid(identifier)) {
|
||||
@@ -274,7 +274,7 @@ public class Recipient {
|
||||
*/
|
||||
@WorkerThread
|
||||
public static @NonNull Recipient externalGroupExact(@NonNull Context context, @NonNull GroupId groupId) {
|
||||
return Recipient.resolved(DatabaseFactory.getRecipientDatabase(context).getOrInsertFromGroupId(groupId));
|
||||
return Recipient.resolved(SignalDatabase.recipients().getOrInsertFromGroupId(groupId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,7 +289,7 @@ public class Recipient {
|
||||
*/
|
||||
@WorkerThread
|
||||
public static @NonNull Recipient externalPossiblyMigratedGroup(@NonNull Context context, @NonNull GroupId groupId) {
|
||||
return Recipient.resolved(DatabaseFactory.getRecipientDatabase(context).getOrInsertFromPossiblyMigratedGroupId(groupId));
|
||||
return Recipient.resolved(SignalDatabase.recipients().getOrInsertFromPossiblyMigratedGroupId(groupId));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -305,7 +305,7 @@ public class Recipient {
|
||||
public static @NonNull Recipient external(@NonNull Context context, @NonNull String identifier) {
|
||||
Preconditions.checkNotNull(identifier, "Identifier cannot be null!");
|
||||
|
||||
RecipientDatabase db = DatabaseFactory.getRecipientDatabase(context);
|
||||
RecipientDatabase db = SignalDatabase.recipients();
|
||||
RecipientId id = null;
|
||||
|
||||
if (UuidUtil.isUuid(identifier)) {
|
||||
|
||||
@@ -10,9 +10,9 @@ import com.annimon.stream.Stream;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase.RegisteredState;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.groups.GroupChangeBusyException;
|
||||
@@ -156,11 +156,11 @@ public class RecipientUtil {
|
||||
GroupManager.leaveGroupFromBlockOrMessageRequest(context, recipient.getGroupId().get().requirePush());
|
||||
}
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setBlocked(recipient.getId(), true);
|
||||
SignalDatabase.recipients().setBlocked(recipient.getId(), true);
|
||||
|
||||
if (recipient.isSystemContact() || recipient.isProfileSharing() || isProfileSharedViaGroup(context, recipient)) {
|
||||
ApplicationDependencies.getJobManager().add(new RotateProfileKeyJob());
|
||||
DatabaseFactory.getRecipientDatabase(context).setProfileSharing(recipient.getId(), false);
|
||||
SignalDatabase.recipients().setProfileSharing(recipient.getId(), false);
|
||||
}
|
||||
|
||||
ApplicationDependencies.getJobManager().add(new MultiDeviceBlockedUpdateJob());
|
||||
@@ -173,8 +173,8 @@ public class RecipientUtil {
|
||||
throw new AssertionError("Recipient is not blockable!");
|
||||
}
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setBlocked(recipient.getId(), false);
|
||||
DatabaseFactory.getRecipientDatabase(context).setProfileSharing(recipient.getId(), true);
|
||||
SignalDatabase.recipients().setBlocked(recipient.getId(), false);
|
||||
SignalDatabase.recipients().setProfileSharing(recipient.getId(), true);
|
||||
ApplicationDependencies.getJobManager().add(new MultiDeviceBlockedUpdateJob());
|
||||
StorageSyncHelper.scheduleSyncForDataChange();
|
||||
|
||||
@@ -196,7 +196,7 @@ public class RecipientUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
||||
ThreadDatabase threadDatabase = SignalDatabase.threads();
|
||||
Recipient threadRecipient = threadDatabase.getRecipientForThreadId(threadId);
|
||||
|
||||
if (threadRecipient == null) {
|
||||
@@ -215,7 +215,7 @@ public class RecipientUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
Long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient.getId());
|
||||
Long threadId = SignalDatabase.threads().getThreadIdFor(threadRecipient.getId());
|
||||
return isMessageRequestAccepted(context, threadId, threadRecipient);
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ public class RecipientUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
Long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(threadRecipient.getId());
|
||||
Long threadId = SignalDatabase.threads().getThreadIdFor(threadRecipient.getId());
|
||||
return isCallRequestAccepted(context, threadId, threadRecipient);
|
||||
}
|
||||
|
||||
@@ -239,7 +239,7 @@ public class RecipientUtil {
|
||||
@WorkerThread
|
||||
public static boolean isPreMessageRequestThread(@NonNull Context context, @Nullable Long threadId) {
|
||||
long beforeTime = SignalStore.misc().getMessageRequestEnableTime();
|
||||
return threadId != null && DatabaseFactory.getMmsSmsDatabase(context).getConversationCount(threadId, beforeTime) > 0;
|
||||
return threadId != null && SignalDatabase.mmsSms().getConversationCount(threadId, beforeTime) > 0;
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
@@ -248,16 +248,16 @@ public class RecipientUtil {
|
||||
return;
|
||||
}
|
||||
|
||||
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdIfExistsFor(recipient.getId());
|
||||
long threadId = SignalDatabase.threads().getThreadIdIfExistsFor(recipient.getId());
|
||||
|
||||
if (isPreMessageRequestThread(context, threadId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean firstMessage = DatabaseFactory.getMmsSmsDatabase(context).getOutgoingSecureConversationCount(threadId) == 0;
|
||||
boolean firstMessage = SignalDatabase.mmsSms().getOutgoingSecureConversationCount(threadId) == 0;
|
||||
|
||||
if (firstMessage) {
|
||||
DatabaseFactory.getRecipientDatabase(context).setProfileSharing(recipient.getId(), true);
|
||||
SignalDatabase.recipients().setProfileSharing(recipient.getId(), true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,7 +280,7 @@ public class RecipientUtil {
|
||||
if (recipient.isProfileSharing()) {
|
||||
return true;
|
||||
} else {
|
||||
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
||||
GroupDatabase groupDatabase = SignalDatabase.groups();
|
||||
return groupDatabase.getPushGroupsContainingMember(recipient.getId())
|
||||
.stream()
|
||||
.anyMatch(GroupDatabase.GroupRecord::isV2Group);
|
||||
@@ -299,10 +299,10 @@ public class RecipientUtil {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (threadId == -1 || !DatabaseFactory.getMmsSmsDatabase(context).hasMeaningfulMessage(threadId)) {
|
||||
DatabaseFactory.getRecipientDatabase(context).setExpireMessages(recipient.getId(), defaultTimer);
|
||||
if (threadId == -1 || !SignalDatabase.mmsSms().hasMeaningfulMessage(threadId)) {
|
||||
SignalDatabase.recipients().setExpireMessages(recipient.getId(), defaultTimer);
|
||||
OutgoingExpirationUpdateMessage outgoingMessage = new OutgoingExpirationUpdateMessage(recipient, System.currentTimeMillis(), defaultTimer * 1000L);
|
||||
MessageSender.send(context, outgoingMessage, DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(recipient), false, null, null);
|
||||
MessageSender.send(context, outgoingMessage, SignalDatabase.threads().getOrCreateThreadIdFor(recipient), false, null, null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -331,7 +331,7 @@ public class RecipientUtil {
|
||||
|
||||
@WorkerThread
|
||||
public static boolean hasSentMessageInThread(@NonNull Context context, @Nullable Long threadId) {
|
||||
return threadId != null && DatabaseFactory.getMmsSmsDatabase(context).getOutgoingSecureConversationCount(threadId) != 0;
|
||||
return threadId != null && SignalDatabase.mmsSms().getOutgoingSecureConversationCount(threadId) != 0;
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
@@ -340,13 +340,13 @@ public class RecipientUtil {
|
||||
return true;
|
||||
}
|
||||
|
||||
return DatabaseFactory.getMmsSmsDatabase(context).getSecureConversationCount(threadId) == 0 &&
|
||||
!DatabaseFactory.getThreadDatabase(context).hasReceivedAnyCallsSince(threadId, 0);
|
||||
return SignalDatabase.mmsSms().getSecureConversationCount(threadId) == 0 &&
|
||||
!SignalDatabase.threads().hasReceivedAnyCallsSince(threadId, 0);
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private static boolean isProfileSharedViaGroup(@NonNull Context context, @NonNull Recipient recipient) {
|
||||
return Stream.of(DatabaseFactory.getGroupDatabase(context).getPushGroupsContainingMember(recipient.getId()))
|
||||
return Stream.of(SignalDatabase.groups().getPushGroupsContainingMember(recipient.getId()))
|
||||
.anyMatch(group -> Recipient.resolved(group.getRecipientId()).isProfileSharing());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,8 @@ import androidx.core.util.Consumer;
|
||||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.contacts.sync.DirectoryHelper;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||
import org.thoughtcrime.securesms.database.IdentityDatabase;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.model.IdentityRecord;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.groups.GroupChangeException;
|
||||
@@ -107,7 +106,7 @@ final class RecipientDialogRepository {
|
||||
void getGroupMembership(@NonNull Consumer<List<RecipientId>> onComplete) {
|
||||
SimpleTask.run(SignalExecutors.UNBOUNDED,
|
||||
() -> {
|
||||
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
||||
GroupDatabase groupDatabase = SignalDatabase.groups();
|
||||
List<GroupDatabase.GroupRecord> groupRecords = groupDatabase.getPushGroupsContainingMember(recipientId);
|
||||
ArrayList<RecipientId> groupRecipients = new ArrayList<>(groupRecords.size());
|
||||
|
||||
@@ -121,7 +120,7 @@ final class RecipientDialogRepository {
|
||||
}
|
||||
|
||||
public void getActiveGroupCount(@NonNull Consumer<Integer> onComplete) {
|
||||
SignalExecutors.BOUNDED.execute(() -> onComplete.accept(DatabaseFactory.getGroupDatabase(context).getActiveGroupCount()));
|
||||
SignalExecutors.BOUNDED.execute(() -> onComplete.accept(SignalDatabase.groups().getActiveGroupCount()));
|
||||
}
|
||||
|
||||
interface RecipientCallback {
|
||||
|
||||
@@ -7,7 +7,7 @@ import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.signal.storageservice.protos.groups.AccessControl;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.groups.GroupChangeBusyException;
|
||||
import org.thoughtcrime.securesms.groups.GroupChangeFailedException;
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
@@ -63,13 +63,13 @@ final class ShareableGroupLinkRepository {
|
||||
|
||||
@WorkerThread
|
||||
private GroupManager.GroupLinkState toggleGroupLinkState(boolean toggleEnabled, boolean toggleApprovalNeeded) {
|
||||
AccessControl.AccessRequired currentState = DatabaseFactory.getGroupDatabase(context)
|
||||
.getGroup(groupId)
|
||||
.get()
|
||||
.requireV2GroupProperties()
|
||||
.getDecryptedGroup()
|
||||
.getAccessControl()
|
||||
.getAddFromInviteLink();
|
||||
AccessControl.AccessRequired currentState = SignalDatabase.groups()
|
||||
.getGroup(groupId)
|
||||
.get()
|
||||
.requireV2GroupProperties()
|
||||
.getDecryptedGroup()
|
||||
.getAccessControl()
|
||||
.getAddFromInviteLink();
|
||||
|
||||
boolean enabled;
|
||||
boolean approvalNeeded;
|
||||
|
||||
Reference in New Issue
Block a user