From f5bb189bf5958af0d7f1db109a48e8342f5f8847 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 31 May 2017 16:33:36 +0300 Subject: [PATCH] move some apirequest shared stuff to apicommon --- .../main/java/org/fox/ttrss/ApiCommon.java | 52 +++++++++++++++++-- .../main/java/org/fox/ttrss/ApiLoader.java | 42 +-------------- .../main/java/org/fox/ttrss/ApiRequest.java | 42 +-------------- 3 files changed, 50 insertions(+), 86 deletions(-) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiCommon.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiCommon.java index e2cc4174..b7308000 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiCommon.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiCommon.java @@ -1,6 +1,7 @@ package org.fox.ttrss; import android.os.Build; +import android.util.Log; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; @@ -12,15 +13,58 @@ import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; -/** - * Created by andrew on 31.05.2017. - */ - public class ApiCommon { + public static final String TAG = "ApiCommon"; + public enum ApiError { NO_ERROR, HTTP_UNAUTHORIZED, HTTP_FORBIDDEN, HTTP_NOT_FOUND, HTTP_SERVER_ERROR, HTTP_OTHER_ERROR, SSL_REJECTED, SSL_HOSTNAME_REJECTED, PARSE_ERROR, IO_ERROR, OTHER_ERROR, API_DISABLED, API_UNKNOWN, LOGIN_FAILED, INVALID_URL, API_INCORRECT_USAGE, NETWORK_UNAVAILABLE, API_UNKNOWN_METHOD } + public static int getErrorMessage(ApiError error) { + switch (error) { + case NO_ERROR: + return R.string.error_unknown; + case HTTP_UNAUTHORIZED: + return R.string.error_http_unauthorized; + case HTTP_FORBIDDEN: + return R.string.error_http_forbidden; + case HTTP_NOT_FOUND: + return R.string.error_http_not_found; + case HTTP_SERVER_ERROR: + return R.string.error_http_server_error; + case HTTP_OTHER_ERROR: + return R.string.error_http_other_error; + case SSL_REJECTED: + return R.string.error_ssl_rejected; + case SSL_HOSTNAME_REJECTED: + return R.string.error_ssl_hostname_rejected; + case PARSE_ERROR: + return R.string.error_parse_error; + case IO_ERROR: + return R.string.error_io_error; + case OTHER_ERROR: + return R.string.error_other_error; + case API_DISABLED: + return R.string.error_api_disabled; + case API_UNKNOWN: + return R.string.error_api_unknown; + case API_UNKNOWN_METHOD: + return R.string.error_api_unknown_method; + case LOGIN_FAILED: + return R.string.error_login_failed; + case INVALID_URL: + return R.string.error_invalid_api_url; + case API_INCORRECT_USAGE: + return R.string.error_api_incorrect_usage; + case NETWORK_UNAVAILABLE: + return R.string.error_network_unavailable; + default: + Log.d(TAG, "getErrorMessage: unknown error code=" + error); + return R.string.error_unknown; + } + + } + public static void trustAllHosts(boolean trustAnyCert, boolean trustAnyHost) { try { if (trustAnyCert) { diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiLoader.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiLoader.java index d483129b..414a8a9d 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiLoader.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiLoader.java @@ -75,47 +75,7 @@ public class ApiLoader extends AsyncTaskLoader { } public int getErrorMessage() { - switch (m_lastError) { - case NO_ERROR: - return R.string.error_unknown; - case HTTP_UNAUTHORIZED: - return R.string.error_http_unauthorized; - case HTTP_FORBIDDEN: - return R.string.error_http_forbidden; - case HTTP_NOT_FOUND: - return R.string.error_http_not_found; - case HTTP_SERVER_ERROR: - return R.string.error_http_server_error; - case HTTP_OTHER_ERROR: - return R.string.error_http_other_error; - case SSL_REJECTED: - return R.string.error_ssl_rejected; - case SSL_HOSTNAME_REJECTED: - return R.string.error_ssl_hostname_rejected; - case PARSE_ERROR: - return R.string.error_parse_error; - case IO_ERROR: - return R.string.error_io_error; - case OTHER_ERROR: - return R.string.error_other_error; - case API_DISABLED: - return R.string.error_api_disabled; - case API_UNKNOWN: - return R.string.error_api_unknown; - case API_UNKNOWN_METHOD: - return R.string.error_api_unknown_method; - case LOGIN_FAILED: - return R.string.error_login_failed; - case INVALID_URL: - return R.string.error_invalid_api_url; - case API_INCORRECT_USAGE: - return R.string.error_api_incorrect_usage; - case NETWORK_UNAVAILABLE: - return R.string.error_network_unavailable; - default: - Log.d(TAG, "getErrorMessage: unknown error code=" + m_lastError); - return R.string.error_unknown; - } + return ApiCommon.getErrorMessage(m_lastError); } public ApiError getLastError() { diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiRequest.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiRequest.java index b001e012..ab52a0c3 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiRequest.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/ApiRequest.java @@ -65,47 +65,7 @@ public class ApiRequest extends AsyncTask, Integer, JsonE } public int getErrorMessage() { - switch (m_lastError) { - case NO_ERROR: - return R.string.error_unknown; - case HTTP_UNAUTHORIZED: - return R.string.error_http_unauthorized; - case HTTP_FORBIDDEN: - return R.string.error_http_forbidden; - case HTTP_NOT_FOUND: - return R.string.error_http_not_found; - case HTTP_SERVER_ERROR: - return R.string.error_http_server_error; - case HTTP_OTHER_ERROR: - return R.string.error_http_other_error; - case SSL_REJECTED: - return R.string.error_ssl_rejected; - case SSL_HOSTNAME_REJECTED: - return R.string.error_ssl_hostname_rejected; - case PARSE_ERROR: - return R.string.error_parse_error; - case IO_ERROR: - return R.string.error_io_error; - case OTHER_ERROR: - return R.string.error_other_error; - case API_DISABLED: - return R.string.error_api_disabled; - case API_UNKNOWN: - return R.string.error_api_unknown; - case API_UNKNOWN_METHOD: - return R.string.error_api_unknown_method; - case LOGIN_FAILED: - return R.string.error_login_failed; - case INVALID_URL: - return R.string.error_invalid_api_url; - case API_INCORRECT_USAGE: - return R.string.error_api_incorrect_usage; - case NETWORK_UNAVAILABLE: - return R.string.error_network_unavailable; - default: - Log.d(TAG, "getErrorMessage: unknown error code=" + m_lastError); - return R.string.error_unknown; - } + return ApiCommon.getErrorMessage(m_lastError); } @Override