diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt index a1852e3..eb69186 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppUtils.kt @@ -6,6 +6,7 @@ import android.content.pm.LauncherActivityInfo import android.content.pm.LauncherApps import android.os.UserHandle import androidx.appcompat.app.AppCompatActivity +import java.lang.reflect.InvocationTargetException class AppUtils { @@ -47,9 +48,11 @@ class AppUtils { packageName: String, profile: Int ): ApplicationInfo? { + return try { + launcherApps.getApplicationInfo(packageName, 0, launcherApps.profiles[profile]) + } catch (_: Exception) { + null + } - return launcherApps.getActivityList( - packageName, launcherApps.profiles[profile] - ).firstOrNull()?.applicationInfo } } \ No newline at end of file diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index b116389..ca53f29 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -619,7 +619,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh }, 100) handler.postDelayed({ CoroutineScope(Dispatchers.Default).launch { - + refreshAppMenu() try { withContext(Dispatchers.Main) { @@ -705,30 +705,22 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } suspend fun refreshAppMenu() { - try { - val updatedApps = appUtils.getInstalledApps(this@MainActivity, launcherApps) - val changes = detectChanges(installedApps, updatedApps) - installedApps = updatedApps - withContext(Dispatchers.Main) { - applyChanges(changes, installedApps) - } - } - catch (_: UninitializedPropertyAccessException) { - } - /* try { val updatedApps = appUtils.getInstalledApps(this@MainActivity, launcherApps) println("update running") - withContext(Dispatchers.Main) { - updateMenu(updatedApps) + if (!listsEqual(installedApps, updatedApps)) { + withContext(Dispatchers.Main) { + updateMenu(updatedApps) + } + installedApps = updatedApps } - installedApps = updatedApps } catch (_: UninitializedPropertyAccessException) { - }*/ + } } + private fun closeKeyboard() { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager @@ -802,7 +794,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh searchView.textAlignment = View.TEXT_ALIGNMENT_VIEW_START } "center" -> { - searchView.textAlignment = TEXT_ALIGNMENT_CENTER + searchView.textAlignment = View.TEXT_ALIGNMENT_CENTER } "right" -> { searchView.textAlignment = View.TEXT_ALIGNMENT_VIEW_END @@ -905,91 +897,4 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh false } } - - fun detectChanges(oldList: List>>, newList: List>>): List { - val changes = mutableListOf() - val removalChanges = mutableListOf() - 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() - - // Detect removals - oldList.forEachIndexed { index, oldItem -> - if (!newSet.contains(Pair(oldItem.first.applicationInfo.packageName, oldItem.second.second))) { - removalChanges.add(Change(ChangeType.REMOVE, index)) - } - } - - // Detect insertions - newList.forEachIndexed { index, newItem -> - if (!oldSet.contains(Pair(newItem.first.applicationInfo.packageName, newItem.second.second))) { - changes.add(Change(ChangeType.INSERT, index)) - } - } - - 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)) - } - if (index != newIndex) { - changes.add(Change(ChangeType.MOVE, index)) - } - - } - } - - changes.addAll(removalChanges.reversed()) - - return changes - } - - @SuppressLint("NotifyDataSetChanged") - fun applyChanges(changes: List, updatedApps: List>>) { - changes.forEach { change -> - when (change.type) { - ChangeType.INSERT -> { - insertItem(change.position, updatedApps[change.position]) - } - ChangeType.REMOVE -> { - try { - removeItem(change.position) - } - catch (_: IndexOutOfBoundsException) { - } - } - ChangeType.UPDATE -> { - updateItem(change.position, updatedApps[change.position]) - } - - ChangeType.MOVE -> { - adapter?.updateApps(updatedApps) - adapter?.notifyDataSetChanged() - println("moved") - } - - } - } - } - - private fun insertItem(position: Int, app: Pair>) { - adapter?.addApp(position, app) - adapter?.notifyItemInserted(position) - } - private fun removeItem(position: Int) { - adapter?.removeApp(position) - adapter?.notifyItemRemoved(position) - } - - private fun updateItem(position: Int, app: Pair>) { - adapter?.updateApp(position, app) - adapter?.notifyItemChanged(position) - } - -} - -data class Change(val type: ChangeType, val position: Int, val newPosition: Int = 0) - -enum class ChangeType { - INSERT, REMOVE, UPDATE, MOVE } \ No newline at end of file