mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-20 08:39:22 +01:00
Clean up contact queries.
// FREEBIE
This commit is contained in:
@@ -23,13 +23,9 @@ import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.provider.ContactsContract;
|
||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||
import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.Data;
|
||||
import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.support.v4.content.Loader;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
@@ -65,35 +61,6 @@ public class ContactAccessor {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public CursorLoader getCursorLoaderForContactsWithNumbers(Context context) {
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
|
||||
|
||||
return new CursorLoader(context, uri, null, selection, null,
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
}
|
||||
|
||||
public CursorLoader getCursorLoaderForContactGroups(Context context) {
|
||||
return new CursorLoader(context, ContactsContract.Groups.CONTENT_URI,
|
||||
null, null, null, ContactsContract.Groups.TITLE + " ASC");
|
||||
}
|
||||
|
||||
public Loader<Cursor> getCursorLoaderForContacts(Context context, String filter) {
|
||||
return new ContactsCursorLoader(context, filter, false);
|
||||
}
|
||||
|
||||
public Loader<Cursor> getCursorLoaderForPushContacts(Context context, String filter) {
|
||||
return new ContactsCursorLoader(context, filter, true);
|
||||
}
|
||||
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
|
||||
|
||||
return context.getContentResolver().query(uri, null, selection, null,
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
}
|
||||
|
||||
public Collection<ContactData> getContactsWithPush(Context context) {
|
||||
final ContentResolver resolver = context.getContentResolver();
|
||||
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
|
||||
@@ -136,34 +103,6 @@ public class ContactAccessor {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getNameForNumber(Context context, String number) {
|
||||
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
||||
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public GroupData getGroupData(Context context, Cursor cursor) {
|
||||
long id = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Groups._ID));
|
||||
String title = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Groups.TITLE));
|
||||
|
||||
return new GroupData(id, title);
|
||||
}
|
||||
|
||||
public ContactData getContactData(Context context, Cursor cursor) {
|
||||
return getContactData(context,
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME)),
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID)));
|
||||
}
|
||||
|
||||
public ContactData getContactData(Context context, Uri uri) {
|
||||
return getContactData(context, getNameFromContact(context, uri), Long.parseLong(uri.getLastPathSegment()));
|
||||
}
|
||||
@@ -193,32 +132,6 @@ public class ContactAccessor {
|
||||
return contactData;
|
||||
}
|
||||
|
||||
public List<ContactData> getGroupMembership(Context context, long groupId) {
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
Cursor groupMembership = null;
|
||||
|
||||
try {
|
||||
String selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ? AND " +
|
||||
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + " = ?";
|
||||
String[] args = new String[] {groupId+"",
|
||||
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE};
|
||||
|
||||
groupMembership = context.getContentResolver().query(Data.CONTENT_URI, null, selection, args, null);
|
||||
|
||||
while (groupMembership != null && groupMembership.moveToNext()) {
|
||||
String displayName = groupMembership.getString(groupMembership.getColumnIndexOrThrow(Data.DISPLAY_NAME));
|
||||
long contactId = groupMembership.getLong(groupMembership.getColumnIndexOrThrow(Data.CONTACT_ID));
|
||||
|
||||
contacts.add(getContactData(context, displayName, contactId));
|
||||
}
|
||||
} finally {
|
||||
if (groupMembership != null)
|
||||
groupMembership.close();
|
||||
}
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public List<String> getNumbersForThreadSearchFilter(Context context, String constraint) {
|
||||
LinkedList<String> numberList = new LinkedList<>();
|
||||
Cursor cursor = null;
|
||||
@@ -258,26 +171,6 @@ public class ContactAccessor {
|
||||
return Phone.getTypeLabel(mContext.getResources(), type, label);
|
||||
}
|
||||
|
||||
private long getContactIdFromLookupUri(Context context, Uri uri) {
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri,
|
||||
new String[] {ContactsContract.Contacts._ID},
|
||||
null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getLong(0);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static class NumberData implements Parcelable {
|
||||
|
||||
public static final Parcelable.Creator<NumberData> CREATOR = new Parcelable.Creator<NumberData>() {
|
||||
@@ -313,16 +206,6 @@ public class ContactAccessor {
|
||||
}
|
||||
}
|
||||
|
||||
public static class GroupData {
|
||||
public final long id;
|
||||
public final String name;
|
||||
|
||||
public GroupData(long id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ContactData implements Parcelable {
|
||||
|
||||
public static final Parcelable.Creator<ContactData> CREATOR = new Parcelable.Creator<ContactData>() {
|
||||
@@ -345,13 +228,6 @@ public class ContactAccessor {
|
||||
this.numbers = new LinkedList<NumberData>();
|
||||
}
|
||||
|
||||
public ContactData(long id, String name, List<NumberData> numbers) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.numbers = numbers;
|
||||
}
|
||||
|
||||
|
||||
public ContactData(Parcel in) {
|
||||
id = in.readLong();
|
||||
name = in.readString();
|
||||
|
||||
Reference in New Issue
Block a user