Added indentation action and unit test

pull/14/head
Tim Su 14 years ago
parent 3b6cf82d8d
commit 927ba61e5a

@ -465,6 +465,20 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name="com.todoroo.astrid.gtasks.GtasksIndentAction$GtasksIncreaseIndentAction"
android:label="@string/gtasks_increase_indent">
<intent-filter>
<action android:name="com.todoroo.astrid.CONTEXT_MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name="com.todoroo.astrid.gtasks.GtasksIndentAction$GtasksDecreaseIndentAction"
android:label="@string/gtasks_decrease_indent">
<intent-filter>
<action android:name="com.todoroo.astrid.CONTEXT_MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>

@ -0,0 +1,57 @@
package com.todoroo.astrid.gtasks;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Metadata;
abstract public class GtasksIndentAction extends BroadcastReceiver {
@Autowired private GtasksMetadataService gtasksMetadataService;
abstract int getDelta();
@Override
public void onReceive(Context context, Intent intent) {
ContextManager.setContext(context);
DependencyInjectionService.getInstance().inject(this);
long taskId = intent.getLongExtra(AstridApiConstants.EXTRAS_TASK_ID, -1);
if(taskId == -1)
return;
Metadata metadata = gtasksMetadataService.getTaskMetadata(taskId);
if(metadata == null)
metadata = GtasksMetadata.createEmptyMetadata();
int newIndent = Math.max(0, metadata.getValue(GtasksMetadata.INDENTATION) + getDelta());
metadata.setValue(GtasksMetadata.INDENTATION, newIndent);
PluginServices.getMetadataService().save(metadata);
Toast.makeText(context, context.getString(R.string.gtasks_indent_toast, newIndent),
Toast.LENGTH_SHORT).show();
}
public static class GtasksIncreaseIndentAction extends GtasksIndentAction {
@Override
public int getDelta() {
return 1;
}
}
public static class GtasksDecreaseIndentAction extends GtasksIndentAction {
@Override
public int getDelta() {
return -1;
}
}
}

@ -2,13 +2,22 @@
<!-- See the file "LICENSE" for the full license governing this code. -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ============================================= Plugin Boilerplate == -->
<!-- =========================================================== Misc == -->
<!-- filters header: GTasks -->
<string name="gtasks_FEx_header">Google Tasks</string>
<!-- filter category for GTasks lists -->
<string name="gtasks_FEx_list">By List</string>
<!-- GTasks Indent changed message (%d => new level) -->
<string name="gtasks_indent_toast">New Indentation: %d</string>
<!-- GTasks label: increase indent -->
<string name="gtasks_increase_indent">Move Right</string>
<!-- GTasks label: decrease indent -->
<string name="gtasks_decrease_indent">Move Left</string>
<!-- ============================================== GtasksPreferences == -->

