From d358ac0ba4630ff6f94081cb8a4891840a517ed4 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Wed, 1 May 2019 14:47:27 +0100 Subject: [PATCH] Import: implement importing tasks from file. Fixes #74. --- .../syncadapter/ui/ViewCollectionActivity.kt | 8 --- .../ui/importlocal/ImportActivity.kt | 4 ++ .../ui/importlocal/ImportFragment.kt | 50 ++++++++++++++++--- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt index 590b14e3..fa44e2ae 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/ViewCollectionActivity.kt @@ -142,14 +142,6 @@ class ViewCollectionActivity : BaseActivity(), Refreshable { } fun onImport(item: MenuItem) { - if (info.type == CollectionInfo.Type.TASKS) { - val dialog = AlertDialog.Builder(this) - .setIcon(R.drawable.ic_info_dark) - .setTitle("Not Implemented") - .setMessage("Importing tasks is not yet implemented") - dialog.show() - return - } startActivity(ImportActivity.newIntent(this@ViewCollectionActivity, account, info)) } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.kt b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.kt index e86550be..96cbdf59 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.kt @@ -144,6 +144,10 @@ class ImportActivity : BaseActivity(), SelectImportMethod, ResultFragment.OnImpo text.setText(R.string.import_button_local) card.setOnClickListener { mSelectImportMethod!!.importAccount() } + if ((activity as ImportActivity).info.type == CollectionInfo.Type.TASKS) { + card.visibility = View.GONE + } + return v } } diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt index 072d3099..c3380fb2 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportFragment.kt @@ -14,9 +14,7 @@ import android.os.Bundle import android.provider.CalendarContract import android.provider.ContactsContract import androidx.fragment.app.DialogFragment -import at.bitfire.ical4android.CalendarStorageException -import at.bitfire.ical4android.Event -import at.bitfire.ical4android.InvalidCalendarException +import at.bitfire.ical4android.* import at.bitfire.vcard4android.BatchOperation import at.bitfire.vcard4android.Contact import at.bitfire.vcard4android.ContactsStorageException @@ -117,10 +115,10 @@ class ImportFragment : DialogFragment() { intent.addCategory(Intent.CATEGORY_OPENABLE) intent.action = Intent.ACTION_GET_CONTENT - if (info.type == CollectionInfo.Type.CALENDAR) { - intent.type = "text/calendar" - } else if (info.type == CollectionInfo.Type.ADDRESS_BOOK) { - intent.type = "text/x-vcard" + when (info.type) { + CollectionInfo.Type.CALENDAR -> intent.type = "text/calendar" + CollectionInfo.Type.TASKS -> intent.type = "text/calendar" + CollectionInfo.Type.ADDRESS_BOOK -> intent.type = "text/x-vcard" } val chooser = Intent.createChooser( @@ -256,6 +254,44 @@ class ImportFragment : DialogFragment() { e.printStackTrace() } + entryProcessed() + } + } else if (info.type == CollectionInfo.Type.TASKS) { + val tasks = Task.fromReader(importReader) + importReader.close() + + if (tasks.isEmpty()) { + Logger.log.warning("Empty/invalid file.") + result.e = Exception("Empty/invalid file.") + return result + } + + result.total = tasks.size.toLong() + + finishParsingFile(tasks.size) + + val provider = TaskProvider.acquire(context, TaskProvider.ProviderName.OpenTasks)!! + val localTaskList: LocalTaskList? + try { + localTaskList = LocalTaskList.findByName(account, provider, LocalTaskList.Factory, info.uid!!) + if (localTaskList == null) { + throw FileNotFoundException("Failed to load local resource.") + } + } catch (e: FileNotFoundException) { + Logger.log.info("Fail" + e.localizedMessage) + result.e = e + return result + } + + for (task in tasks) { + try { + val localTask = LocalTask(localTaskList, task, task.uid, null) + localTask.addAsDirty() + result.added++ + } catch (e: CalendarStorageException) { + e.printStackTrace() + } + entryProcessed() } } else if (info.type == CollectionInfo.Type.ADDRESS_BOOK) {