adding initial search, related to #34

This commit is contained in:
tibbi 2018-01-07 23:51:33 +01:00
parent bef2f89b79
commit e41d5de4aa
5 changed files with 95 additions and 4 deletions

View File

@ -25,6 +25,15 @@
</activity> </activity>
<activity android:name=".activities.MainActivity"> <activity android:name=".activities.MainActivity">
<meta-data
android:name="android.app.default_searchable"
android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.GET_CONTENT"/> <action android:name="android.intent.action.GET_CONTENT"/>
<data android:mimeType="*/*"/> <data android:mimeType="*/*"/>

View File

@ -1,11 +1,15 @@
package com.simplemobiletools.filemanager.activities package com.simplemobiletools.filemanager.activities
import android.app.Activity import android.app.Activity
import android.app.SearchManager
import android.content.ClipData import android.content.ClipData
import android.content.Context
import android.content.Intent import android.content.Intent
import android.media.RingtoneManager import android.media.RingtoneManager
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.support.v4.view.MenuItemCompat
import android.support.v7.widget.SearchView
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
@ -26,9 +30,13 @@ import java.io.File
import java.util.* import java.util.*
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
var isSearchOpen = false
private val BACK_PRESS_TIMEOUT = 5000 private val BACK_PRESS_TIMEOUT = 5000
private var wasBackJustPressed = false private var wasBackJustPressed = false
private var mStoredUseEnglish = false private var searchMenuItem: MenuItem? = null
private var storedUseEnglish = false
private lateinit var fragment: ItemsFragment private lateinit var fragment: ItemsFragment
@ -54,7 +62,7 @@ class MainActivity : SimpleActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (mStoredUseEnglish != config.useEnglish) { if (storedUseEnglish != config.useEnglish) {
restartActivity() restartActivity()
return return
} }
@ -85,11 +93,45 @@ class MainActivity : SimpleActivity() {
findItem(R.id.stop_showing_hidden).isVisible = config.temporarilyShowHidden findItem(R.id.stop_showing_hidden).isVisible = config.temporarilyShowHidden
} }
setupSearch(menu)
return true return true
} }
private fun storeStateVariables() { private fun storeStateVariables() {
mStoredUseEnglish = config.useEnglish storedUseEnglish = config.useEnglish
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
searchMenuItem = menu.findItem(R.id.search)
(searchMenuItem!!.actionView as SearchView).apply {
setSearchableInfo(searchManager.getSearchableInfo(componentName))
isSubmitButtonEnabled = false
queryHint = getString(R.string.search_folder)
setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String) = false
override fun onQueryTextChange(newText: String): Boolean {
if (isSearchOpen) {
fragment.searchQueryChanged(newText)
}
return true
}
})
}
MenuItemCompat.setOnActionExpandListener(searchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
isSearchOpen = true
return true
}
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
isSearchOpen = false
fragment.searchClosed()
return true
}
})
} }
private fun tryInitFileManager() { private fun tryInitFileManager() {
@ -275,6 +317,10 @@ class MainActivity : SimpleActivity() {
finish() finish()
} }
fun openedDirectory() {
MenuItemCompat.collapseActionView(searchMenuItem)
}
private fun checkWhatsNewDialog() { private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply { arrayListOf<Release>().apply {
add(Release(26, R.string.release_26)) add(Release(26, R.string.release_26))

View File

@ -36,11 +36,13 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
var isGetRingtonePicker = false var isGetRingtonePicker = false
var isPickMultipleIntent = false var isPickMultipleIntent = false
private var storedTextColor = 0
private var showHidden = false private var showHidden = false
private var skipItemUpdating = false
private var storedItems = ArrayList<FileDirItem>() private var storedItems = ArrayList<FileDirItem>()
private var scrollStates = HashMap<String, Parcelable>() private var scrollStates = HashMap<String, Parcelable>()
private var storedTextColor = 0
private lateinit var mView: View private lateinit var mView: View
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
@ -127,6 +129,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
} }
private fun addItems(items: ArrayList<FileDirItem>) { private fun addItems(items: ArrayList<FileDirItem>) {
skipItemUpdating = false
mView.apply { mView.apply {
activity?.runOnUiThread { activity?.runOnUiThread {
items_swipe_refresh?.isRefreshing = false items_swipe_refresh?.isRefreshing = false
@ -168,6 +171,7 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
private fun getRecyclerLayoutManager() = (mView.items_list.layoutManager as LinearLayoutManager) private fun getRecyclerLayoutManager() = (mView.items_list.layoutManager as LinearLayoutManager)
private fun getItems(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) { private fun getItems(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) {
skipItemUpdating = false
Thread { Thread {
if (activity?.isActivityDestroyed() == false) { if (activity?.isActivityDestroyed() == false) {
if (!context!!.config.enableRootAccess || !context!!.isPathOnRoot(path)) { if (!context!!.config.enableRootAccess || !context!!.isPathOnRoot(path)) {
@ -227,6 +231,10 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
private fun itemClicked(item: FileDirItem) { private fun itemClicked(item: FileDirItem) {
if (item.isDirectory) { if (item.isDirectory) {
(activity as? MainActivity)?.apply {
skipItemUpdating = isSearchOpen
openedDirectory()
}
openPath(item.path) openPath(item.path)
} else { } else {
val path = item.path val path = item.path
@ -245,6 +253,23 @@ class ItemsFragment : Fragment(), ItemsAdapter.ItemOperationsListener, Breadcrum
} }
} }
fun searchQueryChanged(text: String) {
Thread {
val filtered = storedItems.filter { it.name.contains(text, true) } as ArrayList
filtered.sortBy { !it.name.startsWith(text, true) }
activity?.runOnUiThread {
(items_list.adapter as ItemsAdapter).updateItems(filtered)
}
}.start()
}
fun searchClosed() {
if (!skipItemUpdating) {
(items_list.adapter as ItemsAdapter).updateItems(storedItems)
}
skipItemUpdating = false
}
private fun createNewItem() { private fun createNewItem() {
CreateNewItemDialog(activity as SimpleActivity, currentPath) { CreateNewItemDialog(activity as SimpleActivity, currentPath) {
if (it) { if (it) {

View File

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|ifRoom"/>
<item <item
android:id="@+id/go_home" android:id="@+id/go_home"
android:icon="@drawable/ic_home" android:icon="@drawable/ic_home"

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<searchable
xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_folder"
android:label="@string/app_name"/>