diff --git a/src/org/thoughtcrime/securesms/recipients/NewRecipientProvider.java b/src/org/thoughtcrime/securesms/recipients/NewRecipientProvider.java deleted file mode 100644 index 3b66a3a39a..0000000000 --- a/src/org/thoughtcrime/securesms/recipients/NewRecipientProvider.java +++ /dev/null @@ -1,102 +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 . - */ -package org.thoughtcrime.securesms.recipients; - -import android.content.Context; -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[] { - PhoneLookup.DISPLAY_NAME, - PhoneLookup.LOOKUP_KEY, - PhoneLookup._ID, - }; - - 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) { - Cursor cursor = context.getContentResolver().query(uri, CONTENT_URI_PROJECTION, null, null, null); - - try { - if (cursor.moveToFirst()) { - long rowId = cursor.getLong(0); - Uri contactUri = Contacts.getLookupUri(rowId, cursor.getString(2)); - Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI, - rowId+"")); - 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); - - if (cursor.moveToFirst()) - return new Recipient(displayName, cursor.getString(0), contactUri, contactPhoto); - else - return new Recipient(displayName, null, contactUri, contactPhoto); - } - } finally { - cursor.close(); - } - - return null; - } - - @Override - public Recipient getRecipient(Context context, String number) { - Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); - Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION, null, null, null); - - try { - if (cursor != null && cursor.moveToFirst()) { - Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1)); - Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI, - cursor.getLong(2)+"")); - - Recipient recipient = new Recipient(cursor.getString(0), number, contactUri, contactPhoto); - return recipient; - } - } finally { - if (cursor != null) - cursor.close(); - } - - return null; - } - - private Bitmap getContactPhoto(Context context, Uri uri) { - InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri); - - if (inputStream == null) - return getDefaultContactPhoto(context); - else - return BitmapFactory.decodeStream(inputStream); - } - -} diff --git a/src/org/thoughtcrime/securesms/recipients/RecipientFactory.java b/src/org/thoughtcrime/securesms/recipients/RecipientFactory.java index b0616c34cf..573ec1b9bf 100644 --- a/src/org/thoughtcrime/securesms/recipients/RecipientFactory.java +++ b/src/org/thoughtcrime/securesms/recipients/RecipientFactory.java @@ -39,7 +39,7 @@ public class RecipientFactory { private static final Map recipientIdCache = Collections.synchronizedMap(new LRUHashMap()); private static final Map recipientUriCache = Collections.synchronizedMap(new HashMap()); - private static final RecipientProvider provider = new NewRecipientProvider(); + private static final RecipientProvider provider = new RecipientProvider(); public static RecipientProvider getRecipientProvider() { return provider; diff --git a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java index 783b947e57..0d3d735b31 100644 --- a/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java +++ b/src/org/thoughtcrime/securesms/recipients/RecipientProvider.java @@ -17,18 +17,80 @@ package org.thoughtcrime.securesms.recipients; import android.content.Context; +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 org.thoughtcrime.securesms.R; -public abstract class RecipientProvider { +import java.io.InputStream; + +public class RecipientProvider { private static Bitmap defaultContactPhoto; - public abstract Recipient getRecipient(Context context, String number); - public abstract Recipient getRecipient(Context context, Uri uri); + private static final String[] CALLER_ID_PROJECTION = new String[] { + PhoneLookup.DISPLAY_NAME, + PhoneLookup.LOOKUP_KEY, + PhoneLookup._ID, + }; + + private static final String[] CONTENT_URI_PROJECTION = new String[] { + ContactsContract.Contacts._ID, + ContactsContract.Contacts.DISPLAY_NAME, + ContactsContract.Contacts.LOOKUP_KEY + }; + + 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 contactUri = Contacts.getLookupUri(rowId, cursor.getString(2)); + Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI, + rowId+"")); + 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); + + if (cursor.moveToFirst()) + return new Recipient(displayName, cursor.getString(0), contactUri, contactPhoto); + else + return new Recipient(displayName, null, contactUri, contactPhoto); + } + } finally { + cursor.close(); + } + + return null; + } + + public Recipient getRecipient(Context context, String number) { + Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); + Cursor cursor = context.getContentResolver().query(uri, CALLER_ID_PROJECTION, null, null, null); + + try { + if (cursor != null && cursor.moveToFirst()) { + Uri contactUri = Contacts.getLookupUri(cursor.getLong(2), cursor.getString(1)); + Bitmap contactPhoto = getContactPhoto(context, Uri.withAppendedPath(Contacts.CONTENT_URI, + cursor.getLong(2)+"")); + + Recipient recipient = new Recipient(cursor.getString(0), number, contactUri, contactPhoto); + return recipient; + } + } finally { + if (cursor != null) + cursor.close(); + } + + return null; + } public Bitmap getDefaultContactPhoto(Context context) { synchronized (this) { @@ -38,4 +100,14 @@ public abstract class RecipientProvider { return defaultContactPhoto; } + + private Bitmap getContactPhoto(Context context, Uri uri) { + InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri); + + if (inputStream == null) + return getDefaultContactPhoto(context); + else + return BitmapFactory.decodeStream(inputStream); + } + } \ No newline at end of file