@ -58,7 +58,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.Pair;
import com.todoroo.andlib.widget.GestureService;
import com.todoroo.andlib.widget.GestureService.GestureInterface;
import com.todoroo.astrid.activity.SortSelectionActivity.OnSortSelectedListener;
@ -76,6 +75,8 @@ import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.helper.TaskListContextMenuExtensionLoader;
import com.todoroo.astrid.helper.TaskListContextMenuExtensionLoader.ContextMenuItem;
import com.todoroo.astrid.reminders.Notifications;
import com.todoroo.astrid.reminders.ReminderService;
import com.todoroo.astrid.reminders.ReminderService.AlarmScheduler;
@ -161,6 +162,9 @@ public class TaskListActivity extends ListActivity implements OnScrollListener,
private Timer backgroundTimer;
private final LinkedHashSet<SyncAction> syncActions = new LinkedHashSet<SyncAction>();
private final TaskListContextMenuExtensionLoader contextMenuExtensionLoader = new TaskListContextMenuExtensionLoader();
/* ======================================================================
* ======================================================= initialization
* ====================================================================== */
@ -210,12 +214,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener,
if(Constants.DEBUG)
setTitle("[D] " + filter.title); //$NON-NLS-1$
// perform caching
new Thread(new Runnable() {
public void run() {
loadContextMenuIntents();
}
}).start();
contextMenuExtensionLoader.loadInNewThread(this);
}
/**
@ -746,24 +745,6 @@ public class TaskListActivity extends ListActivity implements OnScrollListener,
return task;
}
protected Pair<CharSequence, Intent>[] contextMenuItemCache = null;
protected void loadContextMenuIntents() {
Intent queryIntent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
PackageManager pm = getPackageManager();
List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(queryIntent, 0);
int length = resolveInfoList.size();
contextMenuItemCache = new Pair[length];
for(int i = 0; i < length; i++) {
ResolveInfo resolveInfo = resolveInfoList.get(i);
Intent intent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
intent.setClassName(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
CharSequence title = resolveInfo.loadLabel(pm);
contextMenuItemCache[i] = Pair.create(title, intent);
}
}
@SuppressWarnings("nls")
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
@ -785,25 +766,20 @@ public class TaskListActivity extends ListActivity implements OnScrollListener,
menu.add(id, CONTEXT_MENU_DELETE_TASK_ID, Menu.NONE,
R.string.TAd_contextDeleteTask);
long taskId = task.getId();
for(ContextMenuItem item : contextMenuExtensionLoader.getList()) {
MenuItem menuItem = menu.add(id, CONTEXT_MENU_ADDON_INTENT_ID, Menu.NONE,
item.title);
item.intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
menuItem.setIntent(item.intent);
}
if(Constants.DEBUG) {
menu.add("--- debug ---");
menu.add(id, CONTEXT_MENU_DEBUG, Menu.NONE,
"when alarm?");
"when alarm?");
menu.add(id, CONTEXT_MENU_DEBUG + 1, Menu.NONE,
"make notification");
}
if(contextMenuItemCache == null)
return;
// ask about plug-ins
long taskId = task.getId();
for(int i = 0; i < contextMenuItemCache.length; i++) {
Intent intent = contextMenuItemCache[i].getRight();
MenuItem item = menu.add(id, CONTEXT_MENU_ADDON_INTENT_ID, Menu.NONE,
contextMenuItemCache[i].getLeft());
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
item.setIntent(intent);
"make notification");
}
}
}

@ -0,0 +1,55 @@
package com.todoroo.astrid.helper;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import com.todoroo.astrid.api.AstridApiConstants;
public class TaskListContextMenuExtensionLoader {
public static class ContextMenuItem {
public CharSequence title;
public Intent intent;
}
private ContextMenuItem[] contextMenuItemCache = new ContextMenuItem[0];
public void loadContextMenuIntents(Context context) {
Intent queryIntent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfoList = pm.queryBroadcastReceivers(queryIntent,
PackageManager.GET_META_DATA);
int length = resolveInfoList.size();
contextMenuItemCache = new ContextMenuItem[length];
for(int i = 0; i < length; i++) {
ResolveInfo resolveInfo = resolveInfoList.get(i);
ContextMenuItem item = new ContextMenuItem();
item.intent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
item.intent.setClassName(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
item.title = resolveInfo.loadLabel(pm);
contextMenuItemCache[i] = item;
}
}
public void loadInNewThread(final Context context) {
new Thread(new Runnable() {
public void run() {
loadContextMenuIntents(context);
}
}).start();
}
public ContextMenuItem[] getList() {
return contextMenuItemCache;
}
}

@ -0,0 +1,100 @@
package com.todoroo.astrid.gtasks;
import android.content.BroadcastReceiver;
import android.content.Intent;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gtasks.GtasksIndentAction.GtasksDecreaseIndentAction;
import com.todoroo.astrid.gtasks.GtasksIndentAction.GtasksIncreaseIndentAction;
import com.todoroo.astrid.test.DatabaseTestCase;
public class GtasksIndentActionTest extends DatabaseTestCase {
@Autowired private GtasksMetadataService gtasksMetadataService;
private Task task;
public void testIndentWithoutMetadata() {
givenTask(taskWithoutMetadata());
whenTrigger(new GtasksIncreaseIndentAction());
thenExpectIndentationLevel(1);
}
public void testIndentWithMetadata() {
givenTask(taskWithMetadata(0));
whenTrigger(new GtasksIncreaseIndentAction());
thenExpectIndentationLevel(1);
}
public void testDeindentWithMetadata() {
givenTask(taskWithMetadata(1));
whenTrigger(new GtasksDecreaseIndentAction());
thenExpectIndentationLevel(0);
}
public void testDeindentWithoutMetadata() {
givenTask(taskWithoutMetadata());
whenTrigger(new GtasksDecreaseIndentAction());
thenExpectIndentationLevel(0);
}
public void testDeindentWhenAlreadyZero() {
givenTask(taskWithMetadata(0));
whenTrigger(new GtasksDecreaseIndentAction());
thenExpectIndentationLevel(0);
}
// --- helpers
private Task taskWithMetadata(int indentation) {
Task task = new Task();
PluginServices.getTaskService().save(task);
Metadata metadata = GtasksMetadata.createEmptyMetadata();
metadata.setValue(GtasksMetadata.INDENTATION, indentation);
metadata.setValue(Metadata.TASK, task.getId());
PluginServices.getMetadataService().save(metadata);
return task;
}
private void thenExpectIndentationLevel(int expected) {
Metadata metadata = gtasksMetadataService.getTaskMetadata(task.getId());
assertNotNull(metadata);
int indentation = metadata.getValue(GtasksMetadata.INDENTATION);
assertTrue("indentation: " + indentation,
indentation == expected);
}
private void whenTrigger(BroadcastReceiver action) {
Intent intent = new Intent(AstridApiConstants.ACTION_TASK_CONTEXT_MENU);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
action.onReceive(getContext(), intent);
}
private void givenTask(Task taskToTest) {
task = taskToTest;
}
private Task taskWithoutMetadata() {
Task task = new Task();
PluginServices.getTaskService().save(task);
return task;
}
}
Loading…
Cancel
Save