From 891bae537827c048e4c675dfafeedbd39c3cb643 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 26 Jul 2017 20:15:37 +0200 Subject: [PATCH] recheck google events every 4 hours if the app isnt launched meanwhile --- app/src/main/AndroidManifest.xml | 2 ++ .../calendar/activities/MainActivity.kt | 3 +- .../calendar/activities/SettingsActivity.kt | 25 ++++++++++++-- .../asynctasks/FetchGoogleEventsTask.kt | 33 +++++++------------ .../calendar/extensions/Context.kt | 14 ++++++++ .../calendar/helpers/Config.kt | 6 +++- .../receivers/BootCompletedReceiver.kt | 9 +++-- .../calendar/receivers/GoogleSyncReceiver.kt | 13 ++++++++ 8 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/calendar/receivers/GoogleSyncReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5c90b62ab..32e8f0db0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -135,6 +135,8 @@ + + () { +class FetchGoogleEventsTask(val context: Context, val googleSyncListener: GoogleSyncListener? = null) : AsyncTask() { private val ITEMS = "items" private val OVERRIDES = "overrides" private val NEXT_PAGE_TOKEN = "nextPageToken" - private var lastError: Exception? = null - private var dbHelper = activity.dbHelper + private var dbHelper = context.dbHelper private var eventTypes = ArrayList() private var eventColors = SparseIntArray() - private var service = activity.getGoogleSyncService() + private var service = context.getGoogleSyncService() override fun doInBackground(vararg params: Void): String { + if (!context.isGoogleSyncActive()) + return "" + try { getColors() getDataFromApi() } catch (e: Exception) { - lastError = e cancel(true) } googleSyncListener?.syncCompleted() @@ -53,6 +49,7 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog private fun getDataFromApi() { var currToken = "" while (true) { + service.colors().get().execute() val events = service.events().list(PRIMARY) .setPageToken(currToken) .execute() @@ -78,7 +75,7 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog val remoteImportIds = ArrayList() var updateEvent = false var eventId = 0 - val importIDs = activity.dbHelper.getImportIds() + val importIDs = context.dbHelper.getImportIds() val token = object : TypeToken>() {}.type val googleEvents = Gson().fromJson>(json, token) ?: ArrayList(8) for (googleEvent in googleEvents) { @@ -152,7 +149,7 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog } if (eventTypeId == -1) { - val newColor = if (eventColors[colorId] != 0) eventColors[colorId] else activity.config.primaryColor + val newColor = if (eventColors[colorId] != 0) eventColors[colorId] else context.config.primaryColor val newEventType = EventType(0, eventType, newColor) eventTypeId = dbHelper.insertEventType(newEventType) eventTypes.add(newEventType) @@ -182,12 +179,4 @@ class FetchGoogleEventsTask(val activity: Activity, val googleSyncListener: Goog } return reminderMinutes } - - override fun onCancelled() { - if (lastError != null) { - if (lastError is UserRecoverableAuthIOException) { - activity.startActivityForResult((lastError as UserRecoverableAuthIOException).intent, SettingsActivity.REQUEST_AUTHORIZATION) - } - } - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index a5150f7f8..fc620dc0d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -24,6 +24,7 @@ import com.simplemobiletools.calendar.activities.EventActivity import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.models.Event +import com.simplemobiletools.calendar.receivers.GoogleSyncReceiver import com.simplemobiletools.calendar.receivers.NotificationReceiver import com.simplemobiletools.calendar.services.SnoozeService import com.simplemobiletools.commons.extensions.getContrastColor @@ -245,6 +246,19 @@ fun Context.isOnline(): Boolean { return connectivityManager.activeNetworkInfo != null } +fun Context.scheduleGoogleSync(activate: Boolean) { + val syncIntent = Intent(this, GoogleSyncReceiver::class.java) + val pendingIntent = PendingIntent.getBroadcast(this, 0, syncIntent, PendingIntent.FLAG_CANCEL_CURRENT) + val alarm = getSystemService(Context.ALARM_SERVICE) as AlarmManager + + if (activate) { + val syncCheckInterval = 4 * AlarmManager.INTERVAL_HOUR + alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + syncCheckInterval, syncCheckInterval, pendingIntent) + } else { + alarm.cancel(pendingIntent) + } +} + fun Context.getNewEventTimestampFromCode(dayCode: String) = Formatter.getLocalDateTimeFromCode(dayCode).withTime(13, 0, 0, 0).seconds() fun Context.getCurrentOffset() = SimpleDateFormat("Z", Locale.getDefault()).format(Date()) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt index fcbf38af8..3eed674f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/Config.kt @@ -4,6 +4,7 @@ import android.content.Context import android.media.RingtoneManager import android.text.format.DateFormat import com.simplemobiletools.calendar.R +import com.simplemobiletools.calendar.extensions.scheduleGoogleSync import com.simplemobiletools.commons.helpers.BaseConfig import java.util.* @@ -72,7 +73,10 @@ class Config(context: Context) : BaseConfig(context) { var googleSync: Boolean get() = prefs.getBoolean(GOOGLE_SYNC, false) - set(googleSync) = prefs.edit().putBoolean(GOOGLE_SYNC, googleSync).apply() + set(googleSync) { + context.scheduleGoogleSync(googleSync) + prefs.edit().putBoolean(GOOGLE_SYNC, googleSync).apply() + } var syncAccountName: String get() = prefs.getString(SYNC_ACCOUNT_NAME, "") diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt index 5abf5bed2..18c3dfb82 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/BootCompletedReceiver.kt @@ -3,13 +3,18 @@ package com.simplemobiletools.calendar.receivers import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import com.simplemobiletools.calendar.extensions.isGoogleSyncActive import com.simplemobiletools.calendar.extensions.notifyRunningEvents import com.simplemobiletools.calendar.extensions.scheduleAllEvents +import com.simplemobiletools.calendar.extensions.scheduleGoogleSync class BootCompletedReceiver : BroadcastReceiver() { override fun onReceive(context: Context, arg1: Intent) { - context.scheduleAllEvents() - context.notifyRunningEvents() + context.apply { + scheduleAllEvents() + notifyRunningEvents() + scheduleGoogleSync(isGoogleSyncActive()) + } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/GoogleSyncReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/GoogleSyncReceiver.kt new file mode 100644 index 000000000..d6b20350e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/receivers/GoogleSyncReceiver.kt @@ -0,0 +1,13 @@ +package com.simplemobiletools.calendar.receivers + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import com.simplemobiletools.calendar.asynctasks.FetchGoogleEventsTask + +class GoogleSyncReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context, arg1: Intent) { + FetchGoogleEventsTask(context).execute() + } +}