Skip to content

Commit

Permalink
Set contact colors more aggressively.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jun 10, 2020
1 parent 559aa68 commit d60d67e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -518,10 +519,11 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>
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()) {
Expand Down Expand Up @@ -550,7 +552,7 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>
}

for (RecordUpdate<SignalContactRecord> 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) {
Expand Down Expand Up @@ -708,7 +710,7 @@ public void updatePhoneNumbers(@NonNull Map<String, String> 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()) {
Expand All @@ -728,6 +730,11 @@ public void updatePhoneNumbers(@NonNull Map<String, String> 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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d60d67e

Please sign in to comment.