fetch the next 10 events from the google calendar

This commit is contained in:
tibbi 2017-01-30 19:32:01 +01:00
parent 31112a807e
commit c9f0033650
2 changed files with 71 additions and 9 deletions

View File

@ -19,6 +19,7 @@ import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccoun
import com.google.api.client.util.ExponentialBackOff
import com.google.api.services.calendar.CalendarScopes
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.asynctasks.FetchGoogleEventsTask
import com.simplemobiletools.calendar.extensions.config
import com.simplemobiletools.calendar.extensions.getDefaultReminderTypeIndex
import com.simplemobiletools.calendar.extensions.getDefaultReminderValue
@ -35,6 +36,10 @@ class SettingsActivity : SimpleActivity() {
private val REQUEST_ACCOUNT_NAME = 3
private val REQUEST_GOOGLE_PLAY_SERVICES = 4
companion object {
val REQUEST_AUTHORIZATION = 5
}
lateinit var credential: GoogleAccountCredential
override fun onCreate(savedInstanceState: Bundle?) {
@ -226,7 +231,9 @@ class SettingsActivity : SimpleActivity() {
} else if (requestCode == REQUEST_ACCOUNT_NAME && resultData?.extras != null) {
val accountName = resultData!!.getStringExtra(AccountManager.KEY_ACCOUNT_NAME)
config.syncAccountName = accountName
credential.selectedAccountName = accountName
tryEnablingSync()
} else if (requestCode == REQUEST_AUTHORIZATION) {
tryEnablingSync()
}
}
}
@ -234,8 +241,13 @@ class SettingsActivity : SimpleActivity() {
private fun tryEnablingSync() {
if (!isGooglePlayServicesAvailable()) {
acquireGooglePlayServices()
} else if (!hasGetAccountsPermission()) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.GET_ACCOUNTS), ACCOUNTS_PERMISSION)
} else if (config.syncAccountName.isEmpty()) {
checkGetAccountsPermission()
showAccountChooser()
} else {
credential.selectedAccountName = config.syncAccountName
FetchGoogleEventsTask(this, credential).execute()
}
}
@ -253,13 +265,7 @@ class SettingsActivity : SimpleActivity() {
}
}
private fun checkGetAccountsPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.GET_ACCOUNTS), ACCOUNTS_PERMISSION)
} else {
showAccountChooser()
}
}
private fun hasGetAccountsPermission() = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

View File

@ -0,0 +1,56 @@
package com.simplemobiletools.calendar.asynctasks
import android.app.Activity
import android.os.AsyncTask
import com.google.api.client.extensions.android.http.AndroidHttp
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
import com.google.api.client.json.jackson2.JacksonFactory
import com.google.api.client.util.DateTime
import com.google.api.services.calendar.model.Event
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.activities.SettingsActivity
import java.util.*
class FetchGoogleEventsTask(val activity: Activity, credential: GoogleAccountCredential) : AsyncTask<Void, Void, List<Event>>() {
private var service: com.google.api.services.calendar.Calendar
private var lastError: Exception? = null
init {
val transport = AndroidHttp.newCompatibleTransport()
val jsonFactory = JacksonFactory.getDefaultInstance()
service = com.google.api.services.calendar.Calendar.Builder(transport, jsonFactory, credential)
.setApplicationName(activity.resources.getString(R.string.app_name))
.build()
}
override fun doInBackground(vararg params: Void): List<Event>? {
try {
return getDataFromApi()
} catch (e: Exception) {
lastError = e
cancel(true)
return ArrayList()
}
}
private fun getDataFromApi(): List<Event> {
val now = DateTime(System.currentTimeMillis())
val events = service.events().list("primary")
.setMaxResults(10)
.setTimeMin(now)
.setOrderBy("startTime")
.setSingleEvents(true)
.execute()
return events.items
}
override fun onCancelled() {
if (lastError != null) {
if (lastError is UserRecoverableAuthIOException) {
activity.startActivityForResult((lastError as UserRecoverableAuthIOException).intent, SettingsActivity.REQUEST_AUTHORIZATION)
}
}
}
}