11package com.simplemobiletools.filemanager.activities
22
3+ import android.app.SearchManager
4+ import android.content.Context
35import android.net.Uri
46import android.os.Bundle
7+ import android.support.v4.view.MenuItemCompat
8+ import android.support.v7.widget.SearchView
59import android.view.Menu
610import android.view.MenuItem
711import com.simplemobiletools.commons.extensions.*
@@ -17,6 +21,10 @@ import java.io.File
1721
1822class ReadTextActivity : SimpleActivity () {
1923 private var filePath = " "
24+ private var originalText = " "
25+ private var isSearchOpen = false
26+
27+ private var searchMenuItem: MenuItem ? = null
2028
2129 override fun onCreate (savedInstanceState : Bundle ? ) {
2230 super .onCreate(savedInstanceState)
@@ -35,6 +43,7 @@ class ReadTextActivity : SimpleActivity() {
3543
3644 override fun onCreateOptionsMenu (menu : Menu ): Boolean {
3745 menuInflater.inflate(R .menu.menu_editor, menu)
46+ setupSearch(menu)
3847 return true
3948 }
4049
@@ -47,6 +56,43 @@ class ReadTextActivity : SimpleActivity() {
4756 return true
4857 }
4958
59+ private fun setupSearch (menu : Menu ) {
60+ val searchManager = getSystemService(Context .SEARCH_SERVICE ) as SearchManager
61+ searchMenuItem = menu.findItem(R .id.search)
62+ (searchMenuItem!! .actionView as SearchView ).apply {
63+ setSearchableInfo(searchManager.getSearchableInfo(componentName))
64+ isSubmitButtonEnabled = false
65+ setOnQueryTextListener(object : android.support.v7.widget.SearchView .OnQueryTextListener {
66+ override fun onQueryTextSubmit (query : String ) = false
67+
68+ override fun onQueryTextChange (newText : String ): Boolean {
69+ if (isSearchOpen) {
70+ searchQueryChanged(newText)
71+ }
72+ return true
73+ }
74+ })
75+ }
76+
77+ MenuItemCompat .setOnActionExpandListener(searchMenuItem, object : MenuItemCompat .OnActionExpandListener {
78+ override fun onMenuItemActionExpand (item : MenuItem ? ): Boolean {
79+ isSearchOpen = true
80+ searchQueryChanged(" " )
81+ return true
82+ }
83+
84+ override fun onMenuItemActionCollapse (item : MenuItem ? ): Boolean {
85+ isSearchOpen = false
86+ return true
87+ }
88+ })
89+ }
90+
91+ private fun searchQueryChanged (text : String ) {
92+ val textToHighlight = if (text.length < 2 ) " " else text
93+ read_text_view.setText(originalText.highlightTextPart(textToHighlight, getAdjustedPrimaryColor(), true ))
94+ }
95+
5096 private fun saveText () {
5197 if (filePath.isEmpty()) {
5298 filePath = getRealPathFromURI(intent.data) ? : " "
@@ -78,7 +124,7 @@ class ReadTextActivity : SimpleActivity() {
78124 return
79125 }
80126
81- val text = if (uri.scheme == " file" ) {
127+ originalText = if (uri.scheme == " file" ) {
82128 filePath = uri.path
83129 val file = File (filePath)
84130 if (file.exists()) {
@@ -97,6 +143,6 @@ class ReadTextActivity : SimpleActivity() {
97143 }
98144 }
99145
100- read_text_view.setText(text )
146+ read_text_view.setText(originalText )
101147 }
102148}
0 commit comments