From e2c65cf8678bd7365609c468da05087f0a94be74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Blot?= Date: Mon, 22 May 2017 13:26:04 +0200 Subject: [PATCH] Code cleanups --- .../engine/AndroidSmsFetcher.java | 56 ++++++++----------- .../engine/OCSMSOwnCloudClient.java | 39 +++++-------- 2 files changed, 37 insertions(+), 58 deletions(-) diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/AndroidSmsFetcher.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/AndroidSmsFetcher.java index 9165640..d8b196d 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/AndroidSmsFetcher.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/AndroidSmsFetcher.java @@ -44,6 +44,26 @@ public class AndroidSmsFetcher { bufferMailboxMessages(result, MailboxID.DRAFTS); } + private void readMailBox(Cursor c, JSONArray result, MailboxID mbID) { + do { + JSONObject entry = new JSONObject(); + + try { + for (int idx = 0; idx < c.getColumnCount(); idx++) { + handleProviderColumn(c, idx, entry); + } + + // Mailbox ID is required by server + entry.put("mbox", mbID.ordinal()); + result.put(entry); + + } catch (JSONException e) { + Log.e(AndroidSmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); + } + } + while (c.moveToNext()); + } + private void bufferMailboxMessages(JSONArray result, MailboxID mbID) { if ((_context == null)) { return; @@ -64,23 +84,7 @@ public class AndroidSmsFetcher { } // Reading mailbox - do { - JSONObject entry = new JSONObject(); - - try { - for (int idx = 0; idx < c.getColumnCount(); idx++) { - handleProviderColumn(c, idx, entry); - } - - // Mailbox ID is required by server - entry.put("mbox", mbID.ordinal()); - result.put(entry); - - } catch (JSONException e) { - Log.e(AndroidSmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); - } - } - while (c.moveToNext()); + readMailBox(c, result, mbID); Log.i(AndroidSmsFetcher.TAG, c.getCount() + " messages read from " + mbID.getURI()); c.close(); @@ -150,22 +154,8 @@ public class AndroidSmsFetcher { return; } - do { - JSONObject entry = new JSONObject(); - - try { - for (int idx = 0; idx < c.getColumnCount(); idx++) { - handleProviderColumn(c, idx, entry); - } - - // Mailbox ID is required by server - entry.put("mbox", mbID.ordinal()); - result.put(entry); - } catch (JSONException e) { - Log.e(AndroidSmsFetcher.TAG, "JSON Exception when reading SMS Mailbox", e); - } - } - while (c.moveToNext()); + // Read Mailbox + readMailBox(c, result, mbID); Log.i(AndroidSmsFetcher.TAG, c.getCount() + " messages read from " + mbID.getURI()); c.close(); diff --git a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java index 3aac627..8e5552d 100644 --- a/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java +++ b/src/main/java/fr/unix_experience/owncloud_sms/engine/OCSMSOwnCloudClient.java @@ -293,7 +293,7 @@ public class OCSMSOwnCloudClient { // Force loop exit tryNb = OCSMSOwnCloudClient.maximumHttpReqTries; - } catch (ConnectException e) { + } catch (ConnectException | HttpException e) { Log.e(OCSMSOwnCloudClient.TAG, "Unable to perform a connection to ownCloud instance", e); // If it's the last try @@ -301,14 +301,6 @@ public class OCSMSOwnCloudClient { req.releaseConnection(); throw new OCSyncException(R.string.err_sync_http_request_connect, OCSyncErrorType.IO); } - } catch (HttpException e) { - Log.e(OCSMSOwnCloudClient.TAG, "Unable to perform a connection to ownCloud instance", e); - - // If it's the last try - if (tryNb == OCSMSOwnCloudClient.maximumHttpReqTries) { - req.releaseConnection(); - throw new OCSyncException(R.string.err_sync_http_request_httpexception, OCSyncErrorType.IO); - } } catch (IOException e) { Log.e(OCSMSOwnCloudClient.TAG, "Unable to perform a connection to ownCloud instance", e); @@ -321,13 +313,7 @@ public class OCSMSOwnCloudClient { } if (status == 200) { - String response; - try { - response = req.getResponseBodyAsString(); - } catch (IOException e) { - Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e); - throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO); - } + String response = getResponseBody(req); // Parse the response try { @@ -350,15 +336,9 @@ public class OCSMSOwnCloudClient { throw new OCSyncException(R.string.err_sync_auth_failed, OCSyncErrorType.AUTH); } else { // Unk error - String response; - try { - response = req.getResponseBodyAsString(); - } catch (IOException e) { - Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e); - throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.PARSE); - } - + String response = getResponseBody(req); Log.e(OCSMSOwnCloudClient.TAG, "Server set unhandled HTTP return code " + status); + if (response != null) { Log.e(OCSMSOwnCloudClient.TAG, "Status code: " + status + ". Response message: " + response); } else { @@ -368,7 +348,16 @@ public class OCSMSOwnCloudClient { } } - private static final int maximumHttpReqTries = 3; + private String getResponseBody(HttpMethod req) throws OCSyncException { + try { + return req.getResponseBodyAsString(); + } catch (IOException e) { + Log.e(OCSMSOwnCloudClient.TAG, "Unable to parse server response", e); + throw new OCSyncException(R.string.err_sync_http_request_resp, OCSyncErrorType.IO); + } + } + + private static final int maximumHttpReqTries = 3; private final OCHttpClient _http; private final Context _context;