From 0ebcba7fb65986ab63d5d60574b88ee9f1a87602 Mon Sep 17 00:00:00 2001 From: yang <742155745@qq.com> Date: Sun, 14 Aug 2022 22:32:54 +0800 Subject: [PATCH] fix: crash with zero page count while loading failed --- .../main/java/com/zjy/pdfview/PdfView.java | 46 ++++++++----------- .../zjy/pdfview/adapter/PdfPageAdapter.java | 4 +- .../java/com/zjy/pdfview/utils/FileUtils.java | 14 +++--- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/pdfview/src/main/java/com/zjy/pdfview/PdfView.java b/pdfview/src/main/java/com/zjy/pdfview/PdfView.java index 994537b..d34c542 100644 --- a/pdfview/src/main/java/com/zjy/pdfview/PdfView.java +++ b/pdfview/src/main/java/com/zjy/pdfview/PdfView.java @@ -1,5 +1,10 @@ package com.zjy.pdfview; +import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; +import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; +import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION; +import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY; + import android.content.Context; import android.content.Intent; import android.content.IntentFilter; @@ -12,15 +17,12 @@ import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; -import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; -import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.constraintlayout.widget.ConstraintSet; import androidx.recyclerview.widget.RecyclerView; import com.zjy.pdfview.adapter.PdfPageAdapter; @@ -41,11 +43,6 @@ import java.util.ArrayList; import java.util.List; -import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; -import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; -import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION; -import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY; - /** * Date: 2021/1/27 * Author: Yang @@ -144,12 +141,9 @@ public void onPageSelected(int position, boolean isBottom) { }); contentRv.setLayoutManager(pageLayoutManager); - loadingLayout.setLoadLayoutListener(new PdfLoadingLayout.LoadLayoutListener() { - @Override - public void clickRetry() { - if (!TextUtils.isEmpty(pdfUrl)) { - loadPdf(pdfUrl); - } + loadingLayout.setLoadLayoutListener(() -> { + if (!TextUtils.isEmpty(pdfUrl)) { + loadPdf(pdfUrl); } }); @@ -159,19 +153,17 @@ public void clickRetry() { getOperateView().addOperateListener(this); - scrollSlider.setScrollSlideListener(new ScrollSlider.ScrollSlideListener() { - @Override - public boolean scrolling(int scrollY) { - int pageItemHeight = contentRv.getHeight() / pageCount; - int scrollIndex = (int) scrollY / pageItemHeight; - if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) { - scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight); - currentIndex = scrollIndex; - pageLayoutManager.scrollToPosition(currentIndex); - getOperateView().setPageIndexText(generatePageIndexText()); - } - return true; + scrollSlider.setScrollSlideListener(scrollY -> { + if (pageCount == 0) return true; + int pageItemHeight = contentRv.getHeight() / pageCount; + int scrollIndex = scrollY / pageItemHeight; + if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) { + scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight); + currentIndex = scrollIndex; + pageLayoutManager.scrollToPosition(currentIndex); + getOperateView().setPageIndexText(generatePageIndexText()); } + return true; }); } @@ -342,6 +334,8 @@ protected Boolean doInBackground(Void... voids) { @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); + scrollSlider.setVisibility(pageCount == 0 ? GONE : VISIBLE); + if (result) { getOperateView().setPageIndexText(generatePageIndexText()); pageAdapter.notifyDataSetChanged(); diff --git a/pdfview/src/main/java/com/zjy/pdfview/adapter/PdfPageAdapter.java b/pdfview/src/main/java/com/zjy/pdfview/adapter/PdfPageAdapter.java index da5a694..424acbb 100644 --- a/pdfview/src/main/java/com/zjy/pdfview/adapter/PdfPageAdapter.java +++ b/pdfview/src/main/java/com/zjy/pdfview/adapter/PdfPageAdapter.java @@ -26,8 +26,8 @@ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class PdfPageAdapter extends RecyclerView.Adapter { - private Context context; - private List pageList; + final private Context context; + final private List pageList; public PdfPageAdapter(Context context, List pageList) { this.context = context; diff --git a/pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java b/pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java index 160c203..6bf3d9d 100644 --- a/pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java +++ b/pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java @@ -18,16 +18,19 @@ public static File writeAssetsToFile(Context context, String assetPath) throws I String fileName = assetPath.substring(assetPath.lastIndexOf("/") + 1); File resultFile = checkCacheSize(context, assetPath); if (!resultFile.exists()) { - resultFile.createNewFile(); + boolean createResult = resultFile.createNewFile(); + if (!createResult) { + return null; + } } InputStream is = context.getAssets().open(fileName); FileOutputStream fos = null; try { byte[] data = new byte[2048]; - int nbread = 0; + int readBuffer; fos = new FileOutputStream(resultFile); - while ((nbread = is.read(data)) > -1) { - fos.write(data, 0, nbread); + while ((readBuffer = is.read(data)) > -1) { + fos.write(data, 0, readBuffer); } } catch (Exception ex) { PdfLog.logError("Exception: " + ex); @@ -75,8 +78,7 @@ private static File checkCacheSize(Context context, String url) { } File[] cacheList = folder.listFiles(); if (cacheList != null && cacheList.length >= 10) { - for (int i = 0; i < cacheList.length; i++) { - File childFile = cacheList[i]; + for (File childFile : cacheList) { childFile.delete(); } }