Commit d0adb639 authored by Ricki Hirner's avatar Ricki Hirner

Move syncAdapterUri to TaskProvider

parent cf68aac5
...@@ -51,11 +51,7 @@ public class AndroidTaskTest extends InstrumentationTestCase { ...@@ -51,11 +51,7 @@ public class AndroidTaskTest extends InstrumentationTestCase {
// helpers // helpers
private Uri syncAdapterURI(Uri uri) { private Uri syncAdapterURI(Uri uri) {
return uri.buildUpon() return TaskProvider.syncAdapterUri(uri, testAccount);
.appendQueryParameter(TaskContract.ACCOUNT_TYPE, testAccount.type)
.appendQueryParameter(TaskContract.ACCOUNT_NAME, testAccount.name)
.appendQueryParameter(TaskContract.CALLER_IS_SYNCADAPTER, "true")
.build();
} }
......
...@@ -159,7 +159,7 @@ abstract class AndroidTask( ...@@ -159,7 +159,7 @@ abstract class AndroidTask(
@Throws(CalendarStorageException::class) @Throws(CalendarStorageException::class)
fun add(): Uri? { fun add(): Uri? {
val batch = BatchOperation(taskList.provider.client) val batch = BatchOperation(taskList.provider.client)
val builder = ContentProviderOperation.newInsert(taskList.syncAdapterURI(taskList.provider.tasksUri())) val builder = ContentProviderOperation.newInsert(taskList.tasksSyncUri())
buildTask(builder, false) buildTask(builder, false)
batch.enqueue(BatchOperation.Operation(builder)) batch.enqueue(BatchOperation.Operation(builder))
batch.commit() batch.commit()
...@@ -171,7 +171,7 @@ abstract class AndroidTask( ...@@ -171,7 +171,7 @@ abstract class AndroidTask(
@Throws(CalendarStorageException::class) @Throws(CalendarStorageException::class)
fun update(task: Task) { fun update(task: Task) {
this.task = task; this.task = task
val batch = BatchOperation(taskList.provider.client) val batch = BatchOperation(taskList.provider.client)
val builder = ContentProviderOperation.newUpdate(taskSyncURI()) val builder = ContentProviderOperation.newUpdate(taskSyncURI())
...@@ -282,7 +282,7 @@ abstract class AndroidTask( ...@@ -282,7 +282,7 @@ abstract class AndroidTask(
protected fun taskSyncURI(): Uri { protected fun taskSyncURI(): Uri {
val id = requireNotNull(id) val id = requireNotNull(id)
return taskList.syncAdapterURI(ContentUris.withAppendedId(taskList.provider.tasksUri(), id)) return ContentUris.withAppendedId(taskList.tasksSyncUri(), id)
} }
......
...@@ -70,7 +70,7 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -70,7 +70,7 @@ abstract class AndroidTaskList<out T: AndroidTask>(
Constants.log.info("Creating local task list: " + info.toString()) Constants.log.info("Creating local task list: " + info.toString())
try { try {
return provider.client.insert(syncAdapterURI(provider.taskListsUri(), account), info) return provider.client.insert(TaskProvider.syncAdapterUri(provider.taskListsUri(), account), info)
} catch(e: Exception) { } catch(e: Exception) {
throw CalendarStorageException("Couldn't create local task list", e) throw CalendarStorageException("Couldn't create local task list", e)
} }
...@@ -80,7 +80,7 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -80,7 +80,7 @@ abstract class AndroidTaskList<out T: AndroidTask>(
@Throws(FileNotFoundException::class, CalendarStorageException::class) @Throws(FileNotFoundException::class, CalendarStorageException::class)
fun<T: AndroidTaskList<AndroidTask>> findByID(account: Account, provider: TaskProvider, factory: AndroidTaskListFactory<T>, id: Long): T { fun<T: AndroidTaskList<AndroidTask>> findByID(account: Account, provider: TaskProvider, factory: AndroidTaskListFactory<T>, id: Long): T {
try { try {
provider.client.query(syncAdapterURI(ContentUris.withAppendedId(provider.taskListsUri(), id), account), null, null, null, null)?.use { cursor -> provider.client.query(TaskProvider.syncAdapterUri(ContentUris.withAppendedId(provider.taskListsUri(), id), account), null, null, null, null)?.use { cursor ->
if (cursor.moveToNext()) { if (cursor.moveToNext()) {
val taskList = factory.newInstance(account, provider, id) val taskList = factory.newInstance(account, provider, id)
val values = ContentValues(cursor.columnCount) val values = ContentValues(cursor.columnCount)
...@@ -100,7 +100,7 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -100,7 +100,7 @@ abstract class AndroidTaskList<out T: AndroidTask>(
fun<T: AndroidTaskList<AndroidTask>> find(account: Account, provider: TaskProvider, factory: AndroidTaskListFactory<T>, where: String?, whereArgs: Array<String>?): List<T> { fun<T: AndroidTaskList<AndroidTask>> find(account: Account, provider: TaskProvider, factory: AndroidTaskListFactory<T>, where: String?, whereArgs: Array<String>?): List<T> {
val taskLists = LinkedList<T>() val taskLists = LinkedList<T>()
try { try {
provider.client.query(syncAdapterURI(provider.taskListsUri(), account), null, where, whereArgs, null)?.use { cursor -> provider.client.query(TaskProvider.syncAdapterUri(provider.taskListsUri(), account), null, where, whereArgs, null)?.use { cursor ->
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
val values = ContentValues(cursor.columnCount) val values = ContentValues(cursor.columnCount)
DatabaseUtils.cursorRowToContentValues(cursor, values) DatabaseUtils.cursorRowToContentValues(cursor, values)
...@@ -115,13 +115,6 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -115,13 +115,6 @@ abstract class AndroidTaskList<out T: AndroidTask>(
return taskLists return taskLists
} }
@JvmStatic
fun syncAdapterURI(uri: Uri, account: Account) = uri.buildUpon()
.appendQueryParameter(TaskContract.ACCOUNT_NAME, account.name)
.appendQueryParameter(TaskContract.ACCOUNT_TYPE, account.type)
.appendQueryParameter(TaskContract.CALLER_IS_SYNCADAPTER, "true")
.build()!!
} }
protected fun populate(values: ContentValues) { protected fun populate(values: ContentValues) {
...@@ -159,7 +152,7 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -159,7 +152,7 @@ abstract class AndroidTaskList<out T: AndroidTask>(
val tasks = LinkedList<T>() val tasks = LinkedList<T>()
try { try {
provider.client.query( provider.client.query(
syncAdapterURI(provider.tasksUri()), tasksSyncUri(),
taskBaseInfoColumns(), taskBaseInfoColumns(),
where, whereArgs, null)?.use { cursor -> where, whereArgs, null)?.use { cursor ->
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
...@@ -174,14 +167,10 @@ abstract class AndroidTaskList<out T: AndroidTask>( ...@@ -174,14 +167,10 @@ abstract class AndroidTaskList<out T: AndroidTask>(
return tasks return tasks
} }
fun syncAdapterURI(uri: Uri) = uri.buildUpon()
.appendQueryParameter(TaskContract.ACCOUNT_NAME, account.name)
.appendQueryParameter(TaskContract.ACCOUNT_TYPE, account.type)
.appendQueryParameter(TaskContract.CALLER_IS_SYNCADAPTER, "true")
.build()!!
fun taskListSyncUri() = fun taskListSyncUri() =
syncAdapterURI(ContentUris.withAppendedId(provider.taskListsUri(), id)) TaskProvider.syncAdapterUri(ContentUris.withAppendedId(provider.taskListsUri(), id), account)
fun tasksSyncUri() =
TaskProvider.syncAdapterUri(provider.tasksUri(), account)
} }
...@@ -8,10 +8,12 @@ ...@@ -8,10 +8,12 @@
package at.bitfire.ical4android package at.bitfire.ical4android
import android.accounts.Account
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.ContentProviderClient import android.content.ContentProviderClient
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build import android.os.Build
import org.dmfs.tasks.contract.TaskContract import org.dmfs.tasks.contract.TaskContract
import java.io.Closeable import java.io.Closeable
...@@ -40,9 +42,10 @@ class TaskProvider private constructor( ...@@ -40,9 +42,10 @@ class TaskProvider private constructor(
/** /**
* Acquires a content provider for a given task provider. The content provider will * Acquires a content provider for a given task provider. The content provider will
* be released when the TaskProvider is closed. * be released when the TaskProvider is closed.
* @param resolver will be used to acquire the content provider client * @param context will be used to acquire the content provider client
* @param name task provider to acquire content provider for * @param name task provider to acquire content provider for
* @return content provider for the given task provider (may be {@code null}) * @return content provider for the given task provider (may be {@code null})
* @throws [ProviderTooOldException] if the tasks provider is installed, but doesn't meet the minimum version requirement
*/ */
@SuppressLint("Recycle") @SuppressLint("Recycle")
@JvmStatic @JvmStatic
...@@ -74,7 +77,7 @@ class TaskProvider private constructor( ...@@ -74,7 +77,7 @@ class TaskProvider private constructor(
/** /**
* Checks the version code of an installed tasks provider. * Checks the version code of an installed tasks provider.
* @throws PackageManager.NameNotFoundException if the tasks provider is not installed * @throws PackageManager.NameNotFoundException if the tasks provider is not installed
* @throws IllegalArgumentException if the tasks provider is installed, but doesn't meet the minimum version requirement * @throws [ProviderTooOldException] if the tasks provider is installed, but doesn't meet the minimum version requirement
* */ * */
private fun checkVersion(context: Context, name: ProviderName) { private fun checkVersion(context: Context, name: ProviderName) {
// check whether package is available with required minimum version // check whether package is available with required minimum version
...@@ -86,6 +89,13 @@ class TaskProvider private constructor( ...@@ -86,6 +89,13 @@ class TaskProvider private constructor(
} }
} }
@JvmStatic
fun syncAdapterUri(uri: Uri, account: Account) = uri.buildUpon()
.appendQueryParameter(TaskContract.ACCOUNT_NAME, account.name)
.appendQueryParameter(TaskContract.ACCOUNT_TYPE, account.type)
.appendQueryParameter(TaskContract.CALLER_IS_SYNCADAPTER, "true")
.build()!!
} }
...@@ -93,6 +103,7 @@ class TaskProvider private constructor( ...@@ -93,6 +103,7 @@ class TaskProvider private constructor(
fun tasksUri() = TaskContract.Tasks.getContentUri(name.authority)!! fun tasksUri() = TaskContract.Tasks.getContentUri(name.authority)!!
//fun alarmsUri() = TaskContract.Alarms.getContentUri(name.authority)!! //fun alarmsUri() = TaskContract.Alarms.getContentUri(name.authority)!!
override fun close() { override fun close() {
if (Build.VERSION.SDK_INT >= 24) if (Build.VERSION.SDK_INT >= 24)
client.close() client.close()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment