diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4c50f0162..c49d7cf4d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,7 +55,17 @@ + android:parentActivityName=".activities.MainActivity"> + + + + + + + + () @@ -130,6 +135,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + override fun onStop() { + super.onStop() + MenuItemCompat.collapseActionView(mSearchMenuItem) + } + override fun onDestroy() { super.onDestroy() if (config.showAll) @@ -150,6 +160,33 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } + private fun setupSearch(menu: Menu) { + val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager + mSearchMenuItem = menu.findItem(R.id.search) + (mSearchMenuItem!!.actionView as SearchView).apply { + setSearchableInfo(searchManager.getSearchableInfo(componentName)) + isSubmitButtonEnabled = false + setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String) = false + + override fun onQueryTextChange(newText: String): Boolean { + searchQueryChanged(newText) + return true + } + }) + } + } + + private fun searchQueryChanged(text: String) { + Thread { + val filtered = mMedia.filter { it.name.contains(text, true) } as ArrayList + filtered.sortBy { !it.name.startsWith(text, true) } + runOnUiThread { + (media_grid.adapter as? MediaAdapter)?.updateMedia(filtered) + } + }.start() + } + private fun tryloadGallery() { handlePermission(PERMISSION_WRITE_STORAGE) { if (it) { @@ -260,6 +297,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { findItem(R.id.toggle_filename).isVisible = config.viewTypeFiles == VIEW_TYPE_GRID } + setupSearch(menu) return true } diff --git a/app/src/main/res/menu/menu_media.xml b/app/src/main/res/menu/menu_media.xml index 978c684c0..de3b01776 100644 --- a/app/src/main/res/menu/menu_media.xml +++ b/app/src/main/res/menu/menu_media.xml @@ -2,15 +2,21 @@ + android:id="@+id/search" + android:icon="@drawable/ic_search" + android:title="@string/search" + app:actionViewClass="android.support.v7.widget.SearchView" + app:showAsAction="collapseActionView|ifRoom"/> + +