From 23ff67f388504ccc7d07eceb26815d3e2bdd8b34 Mon Sep 17 00:00:00 2001 From: ottoptj Date: Tue, 21 May 2024 02:28:21 +0300 Subject: [PATCH] (Probably buggy) Renaming now moves the app correctly and searched items align to bottom. --- .../eu/ottop/yamlauncher/AppActionMenu.kt | 48 +++++++++++++--- .../eu/ottop/yamlauncher/AppMenuActivity.kt | 57 ++++++++++++------- .../eu/ottop/yamlauncher/AppMenuAdapter.kt | 12 ++-- app/src/main/res/layout/activity_app_menu.xml | 3 +- 4 files changed, 86 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index 90a5ba7..d40659d 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout import android.widget.TextView +import androidx.appcompat.widget.AppCompatButton import eu.ottop.yamlauncher.databinding.ActivityAppMenuBinding import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch @@ -68,17 +69,21 @@ class AppActionMenu { editLayout.visibility = View.VISIBLE actionMenu.visibility = View.INVISIBLE val editText = editLayout.findViewById(R.id.app_name_edit) + val resetButton = editLayout.findViewById(R.id.reset) + + val app = Pair(mainActivity!!, Pair(userHandle, workProfile)) + searchView.visibility = View.INVISIBLE editText.requestFocus() val handler = Handler(Looper.getMainLooper()) handler.postDelayed({ - val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val imm = + activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) }, 100) - binding.root.addOnLayoutChangeListener { - _, _, top, _, bottom, _, oldTop, _, oldBottom -> + binding.root.addOnLayoutChangeListener { _, _, top, _, bottom, _, oldTop, _, oldBottom -> if (bottom - top > oldBottom - oldTop) { editLayout.clearFocus() @@ -90,24 +95,51 @@ class AppActionMenu { editText.setOnEditorActionListener { _, actionId, _ -> if (actionId == EditorInfo.IME_ACTION_DONE) { - val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + val imm = + activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(editText.windowToken, 0) - sharedPreferenceManager.setAppName(activity, appInfo.packageName, workProfile, editText.text.toString()) + sharedPreferenceManager.setAppName( + activity, + appInfo.packageName, + workProfile, + editText.text.toString() + ) + + val newPosition = activity.getInstalledApps() + .indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile } uiScope.launch { - activity.updateItem(position,Pair(mainActivity!!, Pair(userHandle, workProfile))) + activity.updateItem(position, app) + activity.moveItem(position, newPosition) + activity.manualRefresh() } return@setOnEditorActionListener true } false } + + resetButton.setOnClickListener { + val imm = + activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.hideSoftInputFromWindow(editLayout.windowToken, 0) + sharedPreferenceManager.resetAppName( + activity, + app.first.applicationInfo.packageName, + app.second.second + ) + val newPosition = activity.getInstalledApps() + .indexOfFirst { it.first.applicationInfo.packageName == appInfo.packageName && it.second.second == workProfile } + activity.updateItem(position, app) + activity.moveItem(position, newPosition) + activity.manualRefresh() + } } actionMenu.findViewById(R.id.hide).setOnClickListener { - sharedPreferenceManager.setAppHidden(activity, appInfo.packageName, workProfile, true) - textView.visibility = View.GONE editLayout.visibility = View.GONE + textView.visibility = View.GONE actionMenu.visibility = View.GONE + sharedPreferenceManager.setAppHidden(activity, appInfo.packageName, workProfile, true) } actionMenu.findViewById(R.id.close).setOnClickListener { diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuActivity.kt index 075055c..e4008c3 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuActivity.kt @@ -8,7 +8,9 @@ import android.os.Bundle import android.os.UserHandle import android.text.Editable import android.text.TextWatcher +import android.util.Log import android.view.View +import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.LinearLayout import android.widget.TextView @@ -72,7 +74,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, searchView = findViewById(R.id.searchView) recyclerView = findViewById(R.id.recycler_view) - recyclerView.layoutManager = LinearLayoutManager(this) + recyclerView.scrollToPosition(0) installedApps = getInstalledApps() filteredApps = mutableListOf() filteredApps.addAll(installedApps) @@ -88,7 +90,6 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, val mainActivity = launcherApps.getActivityList(appInfo.applicationInfo.packageName, userHandle).firstOrNull() if (mainActivity != null) { launcherApps.startMainActivity(mainActivity.componentName, userHandle, null, null) - finish() } else { Toast.makeText(this, "Cannot launch app", Toast.LENGTH_SHORT).show() } @@ -182,7 +183,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, return this.replace("[^a-zA-Z0-9]".toRegex(), "") } - private fun getInstalledApps(): List>> { + fun getInstalledApps(): List>> { val allApps = mutableListOf>>() val launcherApps = getSystemService(LAUNCHER_APPS_SERVICE) as LauncherApps for (i in launcherApps.profiles.indices) { @@ -202,30 +203,39 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, override fun onStop() { super.onStop() job.cancel() - //finish() } override fun onStart() { super.onStart() startTask() + val imm = + getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.hideSoftInputFromWindow(binding.root.windowToken, 0) } private fun startTask() { job = CoroutineScope(Dispatchers.Default).launch { while (true) { - val updatedApps = getInstalledApps() - val changes = detectChanges(installedApps, updatedApps) - installedApps = updatedApps - withContext(Dispatchers.Main) { - applyChanges(changes, installedApps) - } + manualRefresh() delay(5000) } } } - data class Change(val type: ChangeType, val position: Int) + fun manualRefresh() { + CoroutineScope(Dispatchers.Default).launch { + val updatedApps = getInstalledApps() + val changes = detectChanges(installedApps, updatedApps) + + withContext(Dispatchers.Main) { + applyChanges(changes, installedApps) + } + installedApps = updatedApps + } + } + + data class Change(val type: ChangeType, val position: Int, val newPosition: Int = 0) enum class ChangeType { INSERT, REMOVE, UPDATE @@ -237,6 +247,16 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, val oldSet = oldList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet() val newSet = newList.map { Pair(it.first.applicationInfo.packageName, it.second.second) }.toSet() + oldList.forEachIndexed { index, oldItem -> + if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) { + val newIndex = newList.indexOfFirst { it.first.applicationInfo.packageName == oldItem.first.applicationInfo.packageName && it.second.second == oldItem.second.second } + if (oldItem.first.componentName != newList[newIndex].first.componentName) { + changes.add(Change(ChangeType.UPDATE, index)) + } + + } + } + // Detect insertions newList.forEachIndexed { index, newItem -> if (!oldSet.contains(Pair(newItem.first.applicationInfo.packageName, newItem.second.second))) { @@ -252,14 +272,7 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, } // Detect updates - oldList.forEachIndexed { index, oldItem -> - if (newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) { - val newIndex = newList.indexOfFirst { it.first.applicationInfo.packageName == oldItem.first.applicationInfo.packageName } - if (oldItem.first.componentName != newList[newIndex].first.componentName) { - changes.add(Change(ChangeType.UPDATE, index)) - } - } - } + changes.addAll(removalChanges.reversed()) @@ -296,4 +309,10 @@ class AppMenuActivity : AppCompatActivity(), AppMenuAdapter.OnItemClickListener, adapter.notifyItemChanged(position) } + fun moveItem(position: Int, newPosition: Int) { + Log.d("Movestatus","MOVED") + adapter.moveApp(position, newPosition) + adapter.notifyItemMoved(position, newPosition) + } + } diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index 1363ad7..612b632 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -114,12 +114,6 @@ class AppMenuAdapter( holder.textView.text = sharedPreferenceManager.getAppName(activity, app.first.applicationInfo.packageName,app.second.second, appInfo.loadLabel(holder.itemView.context.packageManager)) holder.editView.findViewById(R.id.app_name_edit).setText(holder.textView.text) holder.textView.visibility = View.VISIBLE - holder.editView.findViewById(R.id.reset).setOnClickListener { - val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - imm.hideSoftInputFromWindow(holder.editView.windowToken, 0) - sharedPreferenceManager.resetAppName(activity, app.first.applicationInfo.packageName, app.second.second) - activity.updateItem(position, app) - } } override fun getItemCount(): Int { @@ -137,4 +131,10 @@ class AppMenuAdapter( fun updateApp(position: Int, app: Pair>) { apps[position] = app } + + fun moveApp(position: Int, newPosition: Int) { + val app = apps.removeAt(position) + apps.add(newPosition, app) + + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_app_menu.xml b/app/src/main/res/layout/activity_app_menu.xml index 078e8c8..f4667d0 100644 --- a/app/src/main/res/layout/activity_app_menu.xml +++ b/app/src/main/res/layout/activity_app_menu.xml @@ -36,7 +36,8 @@ android:padding="0dp" android:requiresFadingEdge="vertical" android:scrollbars="none" - app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"> + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:stackFromEnd="true">