show folders with .nomedia if appropriate

This commit is contained in:
tibbi 2017-02-23 21:29:02 +01:00
parent d03bdfc91a
commit a8c5cc1fdd
5 changed files with 83 additions and 30 deletions

View File

@ -147,17 +147,22 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}
private fun hideFolder() {
addNoMedia(mPath)
if (!config.showHiddenFolders)
finish()
else
invalidateOptionsMenu()
addNoMedia(mPath) {
runOnUiThread {
if (!config.showHiddenFolders)
finish()
else
invalidateOptionsMenu()
}
}
}
private fun unhideFolder() {
removeNoMedia(mPath)
invalidateOptionsMenu()
removeNoMedia(mPath) {
runOnUiThread {
invalidateOptionsMenu()
}
}
}
private fun deleteDirectoryIfEmpty() {

View File

@ -173,14 +173,22 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
val paths = getSelectedPaths()
for (path in paths) {
if (hide) {
activity.addNoMedia(path)
activity.addNoMedia(path) {
noMediaHandled()
}
} else {
activity.removeNoMedia(path)
activity.removeNoMedia(path) {
noMediaHandled()
}
}
}
}
listener?.refreshItems()
actMode?.finish()
private fun noMediaHandled() {
activity.runOnUiThread {
listener?.refreshItems()
actMode?.finish()
}
}
private fun pinFolders(pin: Boolean) {

View File

@ -28,7 +28,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val fileSorting = config.fileSorting
val parents = context.getParents(isPickImage, isPickVideo)
parents.mapNotNull { File(it).listFiles() }
parents.map { File(it).listFiles() }
.forEach {
for (file in it) {
val isImage = file.isImageFast() || file.isGif()
@ -95,6 +95,4 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
super.onPostExecute(dirs)
callback.invoke(dirs)
}
}

View File

@ -143,7 +143,7 @@ fun AppCompatActivity.hideSystemUI() {
View.SYSTEM_UI_FLAG_IMMERSIVE
}
fun SimpleActivity.addNoMedia(path: String) {
fun SimpleActivity.addNoMedia(path: String, callback: () -> Unit) {
val file = File(path, NOMEDIA)
if (file.exists())
return
@ -155,10 +155,12 @@ fun SimpleActivity.addNoMedia(path: String) {
} else {
file.createNewFile()
}
scanFile(file) {}
scanFile(file) {
callback.invoke()
}
}
fun SimpleActivity.removeNoMedia(path: String) {
fun SimpleActivity.removeNoMedia(path: String, callback: () -> Unit) {
val file = File(path, NOMEDIA)
if (!file.exists())
return
@ -173,6 +175,8 @@ fun SimpleActivity.removeNoMedia(path: String) {
}
}
}
scanFile(file) {}
}
scanFile(file) {
callback.invoke()
}
}

View File

@ -6,11 +6,13 @@ import android.database.Cursor
import android.net.Uri
import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.isImageVideoGif
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.VIDEOS
import java.io.File
import java.util.*
@ -69,10 +71,15 @@ fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<St
cursor?.close()
}
val notNull = ArrayList<String>()
parents.mapNotNullTo(notNull, { it })
filterDirectories(notNull)
return notNull
val filtered = ArrayList<String>()
parents.mapNotNullTo(filtered, { it })
if (config.showHiddenFolders) {
filtered.addAll(getNoMediaFolders())
} else {
removeNoMediaFolders(filtered)
}
return filtered
}
fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String {
@ -95,18 +102,12 @@ fun Context.getArgs(isPickImage: Boolean, isPickVideo: Boolean): Array<String> {
}
}
fun Context.filterDirectories(dirs: ArrayList<String>) {
if (!config.showHiddenFolders) {
removeNoMediaFolders(dirs)
}
}
private fun removeNoMediaFolders(paths: MutableList<String>) {
val ignorePaths = ArrayList<String>()
for (path in paths) {
val dir = File(path)
if (dir.exists() && dir.isDirectory) {
val res = dir.list { file, filename -> filename == ".nomedia" }
val res = dir.list { file, filename -> filename == NOMEDIA }
if (res?.isNotEmpty() == true)
ignorePaths.add(path)
}
@ -115,4 +116,41 @@ private fun removeNoMediaFolders(paths: MutableList<String>) {
paths.removeAll(ignorePaths)
}
fun Context.getNoMediaFolders(): ArrayList<String> {
val folders = ArrayList<String>()
val noMediaCondition = "${MediaStore.Files.FileColumns.MEDIA_TYPE} = ${MediaStore.Files.FileColumns.MEDIA_TYPE_NONE}"
val uri = MediaStore.Files.getContentUri("external")
val columns = arrayOf(MediaStore.Files.FileColumns.DATA)
val where = "$noMediaCondition AND ${MediaStore.Files.FileColumns.TITLE} LIKE ?"
val args = arrayOf("%$NOMEDIA%")
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, columns, where, args, null)
if (cursor?.moveToFirst() == true) {
do {
val path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)) ?: continue
val parent = File(path).parentFile
if (hasImageVideoGif(parent)) {
folders.add(parent.absolutePath)
}
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return folders
}
fun hasImageVideoGif(dir: File): Boolean {
if (dir.isDirectory) {
dir.listFiles()
.filter(File::isImageVideoGif)
.forEach { return true }
}
return false
}
val Context.config: Config get() = Config.newInstance(this)