Add faces of people shared with to reminders

pull/14/head
Sam Bosley 12 years ago
parent 360445034a
commit 80ec3cbe17

@ -6,25 +6,42 @@
package com.todoroo.astrid.reminders; package com.todoroo.astrid.reminders;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashSet;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.app.TimePickerDialog.OnTimeSetListener; import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent; import android.content.Intent;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View; import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.TimePicker; import android.widget.TimePicker;
import android.widget.Toast; import android.widget.Toast;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.activity.AstridActivity; import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.PluginServices; import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.helper.AsyncImageView;
import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsConstants;
import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.StatisticsService;
import com.todoroo.astrid.service.TaskService;
import com.todoroo.astrid.tags.TagService;
/** /**
* A dialog that shows your task reminder * A dialog that shows your task reminder
@ -34,9 +51,18 @@ import com.todoroo.astrid.service.StatisticsService;
*/ */
public class ReminderDialog extends Dialog { public class ReminderDialog extends Dialog {
private static final int MAX_FACES = 4;
@Autowired
private TaskService taskService;
@Autowired
private TagService tagService;
public ReminderDialog(final AstridActivity activity, final long taskId, public ReminderDialog(final AstridActivity activity, final long taskId,
String title) { String title) {
super(activity, R.style.ReminderDialog); super(activity, R.style.ReminderDialog);
DependencyInjectionService.getInstance().inject(this);
final SnoozeCallback dialogSnooze = new SnoozeCallback() { final SnoozeCallback dialogSnooze = new SnoozeCallback() {
@Override @Override
public void snoozeForTime(long time) { public void snoozeForTime(long time) {
@ -62,8 +88,7 @@ public class ReminderDialog extends Dialog {
if (Preferences.getBoolean(R.string.p_rmd_nagging, true)) { if (Preferences.getBoolean(R.string.p_rmd_nagging, true)) {
setContentView(R.layout.astrid_reminder_view); setContentView(R.layout.astrid_reminder_view);
((TextView) findViewById(R.id.reminder_message)).setText( setupSpeechBubble(activity, taskId);
Notifications.getRandomReminder(activity.getResources().getStringArray(R.array.reminder_responses)));
} else { } else {
setContentView(R.layout.astrid_reminder_view_portrait); setContentView(R.layout.astrid_reminder_view_portrait);
title = activity.getString(R.string.rmd_NoA_dlg_title) + " " + title; //$NON-NLS-1$ title = activity.getString(R.string.rmd_NoA_dlg_title) + " " + title; //$NON-NLS-1$
@ -118,4 +143,96 @@ public class ReminderDialog extends Dialog {
LinearLayout container = (LinearLayout) findViewById(R.id.speech_bubble_container); LinearLayout container = (LinearLayout) findViewById(R.id.speech_bubble_container);
container.setVisibility(View.GONE); container.setVisibility(View.GONE);
} }
private void setupSpeechBubble(Activity activity, long taskId) {
((TextView) findViewById(R.id.reminder_message)).setText(
Notifications.getRandomReminder(activity.getResources().getStringArray(R.array.reminder_responses)));
if (true)
addFacesToReminder(activity, taskId);
}
private void addFacesToReminder(Activity activity, long taskId) {
LinkedHashSet<String> pictureUrls = new LinkedHashSet<String>();
addSharedWithFaces(taskId, pictureUrls);
if (pictureUrls.size() < MAX_FACES) {
addTagFaces(taskId, pictureUrls);
}
if (pictureUrls.size() > 0) {
DisplayMetrics metrics = activity.getResources().getDisplayMetrics();
LinearLayout layout = new LinearLayout(activity);
LinearLayout.LayoutParams containerParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
containerParams.setMargins((int) (-7 * metrics.density), 0, 0, 0);
layout.setLayoutParams(containerParams);
int padding = (int) (8 * metrics.density);
int count = 0;
for (String url : pictureUrls) {
AsyncImageView image = new AsyncImageView(activity);
image.setDefaultImageResource(R.drawable.icn_default_person_image);
image.setUrl(url);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) (35 * metrics.density), (int) (35 * metrics.density));
lp.setMargins(padding, padding, 0, padding);
image.setLayoutParams(lp);
layout.addView(image);
if (++count >= MAX_FACES)
break;
}
LinearLayout container = (LinearLayout) findViewById(R.id.speech_bubble_content);
container.setOrientation(LinearLayout.VERTICAL);
container.addView(layout, 0);
((TextView) findViewById(R.id.reminder_message)).setText(
Notifications.getRandomReminder(activity.getResources().getStringArray(R.array.reminders_social)));
}
}
private void addPicturesFromJSONArray(JSONArray array, LinkedHashSet<String> pictureUrls) throws JSONException {
for (int i = 0; i < array.length(); i++) {
JSONObject person = array.getJSONObject(i);
if (person.has("picture")) { //$NON-NLS-1$
if (person.optLong("id") == ActFmPreferenceService.userId()) //$NON-NLS-1$
continue;
String pictureUrl = person.getString("picture"); //$NON-NLS-1$
if (!TextUtils.isEmpty(pictureUrl)) {
pictureUrls.add(pictureUrl);
}
}
}
}
private void addSharedWithFaces(long taskId, LinkedHashSet<String> pictureUrls) {
Task t = taskService.fetchById(taskId, Task.SHARED_WITH);
try {
JSONObject sharedWith = new JSONObject(t.getValue(Task.SHARED_WITH));
if (sharedWith.has("p")) { //$NON-NLS-1$
JSONArray people = sharedWith.getJSONArray("p"); //$NON-NLS-1$
addPicturesFromJSONArray(people, pictureUrls);
}
} catch (JSONException e) {
//
}
}
private void addTagFaces(long taskId, LinkedHashSet<String> pictureUrls) {
TodorooCursor<TagData> tags = tagService.getTagDataForTask(taskId, TagData.MEMBER_COUNT.gt(0), TagData.MEMBERS);
try {
TagData td = new TagData();
for (tags.moveToFirst(); !tags.isAfterLast() && pictureUrls.size() < MAX_FACES; tags.moveToNext()) {
td.readFromCursor(tags);
try {
JSONArray people = new JSONArray(td.getValue(TagData.MEMBERS));
addPicturesFromJSONArray(people, pictureUrls);
} catch (JSONException e) {
//
}
}
} finally {
tags.close();
}
}
} }

@ -18,6 +18,7 @@ import android.text.TextUtils;
import android.widget.Toast; import android.widget.Toast;
import com.timsu.astrid.R; import com.timsu.astrid.R;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.data.Property.CountProperty; import com.todoroo.andlib.data.Property.CountProperty;
import com.todoroo.andlib.data.Property.LongProperty; import com.todoroo.andlib.data.Property.LongProperty;
import com.todoroo.andlib.data.Property.StringProperty; import com.todoroo.andlib.data.Property.StringProperty;
@ -259,6 +260,11 @@ public final class TagService {
return metadataDao.query(query); return metadataDao.query(query);
} }
public TodorooCursor<TagData> getTagDataForTask(long taskId, Criterion additionalCriterion, Property<?>... properties) {
Criterion criterion = TagData.NAME.in(Query.select(TAG).from(Metadata.TABLE).where(MetadataCriteria.byTaskAndwithKey(taskId, KEY)));
return tagDataService.query(Query.select(properties).where(Criterion.and(criterion, additionalCriterion)));
}
/** /**
* Return tags as a comma-separated list of strings * Return tags as a comma-separated list of strings
* *

@ -289,6 +289,10 @@
<!-- =============================================== random reminders == --> <!-- =============================================== random reminders == -->
<string-array name="reminders_social">
<item>These people are counting on you!</item>
</string-array>
<string-array name="reminders"> <string-array name="reminders">
<!-- reminders: Make these < 20 chars so the task name is displayed --> <!-- reminders: Make these < 20 chars so the task name is displayed -->
<item>Hi there! Have a sec?</item> <item>Hi there! Have a sec?</item>

Loading…
Cancel
Save