From 7b21001af024d7405b669614f9494fd3e49f4075 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 16 May 2015 14:11:28 +0800 Subject: [PATCH] Show readable error string --- .../hippo/ehviewer/widget/ContentLayout.java | 26 ++++---- .../hippo/network/ResponseCodeException.java | 8 +++ .../java/com/hippo/util/ExceptionUtils.java | 66 +++++++++++++++++++ .../main/res/layout/widget_content_layout.xml | 9 +-- app/src/main/res/values-zh-rCN/strings.xml | 9 +++ app/src/main/res/values/strings.xml | 9 +++ 6 files changed, 107 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/hippo/util/ExceptionUtils.java diff --git a/app/src/main/java/com/hippo/ehviewer/widget/ContentLayout.java b/app/src/main/java/com/hippo/ehviewer/widget/ContentLayout.java index bccc7083d..dc0a438de 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/ContentLayout.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/ContentLayout.java @@ -26,14 +26,16 @@ import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; import com.hippo.effect.ViewTransition; import com.hippo.ehviewer.R; import com.hippo.util.AssertUtils; +import com.hippo.util.ExceptionUtils; import com.hippo.util.IntIdGenerator; import com.hippo.util.LayoutManagerUtils; +import com.hippo.util.Log; import com.hippo.util.UiUtils; -import com.hippo.widget.Snackbar; import com.hippo.widget.recyclerview.EasyRecyclerView; import com.hippo.widget.refreshlayout.RefreshLayout; @@ -48,7 +50,6 @@ public class ContentLayout extends FrameLayout { private EasyRecyclerView mRecyclerView; private View mImageView; private TextView mTextView; - private Snackbar mSnackbar; private ContentHelper mContentHelper; @@ -77,7 +78,6 @@ public class ContentLayout extends FrameLayout { mProgressBar = (ProgressBar) getChildAt(0); mTipView = (ViewGroup) getChildAt(1); mRefreshLayout = (RefreshLayout) getChildAt(2); - mSnackbar = (Snackbar) getChildAt(3); mRecyclerView = (EasyRecyclerView) mRefreshLayout.getChildAt(1); mImageView = mTipView.getChildAt(0); mTextView = (TextView) mTipView.getChildAt(1); @@ -98,9 +98,6 @@ public class ContentLayout extends FrameLayout { // mRecyclerViewOriginTop = mRecyclerView.getPaddingTop(); mRecyclerViewOriginBottom = mRecyclerView.getPaddingBottom(); - - // Snackbar - mSnackbarOriginBottom = mSnackbar.getPaddingBottom(); } public EasyRecyclerView getRecyclerView() { @@ -124,8 +121,6 @@ public class ContentLayout extends FrameLayout { mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop(), mRecyclerView.getPaddingRight(), mRecyclerViewOriginBottom + fitPaddingBottom); - // Snackbar - mSnackbar.setPadding(mSnackbar.getPaddingLeft(), mSnackbar.getPaddingTop(), mSnackbar.getPaddingRight(), mSnackbarOriginBottom + fitPaddingBottom); } @Override @@ -163,7 +158,6 @@ public class ContentLayout extends FrameLayout { private EasyRecyclerView mRecyclerView; private View mImageView; private TextView mTextView; - private Snackbar mSnackbar; private RecyclerView.LayoutManager mLayoutManager; private ViewTransition mViewTransition; @@ -275,7 +269,6 @@ public class ContentLayout extends FrameLayout { mProgressBar = contentLayout.mProgressBar; mTipView = contentLayout.mTipView; mRefreshLayout = contentLayout.mRefreshLayout; - mSnackbar = contentLayout.mSnackbar; mRecyclerView = contentLayout.mRecyclerView; mImageView = contentLayout.mImageView; mTextView = contentLayout.mTextView; @@ -291,7 +284,6 @@ public class ContentLayout extends FrameLayout { mRefreshLayout.setOnFooterRefreshListener(this); mTipView.setOnClickListener(this); - mSnackbar.setAction(mContext.getString(R.string.retry), this); } protected abstract RecyclerView.LayoutManager generateLayoutManager(); @@ -441,7 +433,17 @@ public class ContentLayout extends FrameLayout { if (mCurrentTaskId == taskId) { mRefreshLayout.setHeaderRefreshing(false); mRefreshLayout.setFooterRefreshing(false); - showText(e.getClass().getName()); + Log.d("Get page data failed " + e.getClass().getName() + " " + e.getMessage()); + String readableError = ExceptionUtils.getReadableString(mContext, e); + String reason = ExceptionUtils.getReasonString(mContext, e); + if (reason != null) { + readableError += '\n' + reason; + } + if (mViewTransition.getShownViewIndex() == 0) { + Toast.makeText(mContext, readableError, Toast.LENGTH_SHORT).show(); + } else { + showText(readableError); + } } } diff --git a/app/src/main/java/com/hippo/network/ResponseCodeException.java b/app/src/main/java/com/hippo/network/ResponseCodeException.java index 37c581afe..8b48dfe1d 100644 --- a/app/src/main/java/com/hippo/network/ResponseCodeException.java +++ b/app/src/main/java/com/hippo/network/ResponseCodeException.java @@ -65,6 +65,14 @@ public class ResponseCodeException extends Exception { mMessage = message; } + public int getResponseCode() { + return mResponseCode; + } + + public boolean isIdentifiedResponseCode() { + return !DEFAULT_ERROR_MESSAGE.equals(mMessage); + } + @Override public String getMessage() { return mMessage; diff --git a/app/src/main/java/com/hippo/util/ExceptionUtils.java b/app/src/main/java/com/hippo/util/ExceptionUtils.java new file mode 100644 index 000000000..c3595112e --- /dev/null +++ b/app/src/main/java/com/hippo/util/ExceptionUtils.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2015 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.util; + + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.hippo.ehviewer.R; +import com.hippo.network.RedirectionException; +import com.hippo.network.ResponseCodeException; + +import org.apache.http.conn.ConnectTimeoutException; + +import java.net.SocketException; +import java.net.SocketTimeoutException; +import java.net.UnknownHostException; + +public final class ExceptionUtils { + + public static @NonNull String getReadableString(Context context, Exception e) { + if (e instanceof ConnectTimeoutException || + e instanceof SocketTimeoutException) { + return context.getString(R.string.em_timeout); + } else if (e instanceof UnknownHostException) { + return context.getString(R.string.em_unknown_host); + } else if (e instanceof ResponseCodeException) { + ResponseCodeException responseCodeException = (ResponseCodeException) e; + String error = String.format(context.getString(R.string.em_response_code), + responseCodeException.getResponseCode()); + if (responseCodeException.isIdentifiedResponseCode()) { + error += '\n' + responseCodeException.getMessage(); + } + return error; + } else if (e instanceof RedirectionException) { + return context.getString(R.string.em_redirection); + } else if (e instanceof SocketException) { + return context.getString(R.string.em_socket); + } else { + Log.d("Can't recognize this Exception", e); + return context.getString(R.string.em_unknown); + } + } + + public static @Nullable String getReasonString(Context context, Exception e) { + if (e instanceof UnknownHostException) { + return context.getString(R.string.erm_unknown_host); + } else { + return null; + } + } +} diff --git a/app/src/main/res/layout/widget_content_layout.xml b/app/src/main/res/layout/widget_content_layout.xml index 5ed02ad76..b64f6a33f 100644 --- a/app/src/main/res/layout/widget_content_layout.xml +++ b/app/src/main/res/layout/widget_content_layout.xml @@ -43,6 +43,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" + android:gravity="center_horizontal" android:paddingTop="@dimen/keyline_margin" android:paddingBottom="@dimen/keyline_margin"/> @@ -63,12 +64,4 @@ - - diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index c9d8f9939..bb5b454db 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -60,6 +60,15 @@ 请输入数字 当前页面无效 + + 超时 + 找不到主机 + 返回码错误:%d + 太多重定向 + 网络错误 + 奇怪的问题 + 联网了么? + 关键字 搜索 重试 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 94883f073..584c61d22 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -80,6 +80,15 @@ Please enter a number The page is invaild + + Timeout + Unknown host + Unexpected response code: %d + Too many redirections + Network error + Weird + Network available ? + Keyword Search Retry