Skip to content

Commit de78892

Browse files
authored
Remove possibility to subclass DmfsTask (#155)
* Remove possibility to subclass DmfsTask * Adapt tests
1 parent 5fb54ec commit de78892

File tree

8 files changed

+64
-92
lines changed

8 files changed

+64
-92
lines changed

lib/src/androidTest/kotlin/at/bitfire/ical4android/DmfsTaskListTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import android.content.ContentUris
1111
import android.content.ContentValues
1212
import android.database.DatabaseUtils
1313
import android.provider.CalendarContract
14-
import at.bitfire.ical4android.impl.TestTask
1514
import at.bitfire.ical4android.impl.TestTaskList
1615
import net.fortuna.ical4j.model.property.RelatedTo
1716
import org.dmfs.tasks.contract.TaskContract
@@ -77,9 +76,9 @@ class DmfsTaskListTest(providerName: TaskProvider.ProviderName):
7776
child.relatedTo.add(RelatedTo(parent.uid))
7877

7978
// insert child before parent
80-
val childContentUri = TestTask(taskList, child).add()
79+
val childContentUri = DmfsTask(taskList, child, "452a5672-e2b0-434e-92b4-bc70a7a51ef2", null, 0).add()
8180
val childId = ContentUris.parseId(childContentUri)
82-
val parentContentUri = TestTask(taskList, parent).add()
81+
val parentContentUri = DmfsTask(taskList, parent, "452a5672-e2b0-434e-92b4-bc70a7a51ef2", null, 0).add()
8382
val parentId = ContentUris.parseId(parentContentUri)
8483

8584
// OpenTasks should provide the correct relation

lib/src/androidTest/kotlin/at/bitfire/ical4android/DmfsTaskTest.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import android.database.DatabaseUtils
1313
import android.net.Uri
1414
import android.provider.CalendarContract
1515
import androidx.core.content.contentValuesOf
16-
import at.bitfire.ical4android.impl.TestTask
1716
import at.bitfire.ical4android.impl.TestTaskList
1817
import at.bitfire.synctools.storage.LocalStorageException
1918
import net.fortuna.ical4j.model.Date
@@ -92,7 +91,7 @@ class DmfsTaskTest(
9291
val task = Task().apply {
9392
taskBuilder()
9493
}
95-
val uri = TestTask(taskList!!, task).add()
94+
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
9695
provider.client.query(uri, null, null, null, null)!!.use {
9796
it.moveToNext()
9897
val values = ContentValues()
@@ -688,7 +687,7 @@ class DmfsTaskTest(
688687
task.unknownProperties += XProperty("X-UNKNOWN-PROP", "Unknown Value")
689688

690689
// add to task list
691-
val uri = TestTask(taskList!!, task).add()
690+
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
692691
assertNotNull("Couldn't add task", uri)
693692

694693
// read and parse event from calendar provider
@@ -721,7 +720,7 @@ class DmfsTaskTest(
721720
task.dtStart = DtStart(Date("20150102"))
722721

723722
task.due = Due(Date("20150101"))
724-
TestTask(taskList!!, task).add()
723+
DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
725724
}
726725

727726
@Test
@@ -734,7 +733,7 @@ class DmfsTaskTest(
734733
for (i in 1..1050)
735734
task.alarms += VAlarm(java.time.Duration.ofMinutes(i.toLong()))
736735

737-
val uri = TestTask(taskList!!, task).add()
736+
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
738737
val task2 = taskList!!.findById(ContentUris.parseId(uri))
739738
assertEquals(1050, task2.task?.alarms?.size)
740739
}
@@ -749,7 +748,7 @@ class DmfsTaskTest(
749748
task.location = "Sample location"
750749
task.dtStart = DtStart("20150501T120000", tzVienna)
751750
assertFalse(task.isAllDay())
752-
val uri = TestTask(taskList!!, task).add()
751+
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
753752
assertNotNull(uri)
754753

755754
val testTask = taskList!!.findById(ContentUris.parseId(uri))
@@ -782,7 +781,7 @@ class DmfsTaskTest(
782781
task.dtStart = DtStart(Date("20150501"))
783782
task.due = Due(Date("20150502"))
784783
assertTrue(task.isAllDay())
785-
val uri = TestTask(taskList!!, task).add()
784+
val uri = DmfsTask(taskList!!, task, "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0).add()
786785
assertNotNull(uri)
787786

788787
val testTask = taskList!!.findById(ContentUris.parseId(uri))
@@ -803,16 +802,16 @@ class DmfsTaskTest(
803802
@Test
804803
fun testGetTimeZone() {
805804
// no date/time
806-
var t = TestTask(taskList!!, Task())
805+
var t = DmfsTask(taskList!!, Task(), "9468a4cf-0d5b-4379-a704-12f1f84100ba", null, 0)
807806
assertEquals(tzDefault, t.getTimeZone())
808807

809808
// dtstart with date (no time)
810-
t = TestTask(taskList!!, Task())
809+
t = DmfsTask(taskList!!, Task(), "410c19d7-df79-4d65-8146-40b7bec5923b", null, 0)
811810
t.task!!.dtStart = DtStart("20150101")
812811
assertEquals(tzDefault, t.getTimeZone())
813812

814813
// dtstart with time
815-
t = TestTask(taskList!!, Task())
814+
t = DmfsTask(taskList!!, Task(), "9dc64544-1816-4f04-b952-e894164467f6", null, 0)
816815
t.task!!.dtStart = (DtStart("20150101", tzVienna))
817816
assertEquals(tzVienna, t.getTimeZone())
818817
}

lib/src/androidTest/kotlin/at/bitfire/ical4android/impl/TestTask.kt

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/src/androidTest/kotlin/at/bitfire/ical4android/impl/TestTaskList.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import at.bitfire.ical4android.TaskProvider
1616
import org.dmfs.tasks.contract.TaskContract
1717

1818
class TestTaskList(
19-
account: Account,
20-
provider: ContentProviderClient,
21-
providerName: TaskProvider.ProviderName,
22-
id: Long
23-
): DmfsTaskList<TestTask>(account, provider, providerName, TestTask.Factory, id) {
19+
account: Account,
20+
provider: ContentProviderClient,
21+
providerName: TaskProvider.ProviderName,
22+
id: Long
23+
) : DmfsTaskList(account, provider, providerName, id) {
2424

2525
companion object {
2626

@@ -33,17 +33,21 @@ class TestTaskList(
3333
values.put(TaskContract.TaskListColumns.LIST_COLOR, 0xffff0000)
3434
values.put(TaskContract.TaskListColumns.SYNC_ENABLED, 1)
3535
values.put(TaskContract.TaskListColumns.VISIBLE, 1)
36-
val uri = DmfsTaskList.create(account, provider.client, provider.name, values)
36+
val uri = create(account, provider.client, provider.name, values)
3737

3838
return TestTaskList(account, provider.client, provider.name, ContentUris.parseId(uri))
3939
}
4040

4141
}
4242

4343

44-
object Factory: DmfsTaskListFactory<TestTaskList> {
45-
override fun newInstance(account: Account, provider: ContentProviderClient, providerName: TaskProvider.ProviderName, id: Long) =
46-
TestTaskList(account, provider, providerName, id)
44+
object Factory : DmfsTaskListFactory<TestTaskList> {
45+
override fun newInstance(
46+
account: Account,
47+
provider: ContentProviderClient,
48+
providerName: TaskProvider.ProviderName,
49+
id: Long
50+
) = TestTaskList(account, provider, providerName, id)
4751
}
4852

4953
}

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTask.kt

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,27 @@ import java.util.logging.Logger
6464
* The SEQUENCE field is stored in [Tasks.SYNC_VERSION], so don't use [Tasks.SYNC_VERSION]
6565
* for anything else.
6666
*/
67-
open class DmfsTask(
68-
val taskList: DmfsTaskList<*>
67+
class DmfsTask(
68+
val taskList: DmfsTaskList
6969
) {
7070

71-
protected val logger = Logger.getLogger(javaClass.name)
72-
protected val tzRegistry by lazy { TimeZoneRegistryFactory.getInstance().createRegistry() }
71+
private val logger = Logger.getLogger(javaClass.name)
72+
private val tzRegistry by lazy { TimeZoneRegistryFactory.getInstance().createRegistry() }
7373

7474
var id: Long? = null
7575
var syncId: String? = null
7676
var eTag: String? = null
7777
var flags: Int = 0
7878

7979

80-
constructor(taskList: DmfsTaskList<*>, values: ContentValues): this(taskList) {
80+
constructor(taskList: DmfsTaskList, values: ContentValues): this(taskList) {
8181
id = values.getAsLong(Tasks._ID)
8282
syncId = values.getAsString(Tasks._SYNC_ID)
8383
eTag = values.getAsString(COLUMN_ETAG)
8484
flags = values.getAsInteger(COLUMN_FLAGS) ?: 0
8585
}
8686

87-
constructor(taskList: DmfsTaskList<*>, task: Task, syncId: String?, eTag: String?, flags: Int): this(taskList) {
87+
constructor(taskList: DmfsTaskList, task: Task, syncId: String?, eTag: String?, flags: Int): this(taskList) {
8888
this.task = task
8989
this.syncId = syncId
9090
this.eTag = eTag
@@ -160,7 +160,7 @@ open class DmfsTask(
160160
throw FileNotFoundException("Couldn't find task #$id")
161161
}
162162

163-
protected fun populateTask(values: ContentValues) {
163+
private fun populateTask(values: ContentValues) {
164164
val task = requireNotNull(task)
165165

166166
task.uid = values.getAsString(Tasks._UID)
@@ -267,7 +267,7 @@ open class DmfsTask(
267267
values.getAsString(Tasks.RRULE)?.let { task.rRule = RRule(it) }
268268
}
269269

270-
protected fun populateProperty(row: ContentValues) {
270+
private fun populateProperty(row: ContentValues) {
271271
logger.log(Level.FINER, "Found property", row)
272272

273273
val task = requireNotNull(task)
@@ -287,7 +287,7 @@ open class DmfsTask(
287287
}
288288
}
289289

290-
protected fun populateAlarm(row: ContentValues) {
290+
private fun populateAlarm(row: ContentValues) {
291291
val task = requireNotNull(task)
292292
val props = PropertyList<Property>()
293293

@@ -315,7 +315,7 @@ open class DmfsTask(
315315
task.alarms += VAlarm(props)
316316
}
317317

318-
protected fun populateRelatedTo(row: ContentValues) {
318+
private fun populateRelatedTo(row: ContentValues) {
319319
val uid = row.getAsString(Relation.RELATED_UID)
320320
if (uid == null) {
321321
logger.warning("Task relation doesn't refer to same task list; can't be synchronized")
@@ -380,15 +380,19 @@ open class DmfsTask(
380380
return ContentUris.withAppendedId(Tasks.getContentUri(taskList.providerName.authority), existingId)
381381
}
382382

383-
protected fun insertProperties(batch: TasksBatchOperation, idxTask: Int?) {
383+
fun update(values: ContentValues) {
384+
taskList.provider.update(taskSyncURI(), values, null, null)
385+
}
386+
387+
private fun insertProperties(batch: TasksBatchOperation, idxTask: Int?) {
384388
insertAlarms(batch, idxTask)
385389
insertCategories(batch, idxTask)
386390
insertComment(batch, idxTask)
387391
insertRelatedTo(batch, idxTask)
388392
insertUnknownProperties(batch, idxTask)
389393
}
390394

391-
protected fun insertAlarms(batch: TasksBatchOperation, idxTask: Int?) {
395+
private fun insertAlarms(batch: TasksBatchOperation, idxTask: Int?) {
392396
val task = requireNotNull(task)
393397
for (alarm in task.alarms) {
394398
val (alarmRef, minutes) = ICalendar.vAlarmToMin(
@@ -430,7 +434,7 @@ open class DmfsTask(
430434
}
431435
}
432436

433-
protected fun insertCategories(batch: TasksBatchOperation, idxTask: Int?) {
437+
private fun insertCategories(batch: TasksBatchOperation, idxTask: Int?) {
434438
for (category in requireNotNull(task).categories) {
435439
val builder = CpoBuilder.newInsert(taskList.tasksPropertiesSyncUri())
436440
.withTaskId(Category.TASK_ID, idxTask)
@@ -441,7 +445,7 @@ open class DmfsTask(
441445
}
442446
}
443447

444-
protected fun insertComment(batch: TasksBatchOperation, idxTask: Int?) {
448+
private fun insertComment(batch: TasksBatchOperation, idxTask: Int?) {
445449
val comment = requireNotNull(task).comment ?: return
446450
val builder = CpoBuilder.newInsert(taskList.tasksPropertiesSyncUri())
447451
.withTaskId(Comment.TASK_ID, idxTask)
@@ -451,7 +455,7 @@ open class DmfsTask(
451455
batch += builder
452456
}
453457

454-
protected fun insertRelatedTo(batch: TasksBatchOperation, idxTask: Int?) {
458+
private fun insertRelatedTo(batch: TasksBatchOperation, idxTask: Int?) {
455459
for (relatedTo in requireNotNull(task).relatedTo) {
456460
val relType = when ((relatedTo.getParameter(Parameter.RELTYPE) as RelType?)) {
457461
RelType.CHILD ->
@@ -471,7 +475,7 @@ open class DmfsTask(
471475
}
472476
}
473477

474-
protected fun insertUnknownProperties(batch: TasksBatchOperation, idxTask: Int?) {
478+
private fun insertUnknownProperties(batch: TasksBatchOperation, idxTask: Int?) {
475479
for (property in requireNotNull(task).unknownProperties) {
476480
if (property.value.length > UnknownProperty.MAX_UNKNOWN_PROPERTY_SIZE) {
477481
logger.warning("Ignoring unknown property with ${property.value.length} octets (too long)")
@@ -491,7 +495,7 @@ open class DmfsTask(
491495
return taskList.provider.delete(taskSyncURI(), null, null)
492496
}
493497

494-
protected fun buildTask(builder: CpoBuilder, update: Boolean) {
498+
private fun buildTask(builder: CpoBuilder, update: Boolean) {
495499
if (!update)
496500
builder .withValue(Tasks.LIST_ID, taskList.id)
497501

@@ -602,7 +606,7 @@ open class DmfsTask(
602606
}
603607

604608

605-
protected fun CpoBuilder.withTaskId(column: String, idxTask: Int?): CpoBuilder {
609+
private fun CpoBuilder.withTaskId(column: String, idxTask: Int?): CpoBuilder {
606610
if (idxTask != null)
607611
withValueBackReference(column, idxTask)
608612
else
@@ -611,7 +615,7 @@ open class DmfsTask(
611615
}
612616

613617

614-
protected fun taskSyncURI(loadProperties: Boolean = false): Uri {
618+
private fun taskSyncURI(loadProperties: Boolean = false): Uri {
615619
val id = requireNotNull(id)
616620
return ContentUris.withAppendedId(taskList.tasksSyncUri(loadProperties), id)
617621
}

lib/src/main/kotlin/at/bitfire/ical4android/DmfsTaskFactory.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)