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; package awais.instagrabber.adapters.viewholder;
import android.content.Context;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.net.Uri; import android.net.Uri;
import android.view.View; import android.view.View;
@ -107,8 +108,12 @@ public class FeedGridItemViewHolder extends RecyclerView.ViewHolder {
binding.typeIcon.setImageResource(typeIconRes); binding.typeIcon.setImageResource(typeIconRes);
} }
binding.downloaded.setVisibility(View.GONE); binding.downloaded.setVisibility(View.GONE);
final Context context = itemView.getContext();
if (context == null) {
return;
}
AppExecutors.INSTANCE.getTasksThread().execute(() -> { AppExecutors.INSTANCE.getTasksThread().execute(() -> {
final List<Boolean> checkList = DownloadUtils.checkDownloaded(media); final List<Boolean> checkList = DownloadUtils.checkDownloaded(media, context);
if (checkList.isEmpty()) { if (checkList.isEmpty()) {
return; return;
} }

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.content.DialogInterface import android.content.DialogInterface
import android.content.UriPermission import android.content.UriPermission
import android.net.Uri import android.net.Uri
import android.provider.DocumentsContract
import android.util.Log import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
@ -25,6 +26,7 @@ import java.io.OutputStreamWriter
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
object DownloadUtils { object DownloadUtils {
private val TAG = DownloadUtils::class.java.simpleName private val TAG = DownloadUtils::class.java.simpleName
@ -143,10 +145,8 @@ object DownloadUtils {
private fun getSubPathForUserFolder(username: String?): MutableList<String> { private fun getSubPathForUserFolder(username: String?): MutableList<String> {
val list: MutableList<String> = ArrayList() val list: MutableList<String> = ArrayList()
if (!Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_USER_FOLDER) || isEmpty( if (!Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_USER_FOLDER) ||
username username.isNullOrEmpty()) {
)
) {
list.add(DIR_DOWNLOADS) list.add(DIR_DOWNLOADS)
return list return list
} }
@ -280,7 +280,7 @@ object DownloadUtils {
} }
@JvmStatic @JvmStatic
fun checkDownloaded(media: Media): List<Boolean> { fun checkDownloaded(media: Media, context: Context): List<Boolean> {
val checkList: MutableList<Boolean> = LinkedList() val checkList: MutableList<Boolean> = LinkedList()
val user = media.user val user = media.user
var username = "username" var username = "username"
@ -295,17 +295,13 @@ object DownloadUtils {
media media
) else ResponseBodyUtils.getImageUrl(media) ) else ResponseBodyUtils.getImageUrl(media)
val file = getDownloadSavePaths(ArrayList(userFolderPaths), media.code, url, "") val file = getDownloadSavePaths(ArrayList(userFolderPaths), media.code, url, "")
val fileExists = file!!.first != null && checkPathExists( val fileExists = file!!.first != null && checkPathExists(file.first, context)
file.first
)
var usernameFileExists = false var usernameFileExists = false
if (!fileExists) { if (!fileExists) {
val usernameFile = getDownloadSavePaths( val usernameFile = getDownloadSavePaths(
ArrayList(userFolderPaths), media.code, url, username ArrayList(userFolderPaths), media.code, url, username
) )
usernameFileExists = usernameFile!!.first != null && checkPathExists( usernameFileExists = usernameFile!!.first != null && checkPathExists(usernameFile.first, context)
usernameFile.first
)
} }
checkList.add(fileExists || usernameFileExists) checkList.add(fileExists || usernameFileExists)
} }
@ -321,17 +317,13 @@ object DownloadUtils {
val file = getDownloadChildSavePaths( val file = getDownloadChildSavePaths(
ArrayList(userFolderPaths), media.code, i + 1, url, "" ArrayList(userFolderPaths), media.code, i + 1, url, ""
) )
val fileExists = file!!.first != null && checkPathExists( val fileExists = file!!.first != null && checkPathExists(file.first, context)
file.first
)
var usernameFileExists = false var usernameFileExists = false
if (!fileExists) { if (!fileExists) {
val usernameFile = getDownloadChildSavePaths( val usernameFile = getDownloadChildSavePaths(
ArrayList(userFolderPaths), media.code, i + 1, url, username ArrayList(userFolderPaths), media.code, i + 1, url, username
) )
usernameFileExists = usernameFile!!.first != null && checkPathExists( usernameFileExists = usernameFile!!.first != null && checkPathExists(usernameFile.first, context)
usernameFile.first
)
} }
checkList.add(fileExists || usernameFileExists) checkList.add(fileExists || usernameFileExists)
i++ i++
@ -343,14 +335,29 @@ object DownloadUtils {
return checkList return checkList
} }
private fun checkPathExists(paths: List<String>): Boolean { private fun checkPathExists(paths: List<String>, context: Context): Boolean {
if (root == null) return false if (root == null) return false
var dir = root val uri = root!!.getUri()
var found = false
var docId = DocumentsContract.getTreeDocumentId(uri)
for (path in paths) { for (path in paths) {
dir = dir!!.findFile(path) val docUri = DocumentsContract.buildChildDocumentsUriUsingTree(uri, docId)
if (dir == null || !dir.exists()) { val docCursor = context.contentResolver.query(
return false 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 return true
} }