diff --git a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsListFragment.java index 1b60fc00be99..6adc08963c3e 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsListFragment.java @@ -34,6 +34,8 @@ import org.wordpress.android.util.ptr.PullToRefreshHelper; import org.wordpress.android.util.ptr.PullToRefreshHelper.RefreshListener; import org.xmlrpc.android.ApiHelper; +import org.xmlrpc.android.ApiHelper.ErrorType; +import org.xmlrpc.android.XMLRPCFault; import java.util.HashMap; import java.util.Map; @@ -406,12 +408,12 @@ void updateComments(boolean loadMore) { * task to retrieve latest comments from server */ private class UpdateCommentsTask extends AsyncTask { - boolean isError; - final boolean isLoadingMore; + ErrorType mErrorType = ErrorType.NO_ERROR; + final boolean mIsLoadingMore; boolean mRetryOnCancelled; private UpdateCommentsTask(boolean loadMore) { - isLoadingMore = loadMore; + mIsLoadingMore = loadMore; } public void setRetryOnCancelled(boolean retryOnCancelled) { @@ -422,7 +424,7 @@ public void setRetryOnCancelled(boolean retryOnCancelled) { protected void onPreExecute() { super.onPreExecute(); mIsUpdatingComments = true; - if (isLoadingMore) { + if (mIsLoadingMore) { showLoadingProgress(); } } @@ -447,19 +449,19 @@ protected CommentList doInBackground(Void... args) { Blog blog = WordPress.getCurrentBlog(); if (blog == null) { - isError = true; + mErrorType = ErrorType.INVALID_CURRENT_BLOG; return null; } // the first time this is called, make sure comments deleted on server are removed // from the local database - if (!mHasCheckedDeletedComments && !isLoadingMore) { + if (!mHasCheckedDeletedComments && !mIsLoadingMore) { mHasCheckedDeletedComments = true; ApiHelper.removeDeletedComments(blog); } Map hPost = new HashMap(); - if (isLoadingMore) { + if (mIsLoadingMore) { int numExisting = getCommentAdapter().getCount(); hPost.put("offset", numExisting); hPost.put("number", COMMENTS_PER_PAGE); @@ -473,10 +475,15 @@ protected CommentList doInBackground(Void... args) { hPost }; try { return ApiHelper.refreshComments(getActivity(), blog, params); + } catch (XMLRPCFault xmlrpcFault) { + mErrorType = ErrorType.UNKNOWN_ERROR; + if (xmlrpcFault.getFaultCode() == 401) { + mErrorType = ErrorType.UNAUTHORIZED; + } } catch (Exception e) { - isError = true; - return null; + mErrorType = ErrorType.UNKNOWN_ERROR; } + return null; } protected void onPostExecute(CommentList comments) { @@ -485,7 +492,7 @@ protected void onPostExecute(CommentList comments) { if (!isAdded()) { return; } - if (isLoadingMore) { + if (mIsLoadingMore) { hideLoadingProgress(); } mPullToRefreshHelper.setRefreshing(false); @@ -496,13 +503,16 @@ protected void onPostExecute(CommentList comments) { mCanLoadMoreComments = (comments != null && comments.size() > 0); // result will be null on error OR if no more comments exists - if (comments == null) { - if (isError && !getActivity().isFinishing()) { - ToastUtils.showToast(getActivity(), getString(R.string.error_refresh_comments)); + if (comments == null && !getActivity().isFinishing() && mErrorType != ErrorType.NO_ERROR) { + switch (mErrorType) { + case UNAUTHORIZED: + ToastUtils.showToast(getActivity(), getString(R.string.error_refresh_unauthorized_comments)); + return; + default: + ToastUtils.showToast(getActivity(), getString(R.string.error_refresh_comments)); + return; } - return; } - if (comments.size() > 0) { getCommentAdapter().loadComments(); } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java index 523b17ed1c1f..58fc94f237d3 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListFragment.java @@ -323,9 +323,19 @@ public void onFailure(ApiHelper.ErrorType errorType, String errorMessage, Throwa if (mProgressFooterView != null) { mProgressFooterView.setVisibility(View.GONE); } - if (errorType != ErrorType.TASK_CANCELLED) { - ToastUtils.showToast(getActivity(), - mIsPage ? R.string.error_refresh_pages : R.string.error_refresh_posts, Duration.LONG); + if (errorType != ErrorType.TASK_CANCELLED && errorType != ErrorType.NO_ERROR) { + switch (errorType) { + case UNAUTHORIZED: + ToastUtils.showToast(getActivity(), + mIsPage ? R.string.error_refresh_unauthorized_pages : R.string.error_refresh_unauthorized_posts, + Duration.LONG); + return; + default: + ToastUtils.showToast(getActivity(), + mIsPage ? R.string.error_refresh_pages : R.string.error_refresh_posts, + Duration.LONG); + return; + } } } }); diff --git a/WordPress/src/main/java/org/xmlrpc/android/ApiHelper.java b/WordPress/src/main/java/org/xmlrpc/android/ApiHelper.java index be51a20b63b0..7ee6d4dbe87c 100644 --- a/WordPress/src/main/java/org/xmlrpc/android/ApiHelper.java +++ b/WordPress/src/main/java/org/xmlrpc/android/ApiHelper.java @@ -50,8 +50,9 @@ public class ApiHelper { public enum ErrorType { - NO_ERROR, INVALID_CURRENT_BLOG, NETWORK_XMLRPC, INVALID_CONTEXT, - INVALID_RESULT, NO_UPLOAD_FILES_CAP, CAST_EXCEPTION, TASK_CANCELLED} + NO_ERROR, UNKNOWN_ERROR, INVALID_CURRENT_BLOG, NETWORK_XMLRPC, INVALID_CONTEXT, + INVALID_RESULT, NO_UPLOAD_FILES_CAP, CAST_EXCEPTION, TASK_CANCELLED, UNAUTHORIZED + } public static final Map blogOptionsXMLRPCParameters = new HashMap(); @@ -470,11 +471,20 @@ protected Boolean doInBackground(List... params) { WordPress.wpDB.savePosts(postsList, blog.getLocalTableBlogId(), isPage, !loadMore); } return true; + } catch (XMLRPCFault e) { + mErrorType = ErrorType.NETWORK_XMLRPC; + if (e.getFaultCode() == 401) { + mErrorType = ErrorType.UNAUTHORIZED; + } + mErrorMessage = e.getMessage(); } catch (XMLRPCException e) { + mErrorType = ErrorType.NETWORK_XMLRPC; mErrorMessage = e.getMessage(); } catch (IOException e) { + mErrorType = ErrorType.INVALID_RESULT; mErrorMessage = e.getMessage(); } catch (XmlPullParserException e) { + mErrorType = ErrorType.INVALID_RESULT; mErrorMessage = e.getMessage(); } diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index e5e8e1b98485..9d2f93c16ef5 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -525,13 +525,17 @@ An error occurred while deleting the %s - + Posts couldn\'t be refreshed at this time Pages couldn\'t be refreshed at this time Notifications couldn\'t be refreshed at this time Comments couldn\'t be refreshed at this time Stats couldn\'t be refreshed at this time Something went wrong while refreshing the media library. Try again later. + + You don\'t have permission to view or edit comments + You don\'t have permission to view or edit pages + You don\'t have permission to view or edit posts An error occurred An error occurred while fetching data An error occurred while moderating