Fixed picture loading stuff

pull/14/head
Sam Bosley 13 years ago
parent 48f0ec9ece
commit c469c9afd2

@ -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));

@ -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,11 +38,18 @@ 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);
new Thread() {
@Override
public void run() {
AndroidUtilities.sleepDeep(3000L);
Cursor calls = context.getContentResolver().query(
Calls.CONTENT_URI,
null,
@ -49,15 +57,17 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver {
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)))
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,
@ -65,8 +75,12 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver {
// and will be processed again.
int dateIndex = calls.getColumnIndex(Calls.DATE);
long date = calls.getLong(dateIndex);
if (DateUtilities.now() - date < 2 * DateUtilities.ONE_MINUTE)
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);
@ -75,19 +89,21 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver {
long time = calls.getLong(timeIndex);
String timeString = DateUtilities.getTimeString(context, new Date(time));
String photo = getContactPhotoFromNumber(context, number);
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_PHOTO, photo);
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();
}
}
}.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 "";
}
}

@ -20,6 +20,8 @@
<ImageView
android:id="@+id/contact_picture"
android:layout_marginTop="5dip"
android:layout_marginRight="10dip"
android:visibility="gone"
android:layout_width="46dip"
android:layout_height="46dip"
android:scaleType="fitCenter" />
@ -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"

@ -466,7 +466,7 @@
<!-- ===================================================== MissedCallActivity == -->
<!-- Missed call: return call (%1$s -> caller, %2$s -> time of call)-->
<string name="MCA_title"> %1$s called at %2$s</string>
<string name="MCA_title"> %1$s\ncalled at %2$s</string>
<!-- Missed call: return call -->
<string name="MCA_return_call">Call now</string>

Loading…
Cancel
Save