Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
[AD] Avoid async query to get thumbnail data
Browse files Browse the repository at this point in the history
This fixes #12 by just executing the query synchronously on our own thread
instead of using an async query which requires a looper.
  • Loading branch information
ermau committed Sep 10, 2013
1 parent eed7b38 commit 163f297
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions MonoDroid/Xamarin.Mobile/Contacts/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Android.Content;
using Android.Database;
using Android.Graphics;
using Android.OS;
using Android.Provider;
using Xamarin.Media;

Expand Down Expand Up @@ -150,56 +152,50 @@ public IEnumerable<Phone> Phones
}

public Bitmap GetThumbnail()
{
byte[] data = GetThumbnailBytes();
return (data == null) ? null : BitmapFactory.DecodeByteArray (data, 0, data.Length);
}

public Task<MediaFile> SaveThumbnailAsync (string path)
{
if (path == null)
throw new ArgumentNullException ("path");

return Task.Factory.StartNew (() => {
byte[] bytes = GetThumbnailBytes();
if (bytes == null)
return null;
File.WriteAllBytes (path, bytes);
return new MediaFile (path, deletePathOnDispose: false);
});
}

byte[] GetThumbnailBytes()
{
string lookupColumn = (IsAggregate)
? ContactsContract.ContactsColumns.LookupKey
: ContactsContract.RawContactsColumns.ContactId;

ICursor c = null;
try
{
try {
c = this.content.Query (ContactsContract.Data.ContentUri, new[] { ContactsContract.CommonDataKinds.Photo.PhotoColumnId, ContactsContract.DataColumns.Mimetype },
lookupColumn + "=? AND " + ContactsContract.DataColumns.Mimetype + "=?", new[] { Id, ContactsContract.CommonDataKinds.Photo.ContentItemType }, null);

while (c.MoveToNext())
{
while (c.MoveToNext()) {
byte[] tdata = c.GetBlob (c.GetColumnIndex (ContactsContract.CommonDataKinds.Photo.PhotoColumnId));
if (tdata != null)
return BitmapFactory.DecodeByteArray (tdata, 0, tdata.Length);
return tdata;
}
}
finally
{
} finally {
if (c != null)
c.Close();
}

return null;
}

public Task<MediaFile> SaveThumbnailAsync (string path)
{
if (path == null)
throw new ArgumentNullException ("path");

string lookupColumn = (IsAggregate)
? ContactsContract.ContactsColumns.LookupKey
: ContactsContract.RawContactsColumns.ContactId;

AsyncQuery<byte[]> query = new AsyncQuery<byte[]> (this.content, c => c.GetBlob (c.GetColumnIndex (ContactsContract.CommonDataKinds.Photo.PhotoColumnId)));
query.StartQuery (0, null, ContactsContract.Data.ContentUri, new[] { ContactsContract.CommonDataKinds.Photo.PhotoColumnId, ContactsContract.DataColumns.Mimetype },
lookupColumn + "=? AND " + ContactsContract.DataColumns.Mimetype + "=?", new[] { Id, ContactsContract.CommonDataKinds.Photo.ContentItemType }, null);

return query.Task.ContinueWith (t =>
{
if (t.Result == null)
return null;
File.WriteAllBytes (path, t.Result);
return new MediaFile (path, deletePathOnDispose: false);
}, TaskScheduler.Default);
}

private readonly ContentResolver content;
}
}

0 comments on commit 163f297

Please sign in to comment.