diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt index 6a14d467c..96ddb729a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/SettingsActivity.kt @@ -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, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/SettingsActivity.kt new file mode 100644 index 000000000..54b320635 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/asynctasks/SettingsActivity.kt @@ -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>() { + 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? { + try { + return getDataFromApi() + } catch (e: Exception) { + lastError = e + cancel(true) + return ArrayList() + } + } + + private fun getDataFromApi(): List { + 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) + } + } + } +}