mirror of
https://github.com/oxen-io/session-android.git
synced 2025-12-03 11:03:07 +00:00
@@ -35,6 +35,7 @@ import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.util.Util;
|
||||
import org.whispersystems.libsignal.util.guava.Optional;
|
||||
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
||||
import org.whispersystems.signalservice.api.util.InvalidNumberException;
|
||||
@@ -58,16 +59,16 @@ public class ContactsDatabase {
|
||||
private static final String CALL_MIMETYPE = "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.call";
|
||||
private static final String SYNC = "__TS";
|
||||
|
||||
public static final String ID_COLUMN = "_id";
|
||||
public static final String NAME_COLUMN = "name";
|
||||
public static final String NUMBER_COLUMN = "number";
|
||||
public static final String NUMBER_TYPE_COLUMN = "number_type";
|
||||
public static final String LABEL_COLUMN = "label";
|
||||
public static final String CONTACT_TYPE_COLUMN = "contact_type";
|
||||
static final String ID_COLUMN = "_id";
|
||||
static final String NAME_COLUMN = "name";
|
||||
static final String NUMBER_COLUMN = "number";
|
||||
static final String NUMBER_TYPE_COLUMN = "number_type";
|
||||
static final String LABEL_COLUMN = "label";
|
||||
static final String CONTACT_TYPE_COLUMN = "contact_type";
|
||||
|
||||
public static final int NORMAL_TYPE = 0;
|
||||
public static final int PUSH_TYPE = 1;
|
||||
public static final int NEW_TYPE = 2;
|
||||
static final int NORMAL_TYPE = 0;
|
||||
static final int PUSH_TYPE = 1;
|
||||
static final int NEW_TYPE = 2;
|
||||
|
||||
private final Context context;
|
||||
|
||||
@@ -99,7 +100,8 @@ public class ContactsDatabase {
|
||||
Log.w(TAG, "Adding number: " + registeredNumber);
|
||||
addedNumbers.add(registeredNumber);
|
||||
addTextSecureRawContact(operations, account, systemContactInfo.get().number,
|
||||
systemContactInfo.get().id, registeredContact.isVoice());
|
||||
systemContactInfo.get().name, systemContactInfo.get().id,
|
||||
registeredContact.isVoice());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,6 +120,11 @@ public class ContactsDatabase {
|
||||
} else if (!tokenDetails.isVoice() && currentContactEntry.getValue().isVoiceSupported()) {
|
||||
Log.w(TAG, "Removing voice support: " + currentContactEntry.getKey());
|
||||
removeContactVoiceSupport(operations, currentContactEntry.getValue().getId());
|
||||
} else if (!Util.isStringEquals(currentContactEntry.getValue().getRawDisplayName(),
|
||||
currentContactEntry.getValue().getAggregateDisplayName()))
|
||||
{
|
||||
Log.w(TAG, "Updating display name: " + currentContactEntry.getKey());
|
||||
updateDisplayName(operations, currentContactEntry.getValue().getAggregateDisplayName(), currentContactEntry.getValue().getId(), currentContactEntry.getValue().getDisplayNameSource());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +135,7 @@ public class ContactsDatabase {
|
||||
return addedNumbers;
|
||||
}
|
||||
|
||||
public @NonNull Cursor querySystemContacts(String filter) {
|
||||
@NonNull Cursor querySystemContacts(String filter) {
|
||||
Uri uri;
|
||||
|
||||
if (!TextUtils.isEmpty(filter)) {
|
||||
@@ -176,7 +183,7 @@ public class ContactsDatabase {
|
||||
new Pair<String, Object>(CONTACT_TYPE_COLUMN, NORMAL_TYPE));
|
||||
}
|
||||
|
||||
public @NonNull Cursor queryTextSecureContacts(String filter) {
|
||||
@NonNull Cursor queryTextSecureContacts(String filter) {
|
||||
String[] projection = new String[] {ContactsContract.Data._ID,
|
||||
ContactsContract.Contacts.DISPLAY_NAME,
|
||||
ContactsContract.Data.DATA1};
|
||||
@@ -231,6 +238,30 @@ public class ContactsDatabase {
|
||||
.build());
|
||||
}
|
||||
|
||||
private void updateDisplayName(List<ContentProviderOperation> operations,
|
||||
@Nullable String displayName,
|
||||
long rawContactId, int displayNameSource)
|
||||
{
|
||||
Uri dataUri = ContactsContract.Data.CONTENT_URI.buildUpon()
|
||||
.appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
|
||||
.build();
|
||||
|
||||
if (displayNameSource != ContactsContract.DisplayNameSources.STRUCTURED_NAME) {
|
||||
operations.add(ContentProviderOperation.newInsert(dataUri)
|
||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, rawContactId)
|
||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||
.build());
|
||||
} else {
|
||||
operations.add(ContentProviderOperation.newUpdate(dataUri)
|
||||
.withSelection(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
|
||||
new String[] {String.valueOf(rawContactId), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE})
|
||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
private void removeContactVoiceSupport(List<ContentProviderOperation> operations, long rawContactId) {
|
||||
operations.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI)
|
||||
.withSelection(RawContacts._ID + " = ?", new String[] {String.valueOf(rawContactId)})
|
||||
@@ -245,7 +276,7 @@ public class ContactsDatabase {
|
||||
}
|
||||
|
||||
private void addTextSecureRawContact(List<ContentProviderOperation> operations,
|
||||
Account account, String e164number,
|
||||
Account account, String e164number, String displayName,
|
||||
long aggregateId, boolean supportsVoice)
|
||||
{
|
||||
int index = operations.size();
|
||||
@@ -260,6 +291,12 @@ public class ContactsDatabase {
|
||||
.withValue(RawContacts.SYNC4, String.valueOf(supportsVoice))
|
||||
.build());
|
||||
|
||||
operations.add(ContentProviderOperation.newInsert(dataUri)
|
||||
.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, index)
|
||||
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
|
||||
.build());
|
||||
|
||||
operations.add(ContentProviderOperation.newInsert(dataUri)
|
||||
.withValueBackReference(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, index)
|
||||
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
|
||||
@@ -321,7 +358,15 @@ public class ContactsDatabase {
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(currentContactsUri, new String[] {BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4}, null, null, null);
|
||||
String[] projection;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 11) {
|
||||
projection = new String[] {BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY, RawContacts.DISPLAY_NAME_SOURCE};
|
||||
} else{
|
||||
projection = new String[] {BaseColumns._ID, RawContacts.SYNC1, RawContacts.SYNC4, RawContacts.CONTACT_ID};
|
||||
}
|
||||
|
||||
cursor = context.getContentResolver().query(currentContactsUri, projection, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String currentNumber;
|
||||
@@ -333,7 +378,20 @@ public class ContactsDatabase {
|
||||
currentNumber = cursor.getString(1);
|
||||
}
|
||||
|
||||
signalContacts.put(currentNumber, new SignalContact(cursor.getLong(0), cursor.getString(2)));
|
||||
long rawContactId = cursor.getLong(0);
|
||||
long contactId = cursor.getLong(3);
|
||||
String supportsVoice = cursor.getString(2);
|
||||
String rawContactDisplayName = null;
|
||||
String aggregateDisplayName = null;
|
||||
int rawContactDisplayNameSource = 0;
|
||||
|
||||
if (Build.VERSION.SDK_INT >= 11) {
|
||||
rawContactDisplayName = cursor.getString(4);
|
||||
rawContactDisplayNameSource = cursor.getInt(5);
|
||||
aggregateDisplayName = getDisplayName(contactId);
|
||||
}
|
||||
|
||||
signalContacts.put(currentNumber, new SignalContact(rawContactId, supportsVoice, rawContactDisplayName, aggregateDisplayName, rawContactDisplayNameSource));
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
@@ -386,15 +444,33 @@ public class ContactsDatabase {
|
||||
return Optional.absent();
|
||||
}
|
||||
|
||||
private @Nullable String getDisplayName(long contactId) {
|
||||
Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
|
||||
new String[]{ContactsContract.Contacts.DISPLAY_NAME},
|
||||
ContactsContract.Contacts._ID + " = ?",
|
||||
new String[] {String.valueOf(contactId)},
|
||||
null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getString(0);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null) cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ProjectionMappingCursor extends CursorWrapper {
|
||||
|
||||
private final Map<String, String> projectionMap;
|
||||
private final Pair<String, Object>[] extras;
|
||||
|
||||
@SafeVarargs
|
||||
public ProjectionMappingCursor(Cursor cursor,
|
||||
Map<String, String> projectionMap,
|
||||
Pair<String, Object>... extras)
|
||||
ProjectionMappingCursor(Cursor cursor,
|
||||
Map<String, String> projectionMap,
|
||||
Pair<String, Object>... extras)
|
||||
{
|
||||
super(cursor);
|
||||
this.projectionMap = projectionMap;
|
||||
@@ -498,20 +574,46 @@ public class ContactsDatabase {
|
||||
}
|
||||
|
||||
private static class SignalContact {
|
||||
|
||||
private final long id;
|
||||
@Nullable private final String supportsVoice;
|
||||
@Nullable private final String rawDisplayName;
|
||||
@Nullable private final String aggregateDisplayName;
|
||||
private final int displayNameSource;
|
||||
|
||||
public SignalContact(long id, @Nullable String supportsVoice) {
|
||||
this.id = id;
|
||||
this.supportsVoice = supportsVoice;
|
||||
SignalContact(long id,
|
||||
@Nullable String supportsVoice,
|
||||
@Nullable String rawDisplayName,
|
||||
@Nullable String aggregateDisplayName,
|
||||
int displayNameSource)
|
||||
{
|
||||
this.id = id;
|
||||
this.supportsVoice = supportsVoice;
|
||||
this.rawDisplayName = rawDisplayName;
|
||||
this.aggregateDisplayName = aggregateDisplayName;
|
||||
this.displayNameSource = displayNameSource;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isVoiceSupported() {
|
||||
boolean isVoiceSupported() {
|
||||
return "true".equals(supportsVoice);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
String getRawDisplayName() {
|
||||
return rawDisplayName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
String getAggregateDisplayName() {
|
||||
return aggregateDisplayName;
|
||||
}
|
||||
|
||||
int getDisplayNameSource() {
|
||||
return displayNameSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user