@@ -27,16 +27,19 @@ class ApplicationListActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefres
2727 }
2828
2929 private val allApps = ArrayList <PackageInfo >()
30+ private val filteredApps = ArrayList <PackageInfo >()
31+
3032 private var showSystem = false
3133 private var textFilter = " "
3234
3335
3436 override fun onCreate (savedInstanceState : Bundle ? ) {
3537 super .onCreate(savedInstanceState)
3638 setContentView(R .layout.activity_app_list)
39+
3740 apps_list_recyclerView.adapter =
3841 ApplicationListAdapter (
39- allApps ,
42+ filteredApps ,
4043 { sharedPreferences.contains(it) },
4144 { pInfo, isChecked ->
4245 if (isChecked)
@@ -46,59 +49,61 @@ class ApplicationListActivity : AppCompatActivity(), SwipeRefreshLayout.OnRefres
4649 })
4750 apps_list_swipeRefreshLayout.setOnRefreshListener(this )
4851 apps_list_more_menu.setOnClickListener(this )
52+
4953 apps_list_filterEditText.doAfterTextChanged {
5054 textFilter = it.toString()
51- onRefresh ()
55+ applyFilters ()
5256 }
57+
5358 onRefresh()
5459 }
5560
5661 override fun onRefresh () {
5762 launch(Dispatchers .Main ) {
58- if (apps_list_swipeRefreshLayout.isRefreshing.not ())
63+ if (apps_list_swipeRefreshLayout.isRefreshing.not ()) {
5964 apps_list_swipeRefreshLayout.isRefreshing = true
60- val apps = loadAllApps(showSystem, textFilter)
65+ }
66+
67+ val apps = loadAllApps()
6168 allApps.clear()
6269 allApps.addAll(apps)
63- apps_list_recyclerView.adapter?.notifyDataSetChanged()
70+ applyFilters()
71+
6472 apps_list_swipeRefreshLayout.isRefreshing = false
6573 }
6674 }
6775
68- private suspend fun loadAllApps (
69- showSystem : Boolean ,
70- filterByText : String
71- ): List <PackageInfo > =
76+ private fun applyFilters () {
77+ filteredApps.clear()
78+ filteredApps.addAll(allApps.filter(::matchesFilters))
79+ apps_list_recyclerView.adapter?.notifyDataSetChanged()
80+ }
81+
82+ private fun matchesFilters (app : PackageInfo ): Boolean {
83+ val appInfo = app.applicationInfo
84+ val appLabel = appInfo.loadLabel(packageManager)
85+ val isSystemApp = appInfo.flags and ApplicationInfo .FLAG_SYSTEM == 1
86+
87+ return (textFilter.isEmpty() || appLabel.contains(textFilter, true )) &&
88+ (showSystem || ! isSystemApp)
89+ }
90+
91+ private suspend fun loadAllApps (): List <PackageInfo > =
7292 withContext(Dispatchers .IO ) {
73- return @withContext packageManager.getInstalledPackages(PackageManager .GET_META_DATA )
74- .run { // Text Filter
75- if (filterByText.isBlank())
76- this
77- else
78- this .filter {
79- it.applicationInfo.loadLabel(packageManager)
80- .contains(filterByText, true )
81- }.toMutableList()
82- }.run { // System App Info
83- if (showSystem)
84- this
85- else this .filter {
86- it.applicationInfo.flags and ApplicationInfo .FLAG_SYSTEM != 1
87- }.toMutableList()
88- }.apply { // Sorting Order
89- sortBy {
90- it.applicationInfo.loadLabel(packageManager).toString().toUpperCase(
91- Locale .getDefault()
92- )
93- }
93+ return @withContext packageManager.getInstalledPackages(PackageManager .GET_META_DATA ).apply {
94+ sortBy {
95+ it.applicationInfo.loadLabel(packageManager).toString().toUpperCase(
96+ Locale .getDefault()
97+ )
9498 }
99+ }
95100 }
96101
97102 override fun onMenuItemClick (item : MenuItem ? ): Boolean {
98103 return when (item?.itemId) {
99104 R .id.action_show_system -> {
100105 showSystem = showSystem.not ()
101- onRefresh ()
106+ applyFilters ()
102107 true
103108 }
104109 else -> false
0 commit comments