trial: check downloaded using contentResolver.query

https://redd.it/bbejc4
This commit is contained in:
Austin Huang 2021-06-22 19:11:20 -04:00
parent 3dcc791cba
commit 5c97bea3c2
No known key found for this signature in database
GPG Key ID: 84C23AA04587A91F
2 changed files with 35 additions and 23 deletions

View File

@ -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<Boolean> checkList = DownloadUtils.checkDownloaded(media);
final List<Boolean> checkList = DownloadUtils.checkDownloaded(media, context);
if (checkList.isEmpty()) {
return;
}

View File

@ -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<String> {
val list: MutableList<String> = 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<Boolean> {
fun checkDownloaded(media: Media, context: Context): List<Boolean> {
val checkList: MutableList<Boolean> = 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<String>): Boolean {
private fun checkPathExists(paths: List<String>, 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
}