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; package com.todoroo.astrid.calls;
import java.io.InputStream;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; 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_NUMBER = "number"; //$NON-NLS-1$
public static final String EXTRA_NAME = "name"; //$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_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$ 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); name = intent.getStringExtra(EXTRA_NAME);
number = intent.getStringExtra(EXTRA_NUMBER); number = intent.getStringExtra(EXTRA_NUMBER);
timeString = intent.getStringExtra(EXTRA_TIME); timeString = intent.getStringExtra(EXTRA_TIME);
String picture = intent.getStringExtra(EXTRA_PHOTO); long contactId = intent.getExtras().getLong(EXTRA_CONTACT_ID);
int color = ThemeService.getThemeColor(); int color = ThemeService.getThemeColor();
@ -111,10 +116,14 @@ public class MissedCallActivity extends Activity {
TextUtils.isEmpty(name) ? number : name, timeString)); TextUtils.isEmpty(name) ? number : name, timeString));
ImageView pictureView = ((ImageView) findViewById(R.id.contact_picture)); ImageView pictureView = ((ImageView) findViewById(R.id.contact_picture));
if (TextUtils.isEmpty(picture)) if (contactId >= 0) {
pictureView.setImageDrawable(getResources().getDrawable(R.drawable.ic_contact_picture_2)); Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
else InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);
pictureView.setImageURI(Uri.parse(picture)); if (input == null)
return;
pictureView.setImageBitmap(BitmapFactory.decodeStream(input));
pictureView.setVisibility(View.VISIBLE);
}
Resources r = getResources(); Resources r = getResources();
returnCallButton.setBackgroundColor(r.getColor(color)); returnCallButton.setBackgroundColor(r.getColor(color));

@ -13,6 +13,7 @@ import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences; 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"; private static final String PREF_LAST_INCOMING_NUMBER = "last_incoming_number";
@Override @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)) { if (!Preferences.getBoolean(R.string.p_field_missed_calls, true)) {
Preferences.clear(PREF_LAST_INCOMING_NUMBER); Preferences.clear(PREF_LAST_INCOMING_NUMBER);
return; return;
@ -37,57 +38,72 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver {
Preferences.setString(PREF_LAST_INCOMING_NUMBER, number); Preferences.setString(PREF_LAST_INCOMING_NUMBER, number);
} else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) { } else if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
String lastNumber = Preferences.getStringValue(PREF_LAST_INCOMING_NUMBER); final String lastNumber = Preferences.getStringValue(PREF_LAST_INCOMING_NUMBER);
if (TextUtils.isEmpty(lastNumber)) if (TextUtils.isEmpty(lastNumber)) {
System.err.println("Empty number");
return; return;
}
Preferences.clear(PREF_LAST_INCOMING_NUMBER); Preferences.clear(PREF_LAST_INCOMING_NUMBER);
Cursor calls = context.getContentResolver().query(
Calls.CONTENT_URI, new Thread() {
null, @Override
Calls.TYPE + " = ? AND " + Calls.NEW + " = ?", public void run() {
new String[] { Integer.toString(Calls.MISSED_TYPE), "1" }, AndroidUtilities.sleepDeep(3000L);
Calls.DATE + " DESC" Cursor calls = context.getContentResolver().query(
); Calls.CONTENT_URI,
null,
try { Calls.TYPE + " = ? AND " + Calls.NEW + " = ?",
if (calls.moveToFirst()) { new String[] { Integer.toString(Calls.MISSED_TYPE), "1" },
int numberIndex = calls.getColumnIndex(Calls.NUMBER); Calls.DATE + " DESC"
String number = calls.getString(numberIndex); );
try {
// Check for phone number match if (calls.moveToFirst()) {
if (!lastNumber.equals(digitsOnly(number))) System.err.println("Processing");
return; int numberIndex = calls.getColumnIndex(Calls.NUMBER);
String number = calls.getString(numberIndex);
// 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, // Check for phone number match
// but if you answer this time, the missed call will still be in the database if (!lastNumber.equals(digitsOnly(number))) {
// and will be processed again. System.err.println("Number mismatch");
int dateIndex = calls.getColumnIndex(Calls.DATE); return;
long date = calls.getLong(dateIndex); }
if (DateUtilities.now() - date < 2 * DateUtilities.ONE_MINUTE)
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,
int nameIndex = calls.getColumnIndex(Calls.CACHED_NAME); // but if you answer this time, the missed call will still be in the database
String name = calls.getString(nameIndex); // and will be processed again.
int dateIndex = calls.getColumnIndex(Calls.DATE);
int timeIndex = calls.getColumnIndex(Calls.DATE); long date = calls.getLong(dateIndex);
long time = calls.getLong(timeIndex); if (DateUtilities.now() - date > 2 * DateUtilities.ONE_MINUTE) {
String timeString = DateUtilities.getTimeString(context, new Date(time)); System.err.println("Date: " + date);
System.err.println("Diff: " + (DateUtilities.now() - date));
String photo = getContactPhotoFromNumber(context, number); System.err.println("Time mismatch");
return;
Intent missedCallIntent = new Intent(context, MissedCallActivity.class); }
missedCallIntent.putExtra(MissedCallActivity.EXTRA_NUMBER, number);
missedCallIntent.putExtra(MissedCallActivity.EXTRA_NAME, name); int nameIndex = calls.getColumnIndex(Calls.CACHED_NAME);
missedCallIntent.putExtra(MissedCallActivity.EXTRA_TIME, timeString); String name = calls.getString(nameIndex);
missedCallIntent.putExtra(MissedCallActivity.EXTRA_PHOTO, photo);
missedCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); int timeIndex = calls.getColumnIndex(Calls.DATE);
context.startActivity(missedCallIntent); 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 { }.start();
calls.close();
}
} else { } else {
System.err.println("ASTRID Other state: " + state); System.err.println("ASTRID Other state: " + state);
} }
@ -103,10 +119,6 @@ public class PhoneStateChangedReceiver extends BroadcastReceiver {
return builder.toString(); return builder.toString();
} }
private String getContactPhotoFromNumber(Context context, String number) {
return getPhotoForContactId(context, getContactIdFromNumber(context, number));
}
private long getContactIdFromNumber(Context context, String number) { private long getContactIdFromNumber(Context context, String number) {
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(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); 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; 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 <ImageView
android:id="@+id/contact_picture" android:id="@+id/contact_picture"
android:layout_marginTop="5dip" android:layout_marginTop="5dip"
android:layout_marginRight="10dip"
android:visibility="gone"
android:layout_width="46dip" android:layout_width="46dip"
android:layout_height="46dip" android:layout_height="46dip"
android:scaleType="fitCenter" /> android:scaleType="fitCenter" />
@ -29,7 +31,6 @@
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="3dip" android:layout_marginTop="3dip"
android:layout_marginLeft="5dip"
android:textSize="20sp" android:textSize="20sp"
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:layout_weight="1" android:layout_weight="1"

@ -466,7 +466,7 @@
<!-- ===================================================== MissedCallActivity == --> <!-- ===================================================== MissedCallActivity == -->
<!-- Missed call: return call (%1$s -> caller, %2$s -> time of call)--> <!-- 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 --> <!-- Missed call: return call -->
<string name="MCA_return_call">Call now</string> <string name="MCA_return_call">Call now</string>

Loading…
Cancel
Save