From ca128f033c261a2981fd5abda0ab0f2037a5557f Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Tue, 4 Oct 2011 12:32:30 +0200 Subject: [PATCH] Old API contact picture from number (a bit hackish) To workaround exception on setting view backgroung from uri, the people uri is returned and the bitmap is extracted directly in the LinphoneUtils helper class. --- src/org/linphone/ConferenceActivity.java | 4 +- .../linphone/ContactPickerActivityOld.java | 38 +++++++++++++++++-- src/org/linphone/IncomingCallActivity.java | 2 +- src/org/linphone/LinphoneUtils.java | 10 ++++- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/org/linphone/ConferenceActivity.java b/src/org/linphone/ConferenceActivity.java index 8d815a4c4..0620c7968 100644 --- a/src/org/linphone/ConferenceActivity.java +++ b/src/org/linphone/ConferenceActivity.java @@ -180,7 +180,7 @@ public class ConferenceActivity extends ListActivity implements getContentResolver(), currentCall.getRemoteAddress().getUserName(), currentCall.getRemoteAddress().getDomain()); - LinphoneUtils.setImagePictureFromUri(view, picture, R.drawable.unknown_person); + LinphoneUtils.setImagePictureFromUri(this, view, picture, R.drawable.unknown_person); view.setVisibility(VISIBLE); } @@ -535,7 +535,7 @@ public class ConferenceActivity extends ListActivity implements String domain = call.getRemoteAddress().getDomain(); // May be greatly sped up using a drawable cache Uri uri = LinphoneUtils.findPictureOfContact(getContentResolver(), username, domain); - LinphoneUtils.setImagePictureFromUri(pictureView, uri, R.drawable.unknown_person); + LinphoneUtils.setImagePictureFromUri(ConferenceActivity.this, pictureView, uri, R.drawable.unknown_person); pictureView.setVisibility(VISIBLE); } else { pictureView.setVisibility(GONE); diff --git a/src/org/linphone/ContactPickerActivityOld.java b/src/org/linphone/ContactPickerActivityOld.java index 38c939ce5..1d8eeae46 100644 --- a/src/org/linphone/ContactPickerActivityOld.java +++ b/src/org/linphone/ContactPickerActivityOld.java @@ -49,17 +49,25 @@ public class ContactPickerActivityOld extends Activity { } - private Uri getPhotoUri(Uri photoUri) { - Cursor cursor = getContentResolver().query(photoUri, new String[]{Photos.DATA}, null, null, null); + private Uri getPhotoUri(Uri photoUriToTest) { + return retrievePhotoUri(getContentResolver(), photoUriToTest); + } + + private static Uri retrievePhotoUri(ContentResolver resolver, Uri photoUriToTest) { + Cursor cursor = resolver.query(photoUriToTest, new String[]{Photos.DATA}, null, null, null); try { if (cursor == null || !cursor.moveToNext()) { return null; } byte[] data = cursor.getBlob(0); if (data == null) { + // TODO: simplify all this stuff + // which is here only to check that the + // photoUri really points to some data. + // Not retrieving the data now would be better. return null; } - return photoUri; + return photoUriToTest; } finally { if (cursor != null) cursor.close(); } @@ -93,7 +101,29 @@ public class ContactPickerActivityOld extends Activity { } } + private static Uri retrievePhotoUriAndCloseC(ContentResolver resolver, Cursor c, String column) { + if (c == null) return null; + while (c.moveToNext()) { + long id = c.getLong(c.getColumnIndex(column)); + Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, id); + Uri potentialPictureUri = Uri.withAppendedPath(personUri, Contacts.Photos.CONTENT_DIRECTORY); + Uri pictureUri = retrievePhotoUri(resolver, potentialPictureUri); + if (pictureUri != null) { + c.close(); + return personUri; // FIXME, see LinphoneUtils + } + } + c.close(); + return null; + } + public static Uri findUriPictureOfContact(ContentResolver resolver, String username, String domain) { - throw new RuntimeException("not implemented"); + // A direct qery on the number column doesn't work as the number is stored + // with hyphens inside it. + Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode("0952636505")); + String[] projection = {Contacts.Phones.PERSON_ID}; + Cursor c = resolver.query(contactUri, projection, null, null, null); + + return retrievePhotoUriAndCloseC(resolver, c, Contacts.Phones.PERSON_ID); } } diff --git a/src/org/linphone/IncomingCallActivity.java b/src/org/linphone/IncomingCallActivity.java index 5beb9bfbb..a43c3d02b 100644 --- a/src/org/linphone/IncomingCallActivity.java +++ b/src/org/linphone/IncomingCallActivity.java @@ -95,7 +95,7 @@ public class IncomingCallActivity extends Activity implements OnClickListener { String domain = address.getDomain(); // May be greatly sped up using a drawable cache Uri uri = LinphoneUtils.findPictureOfContact(getContentResolver(), username, domain); - LinphoneUtils.setImagePictureFromUri(mPictureView, uri, R.drawable.unknown_person); + LinphoneUtils.setImagePictureFromUri(this, mPictureView, uri, R.drawable.unknown_person); super.onResume(); } diff --git a/src/org/linphone/LinphoneUtils.java b/src/org/linphone/LinphoneUtils.java index 7234b0758..1a3022927 100644 --- a/src/org/linphone/LinphoneUtils.java +++ b/src/org/linphone/LinphoneUtils.java @@ -29,6 +29,7 @@ import org.linphone.mediastream.video.capture.hwconf.Hacks; import android.app.Activity; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -105,7 +106,7 @@ public final class LinphoneUtils { return null; } - public static void setImagePictureFromUri(ImageView view, Uri uri, int notFoundResource) { + public static void setImagePictureFromUri(Context c, ImageView view, Uri uri, int notFoundResource) { if (uri == null) { view.setImageResource(notFoundResource); return; @@ -115,7 +116,12 @@ public final class LinphoneUtils { if (bm == null) view.setImageResource(notFoundResource); view.setImageBitmap(bm); } else { - view.setImageURI(uri); + if (Version.sdkAboveOrEqual(Version.API06_ECLAIR_20)) { + view.setImageURI(uri); + } else { + Bitmap bitmap = android.provider.Contacts.People.loadContactPhoto(c, uri, notFoundResource, null); + view.setImageBitmap(bitmap); + } } }