From ba789da797cd8abe1df86049c0a8d0e63419f47c Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Tue, 28 Feb 2017 17:37:00 +0000 Subject: [PATCH] Add a UserInactive exception for when the user is no longer active This is a possible error message we get when we get error code 403 (permission denied) from the server. We then handle it explicitly by sending the user to the dashboard. --- .../etesync/syncadapter/NotificationHelper.java | 6 ++++++ .../syncadapter/journalmanager/BaseManager.java | 17 ++++++++++++++++- .../syncadapter/journalmanager/Exceptions.java | 7 +++++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/etesync/syncadapter/NotificationHelper.java b/app/src/main/java/com/etesync/syncadapter/NotificationHelper.java index 9d0872d7..bbc08c8c 100644 --- a/app/src/main/java/com/etesync/syncadapter/NotificationHelper.java +++ b/app/src/main/java/com/etesync/syncadapter/NotificationHelper.java @@ -40,6 +40,9 @@ public class NotificationHelper { if (e instanceof Exceptions.UnauthorizedException) { App.log.log(Level.SEVERE, "Not authorized anymore", e); messageString = R.string.sync_error_unauthorized; + } else if (e instanceof Exceptions.UserInactiveException) { + App.log.log(Level.SEVERE, "User inactive"); + messageString = R.string.sync_error_user_inactive; } else if (e instanceof Exceptions.ServiceUnavailableException) { App.log.log(Level.SEVERE, "Service unavailable"); messageString = R.string.sync_error_unavailable; @@ -91,6 +94,9 @@ public class NotificationHelper { Intent detailsIntent; if (e instanceof Exceptions.UnauthorizedException) { detailsIntent = new Intent(this, AccountSettingsActivity.class); + } else if (e instanceof Exceptions.UserInactiveException) { + startActivity(new Intent(Intent.ACTION_VIEW, Constants.dashboard)); + return; } else { detailsIntent = new Intent(this, DebugInfoActivity.class); } diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.java b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.java index 85269db5..f8ef3f28 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.java +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/BaseManager.java @@ -9,6 +9,7 @@ import java.util.logging.Level; import com.etesync.syncadapter.App; import com.etesync.syncadapter.GsonHelper; + import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; @@ -41,14 +42,28 @@ abstract class BaseManager { throw new Exceptions.ServiceUnavailableException("Service unavailable", Long.valueOf(response.header("Retry-After", "0"))); case HttpURLConnection.HTTP_UNAUTHORIZED: throw new Exceptions.UnauthorizedException("Failed to connect"); + case HttpURLConnection.HTTP_FORBIDDEN: + ApiError apiError = GsonHelper.gson.fromJson(response.body().charStream(), ApiError.class); + if (apiError.code.equals("service_inactive")) { + throw new Exceptions.UserInactiveException(apiError.detail); + } default: - throw new Exceptions.HttpException(response); + // Fall through. We want to always throw when unsuccessful. } + throw new Exceptions.HttpException(response); } return response; } + static class ApiError { + String detail; + String code; + + ApiError() { + } + } + static class Base { @Setter(AccessLevel.PACKAGE) @Getter(AccessLevel.PACKAGE) diff --git a/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.java b/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.java index 9a14d563..968df9c7 100644 --- a/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.java +++ b/app/src/main/java/com/etesync/syncadapter/journalmanager/Exceptions.java @@ -2,6 +2,7 @@ package com.etesync.syncadapter.journalmanager; import java.io.IOException; import java.io.Serializable; +import java.net.HttpURLConnection; import at.bitfire.cert4android.Constants; import okhttp3.Headers; @@ -17,6 +18,12 @@ public class Exceptions { } } + public static class UserInactiveException extends HttpException { + public UserInactiveException(String message) { + super(HttpURLConnection.HTTP_FORBIDDEN, message); + } + } + public static class ServiceUnavailableException extends HttpException { public long retryAfter; public ServiceUnavailableException(String message) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8694b848..e0149513 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -281,6 +281,7 @@ pushing entries post processing Authentication failed + User is inactive EteSync: Connection security