From 8030c7f22066fbdd118a6cf86aa5d71a05bb514f Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 5 Apr 2021 21:01:33 +0900 Subject: [PATCH] Fix downloaded check logic --- .../viewholder/FeedGridItemViewHolder.java | 2 +- .../asyncs/DownloadedCheckerAsyncTask.java | 11 +----- .../instagrabber/utils/DownloadUtils.java | 37 +++++++++++-------- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java b/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java index 3f71c624..c6e1d571 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java @@ -102,7 +102,7 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder { binding.typeIcon.setVisibility(View.VISIBLE); binding.typeIcon.setImageResource(typeIconRes); } - final DownloadedCheckerAsyncTask task = new DownloadedCheckerAsyncTask(itemView.getContext(), result -> { + final DownloadedCheckerAsyncTask task = new DownloadedCheckerAsyncTask(result -> { final List checkList = result.get(media.getPk()); if (checkList == null || checkList.isEmpty()) { return; diff --git a/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java b/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java index fd4ad6df..605bed3f 100644 --- a/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java +++ b/app/src/main/java/awais/instagrabber/asyncs/DownloadedCheckerAsyncTask.java @@ -1,9 +1,7 @@ package awais.instagrabber.asyncs; -import android.content.Context; import android.os.AsyncTask; -import java.lang.ref.WeakReference; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -14,12 +12,9 @@ import awais.instagrabber.utils.DownloadUtils; public final class DownloadedCheckerAsyncTask extends AsyncTask>> { private static final String TAG = "DownloadedCheckerAsyncTask"; - private final WeakReference context; private final OnCheckResultListener listener; - public DownloadedCheckerAsyncTask(final Context context, - final OnCheckResultListener listener) { - this.context = new WeakReference<>(context); + public DownloadedCheckerAsyncTask(final OnCheckResultListener listener) { this.listener = listener; } @@ -30,9 +25,7 @@ public final class DownloadedCheckerAsyncTask extends AsyncTask> map = new HashMap<>(); for (final Media media : feedModels) { - final Context context = this.context.get(); - if (context == null) return map; - map.put(media.getPk(), DownloadUtils.checkDownloaded(context, media)); + map.put(media.getPk(), DownloadUtils.checkDownloaded(media)); } return map; } diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java index 88be98db..e9776e4f 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.java @@ -5,7 +5,6 @@ import android.content.Context; import android.content.DialogInterface; import android.content.UriPermission; import android.net.Uri; -import android.provider.DocumentsContract; import android.util.Log; import android.webkit.MimeTypeMap; import android.widget.Toast; @@ -338,8 +337,7 @@ public final class DownloadUtils { return ""; } - public static List checkDownloaded(@NonNull final Context context, - @NonNull final Media media) { + public static List checkDownloaded(@NonNull final Media media) { final List checkList = new LinkedList<>(); final User user = media.getUser(); String username = "username"; @@ -351,9 +349,10 @@ public final class DownloadUtils { case MEDIA_TYPE_IMAGE: case MEDIA_TYPE_VIDEO: { final String url = ResponseBodyUtils.getImageUrl(media); - final Pair, String> file = getDownloadSavePaths(userFolderPaths, media.getCode(), url, ""); - final Pair, String> usernameFile = getDownloadSavePaths(userFolderPaths, media.getCode(), url, username); - checkList.add(checkPathExists(context, file.first) || checkPathExists(context, usernameFile.first)); + final Pair, String> file = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, ""); + final Pair, String> usernameFile = getDownloadSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), url, + username); + checkList.add(checkPathExists(file.first) || checkPathExists(usernameFile.first)); break; } case MEDIA_TYPE_SLIDER: @@ -362,9 +361,11 @@ public final class DownloadUtils { final Media child = sliderItems.get(i); if (child == null) continue; final String url = ResponseBodyUtils.getImageUrl(child); - final Pair, String> file = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, ""); - final Pair, String> usernameFile = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, username); - checkList.add(checkPathExists(context, file.first) || checkPathExists(context, usernameFile.first)); + final Pair, String> file = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), i + 1, url, + ""); + final Pair, String> usernameFile = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), + i + 1, url, username); + checkList.add(checkPathExists(file.first) || checkPathExists(usernameFile.first)); } break; default: @@ -372,13 +373,16 @@ public final class DownloadUtils { return checkList; } - private static boolean checkPathExists(@NonNull final Context context, - @NonNull final List paths) { + private static boolean checkPathExists(@NonNull final List paths) { if (root == null) return false; - final String joined = android.text.TextUtils.join("/", paths); - final Uri userFolderUri = DocumentsContract.buildDocumentUriUsingTree(root.getUri(), joined); - final DocumentFile userFolder = DocumentFile.fromSingleUri(context, userFolderUri); - return userFolder != null && userFolder.exists(); + DocumentFile dir = root; + for (final String path : paths) { + dir = dir.findFile(path); + if (dir == null || !dir.exists()) { + return false; + } + } + return true; } public static void showDownloadDialog(@NonNull Context context, @@ -501,7 +505,8 @@ public final class DownloadUtils { final String usernamePrepend = Utils.settingsHelper.getBoolean(Constants.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null ? mediaUser.getUsername() : ""; - final Pair, String> pair = getDownloadChildSavePaths(userFolderPaths, media.getCode(), i + 1, url, + final Pair, String> pair = getDownloadChildSavePaths(new ArrayList<>(userFolderPaths), media.getCode(), i + 1, + url, usernamePrepend); final DocumentFile file = createFile(pair); if (file == null) continue;