-
Notifications
You must be signed in to change notification settings - Fork 688
Third Party API
timsu edited this page May 17, 2012
·
1 revision
Here are some brief instructions for getting setup in your Android project to communicate with Astrid.
- Checkout the Astrid project here on github.
- In Eclipse, import all of the projects (astrid, astrid-tests, astridApi, facebook, greendroid)
- Create a new Android project
- Add astridApi as an Android Library dependency
- Add the reading/writing permission to AndroidManifest.xml: <uses-permission android:name="com.todoroo.astrid.READ" /> and <uses-permission android:name="com.todoroo.astrid.WRITE" />
- Hack away
As a starter, you can check out the task and metadata dao unit test for basic syntax:
TaskDaoTests.java on Github MetadataDaoTests.java on Github
The only difference between Astrid internal code and third party apps is that third party apps must use TaskApiDao instead of TaskDao. Those interfaces, however, are almost identical.
As an example:
TaskApiDao dao = new TaskApiDao(context);
Task task = new Task();
task.setValue(Task.TITLE, "blah");
dao.save(task);
// for writing a tag
MetadataApiDao mdao = new MetadataApiDao(context);
Metadata metadata = new Metadata();
metadata.setValue(Metadata.KEY, "tag");
metadata.setValue(Metadata.TASK, task.getId());
metadata.setValue(Metadata.VALUE1, "hello tags");
mdao.save(metadata);
// and another tag
metadata.clearValue(Metadata.ID);
metadata.setValue(Metadata.VALUE1, "another tag");
mdao.save(metadata);
To get all tasks tagged "org", we have to use two queries because you can't do join's via Android's content resolver api:
// read task ids into an array
TodorooCursor<Metadata> idCursor = mdao.query(Query.select(Metadata.TASK).where(
Criterion.and(MetadataCriteria.withKey("tag"), Metadata.VALUE1.eq("org"))));
long[] ids = new long[idCursor.getCount()];
try {
for(int i = 0; i < ids.length; i++)
ids[i] = idCursor.get(Metadata.TASK);
} finally {
idCursor.close();
}
// grab tasks
TodorooCursor<Task> cursor = dao.query(Query.select(Task.PROPERTIES).where(Task.ID.in(ids))));
try {
Task task = new Task();
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
task.readFromCursor(cursor);
task.getValue(Task.TITLE); // etc
}
} finally {
cursor.close();
}
Hope that gets you started! If you have more questions, email Tim