Skip to content

Commit be64e01

Browse files
committed
Simplify ContentValues usage
1 parent a000241 commit be64e01

File tree

16 files changed

+197
-187
lines changed

16 files changed

+197
-187
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import android.Manifest
1010
import android.accounts.Account
1111
import android.content.ContentProviderClient
1212
import android.content.ContentUris
13-
import android.content.ContentValues
1413
import android.provider.CalendarContract
1514
import android.provider.CalendarContract.Calendars
1615
import android.provider.CalendarContract.Colors
16+
import androidx.core.content.contentValuesOf
1717
import androidx.test.platform.app.InstrumentationRegistry
1818
import androidx.test.rule.GrantPermissionRule
1919
import at.bitfire.ical4android.impl.TestCalendar
@@ -70,11 +70,12 @@ class AndroidCalendarTest {
7070
@Test
7171
fun testManageCalendars() {
7272
// create calendar
73-
val info = ContentValues()
74-
info.put(Calendars.NAME, "TestCalendar")
75-
info.put(Calendars.CALENDAR_DISPLAY_NAME, "ical4android Test Calendar")
76-
info.put(Calendars.VISIBLE, 0)
77-
info.put(Calendars.SYNC_EVENTS, 0)
73+
val info = contentValuesOf(
74+
Calendars.NAME to "TestCalendar",
75+
Calendars.CALENDAR_DISPLAY_NAME to "ical4android Test Calendar",
76+
Calendars.VISIBLE to 0,
77+
Calendars.SYNC_EVENTS to 0
78+
)
7879
val uri = AndroidCalendar.create(testAccount, provider, info)
7980
assertNotNull(uri)
8081

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import android.content.ContentProviderClient
1111
import at.bitfire.ical4android.JtxCollection
1212
import at.bitfire.ical4android.JtxCollectionFactory
1313
import at.bitfire.ical4android.JtxICalObject
14-
import at.bitfire.ical4android.util.MiscUtils.toValues
14+
import at.bitfire.synctools.storage.toContentValues
1515
import at.techbee.jtx.JtxContract
1616
import java.util.LinkedList
1717

@@ -35,7 +35,7 @@ class TestJtxCollection(
3535
val iCalObjects = LinkedList<JtxICalObject>()
3636
client.query(jtxSyncURI(), null, where, whereArgs, null)?.use { cursor ->
3737
while (cursor.moveToNext())
38-
iCalObjects += TestJtxIcalObject.Factory.fromProvider(this, cursor.toValues())
38+
iCalObjects += TestJtxIcalObject.Factory.fromProvider(this, cursor.toContentValues())
3939
}
4040
return iCalObjects
4141
}

lib/src/androidTest/kotlin/at/bitfire/ical4android/util/MiscUtilsTest.kt

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,13 @@
77
package at.bitfire.ical4android.util
88

99
import android.accounts.Account
10-
import android.content.ContentValues
11-
import android.database.MatrixCursor
1210
import android.net.Uri
13-
import androidx.test.filters.SmallTest
1411
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
15-
import at.bitfire.ical4android.util.MiscUtils.removeBlankStrings
16-
import at.bitfire.ical4android.util.MiscUtils.toValues
1712
import org.junit.Assert.assertEquals
18-
import org.junit.Assert.assertNull
1913
import org.junit.Test
2014

2115
class MiscUtilsTest {
2216

23-
@Test
24-
@SmallTest
25-
fun testCursorToValues() {
26-
val columns = arrayOf("col1", "col2")
27-
val c = MatrixCursor(columns)
28-
c.addRow(arrayOf("row1_val1", "row1_val2"))
29-
c.moveToFirst()
30-
val values = c.toValues()
31-
assertEquals("row1_val1", values.getAsString("col1"))
32-
assertEquals("row1_val2", values.getAsString("col2"))
33-
}
34-
35-
@Test
36-
@SmallTest
37-
fun testRemoveEmptyAndBlankStrings() {
38-
val values = ContentValues()
39-
values.put("key1", "value")
40-
values.put("key2", 1L)
41-
values.put("key3", "")
42-
values.put("key4", "\n")
43-
values.put("key5", " \n ")
44-
values.put("key6", " ")
45-
values.removeBlankStrings()
46-
assertEquals("value", values.getAsString("key1"))
47-
assertEquals(1L, values.getAsLong("key2"))
48-
assertNull(values.get("key3"))
49-
assertNull(values.get("key4"))
50-
assertNull(values.get("key5"))
51-
assertNull(values.get("key6"))
52-
}
53-
54-
5517
@Test
5618
fun testUriHelper_asSyncAdapter() {
5719
val account = Account("testName", "testType")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of bitfireAT/synctools which is released under GPLv3.
3+
* Copyright © All Contributors. See the LICENSE and AUTHOR files in the root directory for details.
4+
* SPDX-License-Identifier: GPL-3.0-or-later
5+
*/
6+
7+
package at.bitfire.synctools.storage
8+
9+
import android.content.ContentValues
10+
import android.database.MatrixCursor
11+
import org.junit.Assert.assertEquals
12+
import org.junit.Assert.assertNull
13+
import org.junit.Test
14+
15+
class ContentValuesHelpersTest {
16+
17+
@Test
18+
fun testCursorToContentValues() {
19+
val columns = arrayOf("col1", "col2")
20+
val c = MatrixCursor(columns)
21+
c.addRow(arrayOf("row1_val1", "row1_val2"))
22+
c.moveToFirst()
23+
val values = c.toContentValues()
24+
assertEquals("row1_val1", values.getAsString("col1"))
25+
assertEquals("row1_val2", values.getAsString("col2"))
26+
}
27+
28+
@Test
29+
fun testContentValuesRemoveBlank() {
30+
val values = ContentValues()
31+
values.put("key1", "value")
32+
values.put("key2", 1L)
33+
values.put("key3", "")
34+
values.put("key4", "\n")
35+
values.put("key5", " \n ")
36+
values.put("key6", " ")
37+
values.removeBlank()
38+
assertEquals("value", values.getAsString("key1"))
39+
assertEquals(1L, values.getAsLong("key2"))
40+
assertNull(values.get("key3"))
41+
assertNull(values.get("key4"))
42+
assertNull(values.get("key5"))
43+
assertNull(values.get("key6"))
44+
}
45+
46+
}

lib/src/androidTest/kotlin/at/bitfire/synctools/storage/JtxBatchOperationTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package at.bitfire.synctools.storage
99
import android.accounts.Account
1010
import android.content.ContentProviderClient
1111
import android.content.ContentUris
12-
import android.content.ContentValues
12+
import androidx.core.content.contentValuesOf
1313
import androidx.test.platform.app.InstrumentationRegistry
1414
import at.bitfire.ical4android.JtxCollection
1515
import at.bitfire.ical4android.JtxICalObject
@@ -48,11 +48,11 @@ class JtxBatchOperationTest {
4848
@Test
4949
fun testJtxBoard_OperationsPerYieldPoint_501() {
5050
val batch = JtxBatchOperation(provider)
51-
val uri = JtxCollection.create(testAccount, provider, ContentValues().apply {
52-
put(JtxContract.JtxCollection.ACCOUNT_NAME, testAccount.name)
53-
put(JtxContract.JtxCollection.ACCOUNT_TYPE, testAccount.type)
54-
put(JtxContract.JtxCollection.DISPLAYNAME, javaClass.name)
55-
})
51+
val uri = JtxCollection.create(testAccount, provider, contentValuesOf(
52+
JtxContract.JtxCollection.ACCOUNT_NAME to testAccount.name,
53+
JtxContract.JtxCollection.ACCOUNT_TYPE to testAccount.type,
54+
JtxContract.JtxCollection.DISPLAYNAME to javaClass.name
55+
))
5656
val collectionId = ContentUris.parseId(uri)
5757

5858
try {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import android.provider.CalendarContract.Reminders
2020
import androidx.core.content.contentValuesOf
2121
import at.bitfire.ical4android.AndroidCalendar.Companion.find
2222
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
23-
import at.bitfire.ical4android.util.MiscUtils.toValues
23+
import at.bitfire.synctools.storage.toContentValues
2424
import java.io.FileNotFoundException
2525
import java.util.LinkedList
2626
import java.util.logging.Level
@@ -106,7 +106,7 @@ class AndroidCalendar(
106106
val events = LinkedList<AndroidEvent>()
107107
provider.query(Events.CONTENT_URI.asSyncAdapter(account), null, where, whereArgs, null)?.use { cursor ->
108108
while (cursor.moveToNext())
109-
events += AndroidEvent(this, cursor.toValues())
109+
events += AndroidEvent(this, cursor.toContentValues())
110110
}
111111
return events
112112
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import at.bitfire.ical4android.AndroidEvent.Companion.numInstances
2828
import at.bitfire.ical4android.util.AndroidTimeUtils
2929
import at.bitfire.ical4android.util.DateUtils
3030
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
31-
import at.bitfire.ical4android.util.MiscUtils.removeBlankStrings
32-
import at.bitfire.ical4android.util.MiscUtils.toValues
3331
import at.bitfire.ical4android.util.TimeApiExtensions
3432
import at.bitfire.ical4android.util.TimeApiExtensions.requireZoneId
3533
import at.bitfire.ical4android.util.TimeApiExtensions.toIcal4jDate
@@ -42,6 +40,8 @@ import at.bitfire.synctools.exception.InvalidLocalResourceException
4240
import at.bitfire.synctools.storage.BatchOperation.CpoBuilder
4341
import at.bitfire.synctools.storage.CalendarBatchOperation
4442
import at.bitfire.synctools.storage.LocalStorageException
43+
import at.bitfire.synctools.storage.removeBlank
44+
import at.bitfire.synctools.storage.toContentValues
4545
import net.fortuna.ical4j.model.Date
4646
import net.fortuna.ical4j.model.DateList
4747
import net.fortuna.ical4j.model.DateTime
@@ -175,10 +175,10 @@ class AndroidEvent(
175175
// calculate some scheduling properties
176176
val groupScheduled = e.subValues.any { it.uri == Attendees.CONTENT_URI }
177177

178-
populateEvent(e.entityValues.removeBlankStrings(), groupScheduled)
178+
populateEvent(e.entityValues.removeBlank(), groupScheduled)
179179

180180
for (subValue in e.subValues) {
181-
val subValues = subValue.values.removeBlankStrings()
181+
val subValues = subValue.values.removeBlank()
182182
when (subValue.uri) {
183183
Attendees.CONTENT_URI -> populateAttendee(subValues)
184184
Reminders.CONTENT_URI -> populateReminder(subValues)
@@ -512,7 +512,7 @@ class AndroidEvent(
512512
null,
513513
Events.ORIGINAL_ID + "=?", arrayOf(id.toString()), null)?.use { c ->
514514
while (c.moveToNext()) {
515-
val values = c.toValues(true)
515+
val values = c.toContentValues(true)
516516
try {
517517
val exception = AndroidEvent(calendar, values)
518518
val exceptionEvent = exception.event!!

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import android.os.RemoteException
1313
import androidx.annotation.CallSuper
1414
import at.bitfire.ical4android.util.AndroidTimeUtils
1515
import at.bitfire.ical4android.util.DateUtils
16-
import at.bitfire.ical4android.util.MiscUtils.toValues
1716
import at.bitfire.synctools.storage.BatchOperation.CpoBuilder
1817
import at.bitfire.synctools.storage.LocalStorageException
1918
import at.bitfire.synctools.storage.TasksBatchOperation
19+
import at.bitfire.synctools.storage.toContentValues
2020
import net.fortuna.ical4j.model.Date
2121
import net.fortuna.ical4j.model.DateTime
2222
import net.fortuna.ical4j.model.Parameter
@@ -110,7 +110,7 @@ abstract class DmfsTask(
110110
val newTask = Task()
111111
field = newTask
112112

113-
val values = cursor.toValues(true)
113+
val values = cursor.toContentValues(true)
114114
logger.log(Level.FINER, "Found task", values)
115115
populateTask(values)
116116

@@ -120,7 +120,7 @@ abstract class DmfsTask(
120120

121121
while (cursor.moveToNext()) {
122122
// process the other properties
123-
populateProperty(cursor.toValues(true))
123+
populateProperty(cursor.toContentValues(true))
124124
}
125125
}
126126

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import android.net.Uri
1414
import androidx.annotation.CallSuper
1515
import at.bitfire.ical4android.DmfsTaskList.Companion.find
1616
import at.bitfire.ical4android.util.MiscUtils.asSyncAdapter
17-
import at.bitfire.ical4android.util.MiscUtils.toValues
1817
import at.bitfire.synctools.storage.BatchOperation
1918
import at.bitfire.synctools.storage.LocalStorageException
2019
import at.bitfire.synctools.storage.TasksBatchOperation
20+
import at.bitfire.synctools.storage.toContentValues
2121
import org.dmfs.tasks.contract.TaskContract
2222
import org.dmfs.tasks.contract.TaskContract.Property.Relation
2323
import org.dmfs.tasks.contract.TaskContract.TaskLists
@@ -71,7 +71,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
7171
)?.use { cursor ->
7272
if (cursor.moveToNext()) {
7373
val taskList = factory.newInstance(account, provider, providerName, id)
74-
taskList.populate(cursor.toValues())
74+
taskList.populate(cursor.toContentValues())
7575
return taskList
7676
}
7777
}
@@ -95,7 +95,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
9595
null
9696
)?.use { cursor ->
9797
while (cursor.moveToNext()) {
98-
val values = cursor.toValues()
98+
val values = cursor.toContentValues()
9999
val taskList =
100100
factory.newInstance(account, provider, providerName, values.getAsLong(TaskLists._ID))
101101
taskList.populate(values)
@@ -177,7 +177,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
177177
null, null
178178
)?.use { cursor ->
179179
while (cursor.moveToNext()) {
180-
val values = cursor.toValues()
180+
val values = cursor.toContentValues()
181181
val id = values.getAsLong(Relation.PROPERTY_ID)
182182
val propertyContentUri = ContentUris.withAppendedId(tasksPropertiesSyncUri(), id)
183183
batch += BatchOperation.CpoBuilder
@@ -209,7 +209,7 @@ abstract class DmfsTaskList<out T : DmfsTask>(
209209
where, whereArgs, null
210210
)?.use { cursor ->
211211
while (cursor.moveToNext())
212-
tasks += taskFactory.fromProvider(this, cursor.toValues())
212+
tasks += taskFactory.fromProvider(this, cursor.toContentValues())
213213
}
214214
return tasks
215215
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import android.content.ContentUris
1212
import android.content.ContentValues
1313
import android.content.Context
1414
import android.net.Uri
15-
import at.bitfire.ical4android.util.MiscUtils.toValues
1615
import at.bitfire.synctools.storage.LocalStorageException
16+
import at.bitfire.synctools.storage.toContentValues
1717
import at.techbee.jtx.JtxContract
1818
import at.techbee.jtx.JtxContract.asSyncAdapter
1919
import net.fortuna.ical4j.model.Calendar
@@ -45,7 +45,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
4545
val collections = LinkedList<T>()
4646
client.query(JtxContract.JtxCollection.CONTENT_URI.asSyncAdapter(account), null, where, whereArgs, null)?.use { cursor ->
4747
while (cursor.moveToNext()) {
48-
val values = cursor.toValues()
48+
val values = cursor.toContentValues()
4949
val collection = factory.newInstance(account, client, values.getAsLong(JtxContract.JtxCollection.ID))
5050
collection.populate(values, context)
5151
collections += collection
@@ -122,7 +122,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
122122
).use { cursor ->
123123
logger.fine("findDeleted: found ${cursor?.count} deleted records in ${account.name}")
124124
while (cursor?.moveToNext() == true) {
125-
values.add(cursor.toValues())
125+
values.add(cursor.toContentValues())
126126
}
127127
}
128128
return values
@@ -142,7 +142,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
142142
).use { cursor ->
143143
logger.fine("findDirty: found ${cursor?.count} dirty records in ${account.name}")
144144
while (cursor?.moveToNext() == true) {
145-
values.add(cursor.toValues())
145+
values.add(cursor.toContentValues())
146146
}
147147
}
148148
return values
@@ -163,7 +163,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
163163
if (cursor?.count != 1)
164164
return null
165165
cursor.moveToFirst()
166-
return cursor.toValues()
166+
return cursor.toContentValues()
167167
}
168168
}
169169

@@ -178,7 +178,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
178178
if (cursor?.count != 1)
179179
return null
180180
cursor.moveToFirst()
181-
return cursor.toValues()
181+
return cursor.toContentValues()
182182
}
183183
}
184184

@@ -201,7 +201,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
201201
if (cursor?.count != 1)
202202
return null
203203
cursor.moveToFirst()
204-
return cursor.toValues()
204+
return cursor.toContentValues()
205205
}
206206
}
207207

@@ -264,7 +264,7 @@ open class JtxCollection<out T: JtxICalObject>(val account: Account,
264264

265265
while (cursor?.moveToNext() == true) {
266266
val jtxIcalObject = JtxICalObject(this)
267-
jtxIcalObject.populateFromContentValues(cursor.toValues())
267+
jtxIcalObject.populateFromContentValues(cursor.toContentValues())
268268
val singleICS = jtxIcalObject.getICalendarFormat(prodId)
269269
singleICS?.components?.forEach { component ->
270270
if(component is VToDo || component is VJournal)

0 commit comments

Comments
 (0)