mirror of
https://github.com/KokaKiwi/BarInsta
synced 2024-11-22 14:47:29 +00:00
trial: check downloaded using contentResolver.query
https://redd.it/bbejc4
This commit is contained in:
parent
3dcc791cba
commit
5c97bea3c2
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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,15 +335,30 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user