mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
parse reminder minutes from google events
- use the Popup reminder type
This commit is contained in:
@@ -9,15 +9,21 @@ import com.google.api.client.json.gson.GsonFactory
|
|||||||
import com.google.api.client.util.DateTime
|
import com.google.api.client.util.DateTime
|
||||||
import com.google.api.services.calendar.model.Event
|
import com.google.api.services.calendar.model.Event
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import com.simplemobiletools.calendar.R
|
import com.simplemobiletools.calendar.R
|
||||||
import com.simplemobiletools.calendar.activities.SettingsActivity
|
import com.simplemobiletools.calendar.activities.SettingsActivity
|
||||||
import com.simplemobiletools.calendar.models.GoogleEvent
|
import com.simplemobiletools.calendar.models.GoogleEvent
|
||||||
|
import com.simplemobiletools.calendar.models.GoogleEventReminder
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
// more info about event fields at https://developers.google.com/google-apps/calendar/v3/reference/events/insert
|
// 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>>() {
|
class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCredential) : AsyncTask<Void, Void, List<Event>>() {
|
||||||
private val CONFIRMED = "confirmed"
|
private val CONFIRMED = "confirmed"
|
||||||
|
private val PRIMARY = "primary"
|
||||||
|
private val ITEMS = "items"
|
||||||
|
private val OVERRIDES = "overrides"
|
||||||
|
private val POPUP = "popup"
|
||||||
|
|
||||||
private var service: com.google.api.services.calendar.Calendar
|
private var service: com.google.api.services.calendar.Calendar
|
||||||
private var lastError: Exception? = null
|
private var lastError: Exception? = null
|
||||||
@@ -40,15 +46,14 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun getDataFromApi(): List<Event> {
|
private fun getDataFromApi(): List<Event> {
|
||||||
val now = DateTime(System.currentTimeMillis() - getMonthMillis())
|
val minTime = DateTime(System.currentTimeMillis() - getMonthMillis())
|
||||||
val events = service.events().list("primary")
|
val events = service.events().list(PRIMARY)
|
||||||
.setTimeMin(now)
|
.setTimeMin(minTime)
|
||||||
.setOrderBy("startTime")
|
|
||||||
.setSingleEvents(true)
|
.setSingleEvents(true)
|
||||||
.execute()
|
.execute()
|
||||||
|
|
||||||
for (event in events) {
|
for (event in events) {
|
||||||
if (event.key == "items") {
|
if (event.key == ITEMS) {
|
||||||
val parsed = parseEvents(event.value.toString())
|
val parsed = parseEvents(event.value.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,10 +68,24 @@ class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCre
|
|||||||
for (googleEvent in googleEvents) {
|
for (googleEvent in googleEvents) {
|
||||||
if (googleEvent.status != CONFIRMED)
|
if (googleEvent.status != CONFIRMED)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
val reminder = getReminder(googleEvent.reminders)
|
||||||
}
|
}
|
||||||
return events
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getReminder(json: JsonObject): Int {
|
||||||
|
val array = json.getAsJsonArray(OVERRIDES)
|
||||||
|
val token = object : TypeToken<List<GoogleEventReminder>>() {}.type
|
||||||
|
val reminders = Gson().fromJson<ArrayList<GoogleEventReminder>>(array, token) ?: ArrayList<GoogleEventReminder>(2)
|
||||||
|
for ((method, minutes) in reminders) {
|
||||||
|
if (method == POPUP) {
|
||||||
|
return minutes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCancelled() {
|
override fun onCancelled() {
|
||||||
if (lastError != null) {
|
if (lastError != null) {
|
||||||
if (lastError is UserRecoverableAuthIOException) {
|
if (lastError is UserRecoverableAuthIOException) {
|
||||||
|
@@ -1,3 +1,5 @@
|
|||||||
package com.simplemobiletools.calendar.models
|
package com.simplemobiletools.calendar.models
|
||||||
|
|
||||||
data class GoogleEvent(val summary: String, val description: String, val status: String, val start: GoogleEventDateTime, val end: GoogleEventDateTime)
|
import com.google.gson.JsonObject
|
||||||
|
|
||||||
|
data class GoogleEvent(val summary: String, val description: String, val status: String, val start: GoogleEventDateTime, val end: GoogleEventDateTime, val reminders: JsonObject)
|
||||||
|
@@ -0,0 +1,3 @@
|
|||||||
|
package com.simplemobiletools.calendar.models
|
||||||
|
|
||||||
|
data class GoogleEventReminder(val method: String, val minutes: Int)
|
Reference in New Issue
Block a user