From 5c97bea3c2c8a3c6f59eb9026fb61f2dc39fa867 Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Tue, 22 Jun 2021 19:11:20 -0400 Subject: [PATCH] trial: check downloaded using contentResolver.query https://redd.it/bbejc4 --- .../viewholder/FeedGridItemViewHolder.java | 7 ++- .../awais/instagrabber/utils/DownloadUtils.kt | 51 +++++++++++-------- 2 files changed, 35 insertions(+), 23 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 1a52eadf..0d5d4082 100644 --- a/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java +++ b/app/src/main/java/awais/instagrabber/adapters/viewholder/FeedGridItemViewHolder.java @@ -1,5 +1,6 @@ package awais.instagrabber.adapters.viewholder; +import android.content.Context; import android.content.res.ColorStateList; import android.net.Uri; import android.view.View; @@ -107,8 +108,12 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder { binding.typeIcon.setImageResource(typeIconRes); } binding.downloaded.setVisibility(View.GONE); + final Context context = itemView.getContext(); + if (context == null) { + return; + } AppExecutors.INSTANCE.getTasksThread().execute(() -> { - final List checkList = DownloadUtils.checkDownloaded(media); + final List checkList = DownloadUtils.checkDownloaded(media, context); if (checkList.isEmpty()) { return; } diff --git a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt index a5576629..31157157 100644 --- a/app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt @@ -5,6 +5,7 @@ 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.widget.Toast import androidx.appcompat.app.AlertDialog @@ -25,6 +26,7 @@ import java.io.OutputStreamWriter import java.util.* import java.util.regex.Pattern + object DownloadUtils { private val TAG = DownloadUtils::class.java.simpleName @@ -143,10 +145,8 @@ object DownloadUtils { private fun getSubPathForUserFolder(username: String?): MutableList { val list: MutableList = ArrayList() - if (!Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_USER_FOLDER) || isEmpty( - username - ) - ) { + if (!Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_USER_FOLDER) || + username.isNullOrEmpty()) { list.add(DIR_DOWNLOADS) return list } @@ -280,7 +280,7 @@ object DownloadUtils { } @JvmStatic - fun checkDownloaded(media: Media): List { + fun checkDownloaded(media: Media, context: Context): List { val checkList: MutableList = LinkedList() val user = media.user var username = "username" @@ -295,17 +295,13 @@ object DownloadUtils { media ) else ResponseBodyUtils.getImageUrl(media) val file = getDownloadSavePaths(ArrayList(userFolderPaths), media.code, url, "") - val fileExists = file!!.first != null && checkPathExists( - file.first - ) + val fileExists = file!!.first != null && checkPathExists(file.first, context) var usernameFileExists = false if (!fileExists) { val usernameFile = getDownloadSavePaths( ArrayList(userFolderPaths), media.code, url, username ) - usernameFileExists = usernameFile!!.first != null && checkPathExists( - usernameFile.first - ) + usernameFileExists = usernameFile!!.first != null && checkPathExists(usernameFile.first, context) } checkList.add(fileExists || usernameFileExists) } @@ -321,17 +317,13 @@ object DownloadUtils { val file = getDownloadChildSavePaths( ArrayList(userFolderPaths), media.code, i + 1, url, "" ) - val fileExists = file!!.first != null && checkPathExists( - file.first - ) + val fileExists = file!!.first != null && checkPathExists(file.first, context) var usernameFileExists = false if (!fileExists) { val usernameFile = getDownloadChildSavePaths( ArrayList(userFolderPaths), media.code, i + 1, url, username ) - usernameFileExists = usernameFile!!.first != null && checkPathExists( - usernameFile.first - ) + usernameFileExists = usernameFile!!.first != null && checkPathExists(usernameFile.first, context) } checkList.add(fileExists || usernameFileExists) i++ @@ -343,14 +335,29 @@ object DownloadUtils { return checkList } - private fun checkPathExists(paths: List): Boolean { + private fun checkPathExists(paths: List, context: Context): Boolean { if (root == null) return false - var dir = root + val uri = root!!.getUri() + var found = false + var docId = DocumentsContract.getTreeDocumentId(uri) for (path in paths) { - dir = dir!!.findFile(path) - if (dir == null || !dir.exists()) { - return false + val docUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, docId) + val docCursor = context.contentResolver.query( + docUri, arrayOf( + DocumentsContract.Document.COLUMN_DISPLAY_NAME, + DocumentsContract.Document.COLUMN_DOCUMENT_ID + ), null, null, null + ) + if (docCursor == null) return false + while (docCursor!!.moveToNext() && !found) { + if (path.equals(docCursor.getString(0))) { + docId = docCursor.getString(1) + found = true + } } + docCursor.close() + if (!found) return false + found = false } return true }