From a5cc0996053d5fe39dce332719b21ba1e708165b Mon Sep 17 00:00:00 2001 From: Alraj Kabeer Date: Wed, 4 Dec 2024 16:35:17 +0530 Subject: [PATCH] show Launch activity label instead of app name --- .../eu/ottop/yamlauncher/AppActionMenu.kt | 29 +++++++++---------- .../eu/ottop/yamlauncher/AppMenuAdapter.kt | 26 +++++++---------- .../java/eu/ottop/yamlauncher/MainActivity.kt | 28 +++++++++++++----- .../settings/GestureAppsAdapter.kt | 6 ++-- .../settings/GestureAppsFragment.kt | 10 +++---- .../yamlauncher/settings/HiddenAppsAdapter.kt | 5 ++-- .../settings/HiddenAppsFragment.kt | 10 +++---- .../settings/SharedPreferenceManager.kt | 28 +++++++++--------- .../eu/ottop/yamlauncher/utils/AppUtils.kt | 22 ++++++-------- 9 files changed, 83 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt index fbd7f68..3e13e8b 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppActionMenu.kt @@ -33,10 +33,9 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act textView: TextView, editLayout: LinearLayout, actionMenu: View, - appInfo: ApplicationInfo, + appActivity: LauncherActivityInfo, userHandle: UserHandle, - workProfile: Int, - appActivity: LauncherActivityInfo? + workProfile: Int ){ @@ -52,32 +51,32 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act } ViewCompat.addAccessibilityAction(textView, activity.getString(R.string.accessibility_uninstall)) { _, _ -> - uninstallApp(appInfo, userHandle) + uninstallApp(appActivity.applicationInfo, userHandle) true } actionMenu.findViewById(R.id.uninstall).setOnClickListener { - uninstallApp(appInfo, userHandle) + uninstallApp(appActivity.applicationInfo, userHandle) animations.fadeViewOut(actionMenu) textView.visibility = View.VISIBLE } ViewCompat.addAccessibilityAction(textView, activity.getString(R.string.accessibility_rename)) { _, _ -> - renameApp(textView, editLayout, actionMenu, appActivity, appInfo, userHandle, workProfile) + renameApp(textView, editLayout, actionMenu, appActivity, userHandle, workProfile) true } actionMenu.findViewById(R.id.rename).setOnClickListener { - renameApp(textView, editLayout, actionMenu, appActivity, appInfo, userHandle, workProfile) + renameApp(textView, editLayout, actionMenu, appActivity, userHandle, workProfile) } ViewCompat.addAccessibilityAction(textView, activity.getString(R.string.accessibility_hide)) { _, _ -> - hideApp(editLayout, textView, actionMenu, appInfo, workProfile) + hideApp(editLayout, textView, actionMenu, appActivity, workProfile) true } actionMenu.findViewById(R.id.hide).setOnClickListener { - hideApp(editLayout, textView, actionMenu, appInfo, workProfile) + hideApp(editLayout, textView, actionMenu, appActivity, workProfile) } actionMenu.findViewById(R.id.close).setOnClickListener { @@ -109,7 +108,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act activity.returnAllowed = false } - private fun renameApp(textView: TextView, editLayout: LinearLayout, actionMenu: View, appActivity: LauncherActivityInfo?, appInfo: ApplicationInfo, userHandle: UserHandle, workProfile: Int) { + private fun renameApp(textView: TextView, editLayout: LinearLayout, actionMenu: View, appActivity: LauncherActivityInfo, userHandle: UserHandle, workProfile: Int) { activity.disableAppMenuScroll() textView.visibility = View.INVISIBLE animations.fadeViewIn(editLayout) @@ -117,7 +116,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act val editText = editLayout.findViewById(R.id.appNameEdit) val resetButton = editLayout.findViewById(R.id.reset) - val app = Triple(appActivity!!, userHandle, workProfile) + val app = Triple(appActivity, userHandle, workProfile) val searchEnabled = sharedPreferenceManager.isSearchEnabled() @@ -166,7 +165,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(editText.windowToken, 0) sharedPreferenceManager.setAppName( - appInfo.packageName, + app.first.componentName.flattenToString(), workProfile, editText.text.toString() ) @@ -187,7 +186,7 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(editLayout.windowToken, 0) sharedPreferenceManager.resetAppName( - app.first.applicationInfo.packageName, + app.first.componentName.flattenToString(), app.third ) @@ -197,12 +196,12 @@ class AppActionMenu(private val activity: MainActivity, private val binding: Act } } - private fun hideApp(editLayout: LinearLayout, textView: TextView, actionMenu: View, appInfo: ApplicationInfo, workProfile: Int) { + private fun hideApp(editLayout: LinearLayout, textView: TextView, actionMenu: View, appActivity: LauncherActivityInfo, workProfile: Int) { editLayout.visibility = View.GONE textView.visibility = View.GONE actionMenu.visibility = View.GONE activity.lifecycleScope.launch { - sharedPreferenceManager.setAppHidden(appInfo.packageName, workProfile, true) + sharedPreferenceManager.setAppHidden(appActivity.componentName.flattenToString(), workProfile, true) activity.refreshAppMenu() } } diff --git a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt index 222d8b3..f35a4f3 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/AppMenuAdapter.kt @@ -131,25 +131,23 @@ class AppMenuAdapter( holder.textView.setTextColor(sharedPreferenceManager.getTextColor()) // Update the application information (allows updating apps to work) - val appInfo = appUtils.getAppInfo( + val isAppInstalled = appUtils.getAppInfo( app.first.applicationInfo.packageName, app.third - ) + ) != null // Set app name on the menu. If the app has been uninstalled, replace it with "Removing" until the app menu updates. - val appLabel: CharSequence = appInfo?.let { activity.packageManager.getApplicationLabel(it) } ?: activity.getString(R.string.removing) - - if (appInfo != null) { + if (isAppInstalled) { holder.textView.text = sharedPreferenceManager.getAppName( - appInfo.packageName, + app.first.componentName.flattenToString(), app.third, - appLabel + app.first.label ) holder.editText.setText(holder.textView.text) // Remove the uninstall icon for system apps - if (appInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) { + if (app.first.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) { holder.actionMenuLayout.findViewById(R.id.uninstall).visibility = View.GONE } else { @@ -157,22 +155,20 @@ class AppMenuAdapter( View.VISIBLE } } - else {holder.textView.text = appLabel} + else { + holder.textView.text = activity.getString(R.string.removing) + } holder.textView.visibility = View.VISIBLE - if (appInfo != null) { - - val appActivity = launcherApps.getActivityList(appInfo.packageName, app.second).firstOrNull() - + if (isAppInstalled) { appActionMenu.setActionListeners( holder.textView, holder.editView, holder.actionMenuLayout, - appInfo, + app.first, app.second, app.third, - appActivity ) } ViewCompat.addAccessibilityAction(holder.textView, activity.getString(R.string.close_app_menu)) { _, _ -> diff --git a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt index 9500eab..53e9513 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/MainActivity.kt @@ -4,6 +4,7 @@ import android.Manifest import android.annotation.SuppressLint import android.app.SearchManager import android.content.ActivityNotFoundException +import android.content.ComponentName import android.content.ContentResolver import android.content.Context import android.content.Intent @@ -375,7 +376,20 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private fun setShortcutListeners(textView: TextView, savedView: List?) { textView.setOnClickListener { if (savedView != null && canLaunchShortcut) { - appUtils.launchApp(savedView[0], launcherApps.profiles[savedView[1].toInt()]) + val componentName = if (savedView[0].contains("/")) { + val (packageName, className) = savedView[0].split("/") + ComponentName(packageName, className) + } else { + val userHandle = launcherApps.profiles[savedView[1].toInt()] + val mainActivity = launcherApps.getActivityList(savedView[0], userHandle).firstOrNull() + if (mainActivity != null) { + mainActivity.componentName + } else { + Toast.makeText(this, this.getString(R.string.launch_error), Toast.LENGTH_SHORT).show() + return@setOnClickListener + } + } + appUtils.launchApp(componentName, launcherApps.profiles[savedView[1].toInt()]) } } } @@ -993,9 +1007,9 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh updatedApps.forEach { val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( - it.first.applicationInfo.packageName, + it.first.componentName.flattenToString(), it.third, - packageManager.getApplicationLabel(it.first.applicationInfo) + it.first.label ).toString()) if (cleanItemText != null) { if ( @@ -1012,7 +1026,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh private suspend fun applySearchFilter(newFilteredApps: MutableList>) { if (sharedPreferenceManager.isAutoLaunchEnabled() && menuView.displayedChild == 0 && appAdapter?.shortcutTextView == null && newFilteredApps.size == 1) { - appUtils.launchApp(newFilteredApps[0].first.applicationInfo.packageName, newFilteredApps[0].second) + appUtils.launchApp(newFilteredApps[0].first.componentName, newFilteredApps[0].second) } else if (!listsEqual(installedApps, newFilteredApps)) { updateMenu(newFilteredApps) @@ -1072,7 +1086,7 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh } override fun onItemClick(appInfo: LauncherActivityInfo, userHandle: UserHandle) { - appUtils.launchApp(appInfo.applicationInfo.packageName, userHandle) + appUtils.launchApp(appInfo.componentName, userHandle) } override fun onShortcut( @@ -1096,12 +1110,12 @@ class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceCh shortcutView.text = textView.text.toString() shortcutView.setOnClickListener { - appUtils.launchApp(appInfo.applicationInfo.packageName, userHandle) + appUtils.launchApp(appInfo.componentName, userHandle) } sharedPreferenceManager.setShortcut( shortcutIndex, shortcutView.text, - appInfo.applicationInfo.packageName, + appInfo.componentName.flattenToString(), userProfile ) uiUtils.setDrawables(shortcutView, sharedPreferenceManager.getShortcutAlignment()) diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsAdapter.kt index 2b3c876..e04dee8 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsAdapter.kt @@ -67,12 +67,10 @@ class GestureAppsAdapter( uiUtils.setItemSpacing(holder.textView) // Does not need to be specially updated since it's in a separate activity and thus reloads when opened again - val appInfo = app.first.activityInfo.applicationInfo - holder.textView.text = sharedPreferenceManager.getAppName( - app.first.applicationInfo.packageName, + app.first.componentName.flattenToString(), app.third, - context.packageManager.getApplicationLabel(appInfo) + app.first.label ) holder.textView.visibility = View.VISIBLE diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsFragment.kt index 0facebe..c1cef72 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/GestureAppsFragment.kt @@ -122,9 +122,9 @@ class GestureAppsFragment(private val direction: String) : Fragment(), } updatedApps.forEach { val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( - it.first.applicationInfo.packageName, + it.first.componentName.flattenToString(), it.third, - requireContext().packageManager.getApplicationLabel(it.first.applicationInfo) + it.first.label ).toString()) if (cleanItemText != null) { if ( @@ -159,7 +159,7 @@ class GestureAppsFragment(private val direction: String) : Fragment(), private fun performConfirmedAction(appInfo: LauncherActivityInfo, appName: String, profile: Int) { sharedPreferenceManager.setGestures( - direction, "$appName§splitter§${appInfo.applicationInfo.packageName}§splitter§$profile" + direction, "$appName§splitter§${appInfo.componentName.flattenToString()}§splitter§$profile" ) requireActivity().supportFragmentManager.popBackStack() } @@ -167,9 +167,9 @@ class GestureAppsFragment(private val direction: String) : Fragment(), override fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) { showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName( - appInfo.applicationInfo.packageName, + appInfo.componentName.flattenToString(), profile, - requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo) + appInfo.label ).toString(), profile) } diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsAdapter.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsAdapter.kt index 7c4b4e8..9e0adc9 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsAdapter.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsAdapter.kt @@ -68,11 +68,10 @@ class HiddenAppsAdapter( uiUtils.setItemSpacing(holder.textView) // Separate activity from Main so does not need special update - val appInfo = app.first.activityInfo.applicationInfo holder.textView.text = sharedPreferenceManager.getAppName( - app.first.applicationInfo.packageName, + app.first.componentName.flattenToString(), app.third, - context.packageManager.getApplicationLabel(appInfo) + app.first.label ) holder.textView.visibility = View.VISIBLE diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsFragment.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsFragment.kt index ecd6860..31d885a 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsFragment.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/HiddenAppsFragment.kt @@ -122,9 +122,9 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener, Ti } updatedApps.forEach { val cleanItemText = stringUtils.cleanString(sharedPreferenceManager.getAppName( - it.first.applicationInfo.packageName, + it.first.componentName.flattenToString(), it.third, - requireContext().packageManager.getApplicationLabel(it.first.applicationInfo) + it.first.label ).toString()) if (cleanItemText != null) { if ( @@ -158,15 +158,15 @@ class HiddenAppsFragment : Fragment(), HiddenAppsAdapter.OnItemClickListener, Ti } private suspend fun performConfirmedAction(appInfo: LauncherActivityInfo, profile: Int) { - sharedPreferenceManager.setAppVisible(appInfo.applicationInfo.packageName, profile) + sharedPreferenceManager.setAppVisible(appInfo.componentName.flattenToString(), profile) adapter?.updateApps(appUtils.getHiddenApps()) } override fun onItemClick(appInfo: LauncherActivityInfo, profile: Int) { showConfirmationDialog(appInfo, sharedPreferenceManager.getAppName( - appInfo.applicationInfo.packageName, + appInfo.componentName.flattenToString(), profile, - requireContext().packageManager.getApplicationLabel(appInfo.applicationInfo) + appInfo.label ).toString(), profile) } diff --git a/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt b/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt index faa0b73..77c3862 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/settings/SharedPreferenceManager.kt @@ -88,9 +88,9 @@ class SharedPreferenceManager (private val context: Context) { return preferences.getString("dateSize", "medium") } - fun setShortcut(index: Int, text: CharSequence, packageName: String, profile: Int, isContact: Boolean = false) { + fun setShortcut(index: Int, text: CharSequence, componentName: String, profile: Int, isContact: Boolean = false) { val editor = preferences.edit() - editor.putString("shortcut${index}", "$packageName§splitter§$profile§splitter§${text}§splitter§${isContact}") + editor.putString("shortcut${index}", "$componentName§splitter§$profile§splitter§${text}§splitter§${isContact}") editor.apply() } @@ -245,36 +245,36 @@ class SharedPreferenceManager (private val context: Context) { } // Hidden Apps - fun setAppHidden(packageName: String, profile: Int, hidden: Boolean) { + fun setAppHidden(componentName: String, profile: Int, hidden: Boolean) { val editor = preferences.edit() - editor.putBoolean("hidden$packageName-$profile", hidden) + editor.putBoolean("hidden$componentName-$profile", hidden) editor.apply() } - fun isAppHidden(packageName: String, profile: Int): Boolean { - return preferences.getBoolean("hidden$packageName-$profile", false) // Default to false (visible) + fun isAppHidden(componentName: String, profile: Int): Boolean { + return preferences.getBoolean("hidden$componentName-$profile", false) // Default to false (visible) } - fun setAppVisible(packageName: String, profile: Int) { + fun setAppVisible(componentName: String, profile: Int) { val editor = preferences.edit() - editor.remove("hidden$packageName-$profile") + editor.remove("hidden$componentName-$profile") editor.apply() } //Renaming apps - fun setAppName(packageName: String, profile: Int, newName: String) { + fun setAppName(componentName: String, profile: Int, newName: String) { val editor = preferences.edit() - editor.putString("name$packageName-$profile", newName) + editor.putString("name$componentName-$profile", newName) editor.apply() } - fun getAppName(packageName: String, profile: Int, appName: CharSequence): CharSequence? { - return preferences.getString("name$packageName-$profile", appName.toString()) + fun getAppName(componentName: String, profile: Int, appName: CharSequence): CharSequence? { + return preferences.getString("name$componentName-$profile", appName.toString()) } - fun resetAppName(packageName: String, profile: Int) { + fun resetAppName(componentName: String, profile: Int) { val editor = preferences.edit() - editor.remove("name$packageName-$profile") + editor.remove("name$componentName-$profile") editor.apply() } diff --git a/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt b/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt index 341f090..2248d9f 100644 --- a/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt +++ b/app/src/main/java/eu/ottop/yamlauncher/utils/AppUtils.kt @@ -1,5 +1,6 @@ package eu.ottop.yamlauncher.utils +import android.content.ComponentName import android.content.Context import android.content.pm.ApplicationInfo import android.content.pm.LauncherActivityInfo @@ -22,7 +23,7 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA for (i in launcherApps.profiles.indices) { // Check apps on both, normal and work profiles launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app -> if (!sharedPreferenceManager.isAppHidden( // Only include the app if it isn't set as hidden - app.applicationInfo.packageName, + app.componentName.flattenToString(), i ) && app.applicationInfo.packageName != context.applicationInfo.packageName // Hide the launcher itself ) { @@ -34,9 +35,9 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA // Sort apps by name sortedApps = allApps.sortedBy { sharedPreferenceManager.getAppName( - it.first.applicationInfo.packageName, + it.first.componentName.flattenToString(), it.third, - context.packageManager.getApplicationLabel(it.first.applicationInfo) + it.first.label ).toString().lowercase() } } @@ -51,7 +52,7 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA withContext(Dispatchers.Default) { for (i in launcherApps.profiles.indices) { launcherApps.getActivityList(null, launcherApps.profiles[i]).forEach { app -> - if (sharedPreferenceManager.isAppHidden(app.applicationInfo.packageName, i)) { + if (sharedPreferenceManager.isAppHidden(app.componentName.flattenToString(), i)) { allApps.add(Triple(app, launcherApps.profiles[i], i)) } } @@ -60,9 +61,9 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA //Sort apps by name sortedApps = allApps.sortedBy { sharedPreferenceManager.getAppName( - it.first.applicationInfo.packageName, + it.first.componentName.flattenToString(), it.third, - context.packageManager.getApplicationLabel(it.first.applicationInfo) + it.first.label ).toString().lowercase() } } @@ -80,12 +81,7 @@ class AppUtils(private val context: Context, private val launcherApps: LauncherA } } - fun launchApp(packageName: String, userHandle: UserHandle) { - val mainActivity = launcherApps.getActivityList(packageName, userHandle).firstOrNull() - if (mainActivity != null) { - launcherApps.startMainActivity(mainActivity.componentName, userHandle, null, null) - } else { - Toast.makeText(context, context.getString(R.string.launch_error), Toast.LENGTH_SHORT).show() - } + fun launchApp(componentName: ComponentName, userHandle: UserHandle) { + launcherApps.startMainActivity(componentName, userHandle, null, null) } } \ No newline at end of file