Skip to content

Commit

Permalink
make DeepSearch a subclass of FileSearch
Browse files Browse the repository at this point in the history
  • Loading branch information
seelchen committed Jan 15, 2024
1 parent 1093f64 commit e255500
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,69 +21,37 @@
package com.amaze.filemanager.asynchronous.asynctasks.searchfilesystem

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.amaze.filemanager.fileoperations.filesystem.OpenMode
import com.amaze.filemanager.filesystem.HybridFile
import com.amaze.filemanager.filesystem.HybridFileParcelable
import kotlinx.coroutines.isActive
import org.slf4j.LoggerFactory
import java.util.Locale
import java.util.regex.Pattern
import kotlin.coroutines.coroutineContext

class DeepSearch(
context: Context,
private val query: String,
private val path: String,
private val openMode: OpenMode,
private val searchParameters: SearchParameters
) {
private val openMode: OpenMode
) : FileSearch() {
private val LOG = LoggerFactory.getLogger(DeepSearch::class.java)

private val hybridFileParcelables: ArrayList<HybridFileParcelable> = ArrayList()
val mutableLiveData: MutableLiveData<ArrayList<HybridFileParcelable>> = MutableLiveData(
hybridFileParcelables
)

private val applicationContext: Context

init {
applicationContext = context.applicationContext
}

/**
* Search for files, whose names match [query], starting from [path] and add them to
* [mutableLiveData].
*/
suspend fun search() {
val file = HybridFile(openMode, path)
if (file.isSmb) return

// level 1
// if regex or not
if (SearchParameter.REGEX !in searchParameters) {
search(file, query)
} else {
// compile the regular expression in the input
val pattern = Pattern.compile(
bashRegexToJava(query),
Pattern.CASE_INSENSITIVE
)
// level 2
if (SearchParameter.REGEX_MATCHES !in searchParameters) {
searchRegExFind(file, pattern)
} else {
searchRegExMatch(file, pattern)
}
}
}

/**
* Search for occurrences of a given text in file names and publish the result
*
* @param directory the current path
*/
private suspend fun search(directory: HybridFile, filter: SearchFilter) {
override suspend fun search(
path: String,
filter: SearchFilter,
searchParameters: SearchParameters
) {
val directory = HybridFile(openMode, path)
if (directory.isSmb) return

if (directory.isDirectory(applicationContext)) {
// you have permission to read this directory
val worklist = ArrayDeque<HybridFile>()
Expand All @@ -108,68 +76,4 @@ class DeepSearch(
LOG.warn("Cannot search " + directory.path + ": Permission Denied")
}
}

private fun publishProgress(file: HybridFileParcelable) {
hybridFileParcelables.add(file)
mutableLiveData.postValue(hybridFileParcelables)
}

/**
* Recursively search for occurrences of a given text in file names and publish the result
*
* @param file the current path
* @param query the searched text
*/
private suspend fun search(file: HybridFile, query: String) {
search(file) { fileName: String ->
fileName.lowercase(Locale.getDefault()).contains(
query.lowercase(
Locale.getDefault()
)
)
}
}

/**
* Recursively find a java regex pattern [Pattern] in the file names and publish the result
*
* @param file the current file
* @param pattern the compiled java regex
*/
private suspend fun searchRegExFind(file: HybridFile, pattern: Pattern) {
search(file) { fileName: String -> pattern.matcher(fileName).find() }
}

/**
* Recursively match a java regex pattern [Pattern] with the file names and publish the
* result
*
* @param file the current file
* @param pattern the compiled java regex
*/
private suspend fun searchRegExMatch(file: HybridFile, pattern: Pattern) {
search(file) { fileName: String -> pattern.matcher(fileName).matches() }
}

/**
* method converts bash style regular expression to java. See [Pattern]
*
* @return converted string
*/
private fun bashRegexToJava(originalString: String): String {
val stringBuilder = StringBuilder()
for (i in originalString.indices) {
when (originalString[i].toString() + "") {
"*" -> stringBuilder.append("\\w*")
"?" -> stringBuilder.append("\\w")
else -> stringBuilder.append(originalString[i])
}
}
return stringBuilder.toString()
}

fun interface SearchFilter {
/** Returns if the file with the given [fileName] fulfills some predicate */
fun searchFilter(fileName: String): Boolean
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,8 @@ class MainActivityViewModel(val applicationContext: Application) :
fun deepSearch(
mainActivity: MainActivity,
query: String
): MutableLiveData<ArrayList<HybridFileParcelable>> {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(mainActivity)
val searchParameters = searchParametersFromBoolean(
showHiddenFiles = sharedPref.getBoolean(PREFERENCE_SHOW_HIDDENFILES, false),
isRegexEnabled = sharedPref.getBoolean(PREFERENCE_REGEX, false),
isRegexMatchesEnabled = sharedPref.getBoolean(PREFERENCE_REGEX_MATCHES, false),
isRoot = mainActivity.isRootExplorer
)
): LiveData<List<HybridFileParcelable>> {
val searchParameters = createSearchParameters(mainActivity)

val path = mainActivity.currentMainFragment?.currentPath ?: ""
val openMode =
Expand All @@ -212,17 +206,24 @@ class MainActivityViewModel(val applicationContext: Application) :

val deepSearch = DeepSearch(
context,
query,
path,
openMode,
searchParameters
openMode
)

viewModelScope.launch(Dispatchers.IO) {
deepSearch.search()
deepSearch.search(query, path, searchParameters)
}

return deepSearch.mutableLiveData
return deepSearch.foundFilesLiveData
}

private fun createSearchParameters(mainActivity: MainActivity): SearchParameters {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(mainActivity)
return searchParametersFromBoolean(
showHiddenFiles = sharedPref.getBoolean(PREFERENCE_SHOW_HIDDENFILES, false),
isRegexEnabled = sharedPref.getBoolean(PREFERENCE_REGEX, false),
isRegexMatchesEnabled = sharedPref.getBoolean(PREFERENCE_REGEX_MATCHES, false),
isRoot = mainActivity.isRootExplorer
)
}

/**
Expand Down

0 comments on commit e255500

Please sign in to comment.