Set contact colors more aggressively.

This commit is contained in:
Greyson Parrelli 2020-06-10 07:49:22 -07:00 committed by GitHub
parent 559aa687a5
commit d60d67ee7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 9 deletions

View File

@ -19,6 +19,7 @@ import org.signal.zkgroup.groups.GroupMasterKey;
import org.signal.zkgroup.profiles.ProfileKey; import org.signal.zkgroup.profiles.ProfileKey;
import org.signal.zkgroup.profiles.ProfileKeyCredential; import org.signal.zkgroup.profiles.ProfileKeyCredential;
import org.thoughtcrime.securesms.color.MaterialColor; import org.thoughtcrime.securesms.color.MaterialColor;
import org.thoughtcrime.securesms.contacts.avatars.ContactColors;
import org.thoughtcrime.securesms.crypto.ProfileKeyUtil; import org.thoughtcrime.securesms.crypto.ProfileKeyUtil;
import org.thoughtcrime.securesms.database.IdentityDatabase.IdentityRecord; import org.thoughtcrime.securesms.database.IdentityDatabase.IdentityRecord;
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper; import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper;
@ -518,10 +519,11 @@ public class RecipientDatabase extends Database {
try { try {
for (SignalContactRecord insert : contactInserts) { 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); long id = db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
if (id < 0) { 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."); 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()) { if (insert.getAddress().getNumber().isPresent()) {
@ -550,7 +552,7 @@ public class RecipientDatabase extends Database {
} }
for (RecordUpdate<SignalContactRecord> update : contactUpdates) { 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())}); int updateCount = db.update(TABLE_NAME, values, STORAGE_SERVICE_ID + " = ?", new String[]{Base64.encodeBytes(update.getOld().getId().getRaw())});
if (updateCount < 1) { if (updateCount < 1) {
@ -708,7 +710,7 @@ public class RecipientDatabase extends Database {
} }
} }
private static @NonNull ContentValues getValuesForStorageContact(@NonNull SignalContactRecord contact) { private static @NonNull ContentValues getValuesForStorageContact(@NonNull SignalContactRecord contact, boolean isInsert) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
if (contact.getAddress().getUuid().isPresent()) { if (contact.getAddress().getUuid().isPresent()) {
@ -728,6 +730,11 @@ public class RecipientDatabase extends Database {
values.put(BLOCKED, contact.isBlocked() ? "1" : "0"); values.put(BLOCKED, contact.isBlocked() ? "1" : "0");
values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(contact.getId().getRaw())); values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(contact.getId().getRaw()));
values.put(DIRTY, DirtyState.CLEAN.getId()); values.put(DIRTY, DirtyState.CLEAN.getId());
if (contact.isProfileSharingEnabled() && isInsert) {
values.put(COLOR, ContactColors.generateFor(profileName.toString()).serialize());
}
return values; return values;
} }
@ -930,6 +937,23 @@ public class RecipientDatabase extends Database {
} }
} }
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) { public void setDefaultSubscriptionId(@NonNull RecipientId id, int defaultSubscriptionId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId); values.put(DEFAULT_SUBSCRIPTION_ID, defaultSubscriptionId);
@ -1208,7 +1232,11 @@ public class RecipientDatabase extends Database {
public void setProfileSharing(@NonNull RecipientId id, @SuppressWarnings("SameParameterValue") boolean enabled) { public void setProfileSharing(@NonNull RecipientId id, @SuppressWarnings("SameParameterValue") boolean enabled) {
ContentValues contentValues = new ContentValues(1); ContentValues contentValues = new ContentValues(1);
contentValues.put(PROFILE_SHARING, enabled ? 1 : 0); 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); markDirty(id, DirtyState.UPDATE);
Recipient.live(id).refresh(); Recipient.live(id).refresh();
StorageSyncHelper.scheduleSyncForDataChange(); StorageSyncHelper.scheduleSyncForDataChange();
@ -1772,7 +1800,10 @@ public class RecipientDatabase extends Database {
refreshQualifyingValues.put(SYSTEM_PHONE_TYPE, systemPhoneType); refreshQualifyingValues.put(SYSTEM_PHONE_TYPE, systemPhoneType);
refreshQualifyingValues.put(SYSTEM_CONTACT_URI, systemContactUri); 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)); pendingContactInfoMap.put(id, new PendingContactInfo(displayName, photoUri, systemPhoneLabel, systemContactUri));
} }

View File

@ -37,6 +37,7 @@ import org.thoughtcrime.securesms.phonenumbers.PhoneNumberFormatter;
import org.thoughtcrime.securesms.profiles.ProfileName; import org.thoughtcrime.securesms.profiles.ProfileName;
import org.thoughtcrime.securesms.util.FeatureFlags; import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.Util; 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.Optional;
import org.whispersystems.libsignal.util.guava.Preconditions; import org.whispersystems.libsignal.util.guava.Preconditions;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
@ -447,10 +448,13 @@ public class Recipient {
return MaterialColor.GROUP; return MaterialColor.GROUP;
} else if (color != null) { } else if (color != null) {
return color; return color;
} else if (name != null) { } else if (name != null || profileSharing) {
Log.i(TAG, "Saving color for " + id); Log.w(TAG, "Had no color for " + id + "! Saving a new one.");
MaterialColor color = ContactColors.generateFor(name);
DatabaseFactory.getRecipientDatabase(ApplicationDependencies.getApplication()).setColor(id, color); Context context = ApplicationDependencies.getApplication();
MaterialColor color = ContactColors.generateFor(getDisplayName(context));
SignalExecutors.BOUNDED.execute(() -> DatabaseFactory.getRecipientDatabase(context).setColorIfNotSet(id, color));
return color; return color;
} else { } else {
return ContactColors.UNKNOWN_COLOR; return ContactColors.UNKNOWN_COLOR;