Skip to content

Instantly share code, notes, and snippets.

@jackinside
Last active October 9, 2023 14:56
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jackinside/22dd1ed1011403646d6a to your computer and use it in GitHub Desktop.
Save jackinside/22dd1ed1011403646d6a to your computer and use it in GitHub Desktop.
A utility class to work with TickTick's Content Provider. A full guide is available at https://docs.google.com/document/d/1zo0JdIWnQWi-D0v7xikknEnsLj4RmrzOuqzScdR_tm8
/**
* This class provides the URI, const values and some methods to work with TickTick's Content Provider.
*
* A guide is available on https://docs.google.com/document/d/1zo0JdIWnQWi-D0v7xikknEnsLj4RmrzOuqzScdR_tm8
*/
public class TickTickProviderHelper {
private static final Uri TASK_URI = Uri.parse("content://com.ticktick.task.data/tasks");
private static final Uri PROJECT_URI = Uri.parse("content://com.ticktick.task.data/tasklist");
private static final String TASK_CONTENT_ITEM_TYPE = "vnd.android.cursor.item/ticktick.task.task";
enum TaskColumns {
ID, LIST_ID, TITLE, DUEDATE, SORT_ORDER, COMPLETED, PRIORITY, REMINDER_TIME, REPEAT_FLAG
}
enum ProjectColumns {
ID, NAME, COLOR
}
public static final class PriorityLevel {
public final static int Level0 = 0;// none
public final static int Level1 = 1;// low
public final static int Level2 = 2;
public final static int Level3 = 3;// medium
public final static int Level4 = 4;
public final static int Level5 = 5;// high
}
/**
* Return all Projects of current TickTick account
*
* @param context
* @return
*/
public static List<TickTickProject> getAllProjects(Context context) {
List<TickTickProject> projects = new ArrayList<TickTickProject>();
ContentResolver resolver = context.getContentResolver();
Cursor cursor = null;
try {
cursor = resolver.query(PROJECT_URI, null, null, null, null);
if (cursor.moveToFirst()) {
do {
TickTickProject project = cursorToProject(cursor);
projects.add(project);
} while (cursor.moveToNext());
}
return projects;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
/**
* Return all tasks (With completed tasks) of current TickTick account
*
* @param context
* @return
*/
public static List<TickTickTask> getAllTasks(Context context) {
List<TickTickTask> tasks = new ArrayList<TickTickTask>();
ContentResolver resolver = context.getContentResolver();
Cursor cursor = null;
try {
cursor = resolver.query(TASK_URI, null, null, new String[] {
"-1", "true"
}, null);
if (cursor.moveToFirst()) {
do {
TickTickTask task = cursorToTask(cursor);
tasks.add(task);
} while (cursor.moveToNext());
}
return tasks;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
/**
* Add a new task to TickTick
*
* @param projectId
* , the task will be add to this project
* @param context
*/
public static void insertTask(long projectId, Context context) {
Intent editIntent;
editIntent = new Intent(Intent.ACTION_INSERT);
editIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
editIntent.setDataAndType(TASK_URI.buildUpon().appendEncodedPath(projectId + "").build(),
TASK_CONTENT_ITEM_TYPE);
context.startActivity(editIntent);
}
/**
* View a task of TickTick
*
* @param projectId
* , the Project
* @param taskId
* , the task
* @param context
*/
public static void viewTask(long projectId, long taskId, Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("tasklist_id", projectId);
intent.setDataAndType(ContentUris.withAppendedId(TASK_URI, taskId), TASK_CONTENT_ITEM_TYPE);
context.startActivity(intent);
}
private static TickTickTask cursorToTask(Cursor cursor) {
TickTickTask task = new TickTickTask();
task.id = cursor.getLong(TaskColumns.ID.ordinal());
task.projectId = cursor.getLong(TaskColumns.LIST_ID.ordinal());
task.title = cursor.getString(TaskColumns.TITLE.ordinal());
task.dueDate = cursor.getLong(TaskColumns.DUEDATE.ordinal());
task.sortOrder = cursor.getLong(TaskColumns.SORT_ORDER.ordinal());
task.completedTime = cursor.getLong(TaskColumns.COMPLETED.ordinal());
task.priority = cursor.getInt(TaskColumns.PRIORITY.ordinal());
task.reminderTime = cursor.getLong(TaskColumns.REMINDER_TIME.ordinal());
return task;
}
private static TickTickProject cursorToProject(Cursor cursor) {
TickTickProject project = new TickTickProject();
project.id = cursor.getLong(ProjectColumns.ID.ordinal());
project.name = cursor.getString(ProjectColumns.NAME.ordinal());
project.color = cursor.getString(ProjectColumns.COLOR.ordinal());
return project;
}
public static class TickTickTask {
public long id;
public long projectId;
public String title;
public long dueDate;
public long sortOrder;
public long completedTime;
public int priority;
public long reminderTime;
}
public static class TickTickProject {
public long id;
public String name;
public String color;
}
}
@SpadarShut
Copy link

Joining the club of Web API fans

@braydenhouston
Copy link

Web API please! +1

@adiroiban
Copy link

Web API please :)

@aliasgarlabs
Copy link

REST APIs please :)

@vbifonixor
Copy link

+1 for web api. Wanna create some cool cli dashboard for my day

Copy link

ghost commented Feb 26, 2019

Web API pls k thanx bye

@SimonKapl
Copy link

REST API pls

@paxcodes
Copy link

+1 on Web/REST API

@svenkapudija
Copy link

+1 on REST API

@Jedliu
Copy link

Jedliu commented Jun 12, 2019

+1 on REST API

@tigattack
Copy link

+1 on REST API

@rppig42
Copy link

rppig42 commented Aug 27, 2019

+1 on REST API

@silasabbott
Copy link

+1 on REST API

@2bezzat
Copy link

2bezzat commented Aug 28, 2019

+1 on REST API

@jackaaron
Copy link

+1 on REST API

@kadaliao
Copy link

+1 on REST API

@aadilayub
Copy link

+1 on REST API. You guys have built an amazing product. The only thing keeping me from shifting completely from Todoist is API availability (needed for linux integration)

@c01nd01r
Copy link

+1. I am planing to use it for my browser extension.

@emilhorlyck
Copy link

+1 REST API

@Kdkulakov
Copy link

+1 REST API

@aadilayub
Copy link

Also, the documentation linked in the description is not viewable

Copy link

ghost commented Nov 16, 2019

+1 REST API

@szymczakk
Copy link

+1 REST API PLS! The only thing that keep me with todoist is API. Come on! If you need help with it ping me, will be happy to help you 😉

@pavel-alay
Copy link

+1 for REST API

@wieli99
Copy link

wieli99 commented Nov 30, 2019

+1 REST API would be awesome!

@jrosn
Copy link

jrosn commented Dec 3, 2019

+1 REST API

@jackperry
Copy link

+1 for REST API. Really the only holdover keeping me from committing to a move from Todoist.

@trevorlang
Copy link

+1 for REST API. That would be fantastic. Like others, this is the only concern I have in moving over from Todoist.

@kleinjm
Copy link

kleinjm commented Mar 10, 2020

+1

@gazlaws-dev
Copy link

Thank you, it still works! Make sure to add this to your manifest
<uses-permission android:name="com.ticktick.task.permission.READ_TASKS" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment