2012-07-21 05:23:25 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-21 05:23:25 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-21 05:23:25 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.contacts;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
2012-10-23 02:17:08 +00:00
|
|
|
import android.database.MergeCursor;
|
2011-12-20 18:20:44 +00:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2012-10-23 02:17:08 +00:00
|
|
|
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
|
|
|
import android.provider.ContactsContract.Contacts;
|
|
|
|
import android.provider.ContactsContract.PhoneLookup;
|
|
|
|
import android.telephony.PhoneNumberUtils;
|
2017-08-07 21:24:53 +00:00
|
|
|
import android.text.TextUtils;
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-12-14 13:53:56 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
import java.util.ArrayList;
|
2014-03-18 06:25:09 +00:00
|
|
|
import java.util.Collection;
|
2017-08-07 21:24:53 +00:00
|
|
|
import java.util.HashSet;
|
2012-07-21 05:23:25 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2017-08-07 21:24:53 +00:00
|
|
|
import java.util.Set;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-12-14 13:53:56 +00:00
|
|
|
import static org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
/**
|
2012-10-23 02:17:08 +00:00
|
|
|
* This class was originally a layer of indirection between
|
|
|
|
* ContactAccessorNewApi and ContactAccesorOldApi, which corresponded
|
|
|
|
* to the API changes between 1.x and 2.x.
|
|
|
|
*
|
|
|
|
* Now that we no longer support 1.x, this class mostly serves as a place
|
|
|
|
* to encapsulate Contact-related logic. It's still a singleton, mostly
|
|
|
|
* just because that's how it's currently called from everywhere.
|
2012-07-21 05:23:25 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
public class ContactAccessor {
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
public static final String PUSH_COLUMN = "push";
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
private static final ContactAccessor instance = new ContactAccessor();
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static synchronized ContactAccessor getInstance() {
|
2012-10-23 02:17:08 +00:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
public Set<Address> getAllContactsWithNumbers(Context context) {
|
|
|
|
Set<Address> results = new HashSet<>();
|
|
|
|
|
|
|
|
try (Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.NUMBER}, null ,null, null)) {
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
if (!TextUtils.isEmpty(cursor.getString(0))) {
|
|
|
|
results.add(Address.fromExternal(context, cursor.getString(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2017-08-07 22:31:12 +00:00
|
|
|
public Cursor getAllSystemContacts(Context context) {
|
|
|
|
return context.getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.NUMBER, Phone.DISPLAY_NAME}, null, null, null);
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:12:48 +00:00
|
|
|
public boolean isSystemContact(Context context, String number) {
|
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
|
|
|
|
String[] projection = new String[]{PhoneLookup.DISPLAY_NAME, PhoneLookup.LOOKUP_KEY,
|
|
|
|
PhoneLookup._ID, PhoneLookup.NUMBER};
|
|
|
|
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (cursor != null && cursor.moveToFirst()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (cursor != null) cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
public Collection<ContactData> getContactsWithPush(Context context) {
|
2014-02-07 02:06:23 +00:00
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
2014-03-18 06:25:09 +00:00
|
|
|
final String[] inProjection = new String[]{PhoneLookup._ID, PhoneLookup.DISPLAY_NAME};
|
|
|
|
|
2017-08-22 01:47:37 +00:00
|
|
|
final List<Address> registeredAddresses = DatabaseFactory.getRecipientDatabase(context).getRegistered();
|
2017-08-07 21:24:53 +00:00
|
|
|
final Collection<ContactData> lookupData = new ArrayList<>(registeredAddresses.size());
|
2014-03-18 06:25:09 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
for (Address registeredAddress : registeredAddresses) {
|
|
|
|
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(registeredAddress.serialize()));
|
2014-02-07 02:06:23 +00:00
|
|
|
Cursor lookupCursor = resolver.query(uri, inProjection, null, null, null);
|
2017-08-07 21:24:53 +00:00
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
try {
|
|
|
|
if (lookupCursor != null && lookupCursor.moveToFirst()) {
|
2014-03-18 06:25:09 +00:00
|
|
|
final ContactData contactData = new ContactData(lookupCursor.getLong(0), lookupCursor.getString(1));
|
2017-08-07 21:24:53 +00:00
|
|
|
contactData.numbers.add(new NumberData("TextSecure", registeredAddress.serialize()));
|
2014-03-18 06:25:09 +00:00
|
|
|
lookupData.add(contactData);
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (lookupCursor != null)
|
|
|
|
lookupCursor.close();
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 21:24:53 +00:00
|
|
|
|
2014-03-18 06:25:09 +00:00
|
|
|
return lookupData;
|
2014-02-07 02:06:23 +00:00
|
|
|
}
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
public String getNameFromContact(Context context, Uri uri) {
|
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
cursor = context.getContentResolver().query(uri, new String[] {Contacts.DISPLAY_NAME},
|
|
|
|
null, null, null);
|
|
|
|
|
|
|
|
if (cursor != null && cursor.moveToFirst())
|
|
|
|
return cursor.getString(0);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-10-17 00:28:36 +00:00
|
|
|
public ContactData getContactData(Context context, Uri uri) {
|
|
|
|
return getContactData(context, getNameFromContact(context, uri), Long.parseLong(uri.getLastPathSegment()));
|
|
|
|
}
|
2012-10-23 02:17:08 +00:00
|
|
|
|
|
|
|
private ContactData getContactData(Context context, String displayName, long id) {
|
|
|
|
ContactData contactData = new ContactData(id, displayName);
|
|
|
|
Cursor numberCursor = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, null,
|
|
|
|
Phone.CONTACT_ID + " = ?",
|
|
|
|
new String[] {contactData.id + ""}, null);
|
|
|
|
|
|
|
|
while (numberCursor != null && numberCursor.moveToNext()) {
|
|
|
|
int type = numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phone.TYPE));
|
|
|
|
String label = numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.LABEL));
|
|
|
|
String number = numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.NUMBER));
|
|
|
|
String typeLabel = Phone.getTypeLabel(context.getResources(), type, label).toString();
|
|
|
|
|
|
|
|
contactData.numbers.add(new NumberData(typeLabel, number));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (numberCursor != null)
|
|
|
|
numberCursor.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactData;
|
|
|
|
}
|
|
|
|
|
2014-12-14 13:53:56 +00:00
|
|
|
public List<String> getNumbersForThreadSearchFilter(Context context, String constraint) {
|
|
|
|
LinkedList<String> numberList = new LinkedList<>();
|
2012-10-23 02:17:08 +00:00
|
|
|
Cursor cursor = null;
|
|
|
|
|
|
|
|
try {
|
2014-12-14 13:53:56 +00:00
|
|
|
cursor = context.getContentResolver().query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,
|
|
|
|
Uri.encode(constraint)),
|
|
|
|
null, null, null, null);
|
2012-10-23 02:17:08 +00:00
|
|
|
|
|
|
|
while (cursor != null && cursor.moveToNext()) {
|
|
|
|
numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (cursor != null)
|
|
|
|
cursor.close();
|
|
|
|
}
|
|
|
|
|
2014-12-14 13:53:56 +00:00
|
|
|
GroupDatabase.Reader reader = null;
|
|
|
|
GroupRecord record;
|
|
|
|
|
|
|
|
try {
|
|
|
|
reader = DatabaseFactory.getGroupDatabase(context).getGroupsFilteredByTitle(constraint);
|
|
|
|
|
|
|
|
while ((record = reader.getNext()) != null) {
|
|
|
|
numberList.add(record.getEncodedId());
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (reader != null)
|
|
|
|
reader.close();
|
|
|
|
}
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
return numberList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public CharSequence phoneTypeToString(Context mContext, int type, CharSequence label) {
|
|
|
|
return Phone.getTypeLabel(mContext.getResources(), type, label);
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static class NumberData implements Parcelable {
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final Parcelable.Creator<NumberData> CREATOR = new Parcelable.Creator<NumberData>() {
|
|
|
|
public NumberData createFromParcel(Parcel in) {
|
|
|
|
return new NumberData(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
public NumberData[] newArray(int size) {
|
|
|
|
return new NumberData[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
public final String number;
|
|
|
|
public final String type;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public NumberData(String type, String number) {
|
|
|
|
this.type = type;
|
|
|
|
this.number = number;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public NumberData(Parcel in) {
|
|
|
|
number = in.readString();
|
|
|
|
type = in.readString();
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeString(number);
|
|
|
|
dest.writeString(type);
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static class ContactData implements Parcelable {
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public static final Parcelable.Creator<ContactData> CREATOR = new Parcelable.Creator<ContactData>() {
|
|
|
|
public ContactData createFromParcel(Parcel in) {
|
|
|
|
return new ContactData(in);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ContactData[] newArray(int size) {
|
|
|
|
return new ContactData[size];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
public final long id;
|
|
|
|
public final String name;
|
|
|
|
public final List<NumberData> numbers;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
public ContactData(long id, String name) {
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
this.numbers = new LinkedList<NumberData>();
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public ContactData(Parcel in) {
|
|
|
|
id = in.readLong();
|
|
|
|
name = in.readString();
|
|
|
|
numbers = new LinkedList<NumberData>();
|
|
|
|
in.readTypedList(numbers, NumberData.CREATOR);
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public int describeContents() {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-07-21 05:23:25 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public void writeToParcel(Parcel dest, int flags) {
|
|
|
|
dest.writeLong(id);
|
|
|
|
dest.writeString(name);
|
|
|
|
dest.writeTypedList(numbers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 02:17:08 +00:00
|
|
|
/***
|
|
|
|
* If the code below looks shitty to you, that's because it was taken
|
|
|
|
* directly from the Android source, where shitty code is all you get.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Cursor getCursorForRecipientFilter(CharSequence constraint,
|
|
|
|
ContentResolver mContentResolver)
|
|
|
|
{
|
|
|
|
final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," +
|
2014-02-09 22:28:10 +00:00
|
|
|
Contacts.DISPLAY_NAME + "," +
|
|
|
|
Contacts.Data.IS_SUPER_PRIMARY + " DESC," +
|
|
|
|
Phone.TYPE;
|
2012-10-23 02:17:08 +00:00
|
|
|
|
|
|
|
final String[] PROJECTION_PHONE = {
|
|
|
|
Phone._ID, // 0
|
|
|
|
Phone.CONTACT_ID, // 1
|
|
|
|
Phone.TYPE, // 2
|
|
|
|
Phone.NUMBER, // 3
|
|
|
|
Phone.LABEL, // 4
|
|
|
|
Phone.DISPLAY_NAME, // 5
|
|
|
|
};
|
|
|
|
|
|
|
|
String phone = "";
|
|
|
|
String cons = null;
|
|
|
|
|
|
|
|
if (constraint != null) {
|
|
|
|
cons = constraint.toString();
|
|
|
|
|
|
|
|
if (RecipientsAdapter.usefulAsDigits(cons)) {
|
|
|
|
phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
|
2014-02-17 01:44:51 +00:00
|
|
|
if (phone.equals(cons) && !PhoneNumberUtils.isWellFormedSmsAddress(phone)) {
|
2012-10-23 02:17:08 +00:00
|
|
|
phone = "";
|
|
|
|
} else {
|
|
|
|
phone = phone.trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(cons));
|
|
|
|
String selection = String.format("%s=%s OR %s=%s OR %s=%s",
|
|
|
|
Phone.TYPE,
|
|
|
|
Phone.TYPE_MOBILE,
|
|
|
|
Phone.TYPE,
|
|
|
|
Phone.TYPE_WORK_MOBILE,
|
|
|
|
Phone.TYPE,
|
|
|
|
Phone.TYPE_MMS);
|
|
|
|
|
|
|
|
Cursor phoneCursor = mContentResolver.query(uri,
|
|
|
|
PROJECTION_PHONE,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
SORT_ORDER);
|
|
|
|
|
|
|
|
if (phone.length() > 0) {
|
|
|
|
ArrayList result = new ArrayList();
|
|
|
|
result.add(Integer.valueOf(-1)); // ID
|
|
|
|
result.add(Long.valueOf(-1)); // CONTACT_ID
|
|
|
|
result.add(Integer.valueOf(Phone.TYPE_CUSTOM)); // TYPE
|
|
|
|
result.add(phone); // NUMBER
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The "\u00A0" keeps Phone.getDisplayLabel() from deciding
|
|
|
|
* to display the default label ("Home") next to the transformation
|
|
|
|
* of the letters into numbers.
|
|
|
|
*/
|
|
|
|
result.add("\u00A0"); // LABEL
|
|
|
|
result.add(cons); // NAME
|
|
|
|
|
|
|
|
ArrayList<ArrayList> wrap = new ArrayList<ArrayList>();
|
|
|
|
wrap.add(result);
|
|
|
|
|
|
|
|
ArrayListCursor translated = new ArrayListCursor(PROJECTION_PHONE, wrap);
|
|
|
|
|
|
|
|
return new MergeCursor(new Cursor[] { translated, phoneCursor });
|
|
|
|
} else {
|
|
|
|
return phoneCursor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|