mirror of https://github.com/tasks/tasks
Add TasksContentProvider
parent
609a8f8f23
commit
e6dfbea4cb
@ -0,0 +1,54 @@
|
|||||||
|
package org.tasks.activities
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import com.todoroo.astrid.dao.TaskDao
|
||||||
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
|
import io.reactivex.Single
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||||
|
import io.reactivex.schedulers.Schedulers
|
||||||
|
import org.tasks.intents.TaskIntents
|
||||||
|
import org.tasks.provider.TasksContentProvider
|
||||||
|
import org.tasks.provider.TasksContentProvider.Companion.URI_OPEN_TASK
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.util.*
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
@AndroidEntryPoint
|
||||||
|
class UriHandler : AppCompatActivity() {
|
||||||
|
|
||||||
|
@Inject lateinit var taskDao: TaskDao
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
when (TasksContentProvider.URI_MATCHER.match(intent.data ?: Uri.EMPTY)) {
|
||||||
|
URI_OPEN_TASK -> {
|
||||||
|
val id = intent.data?.lastPathSegment?.toLongOrNull() ?: 0
|
||||||
|
if (id > 0) {
|
||||||
|
Single.fromCallable { Optional.ofNullable(taskDao.fetch(id))}
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.doAfterTerminate(this::finish)
|
||||||
|
.subscribe(
|
||||||
|
{ startActivity(TaskIntents.getEditTaskIntent(this, it.get())) },
|
||||||
|
Timber::e)
|
||||||
|
} else {
|
||||||
|
startActivity(TaskIntents.getNewTaskIntent(this, null))
|
||||||
|
}
|
||||||
|
val intent = if (id > 0) {
|
||||||
|
val task = taskDao.fetch(id)
|
||||||
|
TaskIntents.getEditTaskIntent(this, task)
|
||||||
|
} else {
|
||||||
|
TaskIntents.getNewTaskIntent(this, null)
|
||||||
|
}
|
||||||
|
startActivity(intent)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
Timber.w("Invalid uri: ${intent.data}")
|
||||||
|
finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,92 @@
|
|||||||
|
package org.tasks.provider
|
||||||
|
|
||||||
|
import android.content.ContentProvider
|
||||||
|
import android.content.ContentValues
|
||||||
|
import android.content.UriMatcher
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.sqlite.db.SupportSQLiteQueryBuilder
|
||||||
|
import com.todoroo.astrid.data.Task
|
||||||
|
import dagger.hilt.EntryPoint
|
||||||
|
import dagger.hilt.InstallIn
|
||||||
|
import dagger.hilt.android.EntryPointAccessors
|
||||||
|
import dagger.hilt.android.components.ApplicationComponent
|
||||||
|
import org.tasks.BuildConfig
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.analytics.Firebase
|
||||||
|
import org.tasks.data.ContentProviderDao
|
||||||
|
|
||||||
|
class TasksContentProvider : ContentProvider() {
|
||||||
|
|
||||||
|
@EntryPoint
|
||||||
|
@InstallIn(ApplicationComponent::class)
|
||||||
|
interface TasksContentProviderEntryPoint {
|
||||||
|
val contentProviderDao: ContentProviderDao
|
||||||
|
val firebase: Firebase
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun insert(uri: Uri, values: ContentValues?): Uri? = null
|
||||||
|
|
||||||
|
override fun query(
|
||||||
|
uri: Uri,
|
||||||
|
projection: Array<out String>?,
|
||||||
|
selection: String?,
|
||||||
|
selectionArgs: Array<out String>?,
|
||||||
|
sortOrder: String?): Cursor? {
|
||||||
|
val hilt = hilt()
|
||||||
|
return when (URI_MATCHER.match(uri)) {
|
||||||
|
URI_TODO_AGENDA -> {
|
||||||
|
hilt.firebase.logEvent(R.string.event_todoagenda)
|
||||||
|
hilt.contentProviderDao.rawQuery(
|
||||||
|
SupportSQLiteQueryBuilder
|
||||||
|
.builder(TODO_AGENDA_TABLES)
|
||||||
|
.selection(selection, selectionArgs)
|
||||||
|
.create())
|
||||||
|
}
|
||||||
|
URI_TASKS -> hilt.contentProviderDao.getTasks()
|
||||||
|
URI_LISTS -> hilt.contentProviderDao.getLists()
|
||||||
|
URI_GOOGLE_TASK_LISTS -> hilt.contentProviderDao.getGoogleTaskLists()
|
||||||
|
else -> throw IllegalStateException("Unrecognized URI: $uri")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onCreate() = true
|
||||||
|
|
||||||
|
override fun update(
|
||||||
|
uri: Uri,
|
||||||
|
values: ContentValues?,
|
||||||
|
selection: String?,
|
||||||
|
selectionArgs: Array<out String>?) = 0
|
||||||
|
|
||||||
|
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0
|
||||||
|
|
||||||
|
override fun getType(uri: Uri): String? = null
|
||||||
|
|
||||||
|
private fun hilt() =
|
||||||
|
EntryPointAccessors.fromApplication(
|
||||||
|
context!!.applicationContext,
|
||||||
|
TasksContentProviderEntryPoint::class.java)
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TODO_AGENDA_TABLES =
|
||||||
|
"""${Task.TABLE_NAME}
|
||||||
|
LEFT JOIN google_tasks ON gt_task = _id
|
||||||
|
LEFT JOIN google_task_lists ON gtl_remote_id = gt_list_id
|
||||||
|
LEFT JOIN caldav_tasks ON cd_task = _id
|
||||||
|
LEFT JOIN caldav_lists ON cdl_uuid = cd_calendar"""
|
||||||
|
private const val AUTHORITY = BuildConfig.APPLICATION_ID;
|
||||||
|
@JvmField val CONTENT_URI: Uri = Uri.parse("content://$AUTHORITY")
|
||||||
|
const val URI_TASKS = 1
|
||||||
|
const val URI_OPEN_TASK = 2
|
||||||
|
private const val URI_LISTS = 3
|
||||||
|
private const val URI_GOOGLE_TASK_LISTS = 4
|
||||||
|
private const val URI_TODO_AGENDA = 100
|
||||||
|
val URI_MATCHER = UriMatcher(UriMatcher.NO_MATCH).apply {
|
||||||
|
addURI(AUTHORITY, "tasks", URI_TASKS)
|
||||||
|
addURI(AUTHORITY, "tasks/*", URI_OPEN_TASK)
|
||||||
|
addURI(AUTHORITY, "lists", URI_LISTS)
|
||||||
|
addURI(AUTHORITY, "google_lists", URI_GOOGLE_TASK_LISTS)
|
||||||
|
addURI(AUTHORITY, "todoagenda", URI_TODO_AGENDA)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue