From 5dbca6e00f49e054697652757e0f3056841140ad Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 11 Jan 2019 15:40:06 +0000 Subject: [PATCH] Add support for associate accounts. --- .../syncadapter/journalmanager/BaseManager.kt | 8 ++++++-- .../syncadapter/journalmanager/Exceptions.kt | 2 ++ .../syncadapter/SyncAdapterService.kt | 18 +++++++++++------- .../syncadapter/ui/CreateCollectionFragment.kt | 2 ++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt index 86aab047..9ba89835 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.kt @@ -29,8 +29,12 @@ abstract class BaseManager { when (response.code()) { HttpURLConnection.HTTP_UNAVAILABLE -> throw Exceptions.ServiceUnavailableException(response, "Service unavailable") HttpURLConnection.HTTP_UNAUTHORIZED -> throw Exceptions.UnauthorizedException(response, "Unauthorized auth token") - HttpURLConnection.HTTP_FORBIDDEN -> if (apiError.code == "service_inactive") { - throw Exceptions.UserInactiveException(response, apiError.detail) + HttpURLConnection.HTTP_FORBIDDEN -> { + if (apiError.code == "service_inactive") { + throw Exceptions.UserInactiveException(response, apiError.detail) + } else if (apiError.code == "associate_not_allowed") { + throw Exceptions.AssociateNotAllowedException(response, apiError.detail) + } } }// Fall through. We want to always throw when unsuccessful. diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.kt b/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.kt index 8a8faa7d..b45a9077 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.kt +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.kt @@ -8,6 +8,8 @@ import java.io.Serializable import java.security.GeneralSecurityException class Exceptions { + class AssociateNotAllowedException(response: Response, message: String?) : HttpException(response, message) + class UnauthorizedException(response: Response, message: String?) : HttpException(response, message) class UserInactiveException(response: Response, message: String?) : HttpException(response, message) diff --git a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt index 54036725..a85af633 100644 --- a/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt +++ b/app/src/main/java/com/etesync/syncadapter/syncadapter/SyncAdapterService.kt @@ -134,13 +134,17 @@ abstract class SyncAdapterService : Service() { } if (journals.isEmpty()) { - val info = CollectionInfo.defaultForServiceType(serviceType) - val uid = JournalManager.Journal.genUid() - info.uid = uid - val crypto = Crypto.CryptoManager(info.version, settings.password(), uid) - val journal = JournalManager.Journal(crypto, info.toJson(), uid) - journalsManager.create(journal) - journals.add(Pair(journal, info)) + try { + val info = CollectionInfo.defaultForServiceType(serviceType) + val uid = JournalManager.Journal.genUid() + info.uid = uid + val crypto = Crypto.CryptoManager(info.version, settings.password(), uid) + val journal = JournalManager.Journal(crypto, info.toJson(), uid) + journalsManager.create(journal) + journals.add(Pair(journal, info)) + } catch (e: Exceptions.AssociateNotAllowedException) { + // Skip for now + } } saveCollections(journals) diff --git a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt index 8da65548..ffe34042 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt +++ b/app/src/main/java/com/etesync/syncadapter/ui/CreateCollectionFragment.kt @@ -131,6 +131,8 @@ class CreateCollectionFragment : DialogFragment(), LoaderManager.LoaderCallbacks return e } catch (e: Exceptions.GenericCryptoException) { return e + } catch (e: Exceptions.AssociateNotAllowedException) { + return e } return null