Turn avatar images in conversations into QuickContactBadge equivalents.

This requires a few changes to Recipient in order to make sure we
have a Contact URI at click time.  While we're at it, let's git rid
of the OldRecipientProvider, which was for pre-2.0 contact stuff
(no longer supported, woohoo!).
This commit is contained in:
Moxie Marlinspike 2012-09-11 18:23:19 -07:00
parent a834ccad55
commit c6ae07ee5c
6 changed files with 132 additions and 246 deletions

View File

@ -27,6 +27,8 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.provider.Contacts.Intents;
import android.provider.ContactsContract.QuickContact;
import android.telephony.TelephonyManager;
import android.text.Spannable;
import android.text.format.DateUtils;
@ -249,10 +251,24 @@ public class ConversationItem extends LinearLayout {
}
private void setBodyImage(MessageRecord messageRecord) {
Recipient recipient = messageRecord.getMessageRecipient();
final Recipient recipient = messageRecord.getMessageRecipient();
if (!messageRecord.isOutgoing()) contactPhoto.setImageBitmap(recipient.getContactPhoto());
else setContactPhotoForUserIdentity();
if (!messageRecord.isOutgoing()) {
contactPhoto.setImageBitmap(recipient.getContactPhoto());
contactPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (recipient.getContactUri() != null) {
QuickContact.showQuickContact(context, contactPhoto, recipient.getContactUri(), QuickContact.MODE_LARGE, null);
} else {
Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, Uri.fromParts("tel", recipient.getNumber(), null));
context.startActivity(intent);
}
}
});
} else {
setContactPhotoForUserIdentity();
}
contactPhoto.setVisibility(View.VISIBLE);
}

View File

@ -16,19 +16,17 @@
*/
package org.thoughtcrime.securesms.recipients;
import java.io.InputStream;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import java.io.InputStream;
public class NewRecipientProvider extends RecipientProvider {
private static final String[] CALLER_ID_PROJECTION = new String[] {
@ -40,26 +38,27 @@ public class NewRecipientProvider extends RecipientProvider {
private static final String[] CONTENT_URI_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LOOKUP_KEY
};
@Override
public Recipient getRecipient(Context context, Uri uri) {
public Recipient getRecipient(Context context, Uri uri) {
Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION, null, null, null);
try {
if (cursor.moveToFirst()) {
long rowId = cursor.getLong(0);
Uri photoLookupUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);
long rowId = cursor.getLong(0);
Uri contactUri = Contacts.getLookupUri(rowId, cursor.getString(2));
Bitmap contactPhoto = getContactPhoto(context, contactUri);
String displayName = cursor.getString(1);
cursor.close();
Bitmap contactPhoto = getContactPhoto(context, photoLookupUri);
String displayName = cursor.getString(1);
cursor.close();
cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[] {rowId+""}, null);
cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[] {rowId+""}, null);
if (cursor.moveToFirst())
return new Recipient(displayName, cursor.getString(0), rowId, contactPhoto);
else
return new Recipient(displayName, null, rowId, contactPhoto);
if (cursor.moveToFirst())
return new Recipient(displayName, cursor.getString(0), contactUri, contactPhoto);
else
return new Recipient(displayName, null, contactUri, contactPhoto);
}
} finally {
cursor.close();
@ -75,15 +74,15 @@ public class NewRecipientProvider extends RecipientProvider {
try {
if (cursor != null && cursor.moveToFirst()) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, cursor.getLong(2));
Bitmap contactPhoto = getContactPhoto(context, contactUri);
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
Bitmap contactPhoto = getContactPhoto(context, contactUri);
Recipient recipient = new Recipient(cursor.getString(0), number, cursor.getLong(2), contactPhoto);
return recipient;
Recipient recipient = new Recipient(cursor.getString(0), number, contactUri, contactPhoto);
return recipient;
}
} finally {
if (cursor != null)
cursor.close();
cursor.close();
}
return null;
@ -94,24 +93,8 @@ public class NewRecipientProvider extends RecipientProvider {
if (inputStream == null)
return getDefaultContactPhoto(context);
// return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture);
else
return BitmapFactory.decodeStream(inputStream);
}
@Override
public void viewContact(Context context, Recipient recipient) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, recipient.getPersonId());
Intent intent = new Intent(Intent.ACTION_VIEW, contactUri);
context.startActivity(intent);
}
@Override
public void addContact(Context context, Recipient recipient) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, recipient.getNumber());
context.startActivity(intent);
}
}

View File

@ -1,94 +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.recipients;
import org.thoughtcrime.securesms.R;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.Contacts.Intents.Insert;
import android.util.Log;
public class OldRecipientProvider extends RecipientProvider {
private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" +Contacts.Phones.NUMBER + ",?)";
@SuppressWarnings("deprecation")
private static final String[] CALLER_ID_PROJECTION = new String[] {
// Contacts.People.Phones.NUMBER, // 0
// Contacts.People.Phones.LABEL, // 1
Contacts.People.NAME, // 2
Contacts.Phones.PERSON_ID, // 3
Contacts.People.Phones.NUMBER,
};
@Override
public Recipient getRecipient(Context context, Uri uri) {
Cursor cursor = context.getContentResolver().query(uri, new String[] {Contacts.People.NAME, Contacts.People._ID, Contacts.People.NUMBER}, null, null, null);
try {
if (cursor.moveToNext()) {
return new Recipient(cursor.getString(0), cursor.getString(2), cursor.getLong(1),
Contacts.People.loadContactPhoto(context, uri, R.drawable.ic_contact_picture, null));
}
} finally {
cursor.close();
}
return null;
}
@Override
public Recipient getRecipient(Context context, String number) {
String arguments[] = {number};
Cursor cursor = context.getContentResolver().query(Contacts.Phones.CONTENT_URI, CALLER_ID_PROJECTION,
CALLER_ID_SELECTION, arguments, null);
try {
if (cursor.moveToFirst()) {
Uri personUri = Uri.withAppendedPath(Contacts.People.CONTENT_URI, cursor.getLong(1)+"");
Recipient recipient = new Recipient(cursor.getString(0), number, cursor.getLong(1),
Contacts.People.loadContactPhoto(context, personUri, R.drawable.ic_contact_picture, null));
return recipient;
}
} finally {
cursor.close();
}
return null;
}
@Override
public void viewContact(Context context, Recipient recipient) {
Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, recipient.getPersonId());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
}
@Override
public void addContact(Context context, Recipient recipient) {
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(Contacts.People.CONTENT_ITEM_TYPE);
intent.putExtra(Insert.PHONE, recipient.getNumber());
context.startActivity(intent);
}
}

View File

@ -17,6 +17,7 @@
package org.thoughtcrime.securesms.recipients;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
@ -34,12 +35,12 @@ public class Recipient implements Parcelable {
private final String name;
private final String number;
private long personId = -1;
private Uri contactUri;
private Bitmap contactPhoto;
public Recipient(String name, String number, long personId, Bitmap contactPhoto) {
public Recipient(String name, String number, Uri contactUri, Bitmap contactPhoto) {
this(name, number, contactPhoto);
this.personId = personId;
this.contactUri = contactUri;
}
public Recipient(String name, String number, Bitmap contactPhoto) {
@ -49,12 +50,13 @@ public class Recipient implements Parcelable {
}
public Recipient(Parcel in) {
this.name = in.readString();
this.number = in.readString();
this.name = in.readString();
this.number = in.readString();
this.contactUri = in.readParcelable(null);
}
public long getPersonId() {
return personId;
public Uri getContactUri() {
return this.contactUri;
}
public String getName() {
@ -66,13 +68,13 @@ public class Recipient implements Parcelable {
}
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(number);
dest.writeParcelable(contactUri, 0);
}
public String toShortString() {

View File

@ -16,6 +16,16 @@
*/
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.NumberUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -23,42 +33,13 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.util.NumberUtil;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
public class RecipientFactory {
private static final Map<String,Recipient> recipientCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
private static final Map<String,Recipient> recipientIdCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
private static final Map<Uri,Recipient> recipientUriCache = Collections.synchronizedMap(new HashMap<Uri,Recipient>());
private static RecipientProvider provider;
static {
String className;
int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
if (sdkVersion <= Build.VERSION_CODES.DONUT) className = "OldRecipientProvider";
else className = "NewRecipientProvider";
try {
Class<? extends RecipientProvider> clazz =
Class.forName("org.thoughtcrime.securesms.recipients." + className)
.asSubclass(RecipientProvider.class);
provider = clazz.newInstance();
} catch (Exception e) {
throw new AssertionError(e);
}
}
private static final RecipientProvider provider = new NewRecipientProvider();
public static RecipientProvider getRecipientProvider() {
return provider;

View File

@ -16,21 +16,19 @@
*/
package org.thoughtcrime.securesms.recipients;
import org.thoughtcrime.securesms.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import org.thoughtcrime.securesms.R;
public abstract class RecipientProvider {
private static Bitmap defaultContactPhoto;
public abstract Recipient getRecipient(Context context, String number);
public abstract Recipient getRecipient(Context context, Uri uri);
public abstract void viewContact(Context context, Recipient recipient);
public abstract void addContact(Context context, Recipient recipient);
public Bitmap getDefaultContactPhoto(Context context) {
synchronized (this) {