From 0ed664a6638f7e60d82f6090308af415947652f2 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 8 May 2012 16:57:37 -0700 Subject: [PATCH] Commit all the things--mostly working people view --- api/src/com/todoroo/astrid/data/User.java | 8 ++ astrid/AndroidManifest.xml | 10 ++ .../todoroo/astrid/core/PluginServices.java | 8 ++ .../astrid/people/PeopleFilterAdapter.java | 38 ++++++ .../astrid/people/PeopleFilterExposer.java | 113 ++++++++++++++++++ .../astrid/people/PeopleListFragment.java | 20 ++++ .../astrid/people/PeopleViewActivity.java | 80 +++++++++++++ .../astrid/people/PersonViewFragment.java | 23 ++++ astrid/res/layout/header_people_view.xml | 76 ++++++++++++ astrid/res/layout/people_list_fragment.xml | 20 ++++ .../layout/people_view_wrapper_activity.xml | 8 ++ astrid/res/layout/task_list_body_person.xml | 11 ++ .../astrid/activity/FilterListFragment.java | 40 ++++--- .../astrid/activity/TaskListActivity.java | 61 ++++++++-- .../astrid/activity/TaskListFragment.java | 4 +- .../todoroo/astrid/adapter/FilterAdapter.java | 2 +- .../astrid/service/StartupService.java | 1 + .../todoroo/astrid/ui/MainMenuPopover.java | 12 +- 18 files changed, 502 insertions(+), 33 deletions(-) create mode 100644 astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterAdapter.java create mode 100644 astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterExposer.java create mode 100644 astrid/plugin-src/com/todoroo/astrid/people/PeopleListFragment.java create mode 100644 astrid/plugin-src/com/todoroo/astrid/people/PeopleViewActivity.java create mode 100644 astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java create mode 100644 astrid/res/layout/header_people_view.xml create mode 100644 astrid/res/layout/people_list_fragment.xml create mode 100644 astrid/res/layout/people_view_wrapper_activity.xml create mode 100644 astrid/res/layout/task_list_body_person.xml diff --git a/api/src/com/todoroo/astrid/data/User.java b/api/src/com/todoroo/astrid/data/User.java index 0d6d6e2cc..f8cf69c82 100644 --- a/api/src/com/todoroo/astrid/data/User.java +++ b/api/src/com/todoroo/astrid/data/User.java @@ -8,6 +8,7 @@ package com.todoroo.astrid.data; import android.content.ContentValues; import android.net.Uri; +import android.text.TextUtils; import com.todoroo.andlib.data.AbstractModel; import com.todoroo.andlib.data.Property; @@ -91,6 +92,13 @@ public final class User extends RemoteModel { super.readPropertiesFromCursor(cursor); } + public String getDisplayName() { + String name = getValue(NAME); + if (!TextUtils.isEmpty(name)) + return name; + return getValue(EMAIL); + } + @Override public long getId() { return getIdHelper(ID); diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index fa56190d6..91e447e20 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -320,6 +320,16 @@ + + + + + + + + + diff --git a/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java b/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java index b366713a8..ff50eabd6 100644 --- a/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java +++ b/astrid/plugin-src/com/todoroo/astrid/core/PluginServices.java @@ -8,6 +8,7 @@ import com.todoroo.andlib.sql.Query; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria; import com.todoroo.astrid.dao.StoreObjectDao; +import com.todoroo.astrid.dao.UserDao; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.service.AddOnService; import com.todoroo.astrid.service.AstridDependencyInjector; @@ -44,6 +45,9 @@ public final class PluginServices { @Autowired StoreObjectDao storeObjectDao; + @Autowired + UserDao userDao; + private static PluginServices instance; static { @@ -86,6 +90,10 @@ public final class PluginServices { return getInstance().storeObjectDao; } + public static UserDao getUserDao() { + return getInstance().userDao; + } + // -- helpers /** diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterAdapter.java b/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterAdapter.java new file mode 100644 index 000000000..55147ed6b --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterAdapter.java @@ -0,0 +1,38 @@ +package com.todoroo.astrid.people; + +import android.app.Activity; +import android.content.Intent; +import android.content.IntentFilter; +import android.widget.ListView; + +import com.todoroo.astrid.adapter.FilterAdapter; +import com.todoroo.astrid.utility.Constants; + +public class PeopleFilterAdapter extends FilterAdapter { + + public static final String BROADCAST_REQUEST_PEOPLE_FILTERS = Constants.PACKAGE + ".REQUEST_PEOPLE_FILTERS"; //$NON-NLS-1$ + public static final String BROADCAST_SEND_PEOPLE_FILTERS = Constants.PACKAGE + ".SEND_PEOPLE_FILTERS"; //$NON-NLS-1$ + + public PeopleFilterAdapter(Activity activity, ListView listView, + int rowLayout, boolean skipIntentFilters) { + super(activity, listView, rowLayout, skipIntentFilters); + } + + @Override + public void getLists() { + Intent broadcastIntent = new Intent(BROADCAST_REQUEST_PEOPLE_FILTERS); + activity.sendBroadcast(broadcastIntent); + } + + @Override + public void registerRecevier() { + IntentFilter peopleFilter = new IntentFilter(BROADCAST_SEND_PEOPLE_FILTERS); + activity.registerReceiver(filterReceiver, peopleFilter); + getLists(); + } + + @Override + public void unregisterRecevier() { + activity.unregisterReceiver(filterReceiver); + } +} diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterExposer.java b/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterExposer.java new file mode 100644 index 000000000..859430ced --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/people/PeopleFilterExposer.java @@ -0,0 +1,113 @@ +package com.todoroo.astrid.people; + +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; + +import com.todoroo.andlib.data.TodorooCursor; +import com.todoroo.andlib.service.ContextManager; +import com.todoroo.andlib.sql.Criterion; +import com.todoroo.andlib.sql.Field; +import com.todoroo.andlib.sql.Join; +import com.todoroo.andlib.sql.Order; +import com.todoroo.andlib.sql.Query; +import com.todoroo.andlib.sql.QueryTemplate; +import com.todoroo.astrid.api.AstridApiConstants; +import com.todoroo.astrid.api.Filter; +import com.todoroo.astrid.api.FilterListItem; +import com.todoroo.astrid.api.FilterWithCustomIntent; +import com.todoroo.astrid.api.FilterWithUpdate; +import com.todoroo.astrid.core.PluginServices; +import com.todoroo.astrid.data.Metadata; +import com.todoroo.astrid.data.TagData; +import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.User; +import com.todoroo.astrid.tags.TagService; + +public class PeopleFilterExposer extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + FilterListItem[] listAsArray = prepareFilters(context); + + Intent broadcastIntent = new Intent(PeopleFilterAdapter.BROADCAST_SEND_PEOPLE_FILTERS); + broadcastIntent.putExtra(AstridApiConstants.EXTRAS_RESPONSE, listAsArray); + broadcastIntent.putExtra(AstridApiConstants.EXTRAS_ADDON, "people"); //$NON-NLS-1$ + context.sendBroadcast(broadcastIntent); + } + + private FilterListItem[] prepareFilters(Context context) { + TodorooCursor users = PluginServices.getUserDao().query(Query.select(User.PROPERTIES) + .orderBy(Order.asc(User.NAME), Order.asc(User.EMAIL))); + try { + FilterListItem[] items = new FilterListItem[users.getCount()]; + User user = new User(); + int i = 0; + for (users.moveToFirst(); !users.isAfterLast(); users.moveToNext()) { + user.readFromCursor(users); + Filter currFilter = filterFromUserData(user); + items[i] = currFilter; + i++; + } + return items; + } finally { + users.close(); + } + } + + @SuppressWarnings("nls") + public static FilterWithCustomIntent filterFromUserData(User user) { + String email = user.getValue(User.EMAIL); + String[] tags; + + TodorooCursor tagsWithUser = PluginServices.getTagDataService().query(Query.select(TagData.NAME) + .where(Criterion.or( + TagData.MEMBERS.like("%" + email + "%"), + TagData.USER.like("%" + email + "%"), + TagData.USER_ID.eq(user.getId())))); + try { + if (tagsWithUser.getCount() == 0) { + tags = new String[1]; + tags[0] = "\"\""; + } else { + tags = new String[tagsWithUser.getCount()]; + int i = 0; + TagData curr = new TagData(); + for (tagsWithUser.moveToFirst(); !tagsWithUser.isAfterLast(); tagsWithUser.moveToNext()) { + curr.readFromCursor(tagsWithUser); + tags[i] = "\"" + curr.getValue(TagData.NAME) + "\""; + i++; + } + } + } finally { + tagsWithUser.close(); + } + + String title = user.getDisplayName(); + QueryTemplate userTemplate = new QueryTemplate().join(Join.inner(Metadata.TABLE.as("mtags"), + Criterion.and(Task.ID.eq(Field.field("mtags." + Metadata.TASK.name)), + Field.field("mtags." + Metadata.KEY.name).eq(TagService.KEY), + Field.field("mtags." + TagService.TAG.name).in(tags), + Criterion.or(Task.USER.like("%" + email + "%"), + Task.USER_ID.eq(user.getValue(User.REMOTE_ID)))))); + + FilterWithUpdate filter = new FilterWithUpdate(title, title, userTemplate, null); + + filter.customTaskList = new ComponentName(ContextManager.getContext(), PersonViewFragment.class); + + ContentValues values = new ContentValues(); + values.put(Task.USER_ID.name, user.getValue(User.REMOTE_ID)); + filter.valuesForNewTasks = values; + + String imageUrl = user.getValue(User.PICTURE); + filter.imageUrl = imageUrl; + + Bundle extras = new Bundle(); + extras.putLong(PersonViewFragment.EXTRA_USER_ID_LOCAL, user.getId()); + filter.customExtras = extras; + + return filter; + } +} diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PeopleListFragment.java b/astrid/plugin-src/com/todoroo/astrid/people/PeopleListFragment.java new file mode 100644 index 000000000..db7f38e8c --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/people/PeopleListFragment.java @@ -0,0 +1,20 @@ +package com.todoroo.astrid.people; + +import android.app.Activity; + +import com.timsu.astrid.R; +import com.todoroo.astrid.activity.FilterListFragment; +import com.todoroo.astrid.adapter.FilterAdapter; + +public class PeopleListFragment extends FilterListFragment { + + @Override + protected FilterAdapter instantiateAdapter() { + return new PeopleFilterAdapter(getActivity(), null, R.layout.filter_adapter_row, false); + } + + @Override + protected int getLayout(Activity activity) { + return R.layout.people_list_fragment; + } +} diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PeopleViewActivity.java b/astrid/plugin-src/com/todoroo/astrid/people/PeopleViewActivity.java new file mode 100644 index 000000000..27272f3b4 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/people/PeopleViewActivity.java @@ -0,0 +1,80 @@ +package com.todoroo.astrid.people; + +import android.content.Intent; +import android.os.Bundle; + +import com.timsu.astrid.R; +import com.todoroo.andlib.utility.AndroidUtilities; +import com.todoroo.astrid.activity.FilterListFragment; +import com.todoroo.astrid.activity.TaskListActivity; +import com.todoroo.astrid.activity.TaskListFragment; +import com.todoroo.astrid.api.Filter; +import com.todoroo.astrid.api.FilterListItem; +import com.todoroo.astrid.api.FilterWithUpdate; +import com.todoroo.astrid.helper.AsyncImageView; +import com.todoroo.astrid.ui.MainMenuPopover; + +public class PeopleViewActivity extends TaskListActivity { + + private AsyncImageView imageView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + imageView = (AsyncImageView) findViewById(R.id.person_image); + imageView.setDefaultImageResource(R.drawable.icn_default_person_image); + } + + @Override + protected int getContentView() { + return R.layout.people_view_wrapper_activity; + } + + @Override + protected Filter getDefaultFilter() { + // TODO More suitable default + return super.getDefaultFilter(); + } + + @Override + protected Class getFilterListClass() { + return PeopleListFragment.class; + } + + @Override + protected int getHeaderView() { + return R.layout.header_people_view; + } + + @Override + public boolean onFilterItemClicked(FilterListItem item) { + boolean result = super.onFilterItemClicked(item); + if (result && item instanceof FilterWithUpdate) + imageView.setUrl(((FilterWithUpdate) item).imageUrl); + else + imageView.setUrl(null); + return result; + } + + private static final int[] FORBIDDEN_MENU_ITEMS = { + TaskListFragment.MENU_NEW_FILTER_ID, + TaskListFragment.MENU_ADDONS_ID, + MainMenuPopover.MAIN_MENU_ITEM_FRIENDS + }; + + @Override + public boolean shouldAddMenuItem(int itemId) { + return AndroidUtilities.indexOf(FORBIDDEN_MENU_ITEMS, itemId) < 0; + } + + @Override + public void mainMenuItemSelected(int item, Intent customIntent) { + if (item == MainMenuPopover.MAIN_MENU_ITEM_LISTS) { + finish(); + return; + } + + super.mainMenuItemSelected(item, customIntent); + } +} diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java b/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java new file mode 100644 index 000000000..05056c817 --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java @@ -0,0 +1,23 @@ +package com.todoroo.astrid.people; + +import android.view.View; +import android.view.ViewGroup; + +import com.timsu.astrid.R; +import com.todoroo.astrid.activity.TaskListFragment; + +public class PersonViewFragment extends TaskListFragment { + + public static final String EXTRA_USER_ID_LOCAL = "user_local_id"; //$NON-NLS-1$ + + @Override + protected View getListBody(ViewGroup root) { + ViewGroup parent = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.task_list_body_person, root, false); + + View taskListView = super.getListBody(parent); + parent.addView(taskListView); + + return parent; + } + +} diff --git a/astrid/res/layout/header_people_view.xml b/astrid/res/layout/header_people_view.xml new file mode 100644 index 000000000..b25ced504 --- /dev/null +++ b/astrid/res/layout/header_people_view.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + +