-
-
Notifications
You must be signed in to change notification settings - Fork 799
Expand file tree
/
Copy pathSearchActivity.kt
More file actions
308 lines (271 loc) · 11.4 KB
/
Copy pathSearchActivity.kt
File metadata and controls
308 lines (271 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.simplemobiletools.gallery.pro.activities
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.ViewGroup
import android.widget.RelativeLayout
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.commons.views.MyGridLayoutManager
import com.simplemobiletools.gallery.pro.R
import com.simplemobiletools.gallery.pro.adapters.MediaAdapter
import com.simplemobiletools.gallery.pro.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.pro.extensions.*
import com.simplemobiletools.gallery.pro.helpers.GridSpacingItemDecoration
import com.simplemobiletools.gallery.pro.helpers.MediaFetcher
import com.simplemobiletools.gallery.pro.helpers.PATH
import com.simplemobiletools.gallery.pro.helpers.SHOW_ALL
import com.simplemobiletools.gallery.pro.interfaces.MediaOperationsListener
import com.simplemobiletools.gallery.pro.models.Medium
import com.simplemobiletools.gallery.pro.models.ThumbnailItem
import kotlinx.android.synthetic.main.activity_search.*
import java.io.File
class SearchActivity : SimpleActivity(), MediaOperationsListener {
private var mIsSearchOpen = false
private var mLastSearchedText = ""
private var mSearchMenuItem: MenuItem? = null
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mAllMedia = ArrayList<ThumbnailItem>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
setupOptionsMenu()
search_empty_text_placeholder.setTextColor(getProperTextColor())
getAllMedia()
search_fastscroller.updateColors(getProperPrimaryColor())
}
override fun onResume() {
super.onResume()
setupToolbar(search_toolbar, NavigationIcon.Arrow, searchMenuItem = mSearchMenuItem)
}
override fun onDestroy() {
super.onDestroy()
mCurrAsyncTask?.stopFetching()
}
private fun setupOptionsMenu() {
setupSearch(search_toolbar.menu)
search_toolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.toggle_filename -> toggleFilenameVisibility()
else -> return@setOnMenuItemClickListener false
}
return@setOnMenuItemClickListener true
}
}
private fun setupSearch(menu: Menu) {
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
mSearchMenuItem = menu.findItem(R.id.search)
MenuItemCompat.setOnActionExpandListener(mSearchMenuItem, object : MenuItemCompat.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem?): Boolean {
mIsSearchOpen = true
return true
}
// this triggers on device rotation too, avoid doing anything
override fun onMenuItemActionCollapse(item: MenuItem?): Boolean {
if (mIsSearchOpen) {
mIsSearchOpen = false
mLastSearchedText = ""
}
return true
}
})
mSearchMenuItem?.expandActionView()
(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 {
if (mIsSearchOpen) {
mLastSearchedText = newText
textChanged(newText)
}
return true
}
})
}
}
private fun textChanged(text: String) {
ensureBackgroundThread {
try {
val filtered = mAllMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList
filtered.sortBy { it is Medium && !it.name.startsWith(text, true) }
val grouped = MediaFetcher(applicationContext).groupMedia(filtered as ArrayList<Medium>, "")
runOnUiThread {
if (grouped.isEmpty()) {
search_empty_text_placeholder.text = getString(R.string.no_items_found)
search_empty_text_placeholder.beVisible()
} else {
search_empty_text_placeholder.beGone()
}
handleGridSpacing(grouped)
getMediaAdapter()?.updateMedia(grouped)
}
} catch (ignored: Exception) {
}
}
}
private fun setupAdapter() {
val currAdapter = search_grid.adapter
if (currAdapter == null) {
MediaAdapter(this, ArrayList(), this, false, false, "", search_grid) {
if (it is Medium) {
itemClicked(it.path)
}
}.apply {
search_grid.adapter = this
}
setupLayoutManager()
handleGridSpacing(mAllMedia)
} else if (mLastSearchedText.isEmpty()) {
(currAdapter as MediaAdapter).updateMedia(mAllMedia)
handleGridSpacing(mAllMedia)
} else {
textChanged(mLastSearchedText)
}
setupScrollDirection()
}
private fun handleGridSpacing(media: ArrayList<ThumbnailItem>) {
val viewType = config.getFolderViewType(SHOW_ALL)
if (viewType == VIEW_TYPE_GRID) {
if (search_grid.itemDecorationCount > 0) {
search_grid.removeItemDecorationAt(0)
}
val spanCount = config.mediaColumnCnt
val spacing = config.thumbnailSpacing
val decoration = GridSpacingItemDecoration(spanCount, spacing, config.scrollHorizontally, config.fileRoundedCorners, media, true)
search_grid.addItemDecoration(decoration)
}
}
private fun getMediaAdapter() = search_grid.adapter as? MediaAdapter
private fun toggleFilenameVisibility() {
config.displayFileNames = !config.displayFileNames
getMediaAdapter()?.updateDisplayFilenames(config.displayFileNames)
}
private fun itemClicked(path: String) {
val isVideo = path.isVideoFast()
if (isVideo) {
openPath(path, false)
} else {
Intent(this, ViewPagerActivity::class.java).apply {
putExtra(PATH, path)
putExtra(SHOW_ALL, false)
startActivity(this)
}
}
}
private fun setupLayoutManager() {
val viewType = config.getFolderViewType(SHOW_ALL)
if (viewType == VIEW_TYPE_GRID) {
setupGridLayoutManager()
} else {
setupListLayoutManager()
}
}
private fun setupGridLayoutManager() {
val layoutManager = search_grid.layoutManager as MyGridLayoutManager
if (config.scrollHorizontally) {
layoutManager.orientation = RecyclerView.HORIZONTAL
search_grid.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
} else {
layoutManager.orientation = RecyclerView.VERTICAL
search_grid.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
layoutManager.spanCount = config.mediaColumnCnt
val adapter = getMediaAdapter()
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return if (adapter?.isASectionTitle(position) == true) {
layoutManager.spanCount
} else {
1
}
}
}
}
private fun setupListLayoutManager() {
val layoutManager = search_grid.layoutManager as MyGridLayoutManager
layoutManager.spanCount = 1
layoutManager.orientation = RecyclerView.VERTICAL
}
private fun setupScrollDirection() {
val viewType = config.getFolderViewType(SHOW_ALL)
val scrollHorizontally = config.scrollHorizontally && viewType == VIEW_TYPE_GRID
search_fastscroller.setScrollVertically(!scrollHorizontally)
}
private fun getAllMedia() {
getCachedMedia("") {
if (it.isNotEmpty()) {
mAllMedia = it.clone() as ArrayList<ThumbnailItem>
}
runOnUiThread {
setupAdapter()
}
startAsyncTask(false)
}
}
private fun startAsyncTask(updateItems: Boolean) {
mCurrAsyncTask?.stopFetching()
mCurrAsyncTask = GetMediaAsynctask(applicationContext, "", showAll = true) {
mAllMedia = it.clone() as ArrayList<ThumbnailItem>
if (updateItems) {
textChanged(mLastSearchedText)
}
}
mCurrAsyncTask!!.execute()
}
override fun refreshItems() {
startAsyncTask(true)
}
override fun tryDeleteFiles(fileDirItems: ArrayList<FileDirItem>) {
val filtered = fileDirItems.filter { File(it.path).isFile && it.path.isMediaFile() } as ArrayList
if (filtered.isEmpty()) {
return
}
if (config.useRecycleBin && !filtered.first().path.startsWith(recycleBinPath)) {
val movingItems = resources.getQuantityString(R.plurals.moving_items_into_bin, filtered.size, filtered.size)
toast(movingItems)
movePathsInRecycleBin(filtered.map { it.path } as ArrayList<String>) { wasSuccess, range ->
if (wasSuccess) {
val itemsInRange = filtered.subList(range.first, range.second)
deleteFilteredFiles(ArrayList(itemsInRange))
} else {
toast(R.string.unknown_error_occurred)
}
}
} else {
val deletingItems = resources.getQuantityString(R.plurals.deleting_items, filtered.size, filtered.size)
toast(deletingItems)
deleteFilteredFiles(filtered)
}
}
private fun deleteFilteredFiles(filtered: ArrayList<FileDirItem>) {
deleteFiles(filtered) {
if (!it) {
toast(R.string.unknown_error_occurred)
return@deleteFiles
}
mAllMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) }
ensureBackgroundThread {
val useRecycleBin = config.useRecycleBin
filtered.forEach {
if (it.path.startsWith(recycleBinPath) || !useRecycleBin) {
deleteDBPath(it.path)
}
}
}
}
}
override fun selectedPaths(paths: ArrayList<String>) {}
override fun updateMediaGridDecoration(media: ArrayList<ThumbnailItem>) {}
}