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() { private fun hideFolder() {
addNoMedia(mPath) addNoMedia(mPath) {
runOnUiThread {
if (!config.showHiddenFolders) if (!config.showHiddenFolders)
finish() finish()
else else
invalidateOptionsMenu() invalidateOptionsMenu()
}
}
} }
private fun unhideFolder() { private fun unhideFolder() {
removeNoMedia(mPath) removeNoMedia(mPath) {
invalidateOptionsMenu() runOnUiThread {
invalidateOptionsMenu()
}
}
} }
private fun deleteDirectoryIfEmpty() { private fun deleteDirectoryIfEmpty() {

View file

@ -173,14 +173,22 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
val paths = getSelectedPaths() val paths = getSelectedPaths()
for (path in paths) { for (path in paths) {
if (hide) { if (hide) {
activity.addNoMedia(path) activity.addNoMedia(path) {
noMediaHandled()
}
} else { } else {
activity.removeNoMedia(path) activity.removeNoMedia(path) {
noMediaHandled()
}
} }
} }
}
listener?.refreshItems() private fun noMediaHandled() {
actMode?.finish() activity.runOnUiThread {
listener?.refreshItems()
actMode?.finish()
}
} }
private fun pinFolders(pin: Boolean) { 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 fileSorting = config.fileSorting
val parents = context.getParents(isPickImage, isPickVideo) val parents = context.getParents(isPickImage, isPickVideo)
parents.mapNotNull { File(it).listFiles() } parents.map { File(it).listFiles() }
.forEach { .forEach {
for (file in it) { for (file in it) {
val isImage = file.isImageFast() || file.isGif() val isImage = file.isImageFast() || file.isGif()
@ -95,6 +95,4 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
super.onPostExecute(dirs) super.onPostExecute(dirs)
callback.invoke(dirs) callback.invoke(dirs)
} }
} }

View file

@ -143,7 +143,7 @@ fun AppCompatActivity.hideSystemUI() {
View.SYSTEM_UI_FLAG_IMMERSIVE View.SYSTEM_UI_FLAG_IMMERSIVE
} }
fun SimpleActivity.addNoMedia(path: String) { fun SimpleActivity.addNoMedia(path: String, callback: () -> Unit) {
val file = File(path, NOMEDIA) val file = File(path, NOMEDIA)
if (file.exists()) if (file.exists())
return return
@ -155,10 +155,12 @@ fun SimpleActivity.addNoMedia(path: String) {
} else { } else {
file.createNewFile() 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) val file = File(path, NOMEDIA)
if (!file.exists()) if (!file.exists())
return 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.net.Uri
import android.provider.MediaStore import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.humanizePath import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.isImageVideoGif
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.activities.SettingsActivity
import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.IMAGES import com.simplemobiletools.gallery.helpers.IMAGES
import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.helpers.VIDEOS import com.simplemobiletools.gallery.helpers.VIDEOS
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -69,10 +71,15 @@ fun Context.getParents(isPickImage: Boolean, isPickVideo: Boolean): ArrayList<St
cursor?.close() cursor?.close()
} }
val notNull = ArrayList<String>() val filtered = ArrayList<String>()
parents.mapNotNullTo(notNull, { it }) parents.mapNotNullTo(filtered, { it })
filterDirectories(notNull)
return notNull if (config.showHiddenFolders) {
filtered.addAll(getNoMediaFolders())
} else {
removeNoMediaFolders(filtered)
}
return filtered
} }
fun Context.getWhereCondition(isPickImage: Boolean, isPickVideo: Boolean): String { 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>) { private fun removeNoMediaFolders(paths: MutableList<String>) {
val ignorePaths = ArrayList<String>() val ignorePaths = ArrayList<String>()
for (path in paths) { for (path in paths) {
val dir = File(path) val dir = File(path)
if (dir.exists() && dir.isDirectory) { 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) if (res?.isNotEmpty() == true)
ignorePaths.add(path) ignorePaths.add(path)
} }
@ -115,4 +116,41 @@ private fun removeNoMediaFolders(paths: MutableList<String>) {
paths.removeAll(ignorePaths) 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) val Context.config: Config get() = Config.newInstance(this)