properly import events from ics files shared via gmail

This commit is contained in:
tibbi 2017-04-17 16:41:17 +02:00
parent 8536e6cf1f
commit 5d5c981ed6
8 changed files with 47 additions and 38 deletions

View File

@ -4,6 +4,7 @@ import android.Manifest
import android.content.Intent import android.content.Intent
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.support.v4.app.ActivityCompat import android.support.v4.app.ActivityCompat
import android.support.v4.view.ViewPager import android.support.v4.view.ViewPager
@ -37,6 +38,7 @@ import com.simplemobiletools.commons.models.Release
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import org.joda.time.DateTime import org.joda.time.DateTime
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), NavigationListener { class MainActivity : SimpleActivity(), NavigationListener {
@ -73,7 +75,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
if (intent?.action == Intent.ACTION_VIEW && intent.data != null) { if (intent?.action == Intent.ACTION_VIEW && intent.data != null) {
importEventsFromFile(intent.data!!.path) tryImportEventsFromFile(intent.data)
} }
} }
@ -235,22 +237,37 @@ class MainActivity : SimpleActivity(), NavigationListener {
private fun importEvents() { private fun importEvents() {
FilePickerDialog(this) { FilePickerDialog(this) {
importEventsFromFile(it) importEventsDialog(it)
} }
} }
private fun importEventsFromFile(path: String) { private fun tryImportEventsFromFile(uri: Uri) {
if (path.toLowerCase().endsWith(".ics")) { if (uri.scheme == "file") {
ImportEventsDialog(this, path) { importEventsDialog(uri.path)
if (it) { } else if (uri.scheme == "content") {
updateViewPager() val tempFile = getTempFile()
} if (tempFile == null) {
toast(R.string.unknown_error_occurred)
return
} }
val inputStream = contentResolver.openInputStream(uri)
val out = FileOutputStream(tempFile)
inputStream.copyTo(out)
importEventsDialog(tempFile.absolutePath)
} else { } else {
toast(R.string.invalid_file_format) toast(R.string.invalid_file_format)
} }
} }
private fun importEventsDialog(path: String) {
ImportEventsDialog(this, path) {
if (it) {
updateViewPager()
}
}
}
private fun tryExportEvents() { private fun tryExportEvents() {
if (hasReadStoragePermission()) { if (hasReadStoragePermission()) {
exportEvents() exportEvents()

View File

@ -80,6 +80,7 @@ class DayEventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, va
eventIds.add(mItems[it].id) eventIds.add(mItems[it].id)
} }
activity.shareEvents(eventIds.distinct()) activity.shareEvents(eventIds.distinct())
actMode?.finish()
} }
private fun askConfirmDelete() { private fun askConfirmDelete() {

View File

@ -96,6 +96,7 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>,
eventIds.add((mItems[it] as ListEvent).id) eventIds.add((mItems[it] as ListEvent).id)
} }
activity.shareEvents(eventIds.distinct()) activity.shareEvents(eventIds.distinct())
actMode?.finish()
} }
private fun askConfirmDelete() { private fun askConfirmDelete() {

View File

@ -9,7 +9,6 @@ import com.simplemobiletools.calendar.extensions.dbHelper
import com.simplemobiletools.calendar.helpers.DBHelper import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.calendar.helpers.IcsImporter import com.simplemobiletools.calendar.helpers.IcsImporter
import com.simplemobiletools.calendar.helpers.IcsImporter.ImportResult.* import com.simplemobiletools.calendar.helpers.IcsImporter.ImportResult.*
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.setBackgroundWithStroke import com.simplemobiletools.commons.extensions.setBackgroundWithStroke
import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
@ -20,8 +19,6 @@ class ImportEventsDialog(val activity: Activity, val path: String, val callback:
init { init {
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply { val view = (activity.layoutInflater.inflate(R.layout.dialog_import_events, null) as ViewGroup).apply {
import_events_filename.text = activity.humanizePath(path)
updateEventType(this) updateEventType(this)
import_event_type_holder.setOnClickListener { import_event_type_holder.setOnClickListener {
SelectEventTypeDialog(activity, currEventTypeId) { SelectEventTypeDialog(activity, currEventTypeId) {

View File

@ -9,17 +9,13 @@ import com.simplemobiletools.commons.extensions.toast
import java.io.File import java.io.File
fun Activity.shareEvents(ids: List<Int>) { fun Activity.shareEvents(ids: List<Int>) {
val folder = File(cacheDir, "events") val file = getTempFile()
if (!folder.exists()) { if (file == null) {
if (!folder.mkdir()) { toast(R.string.unknown_error_occurred)
toast(R.string.unknown_error_occurred) return
return
}
} }
val file = File(folder, "events.ics")
val events = dbHelper.getEventsWithIds(ids) val events = dbHelper.getEventsWithIds(ids)
val result = IcsExporter().exportEvents(this, file, events) val result = IcsExporter().exportEvents(this, file, events)
if (result == IcsExporter.ExportResult.EXPORT_OK) { if (result == IcsExporter.ExportResult.EXPORT_OK) {
val uri = FileProvider.getUriForFile(this, "com.simplemobiletools.calendar.fileprovider", file) val uri = FileProvider.getUriForFile(this, "com.simplemobiletools.calendar.fileprovider", file)
@ -39,3 +35,15 @@ fun Activity.shareEvents(ids: List<Int>) {
} }
} }
} }
fun Activity.getTempFile(): File? {
val folder = File(cacheDir, "events")
if (!folder.exists()) {
if (!folder.mkdir()) {
toast(R.string.unknown_error_occurred)
return null
}
}
return File(folder, "events.ics")
}

View File

@ -339,7 +339,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} finally { } finally {
cursor?.close() cursor?.close()
} }
return ids return ids.filter { it.trim().isNotEmpty() } as ArrayList<String>
} }
fun getEvent(id: Int): Event? { fun getEvent(id: Int): Event? {

View File

@ -84,7 +84,9 @@ class IcsImporter {
if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId)) if (curTitle.isEmpty() || curStart == -1 || curEnd == -1 || importIDs.contains(curImportId))
continue continue
importIDs.add(curImportId) if (curImportId.isNotEmpty())
importIDs.add(curImportId)
val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }), val event = Event(0, curStart, curEnd, curTitle, curDescription, curReminderMinutes.getOrElse(0, { -1 }),
curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval, curReminderMinutes.getOrElse(1, { -1 }), curReminderMinutes.getOrElse(2, { -1 }), curRepeatInterval,
curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventType) curImportId, curFlags, curRepeatLimit, curRepeatRule, curEventType)

View File

@ -9,23 +9,6 @@
android:paddingRight="@dimen/activity_margin" android:paddingRight="@dimen/activity_margin"
android:paddingTop="@dimen/activity_margin"> android:paddingTop="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/import_events_filename_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/filename"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/import_events_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginLeft="@dimen/activity_margin"
android:paddingBottom="@dimen/small_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"/>
<com.simplemobiletools.commons.views.MyTextView <com.simplemobiletools.commons.views.MyTextView
android:id="@+id/import_events_event_type_label" android:id="@+id/import_events_event_type_label"
android:layout_width="wrap_content" android:layout_width="wrap_content"