@@ -8,6 +8,8 @@ package at.bitfire.synctools.storage.tasks
88
99import android.content.ContentUris
1010import android.content.ContentValues
11+ import android.content.Entity
12+ import android.database.Cursor
1113import android.net.Uri
1214import android.os.RemoteException
1315import at.bitfire.ical4android.DmfsTask
@@ -21,7 +23,7 @@ import java.util.LinkedList
2123import java.util.logging.Logger
2224
2325/* *
24- * Represents a locally stored task list, containing [at.bitfire.ical4android. DmfsTask]s (tasks).
26+ * Represents a locally stored task list, containing [DmfsTask]s (tasks).
2527 * Communicates with tasks.org-compatible content providers (currently tasks.org and OpenTasks) to store the tasks.
2628 */
2729class DmfsTaskList (
@@ -65,19 +67,55 @@ class DmfsTaskList(
6567 val tasks = LinkedList <DmfsTask >()
6668 try {
6769 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())
70+ client.query(tasksUri(), arrayOf(TaskContract .Tasks ._ID ), protectedWhere, protectedWhereArgs, null )?.use { cursor ->
71+ while (cursor.moveToNext()) {
72+ val taskId = cursor.getLong(0 )
73+ val entity = getTaskEntity(taskId)
74+ if (entity != null )
75+ tasks + = DmfsTask (this , entity)
76+ }
7177 }
7278 } catch (e: RemoteException ) {
7379 throw LocalStorageException (" Couldn't query ${providerName.authority} tasks" , e)
7480 }
7581 return tasks
7682 }
7783
78- fun getTask (id : Long ) = findTasks(" ${TaskContract .Tasks ._ID } =?" , arrayOf(id.toString())).firstOrNull()
79- ? : throw LocalStorageException (" Couldn't query ${providerName.authority} tasks" )
80-
84+ fun getTask (id : Long ): DmfsTask ? {
85+ val values = getTaskEntity(id) ? : return null
86+ return DmfsTask (this , values)
87+ }
88+
89+ fun getTaskEntity (id : Long , where : String? = null, whereArgs : Array <String >? = null): Entity ? {
90+ try {
91+ client.query(taskUri(id, true ), null , where, whereArgs, null )?.use { cursor ->
92+ if (cursor.moveToFirst()) {
93+ // first row holds main values, we expect cursor is already at row we need to read from
94+ val entity = Entity (cursor.toContentValues())
95+ addSubValuesToEntity(cursor, entity)
96+ return entity
97+ }
98+ }
99+ } catch (e: RemoteException ) {
100+ throw LocalStorageException (" Couldn't query event entity" , e)
101+ }
102+ return null
103+ }
104+
105+
106+ /* *
107+ * Adds subvalues (extended properties like attendee, attachment, etc) to given entity.
108+ */
109+ private fun addSubValuesToEntity (cursor : Cursor , entity : Entity ) {
110+ // we expect the cursor already is at position to start reading
111+ // all rows hold subvalues (extended properties)
112+ while (cursor.moveToNext()) {
113+ val cv = cursor.toContentValues()
114+ val mimetype = cv.getAsString(TaskContract .PropertyColumns .MIMETYPE ) // CONTENT_ITEM_TYPE of extended property
115+ entity.addSubValue(tasksPropertyUri(mimetype), cv)
116+ }
117+ }
118+
81119 /* *
82120 * Updates tasks in this task list.
83121 *
@@ -155,6 +193,9 @@ class DmfsTaskList(
155193 fun tasksPropertiesUri () =
156194 TaskContract .Properties .getContentUri(providerName.authority).asSyncAdapter(account)
157195
196+ fun tasksPropertyUri (mimetype : String ): Uri =
197+ tasksPropertiesUri().buildUpon().appendPath(mimetype).build()!!
198+
158199 /* *
159200 * Restricts a given selection/where clause to this task list ID.
160201 *
0 commit comments