From 403a1d996759a242fb4f4252a64b42baf946b6ce Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 9 Nov 2010 16:02:47 -0800 Subject: [PATCH] Now sort preferences are stored publicly. Also using sortHelper --- .../activity/DraggableTaskListActivity.java | 9 +- .../activity/SortSelectionActivity.java | 105 +++--------------- .../astrid/activity/TaskListActivity.java | 22 ++-- .../astrid/provider/Astrid2TaskProvider.java | 3 +- .../todoroo/astrid/service/TaskService.java | 13 --- .../astrid/service/UpgradeService.java | 39 ++++++- .../astrid/utility/AstridPreferences.java | 12 ++ .../todoroo/astrid/widget/TasksWidget.java | 11 +- 8 files changed, 90 insertions(+), 124 deletions(-) diff --git a/astrid/src/com/todoroo/astrid/activity/DraggableTaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/DraggableTaskListActivity.java index 5f02c4524..49b6fddbb 100644 --- a/astrid/src/com/todoroo/astrid/activity/DraggableTaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/DraggableTaskListActivity.java @@ -9,16 +9,17 @@ import android.database.Cursor; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; import android.view.View.OnTouchListener; +import android.view.ViewGroup; import android.widget.TextView; import com.commonsware.cwac.tlv.TouchListView; import com.timsu.astrid.R; import com.todoroo.andlib.data.Property; -import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.data.Property.IntegerProperty; +import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.astrid.adapter.TaskAdapter; +import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.data.Task; /** @@ -62,7 +63,7 @@ public class DraggableTaskListActivity extends TaskListActivity { */ @Override protected void setUpTaskList() { - sqlQueryTemplate.set(SortSelectionActivity.adjustQueryForFlagsAndSort(filter.sqlQuery, + sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(filter.sqlQuery, sortFlags, sortSort)); ((TextView)findViewById(R.id.listLabel)).setText(filter.title); @@ -81,7 +82,7 @@ public class DraggableTaskListActivity extends TaskListActivity { registerForContextMenu(getListView()); loadTaskListContent(false); - + getTouchListView().setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { diff --git a/astrid/src/com/todoroo/astrid/activity/SortSelectionActivity.java b/astrid/src/com/todoroo/astrid/activity/SortSelectionActivity.java index dde851c63..0fbab1639 100644 --- a/astrid/src/com/todoroo/astrid/activity/SortSelectionActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/SortSelectionActivity.java @@ -9,13 +9,7 @@ import android.widget.CheckBox; import android.widget.RadioButton; import com.timsu.astrid.R; -import com.todoroo.andlib.sql.Criterion; -import com.todoroo.andlib.sql.Functions; -import com.todoroo.andlib.sql.Order; -import com.todoroo.andlib.utility.DateUtilities; -import com.todoroo.astrid.dao.TaskDao.TaskCriteria; -import com.todoroo.astrid.data.Task; -import com.todoroo.astrid.service.TaskService; +import com.todoroo.astrid.core.SortHelper; /** * Shows the sort / hidden dialog @@ -25,17 +19,6 @@ import com.todoroo.astrid.service.TaskService; */ public class SortSelectionActivity { - public static final int FLAG_REVERSE_SORT = 1 << 0; - public static final int FLAG_SHOW_COMPLETED = 1 << 1; - public static final int FLAG_SHOW_HIDDEN = 1 << 2; - public static final int FLAG_SHOW_DELETED = 1 << 3; - - public static final int SORT_AUTO = 0; - public static final int SORT_ALPHA = 1; - public static final int SORT_DUE = 2; - public static final int SORT_IMPORTANCE = 3; - public static final int SORT_MODIFIED = 4; - public interface OnSortSelectedListener { public void onSortSelected(boolean always, int flags, int sort); } @@ -49,26 +32,26 @@ public class SortSelectionActivity { OnSortSelectedListener listener, int flags, int sort) { View body = activity.getLayoutInflater().inflate(R.layout.sort_selection_dialog, null); - if((flags & FLAG_REVERSE_SORT) > 0) + if((flags & SortHelper.FLAG_REVERSE_SORT) > 0) ((CheckBox)body.findViewById(R.id.reverse)).setChecked(true); - if((flags & FLAG_SHOW_COMPLETED) > 0) + if((flags & SortHelper.FLAG_SHOW_COMPLETED) > 0) ((CheckBox)body.findViewById(R.id.completed)).setChecked(true); - if((flags & FLAG_SHOW_HIDDEN) > 0) + if((flags & SortHelper.FLAG_SHOW_HIDDEN) > 0) ((CheckBox)body.findViewById(R.id.hidden)).setChecked(true); - if((flags & FLAG_SHOW_DELETED) > 0) + if((flags & SortHelper.FLAG_SHOW_DELETED) > 0) ((CheckBox)body.findViewById(R.id.deleted)).setChecked(true); switch(sort) { - case SORT_ALPHA: + case SortHelper.SORT_ALPHA: ((RadioButton)body.findViewById(R.id.sort_alpha)).setChecked(true); break; - case SORT_DUE: + case SortHelper.SORT_DUE: ((RadioButton)body.findViewById(R.id.sort_due)).setChecked(true); break; - case SORT_IMPORTANCE: + case SortHelper.SORT_IMPORTANCE: ((RadioButton)body.findViewById(R.id.sort_importance)).setChecked(true); break; - case SORT_MODIFIED: + case SortHelper.SORT_MODIFIED: ((RadioButton)body.findViewById(R.id.sort_modified)).setChecked(true); break; default: @@ -88,60 +71,8 @@ public class SortSelectionActivity { return dialog; } - @SuppressWarnings("nls") - public static String adjustQueryForFlagsAndSort(String originalSql, int flags, int sort) { - // sort - if(!originalSql.toUpperCase().contains("ORDER BY")) { - Order order; - switch(sort) { - case SortSelectionActivity.SORT_ALPHA: - order = Order.asc(Functions.upper(Task.TITLE)); - break; - case SortSelectionActivity.SORT_DUE: - order = Order.asc(Functions.caseStatement(Task.DUE_DATE.eq(0), - DateUtilities.now()*2, Task.DUE_DATE) + "+" + Task.IMPORTANCE + - "+3*" + Task.COMPLETION_DATE); - break; - case SortSelectionActivity.SORT_IMPORTANCE: - order = Order.asc(Task.IMPORTANCE + "*" + (2*DateUtilities.now()) + //$NON-NLS-1$ - "+" + Functions.caseStatement(Task.DUE_DATE.eq(0), //$NON-NLS-1$ - Functions.now() + "+" + DateUtilities.ONE_WEEK, //$NON-NLS-1$ - Task.DUE_DATE) + "+8*" + Task.COMPLETION_DATE); - break; - case SortSelectionActivity.SORT_MODIFIED: - order = Order.desc(Task.MODIFICATION_DATE); - break; - default: - order = TaskService.defaultTaskOrder(); - } - - if((flags & SortSelectionActivity.FLAG_REVERSE_SORT) > 0) - order = order.reverse(); - originalSql += " ORDER BY " + order; - } - - // flags - if((flags & FLAG_SHOW_COMPLETED) > 0) - originalSql = originalSql.replace(Task.COMPLETION_DATE.eq(0).toString(), - Criterion.all.toString()); - if((flags & FLAG_SHOW_HIDDEN) > 0) - originalSql = originalSql.replace(TaskCriteria.isVisible().toString(), - Criterion.all.toString()); - if((flags & FLAG_SHOW_DELETED) > 0) - originalSql = originalSql.replace(Task.DELETION_DATE.eq(0).toString(), - Criterion.all.toString()); - - return originalSql; - } - // --- internal implementation - /** preference key for sort flags */ - public static final String PREF_SORT_FLAGS = "sort_flags"; //$NON-NLS-1$ - - /** preference key for sort sort */ - public static final String PREF_SORT_SORT = "sort_sort"; //$NON-NLS-1$ - private SortSelectionActivity() { // use the static method } @@ -163,24 +94,24 @@ public class SortSelectionActivity { int sort = 0; if(((CheckBox)body.findViewById(R.id.reverse)).isChecked()) - flags |= FLAG_REVERSE_SORT; + flags |= SortHelper.FLAG_REVERSE_SORT; if(((CheckBox)body.findViewById(R.id.completed)).isChecked()) - flags |= FLAG_SHOW_COMPLETED; + flags |= SortHelper.FLAG_SHOW_COMPLETED; if(((CheckBox)body.findViewById(R.id.hidden)).isChecked()) - flags |= FLAG_SHOW_HIDDEN; + flags |= SortHelper.FLAG_SHOW_HIDDEN; if(((CheckBox)body.findViewById(R.id.deleted)).isChecked()) - flags |= FLAG_SHOW_DELETED; + flags |= SortHelper.FLAG_SHOW_DELETED; if(((RadioButton)body.findViewById(R.id.sort_alpha)).isChecked()) - sort = SORT_ALPHA; + sort = SortHelper.SORT_ALPHA; else if(((RadioButton)body.findViewById(R.id.sort_due)).isChecked()) - sort = SORT_DUE; + sort = SortHelper.SORT_DUE; else if(((RadioButton)body.findViewById(R.id.sort_importance)).isChecked()) - sort = SORT_IMPORTANCE; + sort = SortHelper.SORT_IMPORTANCE; else if(((RadioButton)body.findViewById(R.id.sort_modified)).isChecked()) - sort = SORT_MODIFIED; + sort = SortHelper.SORT_MODIFIED; else - sort = SORT_AUTO; + sort = SortHelper.SORT_AUTO; listener.onSortSelected(always, flags, sort); } diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 0c200f4cc..65bc3312a 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -17,6 +17,8 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.database.Cursor; @@ -58,7 +60,6 @@ import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; import com.todoroo.andlib.utility.AndroidUtilities; -import com.todoroo.andlib.utility.Preferences; import com.todoroo.andlib.widget.GestureService; import com.todoroo.andlib.widget.GestureService.GestureInterface; import com.todoroo.astrid.activity.SortSelectionActivity.OnSortSelectedListener; @@ -71,6 +72,7 @@ import com.todoroo.astrid.api.SyncAction; import com.todoroo.astrid.api.TaskAction; import com.todoroo.astrid.api.TaskDecoration; import com.todoroo.astrid.core.CoreFilterExposer; +import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; @@ -86,6 +88,7 @@ import com.todoroo.astrid.service.MetadataService; import com.todoroo.astrid.service.StartupService; import com.todoroo.astrid.service.StatisticsService; import com.todoroo.astrid.service.TaskService; +import com.todoroo.astrid.utility.AstridPreferences; import com.todoroo.astrid.utility.Constants; import com.todoroo.astrid.utility.Flags; import com.todoroo.astrid.widget.TasksWidget; @@ -145,9 +148,6 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, @Autowired protected Database database; - @Autowired - private AddOnService addOnService; - protected TaskAdapter taskAdapter = null; protected DetailReceiver detailReceiver = new DetailReceiver(); protected RefreshReceiver refreshReceiver = new RefreshReceiver(); @@ -391,8 +391,9 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, // failed check, no gestures :P } - sortFlags = Preferences.getInt(SortSelectionActivity.PREF_SORT_FLAGS, 0); - sortSort = Preferences.getInt(SortSelectionActivity.PREF_SORT_SORT, 0); + SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(this); + sortFlags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0); + sortSort = publicPrefs.getInt(SortHelper.PREF_SORT_SORT, 0); // dithering getWindow().setFormat(PixelFormat.RGBA_8888); @@ -619,7 +620,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, * @param withCustomId force task with given custom id to be part of list */ protected void setUpTaskList() { - sqlQueryTemplate.set(SortSelectionActivity.adjustQueryForFlagsAndSort(filter.sqlQuery, + sqlQueryTemplate.set(SortHelper.adjustQueryForFlagsAndSort(filter.sqlQuery, sortFlags, sortSort)); ((TextView)findViewById(R.id.listLabel)).setText(filter.title); @@ -978,8 +979,11 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, sortSort = sort; if(always) { - Preferences.setInt(SortSelectionActivity.PREF_SORT_FLAGS, flags); - Preferences.setInt(SortSelectionActivity.PREF_SORT_SORT, sort); + SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(this); + Editor editor = publicPrefs.edit(); + editor.putInt(SortHelper.PREF_SORT_FLAGS, flags); + editor.putInt(SortHelper.PREF_SORT_SORT, sort); + editor.commit(); ContextManager.getContext().startService(new Intent(ContextManager.getContext(), TasksWidget.UpdateService.class)); } diff --git a/astrid/src/com/todoroo/astrid/provider/Astrid2TaskProvider.java b/astrid/src/com/todoroo/astrid/provider/Astrid2TaskProvider.java index 0b0907d11..895eb427e 100644 --- a/astrid/src/com/todoroo/astrid/provider/Astrid2TaskProvider.java +++ b/astrid/src/com/todoroo/astrid/provider/Astrid2TaskProvider.java @@ -20,6 +20,7 @@ import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.Task; @@ -179,7 +180,7 @@ public class Astrid2TaskProvider extends ContentProvider { TodorooCursor cursor = taskService.query(Query.select(Task.ID, Task.TITLE, Task.IMPORTANCE, Task.DUE_DATE).where(Criterion.and(TaskCriteria.isActive(), TaskCriteria.isVisible())). - orderBy(TaskService.defaultTaskOrder()).limit(MAX_NUMBER_OF_TASKS)); + orderBy(SortHelper.defaultTaskOrder()).limit(MAX_NUMBER_OF_TASKS)); try { int[] importanceColors = Task.getImportanceColors(ctx.getResources()); Task task = new Task(); diff --git a/astrid/src/com/todoroo/astrid/service/TaskService.java b/astrid/src/com/todoroo/astrid/service/TaskService.java index b8043d401..8de7d3755 100644 --- a/astrid/src/com/todoroo/astrid/service/TaskService.java +++ b/astrid/src/com/todoroo/astrid/service/TaskService.java @@ -8,7 +8,6 @@ import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.sql.Criterion; import com.todoroo.andlib.sql.Functions; -import com.todoroo.andlib.sql.Order; import com.todoroo.andlib.sql.Query; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.api.Filter; @@ -198,18 +197,6 @@ public class TaskService { return taskDao.query(Query.select(properties).withQueryTemplate(sql)); } - /** - * Return the default task ordering - * @return - */ - @SuppressWarnings("nls") - public static Order defaultTaskOrder() { - return Order.asc(Functions.caseStatement(Task.DUE_DATE.eq(0), - DateUtilities.now() + DateUtilities.ONE_WEEK, - Task.DUE_DATE) + " + 200000000 * " + - Task.IMPORTANCE + " + 2*" + Task.COMPLETION_DATE); - } - /** * @param query * @return how many tasks are matched by this query diff --git a/astrid/src/com/todoroo/astrid/service/UpgradeService.java b/astrid/src/com/todoroo/astrid/service/UpgradeService.java index 34aa19374..7ba568ff7 100644 --- a/astrid/src/com/todoroo/astrid/service/UpgradeService.java +++ b/astrid/src/com/todoroo/astrid/service/UpgradeService.java @@ -13,11 +13,14 @@ import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.utility.DialogUtilities; import com.todoroo.andlib.utility.Preferences; import com.todoroo.astrid.activity.TaskListActivity; +import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.dao.Database; +import com.todoroo.astrid.utility.AstridPreferences; public final class UpgradeService { + public static final int V3_6_0 = 166; public static final int V3_5_0 = 165; public static final int V3_4_0 = 162; public static final int V3_3_0 = 155; @@ -98,7 +101,7 @@ public final class UpgradeService { StringBuilder changeLog = new StringBuilder(); if(from <= V2_14_4) { - newVersionString(changeLog, "3.5.0 (10/25/10)", new String[] { + newVersionString(changeLog, "3.6.0 (11/10/10)", new String[] { "Astrid is brand new inside and out! In addition to a new " + "look and feel, a new add-on system allows Astrid to become " + "more powerful, while other improvements have made it faster " + @@ -107,12 +110,22 @@ public final class UpgradeService { "clicking here", }); } else { - newVersionString(changeLog, "3.5.0", new String[] { - "Google Tasks Sync (beta!)", - "Bug fix with RMilk & new tasks not getting synced", - "Fixed Force Closes and other bugs", + // current message + newVersionString(changeLog, "3.6.0 (11/10/10)", new String[] { + "Astrid Power Pack is now launched to the Android Market. " + + "Power Pack features include 4x2 and 4x4 widgets and voice " + + "task reminders and creation. Go to the add-ons page to find out more!", }); - if(from >= V3_3_0 && from < V3_4_0) + upgrade3To3_6(context); + + // old messages + if(from >= V3_0_0 && from < V3_5_0) + newVersionString(changeLog, "3.5.0 (10/25/10)", new String[] { + "Google Tasks Sync (beta!)", + "Bug fix with RMilk & new tasks not getting synced", + "Fixed Force Closes and other bugs", + }); + if(from >= V3_0_0 && from < V3_4_0) newVersionString(changeLog, "3.4.0 (10/08/10)", new String[] { "End User License Agreement", "Option to disable usage statistics", @@ -202,6 +215,20 @@ public final class UpgradeService { } } + /** + * Moves sorting prefs to public pref store + * @param context + */ + private void upgrade3To3_6(final Context context) { + SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(context); + Editor editor = publicPrefs.edit(); + editor.putInt(SortHelper.PREF_SORT_FLAGS, + Preferences.getInt(SortHelper.PREF_SORT_FLAGS, 0)); + editor.putInt(SortHelper.PREF_SORT_SORT, + Preferences.getInt(SortHelper.PREF_SORT_SORT, 0)); + editor.commit(); + } + // --- secondary upgrade /** diff --git a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java index 78699ad02..44ba6f489 100644 --- a/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java +++ b/astrid/src/com/todoroo/astrid/utility/AstridPreferences.java @@ -8,6 +8,7 @@ import android.content.res.Resources; import com.timsu.astrid.R; import com.todoroo.andlib.service.ContextManager; import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.api.AstridApiConstants; public class AstridPreferences { @@ -31,6 +32,17 @@ public class AstridPreferences { editor.commit(); } + /* ====================================================================== + * ========================================================= public prefs + * ====================================================================== */ + + /** Get publicly readable preferences */ + public static SharedPreferences getPublicPrefs(Context context) { + context = context.getApplicationContext(); + return context.getSharedPreferences(AstridApiConstants.PUBLIC_PREFS, + Context.MODE_WORLD_READABLE); + } + /* ====================================================================== * ========================================================= system prefs * ====================================================================== */ diff --git a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java index 1dd85a98a..4da8fb58e 100644 --- a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java +++ b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java @@ -7,6 +7,7 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Color; import android.os.IBinder; import android.util.Log; @@ -21,15 +22,16 @@ import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.Preferences; -import com.todoroo.astrid.activity.SortSelectionActivity; import com.todoroo.astrid.activity.TaskEditActivity; import com.todoroo.astrid.activity.TaskListActivity; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.core.CoreFilterExposer; +import com.todoroo.astrid.core.SortHelper; import com.todoroo.astrid.dao.Database; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.TaskService; +import com.todoroo.astrid.utility.AstridPreferences; public class TasksWidget extends AppWidgetProvider { @@ -144,9 +146,10 @@ public class TasksWidget extends AppWidgetProvider { filter = getFilter(widgetId); views.setTextViewText(R.id.widget_title, filter.title); - int flags = Preferences.getInt(SortSelectionActivity.PREF_SORT_FLAGS, 0); - int sort = Preferences.getInt(SortSelectionActivity.PREF_SORT_SORT, 0); - String query = SortSelectionActivity.adjustQueryForFlagsAndSort( + SharedPreferences publicPrefs = AstridPreferences.getPublicPrefs(this); + int flags = publicPrefs.getInt(SortHelper.PREF_SORT_FLAGS, 0); + int sort = publicPrefs.getInt(SortHelper.PREF_SORT_SORT, 0); + String query = SortHelper.adjustQueryForFlagsAndSort( filter.sqlQuery, flags, sort) + " LIMIT " + numberOfTasks; database.openForReading();