@@ -8,6 +8,7 @@ package at.bitfire.synctools.storage.tasks
88
99import android.content.ContentUris
1010import android.content.ContentValues
11+ import android.content.Entity
1112import android.net.Uri
1213import android.os.RemoteException
1314import at.bitfire.ical4android.DmfsTask
@@ -21,7 +22,7 @@ import java.util.LinkedList
2122import java.util.logging.Logger
2223
2324/* *
24- * Represents a locally stored task list, containing [at.bitfire.ical4android. DmfsTask]s (tasks).
25+ * Represents a locally stored task list, containing [DmfsTask]s (tasks).
2526 * Communicates with tasks.org-compatible content providers (currently tasks.org and OpenTasks) to store the tasks.
2627 */
2728class DmfsTaskList (
@@ -59,25 +60,65 @@ class DmfsTaskList(
5960 * @param where selection
6061 * @param whereArgs arguments for selection
6162 *
62- * @return events from this task list which match the selection
63+ * @return tasks from this task list which match the selection
6364 */
6465 fun findTasks (where : String? = null, whereArgs : Array <String >? = null): List <DmfsTask > {
6566 val tasks = LinkedList <DmfsTask >()
6667 try {
6768 val (protectedWhere, protectedWhereArgs) = whereWithTaskListId(where, whereArgs)
68- client.query(tasksUri(), null , protectedWhere, protectedWhereArgs, null )?.use { cursor ->
69- while (cursor.moveToNext())
70- tasks + = DmfsTask (this , cursor.toContentValues())
69+ client.query(tasksUri(), arrayOf(TaskContract .Tasks ._ID ), protectedWhere, protectedWhereArgs, null )?.use { cursor ->
70+ while (cursor.moveToNext()) {
71+ val taskId = cursor.getLong(0 )
72+ val entity = getTaskEntity(taskId)
73+ if (entity != null )
74+ tasks + = DmfsTask (this , entity)
75+ }
7176 }
7277 } catch (e: RemoteException ) {
7378 throw LocalStorageException (" Couldn't query ${providerName.authority} tasks" , e)
7479 }
7580 return tasks
7681 }
7782
78- fun getTask (id : Long ) = findTasks(" ${TaskContract .Tasks ._ID } =?" , arrayOf(id.toString())).firstOrNull()
79- ? : throw LocalStorageException (" Couldn't query ${providerName.authority} tasks" )
80-
83+ /* *
84+ * Gets a task from this task list by given id.
85+ *
86+ * @return task from this task list which matches the selection
87+ */
88+ fun getTask (id : Long ): DmfsTask ? {
89+ val values = getTaskEntity(id) ? : return null
90+ return DmfsTask (this , values)
91+ }
92+
93+ /* *
94+ * Retrieves a task as entity from this task list by given id and selection.
95+ *
96+ * @param where selection
97+ * @param whereArgs arguments for selection
98+ *
99+ * @return task from this task list which matches the selection
100+ */
101+ fun getTaskEntity (id : Long , where : String? = null, whereArgs : Array <String >? = null): Entity ? {
102+ try {
103+ client.query(taskUri(id, true ), null , where, whereArgs, null )?.use { cursor ->
104+ if (cursor.moveToFirst()) {
105+ // first row holds entity main values
106+ val entity = Entity (cursor.toContentValues())
107+ // remaining rows hold entity subvalues (extended properties)
108+ while (cursor.moveToNext()) {
109+ val cv = cursor.toContentValues()
110+ val mimetype = cv.getAsString(TaskContract .PropertyColumns .MIMETYPE ) // CONTENT_ITEM_TYPE of extended property
111+ entity.addSubValue(tasksPropertyUri(mimetype), cv)
112+ }
113+ return entity
114+ }
115+ }
116+ } catch (e: RemoteException ) {
117+ throw LocalStorageException (" Couldn't query task entity" , e)
118+ }
119+ return null
120+ }
121+
81122 /* *
82123 * Updates tasks in this task list.
83124 *
@@ -155,6 +196,9 @@ class DmfsTaskList(
155196 fun tasksPropertiesUri () =
156197 TaskContract .Properties .getContentUri(providerName.authority).asSyncAdapter(account)
157198
199+ fun tasksPropertyUri (mimetype : String ): Uri =
200+ tasksPropertiesUri().buildUpon().appendPath(mimetype).build()!!
201+
158202 /* *
159203 * Restricts a given selection/where clause to this task list ID.
160204 *
0 commit comments