mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
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:
parent
a834ccad55
commit
c6ae07ee5c
@ -27,6 +27,8 @@ import android.os.Environment;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.provider.Contacts.Intents;
|
||||||
|
import android.provider.ContactsContract.QuickContact;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
@ -249,10 +251,24 @@ public class ConversationItem extends LinearLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setBodyImage(MessageRecord messageRecord) {
|
private void setBodyImage(MessageRecord messageRecord) {
|
||||||
Recipient recipient = messageRecord.getMessageRecipient();
|
final Recipient recipient = messageRecord.getMessageRecipient();
|
||||||
|
|
||||||
if (!messageRecord.isOutgoing()) contactPhoto.setImageBitmap(recipient.getContactPhoto());
|
if (!messageRecord.isOutgoing()) {
|
||||||
else setContactPhotoForUserIdentity();
|
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);
|
contactPhoto.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
|
@ -16,19 +16,17 @@
|
|||||||
*/
|
*/
|
||||||
package org.thoughtcrime.securesms.recipients;
|
package org.thoughtcrime.securesms.recipients;
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
|
|
||||||
import android.content.ContentUris;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
|
import android.provider.ContactsContract.Contacts;
|
||||||
import android.provider.ContactsContract.PhoneLookup;
|
import android.provider.ContactsContract.PhoneLookup;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class NewRecipientProvider extends RecipientProvider {
|
public class NewRecipientProvider extends RecipientProvider {
|
||||||
|
|
||||||
private static final String[] CALLER_ID_PROJECTION = new String[] {
|
private static final String[] CALLER_ID_PROJECTION = new String[] {
|
||||||
@ -40,6 +38,7 @@ public class NewRecipientProvider extends RecipientProvider {
|
|||||||
private static final String[] CONTENT_URI_PROJECTION = new String[] {
|
private static final String[] CONTENT_URI_PROJECTION = new String[] {
|
||||||
ContactsContract.Contacts._ID,
|
ContactsContract.Contacts._ID,
|
||||||
ContactsContract.Contacts.DISPLAY_NAME,
|
ContactsContract.Contacts.DISPLAY_NAME,
|
||||||
|
ContactsContract.Contacts.LOOKUP_KEY
|
||||||
};
|
};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -49,17 +48,17 @@ public class NewRecipientProvider extends RecipientProvider {
|
|||||||
try {
|
try {
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
long rowId = cursor.getLong(0);
|
long rowId = cursor.getLong(0);
|
||||||
Uri photoLookupUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);
|
Uri contactUri = Contacts.getLookupUri(rowId, cursor.getString(2));
|
||||||
|
Bitmap contactPhoto = getContactPhoto(context, contactUri);
|
||||||
Bitmap contactPhoto = getContactPhoto(context, photoLookupUri);
|
|
||||||
String displayName = cursor.getString(1);
|
String displayName = cursor.getString(1);
|
||||||
cursor.close();
|
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())
|
if (cursor.moveToFirst())
|
||||||
return new Recipient(displayName, cursor.getString(0), rowId, contactPhoto);
|
return new Recipient(displayName, cursor.getString(0), contactUri, contactPhoto);
|
||||||
else
|
else
|
||||||
return new Recipient(displayName, null, rowId, contactPhoto);
|
return new Recipient(displayName, null, contactUri, contactPhoto);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
cursor.close();
|
cursor.close();
|
||||||
@ -75,10 +74,10 @@ public class NewRecipientProvider extends RecipientProvider {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (cursor != null && cursor.moveToFirst()) {
|
if (cursor != null && cursor.moveToFirst()) {
|
||||||
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, cursor.getLong(2));
|
Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1));
|
||||||
Bitmap contactPhoto = getContactPhoto(context, contactUri);
|
Bitmap contactPhoto = getContactPhoto(context, contactUri);
|
||||||
|
|
||||||
Recipient recipient = new Recipient(cursor.getString(0), number, cursor.getLong(2), contactPhoto);
|
Recipient recipient = new Recipient(cursor.getString(0), number, contactUri, contactPhoto);
|
||||||
return recipient;
|
return recipient;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
@ -94,24 +93,8 @@ public class NewRecipientProvider extends RecipientProvider {
|
|||||||
|
|
||||||
if (inputStream == null)
|
if (inputStream == null)
|
||||||
return getDefaultContactPhoto(context);
|
return getDefaultContactPhoto(context);
|
||||||
// return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_contact_picture);
|
|
||||||
else
|
else
|
||||||
return BitmapFactory.decodeStream(inputStream);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -17,6 +17,7 @@
|
|||||||
package org.thoughtcrime.securesms.recipients;
|
package org.thoughtcrime.securesms.recipients;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
@ -34,12 +35,12 @@ public class Recipient implements Parcelable {
|
|||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final String number;
|
private final String number;
|
||||||
private long personId = -1;
|
private Uri contactUri;
|
||||||
private Bitmap contactPhoto;
|
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(name, number, contactPhoto);
|
||||||
this.personId = personId;
|
this.contactUri = contactUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Recipient(String name, String number, Bitmap contactPhoto) {
|
public Recipient(String name, String number, Bitmap contactPhoto) {
|
||||||
@ -51,10 +52,11 @@ public class Recipient implements Parcelable {
|
|||||||
public Recipient(Parcel in) {
|
public Recipient(Parcel in) {
|
||||||
this.name = in.readString();
|
this.name = in.readString();
|
||||||
this.number = in.readString();
|
this.number = in.readString();
|
||||||
|
this.contactUri = in.readParcelable(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getPersonId() {
|
public Uri getContactUri() {
|
||||||
return personId;
|
return this.contactUri;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -66,13 +68,13 @@ public class Recipient implements Parcelable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int describeContents() {
|
public int describeContents() {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
dest.writeString(name);
|
dest.writeString(name);
|
||||||
dest.writeString(number);
|
dest.writeString(number);
|
||||||
|
dest.writeParcelable(contactUri, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toShortString() {
|
public String toShortString() {
|
||||||
|
@ -16,6 +16,16 @@
|
|||||||
*/
|
*/
|
||||||
package org.thoughtcrime.securesms.recipients;
|
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.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -23,42 +33,13 @@ import java.util.LinkedHashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.StringTokenizer;
|
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 {
|
public class RecipientFactory {
|
||||||
|
|
||||||
private static final Map<String,Recipient> recipientCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
|
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<String,Recipient> recipientIdCache = Collections.synchronizedMap(new LRUHashMap<String,Recipient>());
|
||||||
private static final Map<Uri,Recipient> recipientUriCache = Collections.synchronizedMap(new HashMap<Uri,Recipient>());
|
private static final Map<Uri,Recipient> recipientUriCache = Collections.synchronizedMap(new HashMap<Uri,Recipient>());
|
||||||
|
|
||||||
private static RecipientProvider provider;
|
private static final RecipientProvider provider = new NewRecipientProvider();
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static RecipientProvider getRecipientProvider() {
|
public static RecipientProvider getRecipientProvider() {
|
||||||
return provider;
|
return provider;
|
||||||
|
@ -16,21 +16,19 @@
|
|||||||
*/
|
*/
|
||||||
package org.thoughtcrime.securesms.recipients;
|
package org.thoughtcrime.securesms.recipients;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.R;
|
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.R;
|
||||||
|
|
||||||
public abstract class RecipientProvider {
|
public abstract class RecipientProvider {
|
||||||
|
|
||||||
private static Bitmap defaultContactPhoto;
|
private static Bitmap defaultContactPhoto;
|
||||||
|
|
||||||
public abstract Recipient getRecipient(Context context, String number);
|
public abstract Recipient getRecipient(Context context, String number);
|
||||||
public abstract Recipient getRecipient(Context context, Uri uri);
|
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) {
|
public Bitmap getDefaultContactPhoto(Context context) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
Loading…
Reference in New Issue
Block a user