From d60d67ee7e46e3c4f8b74637f031de1964e6cb6b Mon Sep 17 00:00:00 2001 From: Greyson Parrelli <37311915+greyson-signal@users.noreply.github.com> Date: Wed, 10 Jun 2020 07:49:22 -0700 Subject: [PATCH] Set contact colors more aggressively. --- .../securesms/database/RecipientDatabase.java | 41 ++++++++++++++++--- .../securesms/recipients/Recipient.java | 12 ++++-- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java index 59448aa2209..7aaf6345be8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/app/src/main/java/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -19,6 +19,7 @@ import org.signal.zkgroup.profiles.ProfileKey; import org.signal.zkgroup.profiles.ProfileKeyCredential; import org.thoughtcrime.securesms.color.MaterialColor; +import org.thoughtcrime.securesms.contacts.avatars.ContactColors; import org.thoughtcrime.securesms.crypto.ProfileKeyUtil; import org.thoughtcrime.securesms.database.IdentityDatabase.IdentityRecord; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; @@ -518,10 +519,11 @@ public void applyStorageSyncUpdates(@NonNull Collection try { for (SignalContactRecord insert : contactInserts) { - ContentValues values = validateContactValuesForInsert(getValuesForStorageContact(insert)); + ContentValues values = validateContactValuesForInsert(getValuesForStorageContact(insert, true)); long id = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE); if (id < 0) { + values = validateContactValuesForInsert(getValuesForStorageContact(insert, false)); Log.w(TAG, "Failed to insert! It's likely that these were newly-registered users that were missed in the merge. Doing an update instead."); if (insert.getAddress().getNumber().isPresent()) { @@ -550,7 +552,7 @@ public void applyStorageSyncUpdates(@NonNull Collection } for (RecordUpdate update : contactUpdates) { - ContentValues values = getValuesForStorageContact(update.getNew()); + ContentValues values = getValuesForStorageContact(update.getNew(), false); int updateCount = db.update(TABLE_NAME, values, STORAGE_SERVICE_ID + " = ?", new String[]{Base64.encodeBytes(update.getOld().getId().getRaw())}); if (updateCount < 1) { @@ -708,7 +710,7 @@ public void updatePhoneNumbers(@NonNull Map mapping) { } } - private static @NonNull ContentValues getValuesForStorageContact(@NonNull SignalContactRecord contact) { + private static @NonNull ContentValues getValuesForStorageContact(@NonNull SignalContactRecord contact, boolean isInsert) { ContentValues values = new ContentValues(); if (contact.getAddress().getUuid().isPresent()) { @@ -728,6 +730,11 @@ public void updatePhoneNumbers(@NonNull Map mapping) { values.put(BLOCKED, contact.isBlocked() ? "1" : "0"); values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(contact.getId().getRaw())); values.put(DIRTY, DirtyState.CLEAN.getId()); + + if (contact.isProfileSharingEnabled() && isInsert) { + values.put(COLOR, ContactColors.generateFor(profileName.toString()).serialize()); + } + return values; } @@ -930,6 +937,23 @@ public void setColor(@NonNull RecipientId id, @NonNull MaterialColor color) { } } + public void setColorIfNotSet(@NonNull RecipientId id, @NonNull MaterialColor color) { + if (setColorIfNotSetInternal(id, color)) { + Recipient.live(id).refresh(); + } + } + + private boolean setColorIfNotSetInternal(@NonNull RecipientId id, @NonNull MaterialColor color) { + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + String query = ID + " = ? AND " + COLOR + " IS NULL"; + String[] args = new String[]{ id.serialize() }; + + ContentValues values = new ContentValues(); + values.put(COLOR, color.serialize()); + + return db.update(TABLE_NAME, values, query, args) > 0; + } + public void setDefaultSubscriptionId(@NonNull RecipientId id, int defaultSubscriptionId) { ContentValues values = new ContentValues(); values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId); @@ -1208,7 +1232,11 @@ public void setProfileAvatar(@NonNull RecipientId id, @Nullable String profileAv public void setProfileSharing(@NonNull RecipientId id, @SuppressWarnings("SameParameterValue") boolean enabled) { ContentValues contentValues = new ContentValues(1); contentValues.put(PROFILE_SHARING, enabled ? 1 : 0); - if (update(id, contentValues)) { + + boolean profiledUpdated = update(id, contentValues); + boolean colorUpdated = enabled && setColorIfNotSetInternal(id, ContactColors.generateFor(Recipient.resolved(id).getDisplayName(context))); + + if (profiledUpdated || colorUpdated) { markDirty(id, DirtyState.UPDATE); Recipient.live(id).refresh(); StorageSyncHelper.scheduleSyncForDataChange(); @@ -1772,7 +1800,10 @@ public void setSystemContactInfo(@NonNull RecipientId id, refreshQualifyingValues.put(SYSTEM_PHONE_TYPE, systemPhoneType); refreshQualifyingValues.put(SYSTEM_CONTACT_URI, systemContactUri); - if (update(id, refreshQualifyingValues)) { + boolean updatedValues = update(id, refreshQualifyingValues); + boolean updatedColor = displayName != null && setColorIfNotSetInternal(id, ContactColors.generateFor(displayName)); + + if (updatedValues || updatedColor) { pendingContactInfoMap.put(id, new PendingContactInfo(displayName, photoUri, systemPhoneLabel, systemContactUri)); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java b/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java index fddb0084a83..28eccf965ba 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/Recipient.java @@ -37,6 +37,7 @@ import org.thoughtcrime.securesms.profiles.ProfileName; import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.Util; +import org.thoughtcrime.securesms.util.concurrent.SignalExecutors; import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Preconditions; import org.whispersystems.signalservice.api.push.SignalServiceAddress; @@ -447,10 +448,13 @@ public boolean hasAUserSetDisplayName(@NonNull Context context) { return MaterialColor.GROUP; } else if (color != null) { return color; - } else if (name != null) { - Log.i(TAG, "Saving color for " + id); - MaterialColor color = ContactColors.generateFor(name); - DatabaseFactory.getRecipientDatabase(ApplicationDependencies.getApplication()).setColor(id, color); + } else if (name != null || profileSharing) { + Log.w(TAG, "Had no color for " + id + "! Saving a new one."); + + Context context = ApplicationDependencies.getApplication(); + MaterialColor color = ContactColors.generateFor(getDisplayName(context)); + + SignalExecutors.BOUNDED.execute(() -> DatabaseFactory.getRecipientDatabase(context).setColorIfNotSet(id, color)); return color; } else { return ContactColors.UNKNOWN_COLOR;