From 4ff91983193e2d6d542176204f77abe77d6328ec Mon Sep 17 00:00:00 2001 From: stefan-niedermann Date: Sun, 29 Dec 2019 10:56:45 +0100 Subject: [PATCH] Handle not installed Files app more gracefully --- .../owncloud/notes/model/LoginStatus.java | 3 ++- .../notes/persistence/NoteServerSyncHelper.java | 10 ++++++++-- .../owncloud/notes/persistence/NotesClient.java | 16 ++++++++++------ .../owncloud/notes/util/ServerResponse.java | 4 ++-- app/src/main/res/values/strings.xml | 1 + .../metadata/android/en-US/changelogs/59.txt | 3 ++- 6 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/it/niedermann/owncloud/notes/model/LoginStatus.java b/app/src/main/java/it/niedermann/owncloud/notes/model/LoginStatus.java index 05a91902..51e0a042 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/model/LoginStatus.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/model/LoginStatus.java @@ -7,7 +7,8 @@ import it.niedermann.owncloud.notes.R; public enum LoginStatus { OK(0), NO_NETWORK(R.string.error_no_network), - JSON_FAILED(R.string.error_json); + JSON_FAILED(R.string.error_json), + PROBLEM_WITH_FILES_APP(R.string.error_files_app); @StringRes public final int str; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java index 8db3f942..f8ad118c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NoteServerSyncHelper.java @@ -12,6 +12,7 @@ import android.preference.PreferenceManager; import android.util.Log; import android.widget.Toast; +import com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException; import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; @@ -327,11 +328,14 @@ public class NoteServerSyncHelper { Log.e(TAG, "Exception", e); exceptions.add(e); } catch (NextcloudHttpRequestFailedException e) { - if(e.getStatusCode() == 304) { + if (e.getStatusCode() == 304) { Log.d(TAG, "Server returned HTTP Status Code 304 - Not Modified"); } else { e.printStackTrace(); } + } catch (NextcloudApiNotRespondingException e) { + Log.e(TAG, "Exception", e); + e.printStackTrace(); } } } @@ -384,13 +388,15 @@ public class NoteServerSyncHelper { exceptions.add(e); status = LoginStatus.JSON_FAILED; } catch (NextcloudHttpRequestFailedException e) { - if(e.getStatusCode() == 304) { + if (e.getStatusCode() == 304) { Log.d(TAG, "Server returned HTTP Status Code 304 - Not Modified"); return LoginStatus.OK; } else { e.printStackTrace(); return LoginStatus.JSON_FAILED; } + } catch (NextcloudApiNotRespondingException e) { + return LoginStatus.PROBLEM_WITH_FILES_APP; } return status; } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java index 11d89b09..7e54e8ec 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java @@ -3,11 +3,14 @@ package it.niedermann.owncloud.notes.persistence; import android.content.Context; import android.util.Log; +import androidx.annotation.WorkerThread; + import com.google.gson.GsonBuilder; import com.nextcloud.android.sso.aidl.NextcloudRequest; import com.nextcloud.android.sso.api.AidlNetworkRequest; import com.nextcloud.android.sso.api.NextcloudAPI; import com.nextcloud.android.sso.api.Response; +import com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException; import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException; import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException; import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException; @@ -26,7 +29,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import androidx.annotation.WorkerThread; import it.niedermann.owncloud.notes.model.CloudNote; import it.niedermann.owncloud.notes.util.ServerResponse.NoteResponse; import it.niedermann.owncloud.notes.util.ServerResponse.NotesResponse; @@ -118,13 +120,13 @@ public class NotesClient { } } - NotesResponse getNotes(long lastModified, String lastETag) throws NextcloudHttpRequestFailedException { + NotesResponse getNotes(long lastModified, String lastETag) throws NextcloudHttpRequestFailedException, NextcloudApiNotRespondingException { Map parameter = new HashMap<>(); parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified)); return new NotesResponse(requestServer("notes", METHOD_GET, parameter, null, lastETag)); } - private NoteResponse putNote(CloudNote note, String path, String method) throws JSONException, NextcloudHttpRequestFailedException { + private NoteResponse putNote(CloudNote note, String path, String method) throws JSONException, NextcloudHttpRequestFailedException, NextcloudApiNotRespondingException { JSONObject paramObject = new JSONObject(); paramObject.accumulate(JSON_CONTENT, note.getContent()); paramObject.accumulate(JSON_MODIFIED, note.getModified().getTimeInMillis() / 1000); @@ -141,11 +143,11 @@ public class NotesClient { * @throws JSONException * @throws NextcloudHttpRequestFailedException */ - NoteResponse createNote(CloudNote note) throws JSONException, NextcloudHttpRequestFailedException { + NoteResponse createNote(CloudNote note) throws JSONException, NextcloudHttpRequestFailedException, NextcloudApiNotRespondingException { return putNote(note, "notes", METHOD_POST); } - NoteResponse editNote(CloudNote note) throws JSONException, NextcloudHttpRequestFailedException { + NoteResponse editNote(CloudNote note) throws JSONException, NextcloudHttpRequestFailedException, NextcloudApiNotRespondingException { return putNote(note, "notes/" + note.getRemoteId(), METHOD_PUT); } @@ -167,7 +169,7 @@ public class NotesClient { * @param lastETag optional ETag of last response * @return Body of answer */ - private ResponseData requestServer(String target, String method, Map parameter, JSONObject requestBody, String lastETag) throws NextcloudHttpRequestFailedException { + private ResponseData requestServer(String target, String method, Map parameter, JSONObject requestBody, String lastETag) throws NextcloudHttpRequestFailedException, NextcloudApiNotRespondingException { NextcloudRequest.Builder requestBuilder = new NextcloudRequest.Builder() .setMethod(method) .setUrl(API_PATH + target); @@ -216,6 +218,8 @@ public class NotesClient { } catch (Exception e) { if(e instanceof NextcloudHttpRequestFailedException) { throw (NextcloudHttpRequestFailedException) e; + } else if(e instanceof NextcloudApiNotRespondingException) { + throw (NextcloudApiNotRespondingException) e; } else { e.printStackTrace(); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/util/ServerResponse.java b/app/src/main/java/it/niedermann/owncloud/notes/util/ServerResponse.java index 5c649ee5..286282b7 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/util/ServerResponse.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/util/ServerResponse.java @@ -46,7 +46,7 @@ public class ServerResponse { private final NotesClient.ResponseData response; - public ServerResponse(NotesClient.ResponseData response) { + ServerResponse(NotesClient.ResponseData response) { this.response = response; } @@ -62,7 +62,7 @@ public class ServerResponse { return response.getLastModified(); } - protected CloudNote getNoteFromJSON(JSONObject json) throws JSONException { + CloudNote getNoteFromJSON(JSONObject json) throws JSONException { long id = 0; String title = ""; String content = ""; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index eef983ae..d55097d1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -52,6 +52,7 @@ Synchronization failed: %1$s is the Notes app activated on the server? no network connection + do you have installed the files app? diff --git a/fastlane/metadata/android/en-US/changelogs/59.txt b/fastlane/metadata/android/en-US/changelogs/59.txt index 35be4a58..5e33be02 100644 --- a/fastlane/metadata/android/en-US/changelogs/59.txt +++ b/fastlane/metadata/android/en-US/changelogs/59.txt @@ -1,2 +1,3 @@ - Disable accountchooser after added a new account -- Enable ripple effect on selecting a note \ No newline at end of file +- Enable ripple effect on selecting a note +- Handle not installed Files app more gracefully \ No newline at end of file