Skip to content

Commit

Permalink
Load tag data during push notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Su committed May 24, 2011
1 parent 0d2921a commit d314e1d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
52 changes: 48 additions & 4 deletions astrid/plugin-src/com/timsu/astrid/C2DMReceiver.java
Expand Up @@ -2,22 +2,30 @@

import java.io.IOException;

import org.json.JSONException;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.service.NotificationManager;
import com.todoroo.andlib.service.NotificationManager.AndroidNotificationManager;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.actfm.sync.ActFmSyncService;
import com.todoroo.astrid.activity.ShortcutActivity;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.core.CoreFilterExposer;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.service.TagDataService;
import com.todoroo.astrid.tags.TagFilterExposer;
import com.todoroo.astrid.utility.Constants;

@SuppressWarnings("nls")
Expand All @@ -28,6 +36,7 @@ public class C2DMReceiver extends BroadcastReceiver {
private static final String PREF_REGISTRATION = "c2dm_key";

@Autowired ActFmSyncService actFmSyncService;
@Autowired TagDataService tagDataService;

@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -45,9 +54,42 @@ private void handleMessage(Intent intent) {
Context context = ContextManager.getContext();

Intent notifyIntent;
if(intent.hasExtra("tag_id")) {
TodorooCursor<TagData> cursor = tagDataService.query(
Query.select(TagData.ID).where(TagData.REMOTE_ID.eq(
intent.getLongExtra("tag_id", -1))));
try {
final TagData tagData = new TagData();
if(cursor.getCount() == 0) {
tagData.setValue(TagData.NAME, intent.getStringExtra("title"));
tagData.setValue(TagData.REMOTE_ID, intent.getLongExtra("tag_id", 0));
tagDataService.save(tagData);
new Thread(new Runnable() {
@Override
public void run() {
try {
actFmSyncService.fetchTag(tagData);
} catch (IOException e) {
Log.e("astrid-c2dm", "error fetching", e);
} catch (JSONException e) {
Log.e("astrid-c2dm", "error fetching", e);
}
}
}).start();
} else {
cursor.moveToNext();
tagData.readFromCursor(cursor);
}

Filter filter= TagFilterExposer.filterFromTagData(context, tagData);
notifyIntent = ShortcutActivity.createIntent(filter);
} finally {
cursor.close();
}
} else {
notifyIntent = ShortcutActivity.createIntent(CoreFilterExposer.buildInboxFilter(context.getResources()));
}


notifyIntent = ShortcutActivity.createIntent(CoreFilterExposer.buildInboxFilter(context.getResources()));
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,
Constants.NOTIFICATION_ACTFM, notifyIntent, 0);
Expand All @@ -56,8 +98,10 @@ private void handleMessage(Intent intent) {
NotificationManager nm = new AndroidNotificationManager(ContextManager.getContext());
Notification notification = new Notification(R.drawable.notif_pink_alarm,
message, System.currentTimeMillis());
String appName = ContextManager.getString(R.string.app_name);
notification.setLatestEventInfo(ContextManager.getContext(), appName,
String title = ContextManager.getString(R.string.app_name);
if(intent.hasExtra("title"))
title += ": " + intent.getStringExtra("title");
notification.setLatestEventInfo(ContextManager.getContext(), title,
message, pendingIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
if("true".equals(intent.getStringExtra("sound")))
Expand Down
13 changes: 11 additions & 2 deletions astrid/plugin-src/com/todoroo/astrid/tags/TagFilterExposer.java
Expand Up @@ -57,8 +57,9 @@ public class TagFilterExposer extends BroadcastReceiver {

private TagService tagService;

/** Create filter from new tag object */
@SuppressWarnings("nls")
private Filter filterFromTag(Context context, Tag tag, Criterion criterion) {
public static Filter filterFromTag(Context context, Tag tag, Criterion criterion) {
String listTitle = tag.tag + " (" + tag.count + ")";
String title = context.getString(R.string.tag_FEx_name, tag.tag);
QueryTemplate tagTemplate = tag.queryTemplate(criterion);
Expand Down Expand Up @@ -89,7 +90,15 @@ private Filter filterFromTag(Context context, Tag tag, Criterion criterion) {
return filter;
}

private Intent newTagIntent(Context context, Class<? extends Activity> activity, Tag tag) {
/** Create a filter from tag data object */
public static Filter filterFromTagData(Context context, TagData tagData) {
Tag tag = new Tag(tagData.getValue(TagData.NAME),
tagData.getValue(TagData.TASK_COUNT),
tagData.getValue(TagData.REMOTE_ID));
return filterFromTag(context, tag, TaskCriteria.activeAndVisible());
}

private static Intent newTagIntent(Context context, Class<? extends Activity> activity, Tag tag) {
Intent ret = new Intent(context, activity);
ret.putExtra(TAG, tag.tag);
return ret;
Expand Down

0 comments on commit d314e1d

Please sign in to comment.