mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-25 22:59:25 +00:00
Fix the look of tabbed multi-contact selector.
1) Updated to ActionBar style. 2) Split out into fragments. 3) Switch to cursor loaders.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
*
|
||||
* 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
|
||||
@@ -10,25 +10,25 @@
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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 java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Android changed their contacts API pretty heavily between
|
||||
@@ -36,37 +36,17 @@ import android.os.Parcelable;
|
||||
* API operations, using a singleton pattern that will Class.forName
|
||||
* the correct one so we don't trigger NoClassDefFound exceptions on
|
||||
* old platforms.
|
||||
*
|
||||
*
|
||||
* @author Moxie Marlinspike
|
||||
*/
|
||||
|
||||
public abstract class ContactAccessor {
|
||||
public abstract class ContactAccessor {
|
||||
public static final int UNIQUE_ID = 0;
|
||||
public static final int DISPLAY_NAME = 1;
|
||||
|
||||
private static ContactAccessor sInstance;
|
||||
|
||||
public static synchronized ContactAccessor getInstance() {
|
||||
if (sInstance == null) {
|
||||
String className;
|
||||
private static final ContactAccessor sInstance = new ContactAccessorNewApi();
|
||||
|
||||
if (Integer.parseInt(Build.VERSION.SDK) <= Build.VERSION_CODES.DONUT)
|
||||
className = "ContactAccessorOldApi";
|
||||
else
|
||||
className = "ContactAccessorNewApi";
|
||||
|
||||
try {
|
||||
Class<? extends ContactAccessor> clazz =
|
||||
Class.forName("org.thoughtcrime.securesms.contacts." + className )
|
||||
.asSubclass(ContactAccessor.class);
|
||||
|
||||
sInstance = clazz.newInstance();
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized ContactAccessor getInstance() {
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
@@ -78,6 +58,8 @@ public abstract class ContactAccessor {
|
||||
public abstract List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver);
|
||||
public abstract List<ContactData> getGroupMembership(Context context, long groupId);
|
||||
public abstract Cursor getCursorForContactGroups(Context context);
|
||||
public abstract CursorLoader getCursorLoaderForContactGroups(Context context);
|
||||
public abstract CursorLoader getCursorLoaderForContactsWithNumbers(Context context);
|
||||
public abstract Cursor getCursorForContactsWithNumbers(Context context);
|
||||
public abstract GroupData getGroupData(Context context, Cursor cursor);
|
||||
public abstract ContactData getContactData(Context context, Cursor cursor);
|
||||
@@ -85,9 +67,9 @@ public abstract class ContactAccessor {
|
||||
public abstract CharSequence phoneTypeToString(Context mContext, int type, CharSequence label);
|
||||
public abstract String getNameForNumber(Context context, String number);
|
||||
public abstract Uri getContactsUri();
|
||||
|
||||
|
||||
public static class NumberData implements Parcelable {
|
||||
|
||||
|
||||
public static final Parcelable.Creator<NumberData> CREATOR = new Parcelable.Creator<NumberData>() {
|
||||
public NumberData createFromParcel(Parcel in) {
|
||||
return new NumberData(in);
|
||||
@@ -105,12 +87,12 @@ public abstract class ContactAccessor {
|
||||
this.type = type;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
public NumberData(Parcel in) {
|
||||
number = in.readString();
|
||||
type = in.readString();
|
||||
}
|
||||
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
@@ -120,14 +102,14 @@ public abstract class ContactAccessor {
|
||||
dest.writeString(type);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class GroupData {
|
||||
public long id;
|
||||
public String name;
|
||||
}
|
||||
|
||||
|
||||
public static class ContactData implements Parcelable {
|
||||
|
||||
|
||||
public static final Parcelable.Creator<ContactData> CREATOR = new Parcelable.Creator<ContactData>() {
|
||||
public ContactData createFromParcel(Parcel in) {
|
||||
return new ContactData(in);
|
||||
@@ -143,18 +125,18 @@ public abstract class ContactAccessor {
|
||||
public List<NumberData> numbers;
|
||||
|
||||
public ContactData() {}
|
||||
|
||||
|
||||
public ContactData(Parcel in) {
|
||||
id = in.readLong();
|
||||
name = in.readString();
|
||||
numbers = new LinkedList<NumberData>();
|
||||
in.readTypedList(numbers, NumberData.CREATOR);
|
||||
}
|
||||
|
||||
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
dest.writeLong(id);
|
||||
dest.writeString(name);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
*
|
||||
* 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
|
||||
@@ -10,21 +10,12 @@
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* 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 java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
import org.thoughtcrime.securesms.crypto.InvalidKeyException;
|
||||
import org.thoughtcrime.securesms.util.Base64;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
@@ -39,17 +30,27 @@ import android.provider.ContactsContract.Contacts;
|
||||
import android.provider.ContactsContract.Data;
|
||||
import android.provider.ContactsContract.PhoneLookup;
|
||||
import android.provider.ContactsContract.RawContacts;
|
||||
import android.support.v4.content.CursorLoader;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
import org.thoughtcrime.securesms.crypto.InvalidKeyException;
|
||||
import org.thoughtcrime.securesms.util.Base64;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Interface into the Android 2.x+ contacts operations.
|
||||
*
|
||||
*
|
||||
* @author Stuart Anderson
|
||||
*/
|
||||
|
||||
public class ContactAccessorNewApi extends ContactAccessor {
|
||||
|
||||
|
||||
private static final String SORT_ORDER = Contacts.TIMES_CONTACTED + " DESC," + Contacts.DISPLAY_NAME + "," + Phone.TYPE;
|
||||
|
||||
private static final String[] PROJECTION_PHONE = {
|
||||
@@ -65,22 +66,22 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
public List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver) {
|
||||
LinkedList<String> numberList = new LinkedList<String>();
|
||||
Cursor cursor = null;
|
||||
|
||||
|
||||
try {
|
||||
cursor = contentResolver.query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)),
|
||||
null, null, null, null);
|
||||
|
||||
cursor = contentResolver.query(Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(constraint)),
|
||||
null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext())
|
||||
numberList.add(cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER)));
|
||||
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return numberList;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForRecipientFilter(CharSequence constraint, ContentResolver mContentResolver) {
|
||||
String phone = "";
|
||||
@@ -97,22 +98,22 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
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);
|
||||
|
||||
PROJECTION_PHONE,
|
||||
null,
|
||||
null,
|
||||
SORT_ORDER);
|
||||
|
||||
|
||||
|
||||
if (phone.length() > 0) {
|
||||
@@ -139,9 +140,9 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
} else {
|
||||
return phoneCursor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public CharSequence phoneTypeToString( Context mContext, int type, CharSequence label ) {
|
||||
return Phone.getTypeLabel(mContext.getResources(), type, label);
|
||||
@@ -156,50 +157,50 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private ArrayList<Long> getRawContactIds(Context context, long contactId) {
|
||||
Cursor cursor = null;
|
||||
ArrayList<Long> rawContactIds = new ArrayList<Long>();
|
||||
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
|
||||
RawContacts.CONTACT_ID + " = ?", new String[] {contactId+""},
|
||||
null);
|
||||
|
||||
cursor = context.getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
|
||||
RawContacts.CONTACT_ID + " = ?", new String[] {contactId+""},
|
||||
null);
|
||||
|
||||
if (cursor == null)
|
||||
return rawContactIds;
|
||||
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
rawContactIds.add(new Long(cursor.getLong(0)));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return rawContactIds;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void insertIdentityKey(Context context, Uri uri, IdentityKey identityKey) {
|
||||
long contactId = getContactIdFromLookupUri(context, uri);
|
||||
Log.w("ContactAccessorNewApi", "Got contact ID: " + contactId + " from uri: " + uri.toString());
|
||||
ArrayList<Long> rawContactIds = getRawContactIds(context, contactId);
|
||||
|
||||
|
||||
for (long rawContactId : rawContactIds) {
|
||||
Log.w("ContactAccessorNewApi", "Inserting data for raw contact id: " + rawContactId);
|
||||
ContentValues contentValues = new ContentValues();
|
||||
@@ -208,9 +209,9 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
contentValues.put(Im.PROTOCOL, Im.PROTOCOL_CUSTOM);
|
||||
contentValues.put(Im.CUSTOM_PROTOCOL, "TextSecure-IdentityKey");
|
||||
contentValues.put(Im.DATA, Base64.encodeBytes(identityKey.serialize()));
|
||||
|
||||
context.getContentResolver().insert(Data.CONTENT_URI, contentValues);
|
||||
}
|
||||
|
||||
context.getContentResolver().insert(Data.CONTENT_URI, contentValues);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -218,16 +219,16 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
long contactId = getContactIdFromLookupUri(context, uri);
|
||||
String selection = Im.CONTACT_ID + " = ? AND " + Im.PROTOCOL + " = ? AND " + Im.CUSTOM_PROTOCOL + " = ?";
|
||||
String[] selectionArgs = new String[] {contactId+"", Im.PROTOCOL_CUSTOM+"", "TextSecure-IdentityKey"};
|
||||
|
||||
|
||||
Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, null, selection, selectionArgs, null);
|
||||
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
String data = cursor.getString(cursor.getColumnIndexOrThrow(Im.DATA));
|
||||
|
||||
if (data != null)
|
||||
|
||||
if (data != null)
|
||||
return new IdentityKey(Base64.decode(data), 0);
|
||||
|
||||
|
||||
}
|
||||
} catch (InvalidKeyException e) {
|
||||
Log.w("ContactAccessorNewApi", e);
|
||||
@@ -246,10 +247,10 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
@Override
|
||||
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);
|
||||
|
||||
@@ -257,24 +258,24 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private String getMobileNumberForId(Context context, long id) {
|
||||
Cursor cursor = null;
|
||||
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ? AND " + Phone.TYPE + " = ?",
|
||||
new String[] {id+"", Phone.TYPE_MOBILE+""}, null);
|
||||
|
||||
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ? AND " + Phone.TYPE + " = ?",
|
||||
new String[] {id+"", Phone.TYPE_MOBILE+""}, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -282,29 +283,41 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
public NameAndNumber getNameAndNumberFromContact(Context context, Uri uri) {
|
||||
Log.w("ContactAccessorNewApi", "Get name and number from: " + uri.toString());
|
||||
Cursor cursor = null;
|
||||
|
||||
|
||||
try {
|
||||
NameAndNumber results = new NameAndNumber();
|
||||
cursor = context.getContentResolver().query(uri, new String[] {Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null);
|
||||
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
results.name = cursor.getString(1);
|
||||
results.number = getMobileNumberForId(context, cursor.getLong(0));
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
public CursorLoader getCursorLoaderForContactsWithNumbers(Context context) {
|
||||
Uri uri = ContactsContract.Contacts.CONTENT_URI;
|
||||
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1";
|
||||
return context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
|
||||
return new CursorLoader(context, uri, null, selection, null,
|
||||
ContactsContract.Contacts.DISPLAY_NAME + " ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
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");
|
||||
}
|
||||
|
||||
private ContactData getContactData(Context context, String displayName, long id) {
|
||||
@@ -312,52 +325,58 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
contactData.id = id;
|
||||
contactData.name = displayName;
|
||||
contactData.numbers = new LinkedList<NumberData>();
|
||||
|
||||
|
||||
Cursor numberCursor = null;
|
||||
|
||||
|
||||
try {
|
||||
numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?",
|
||||
new String[] {contactData.id + ""}, null);
|
||||
|
||||
numberCursor = context.getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?",
|
||||
new String[] {contactData.id + ""}, null);
|
||||
|
||||
while (numberCursor != null && numberCursor.moveToNext())
|
||||
contactData.numbers.add(new NumberData(Phone.getTypeLabel(context.getResources(),
|
||||
numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phone.TYPE)),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.LABEL))).toString(),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.NUMBER))));
|
||||
contactData.numbers.add(new NumberData(Phone.getTypeLabel(context.getResources(),
|
||||
numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phone.TYPE)),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.LABEL))).toString(),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phone.NUMBER))));
|
||||
} finally {
|
||||
if (numberCursor != null)
|
||||
numberCursor.close();
|
||||
}
|
||||
|
||||
|
||||
return contactData;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ContactData getContactData(Context context, Cursor cursor) {
|
||||
return getContactData(context,
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME)),
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID)));
|
||||
return getContactData(context,
|
||||
cursor.getString(cursor.getColumnIndexOrThrow(Contacts.DISPLAY_NAME)),
|
||||
cursor.getLong(cursor.getColumnIndexOrThrow(Contacts._ID)));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactGroups(Context context) {
|
||||
return context.getContentResolver().query(ContactsContract.Groups.CONTENT_URI, null, null, null, ContactsContract.Groups.TITLE + " ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CursorLoader getCursorLoaderForContactGroups(Context context) {
|
||||
return new CursorLoader(context, ContactsContract.Groups.CONTENT_URI,
|
||||
null, null, null, ContactsContract.Groups.TITLE + " ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ContactData> getGroupMembership(Context context, long groupId) {
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
Cursor groupMembership = null;
|
||||
|
||||
|
||||
try {
|
||||
String selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + " = ? AND " +
|
||||
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));
|
||||
@@ -368,16 +387,16 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
if (groupMembership != null)
|
||||
groupMembership.close();
|
||||
}
|
||||
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public GroupData getGroupData(Context context, Cursor cursor) {
|
||||
GroupData groupData = new GroupData();
|
||||
groupData.id = cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Groups._ID));
|
||||
groupData.name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Groups.TITLE));
|
||||
|
||||
|
||||
return groupData;
|
||||
}
|
||||
|
||||
@@ -393,7 +412,7 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -401,5 +420,5 @@ public class ContactAccessorNewApi extends ContactAccessor {
|
||||
public Uri getContactsUri() {
|
||||
return ContactsContract.Contacts.CONTENT_URI;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,328 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Whisper Systems
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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 java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.thoughtcrime.securesms.crypto.IdentityKey;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.database.DatabaseUtils;
|
||||
import android.database.MergeCursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.Contacts;
|
||||
import android.provider.Contacts.GroupMembership;
|
||||
import android.provider.Contacts.People;
|
||||
import android.provider.Contacts.Phones;
|
||||
import android.telephony.PhoneNumberUtils;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
/**
|
||||
* A contact interface into the 1.x API for older clients.
|
||||
*
|
||||
* @author Stuart Anderson
|
||||
*/
|
||||
|
||||
public class ContactAccessorOldApi extends ContactAccessor {
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static final String SORT_ORDER = Phones.NAME + "," + Phones.TYPE;
|
||||
@SuppressWarnings("deprecation")
|
||||
private static final String[] PROJECTION_PHONE = {
|
||||
Phones._ID, // 0
|
||||
Phones.PERSON_ID, // 1
|
||||
Phones.TYPE, // 2
|
||||
Phones.NUMBER, // 3
|
||||
Phones.LABEL, // 4
|
||||
Phones.DISPLAY_NAME, // 5
|
||||
};
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public Cursor getCursorForRecipientFilter(CharSequence constraint,
|
||||
ContentResolver mContentResolver) {
|
||||
String phone = "";
|
||||
String wherePhone = null;
|
||||
|
||||
String cons = null;
|
||||
if (constraint != null) {
|
||||
cons = constraint.toString();
|
||||
|
||||
if (RecipientsAdapter.usefulAsDigits(cons)) {
|
||||
phone = PhoneNumberUtils.convertKeypadLettersToDigits(cons);
|
||||
if (phone.equals(cons)) {
|
||||
phone = "";
|
||||
} else {
|
||||
phone = phone.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String filter = DatabaseUtils.sqlEscapeString(cons + '%');
|
||||
String filterLastName = DatabaseUtils.sqlEscapeString("% " + cons + '%');
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
s.append("((name LIKE ");
|
||||
s.append(filter);
|
||||
s.append(") OR (name LIKE ");
|
||||
s.append(filterLastName);
|
||||
s.append(") OR (REPLACE(REPLACE(REPLACE(REPLACE(number, ' ', ''), '(', ''), ')', ''), '-', '') LIKE ");
|
||||
s.append(filter);
|
||||
s.append("))");
|
||||
|
||||
wherePhone = s.toString();
|
||||
|
||||
Cursor phoneCursor = mContentResolver.query(Phones.CONTENT_URI,
|
||||
PROJECTION_PHONE,
|
||||
wherePhone,
|
||||
null,
|
||||
SORT_ORDER);
|
||||
|
||||
//dumpCursor(phoneCursor);
|
||||
|
||||
|
||||
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(Phones.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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public CharSequence phoneTypeToString(Context mContext, int type,
|
||||
CharSequence label) {
|
||||
return Phones.getDisplayLabel(mContext, type, label);
|
||||
}
|
||||
|
||||
public static void dumpCursor( Cursor c ) {
|
||||
c.moveToFirst();
|
||||
Log.d( "DC", "Begin:" );
|
||||
for( int i=0; i < c.getCount(); i++ ) {
|
||||
String rowStr = "";
|
||||
for( int j=0; j < c.getColumnCount(); j++ ) {
|
||||
rowStr = rowStr + c.getColumnName(j) + "=" + c.getString(j) +" ";
|
||||
}
|
||||
Log.d( "DC", rowStr + "\n" );
|
||||
c.moveToNext();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Intent getIntentForContactSelection() {
|
||||
return new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertIdentityKey(Context context, Uri uri, IdentityKey identityKey) {
|
||||
Toast.makeText(context, "Sorry, reading and writing identity keys to the contacts database is not supported on Android 1.X", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityKey importIdentityKey(Context context, Uri uri) {
|
||||
Toast.makeText(context, "Sorry, reading and writing identity keys to the contacts database is not supported on Android 1.X", Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameFromContact(Context context, Uri uri) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNumbersForThreadSearchFilter(String constraint, ContentResolver contentResolver) {
|
||||
LinkedList<String> numberList = new LinkedList<String>();
|
||||
Cursor cursor = null;
|
||||
|
||||
try {
|
||||
cursor = contentResolver.query(Uri.withAppendedPath(Contacts.People.CONTENT_FILTER_URI, Uri.encode(constraint)),
|
||||
null, null, null, null);
|
||||
|
||||
while (cursor != null && cursor.moveToNext()) {
|
||||
String number = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.NUMBER));
|
||||
if (number != null)
|
||||
numberList.add(number);
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return numberList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NameAndNumber getNameAndNumberFromContact(Context context, Uri uri) {
|
||||
Cursor cursor = null;
|
||||
NameAndNumber result = new NameAndNumber();
|
||||
|
||||
try {
|
||||
cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
result.name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
|
||||
result.number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
|
||||
|
||||
return result;
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactsWithNumbers(Context context) {
|
||||
return context.getContentResolver().query(People.CONTENT_URI,new String[]{People._ID,People.DISPLAY_NAME},
|
||||
People.NUMBER + " NOT NULL", null, "UPPER( " + People.DISPLAY_NAME + " ) ASC");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactData getContactData(Context context, Cursor cursor) {
|
||||
ContactData contactData = new ContactData();
|
||||
contactData.id = cursor.getLong(cursor.getColumnIndexOrThrow(People._ID));
|
||||
contactData.name = cursor.getString(cursor.getColumnIndexOrThrow(People.DISPLAY_NAME));
|
||||
contactData.numbers = getNumberDataForPersonId(context, contactData.id);
|
||||
|
||||
return contactData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor getCursorForContactGroups(Context context) {
|
||||
return context.getContentResolver().query(Contacts.Groups.CONTENT_URI, null, null, null, Contacts.Groups.NAME + " ASC");
|
||||
}
|
||||
|
||||
private LinkedList<NumberData> getNumberDataForPersonId(Context context, long personId) {
|
||||
LinkedList<NumberData> numbers = new LinkedList<NumberData>();
|
||||
Cursor numberCursor = context.getContentResolver().query(Phones.CONTENT_URI, null,
|
||||
Phones.PERSON_ID + " = ?",
|
||||
new String[] {personId+""}, null);
|
||||
try {
|
||||
while (numberCursor != null && numberCursor.moveToNext()) {
|
||||
numbers.add(new NumberData(Phones.getDisplayLabel(context, numberCursor.getInt(numberCursor.getColumnIndexOrThrow(Phones.TYPE)), "").toString(),
|
||||
numberCursor.getString(numberCursor.getColumnIndexOrThrow(Phones.NUMBER))));
|
||||
}
|
||||
} finally {
|
||||
if (numberCursor != null)
|
||||
numberCursor.close();
|
||||
}
|
||||
|
||||
return numbers;
|
||||
}
|
||||
|
||||
private ContactData getContactDataFromGroupMembership(Context context, Cursor cursor) {
|
||||
ContactData contactData = new ContactData();
|
||||
contactData.id = cursor.getLong(cursor.getColumnIndexOrThrow(GroupMembership.PERSON_ID));
|
||||
|
||||
Cursor personCursor = context.getContentResolver().query(Uri.withAppendedPath(People.CONTENT_URI, contactData.id+""), null, null, null, null);
|
||||
|
||||
try {
|
||||
if (personCursor == null || !personCursor.moveToFirst())
|
||||
throw new AssertionError("Non-existent user in group?");
|
||||
|
||||
contactData.name = personCursor.getString(personCursor.getColumnIndexOrThrow(People.DISPLAY_NAME));
|
||||
contactData.numbers = getNumberDataForPersonId(context, contactData.id);
|
||||
|
||||
return contactData;
|
||||
|
||||
} finally {
|
||||
if (personCursor != null)
|
||||
personCursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ContactData> getGroupMembership(Context context, long groupId) {
|
||||
LinkedList<ContactData> contacts = new LinkedList<ContactData>();
|
||||
Cursor groupMembershipCursor = context.getContentResolver().query(Contacts.GroupMembership.CONTENT_URI, null,
|
||||
GroupMembership.GROUP_ID + " = ?",
|
||||
new String[] {groupId+""}, null);
|
||||
|
||||
try {
|
||||
while (groupMembershipCursor != null && groupMembershipCursor.moveToNext()) {
|
||||
contacts.add(getContactDataFromGroupMembership(context, groupMembershipCursor));
|
||||
}
|
||||
} finally {
|
||||
if (groupMembershipCursor != null)
|
||||
groupMembershipCursor.close();
|
||||
}
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupData getGroupData(Context context, Cursor cursor) {
|
||||
GroupData groupData = new GroupData();
|
||||
groupData.id = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.Groups._ID));
|
||||
groupData.name = cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Groups.NAME));
|
||||
|
||||
return groupData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNameForNumber(Context context, String number) {
|
||||
Cursor cursor = context.getContentResolver().query(Contacts.Phones.CONTENT_URI, null,
|
||||
Phones.NUMBER + " = ?",
|
||||
new String[] {number}, null);
|
||||
|
||||
try {
|
||||
if (cursor != null && cursor.moveToFirst())
|
||||
return cursor.getString(cursor.getColumnIndexOrThrow(Contacts.Phones.DISPLAY_NAME));
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri getContactsUri() {
|
||||
return Contacts.People.CONTENT_URI;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user