add some google events parsing

This commit is contained in:
tibbi 2017-01-31 00:09:12 +01:00
parent 238fb44b39
commit af3031e503
3 changed files with 29 additions and 0 deletions

View File

@ -8,11 +8,17 @@ import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecovera
import com.google.api.client.json.gson.GsonFactory
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.model.Event
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SettingsActivity
import com.simplemobiletools.calendar.models.GoogleEvent
import java.util.*
// more info about event fields at https://developers.google.com/google-apps/calendar/v3/reference/events/insert
class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCredential) : AsyncTask<Void, Void, List<Event>>() {
private val CONFIRMED = "confirmed"
private var service: com.google.api.services.calendar.Calendar
private var lastError: Exception? = null
@ -41,9 +47,26 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
.setSingleEvents(true)
.execute()
for (event in events) {
if (event.key == "items") {
val parsed = parseEvents(event.value.toString())
}
}
return events.items
}
private fun parseEvents(json: String): List<com.simplemobiletools.calendar.models.Event> {
val events = ArrayList<com.simplemobiletools.calendar.models.Event>()
val token = object : TypeToken<List<GoogleEvent>>() {}.type
val googleEvents = Gson().fromJson<ArrayList<GoogleEvent>>(json, token) ?: ArrayList<GoogleEvent>(8)
for (googleEvent in googleEvents) {
if (googleEvent.status != CONFIRMED)
continue
}
return events
}
override fun onCancelled() {
if (lastError != null) {
if (lastError is UserRecoverableAuthIOException) {

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.calendar.models
data class GoogleEvent(val summary: String, val description: String, val status: String, val start: GoogleEventDateTime, val end: GoogleEventDateTime)

View File

@ -0,0 +1,3 @@
package com.simplemobiletools.calendar.models
data class GoogleEventDateTime(val date: String, val dateTime: String, val timeZone: String)