From c469c9afd2cfe011f4c4a1c5d711a12b3a6a1524 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 30 Apr 2012 20:54:13 -0700 Subject: [PATCH] Fixed picture loading stuff --- .../astrid/calls/MissedCallActivity.java | 21 ++- .../calls/PhoneStateChangedReceiver.java | 133 +++++++++--------- astrid/res/layout/astrid_missed_call_view.xml | 3 +- astrid/res/values/strings-core.xml | 2 +- 4 files changed, 82 insertions(+), 77 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/calls/MissedCallActivity.java b/astrid/plugin-src/com/todoroo/astrid/calls/MissedCallActivity.java index 6d4d44ef5..d3120c5b6 100644 --- a/astrid/plugin-src/com/todoroo/astrid/calls/MissedCallActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/calls/MissedCallActivity.java @@ -1,12 +1,17 @@ package com.todoroo.astrid.calls; +import java.io.InputStream; + import android.app.Activity; import android.app.AlertDialog; +import android.content.ContentUris; import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; +import android.provider.ContactsContract; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; @@ -30,7 +35,7 @@ public class MissedCallActivity extends Activity { public static final String EXTRA_NUMBER = "number"; //$NON-NLS-1$ public static final String EXTRA_NAME = "name"; //$NON-NLS-1$ public static final String EXTRA_TIME = "time"; //$NON-NLS-1$ - public static final String EXTRA_PHOTO = "photo"; //$NON-NLS-1$ + public static final String EXTRA_CONTACT_ID = "contactId"; //$NON-NLS-1$ private static final String PREF_IGNORE_PRESSES = "missedCallsIgnored"; //$NON-NLS-1$ @@ -98,7 +103,7 @@ public class MissedCallActivity extends Activity { name = intent.getStringExtra(EXTRA_NAME); number = intent.getStringExtra(EXTRA_NUMBER); timeString = intent.getStringExtra(EXTRA_TIME); - String picture = intent.getStringExtra(EXTRA_PHOTO); + long contactId = intent.getExtras().getLong(EXTRA_CONTACT_ID); int color = ThemeService.getThemeColor(); @@ -111,10 +116,14 @@ public class MissedCallActivity extends Activity { TextUtils.isEmpty(name) ? number : name, timeString)); ImageView pictureView = ((ImageView) findViewById(R.id.contact_picture)); - if (TextUtils.isEmpty(picture)) - pictureView.setImageDrawable(getResources().getDrawable(R.drawable.ic_contact_picture_2)); - else - pictureView.setImageURI(Uri.parse(picture)); + if (contactId >= 0) { + Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); + InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri); + if (input == null) + return; + pictureView.setImageBitmap(BitmapFactory.decodeStream(input)); + pictureView.setVisibility(View.VISIBLE); + } Resources r = getResources(); returnCallButton.setBackgroundColor(r.getColor(color)); diff --git a/astrid/plugin-src/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java b/astrid/plugin-src/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java index 7fe0d8c4a..4063de6d1 100644 --- a/astrid/plugin-src/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java +++ b/astrid/plugin-src/com/todoroo/astrid/calls/PhoneStateChangedReceiver.java @@ -13,6 +13,7 @@ import android.telephony.TelephonyManager; import android.text.TextUtils; import com.timsu.astrid.R; +import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; @@ -22,7 +23,7 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver { private static final String PREF_LAST_INCOMING_NUMBER = "last_incoming_number"; @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(final Context context, Intent intent) { if (!Preferences.getBoolean(R.string.p_field_missed_calls, true)) { Preferences.clear(PREF_LAST_INCOMING_NUMBER); return; @@ -37,57 +38,72 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver { Preferences.setString(PREF_LAST_INCOMING_NUMBER, number); } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) { - String lastNumber = Preferences.getStringValue(PREF_LAST_INCOMING_NUMBER); - if (TextUtils.isEmpty(lastNumber)) + final String lastNumber = Preferences.getStringValue(PREF_LAST_INCOMING_NUMBER); + if (TextUtils.isEmpty(lastNumber)) { + System.err.println("Empty number"); return; + } Preferences.clear(PREF_LAST_INCOMING_NUMBER); - Cursor calls = context.getContentResolver().query( - Calls.CONTENT_URI, - null, - Calls.TYPE + " = ? AND " + Calls.NEW + " = ?", - new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, - Calls.DATE + " DESC" - ); - - try { - if (calls.moveToFirst()) { - int numberIndex = calls.getColumnIndex(Calls.NUMBER); - String number = calls.getString(numberIndex); - - // Check for phone number match - if (!lastNumber.equals(digitsOnly(number))) - return; - - // If a lot of time has passed since the most recent missed call, ignore - // It could be the same person calling you back before you call them back, - // but if you answer this time, the missed call will still be in the database - // and will be processed again. - int dateIndex = calls.getColumnIndex(Calls.DATE); - long date = calls.getLong(dateIndex); - if (DateUtilities.now() - date < 2 * DateUtilities.ONE_MINUTE) - return; - - int nameIndex = calls.getColumnIndex(Calls.CACHED_NAME); - String name = calls.getString(nameIndex); - - int timeIndex = calls.getColumnIndex(Calls.DATE); - long time = calls.getLong(timeIndex); - String timeString = DateUtilities.getTimeString(context, new Date(time)); - - String photo = getContactPhotoFromNumber(context, number); - - Intent missedCallIntent = new Intent(context, MissedCallActivity.class); - missedCallIntent.putExtra(MissedCallActivity.EXTRA_NUMBER, number); - missedCallIntent.putExtra(MissedCallActivity.EXTRA_NAME, name); - missedCallIntent.putExtra(MissedCallActivity.EXTRA_TIME, timeString); - missedCallIntent.putExtra(MissedCallActivity.EXTRA_PHOTO, photo); - missedCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); - context.startActivity(missedCallIntent); + + new Thread() { + @Override + public void run() { + AndroidUtilities.sleepDeep(3000L); + Cursor calls = context.getContentResolver().query( + Calls.CONTENT_URI, + null, + Calls.TYPE + " = ? AND " + Calls.NEW + " = ?", + new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, + Calls.DATE + " DESC" + ); + try { + if (calls.moveToFirst()) { + System.err.println("Processing"); + int numberIndex = calls.getColumnIndex(Calls.NUMBER); + String number = calls.getString(numberIndex); + + // Check for phone number match + if (!lastNumber.equals(digitsOnly(number))) { + System.err.println("Number mismatch"); + return; + } + + // If a lot of time has passed since the most recent missed call, ignore + // It could be the same person calling you back before you call them back, + // but if you answer this time, the missed call will still be in the database + // and will be processed again. + int dateIndex = calls.getColumnIndex(Calls.DATE); + long date = calls.getLong(dateIndex); + if (DateUtilities.now() - date > 2 * DateUtilities.ONE_MINUTE) { + System.err.println("Date: " + date); + System.err.println("Diff: " + (DateUtilities.now() - date)); + System.err.println("Time mismatch"); + return; + } + + int nameIndex = calls.getColumnIndex(Calls.CACHED_NAME); + String name = calls.getString(nameIndex); + + int timeIndex = calls.getColumnIndex(Calls.DATE); + long time = calls.getLong(timeIndex); + String timeString = DateUtilities.getTimeString(context, new Date(time)); + + long contactId = getContactIdFromNumber(context, number); + + Intent missedCallIntent = new Intent(context, MissedCallActivity.class); + missedCallIntent.putExtra(MissedCallActivity.EXTRA_NUMBER, number); + missedCallIntent.putExtra(MissedCallActivity.EXTRA_NAME, name); + missedCallIntent.putExtra(MissedCallActivity.EXTRA_TIME, timeString); + missedCallIntent.putExtra(MissedCallActivity.EXTRA_CONTACT_ID, contactId); + missedCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); + context.startActivity(missedCallIntent); + } + } finally { + calls.close(); + } } - } finally { - calls.close(); - } + }.start(); } else { System.err.println("ASTRID Other state: " + state); } @@ -103,10 +119,6 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver { return builder.toString(); } - private String getContactPhotoFromNumber(Context context, String number) { - return getPhotoForContactId(context, getContactIdFromNumber(context, number)); - } - private long getContactIdFromNumber(Context context, String number) { Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); Cursor c = context.getContentResolver().query(contactUri, new String[] { ContactsContract.PhoneLookup._ID }, null, null, null); @@ -122,21 +134,4 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver { return -1; } - private String getPhotoForContactId(Context context, long contactId) { - if (contactId < 0) - return ""; - Cursor c = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_FILTER_URI, - new String[] { ContactsContract.Contacts.PHOTO_URI }, - ContactsContract.Contacts._ID + " = ?", new String[] { Long.toString(contactId) }, null); - try { - if (c.moveToFirst()) { - String uri = c.getString(c.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)); - return uri; - } - } finally { - c.close(); - } - return ""; - } - } diff --git a/astrid/res/layout/astrid_missed_call_view.xml b/astrid/res/layout/astrid_missed_call_view.xml index fd6b4946c..d5b27eb55 100644 --- a/astrid/res/layout/astrid_missed_call_view.xml +++ b/astrid/res/layout/astrid_missed_call_view.xml @@ -20,6 +20,8 @@ @@ -29,7 +31,6 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="3dip" - android:layout_marginLeft="5dip" android:textSize="20sp" android:textColor="@android:color/white" android:layout_weight="1" diff --git a/astrid/res/values/strings-core.xml b/astrid/res/values/strings-core.xml index fd62ea57b..e4e9dd612 100644 --- a/astrid/res/values/strings-core.xml +++ b/astrid/res/values/strings-core.xml @@ -466,7 +466,7 @@ - %1$s called at %2$s + %1$s\ncalled at %2$s Call now