From a8a1d28c413d8a679637afaa589425b0fdf17cf5 Mon Sep 17 00:00:00 2001 From: Md Rasel Hossain <77199897+iamrasel@users.noreply.github.com> Date: Tue, 12 Mar 2024 19:29:08 +0600 Subject: [PATCH] Few more improvements --- .../rasel/lunar/launcher/apps/AppDrawer.kt | 29 +++++++-------- .../rasel/lunar/launcher/apps/AppMenu.kt | 37 +++++++++---------- .../rasel/lunar/launcher/apps/AppsAdapter.kt | 9 +---- .../rasel/lunar/launcher/helpers/UniUtils.kt | 1 - app/src/main/res/layout/app_menu.xml | 2 - 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppDrawer.kt b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppDrawer.kt index 1353c89..ceea36d 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppDrawer.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppDrawer.kt @@ -80,25 +80,28 @@ internal class AppDrawer : Fragment() { @JvmStatic var alphabetList = mutableListOf() @JvmStatic var letterPreview: MaterialTextView? = null + private fun appName(resolver: ResolveInfo): String { + return appNamesPrefs?.getString(resolver.activityInfo.packageName, resolver.loadLabel(packageManager).toString())!! + } + fun listenScroll(letter: String) { packageList.clear() for (resolver in packageInfoList) { - val appName = appNamesPrefs?.getString(resolver.activityInfo.packageName, resolver.loadLabel(packageManager).toString())!! when { letter == "#" -> { - if (numberPattern.matcher(appName.first().uppercase()).matches()) { - packageList.add(Packages(resolver.activityInfo.packageName, appName)) + if (numberPattern.matcher(appName(resolver).first().uppercase()).matches()) { + packageList.add(Packages(resolver.activityInfo.packageName, appName(resolver))) } } alphabetPattern.matcher(letter).matches() -> { - if (appName.first().uppercase() == letter) { - packageList.add(Packages(resolver.activityInfo.packageName, appName)) + if (appName(resolver).first().uppercase() == letter) { + packageList.add(Packages(resolver.activityInfo.packageName, appName(resolver))) } } letter == "⠶" -> { - if (!numberPattern.matcher(appName.first().uppercase()).matches() && - !alphabetPattern.matcher(appName.first().uppercase()).matches()) { - packageList.add(Packages(resolver.activityInfo.packageName, appName)) + if (!numberPattern.matcher(appName(resolver).first().uppercase()).matches() && + !alphabetPattern.matcher(appName(resolver).first().uppercase()).matches()) { + packageList.add(Packages(resolver.activityInfo.packageName, appName(resolver))) } } } @@ -195,7 +198,6 @@ internal class AppDrawer : Fragment() { PackageManager.ResolveInfoFlags.of(0) ) } else { - @Suppress("DEPRECATION") (packageManager?.queryIntentActivities( Intent(Intent.ACTION_MAIN, null).addCategory(Intent.CATEGORY_LAUNCHER), 0)) })?.apply { @@ -206,10 +208,7 @@ internal class AppDrawer : Fragment() { /* add package and app names to the list */ packageList.clear() for (resolver in packageInfoList) { - packageList.add(Packages( - resolver.activityInfo.packageName, appNamesPrefs?.getString(resolver.activityInfo.packageName, - resolver.loadLabel(packageManager).toString())!! - )) + packageList.add(Packages(resolver.activityInfo.packageName, appName(resolver))) } when { @@ -247,9 +246,7 @@ internal class AppDrawer : Fragment() { /* check each app name and add if it matches the search string */ packageList.clear() for (resolver in packageInfoList) { - val appName = appNamesPrefs?.getString(resolver.activityInfo.packageName, resolver.loadLabel(packageManager).toString())!! - - appName.let { + appName(resolver).let { if (normalize(it).contains(searchString)) { packageList.add(Packages(resolver.activityInfo.packageName, it)) } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppMenu.kt b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppMenu.kt index 94f57ef..7d758e2 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppMenu.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppMenu.kt @@ -88,17 +88,19 @@ internal class AppMenu : BottomSheetDialogFragment() { packageManager.getApplicationInfo(packageName, PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong())) } else { - @Suppress("DEPRECATION") packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA) } /* get default app name */ - val resolve = packageManager.resolveActivity(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER).setPackage(packageName), 0) - defAppName = resolve?.loadLabel(packageManager).toString() + defAppName = packageManager.resolveActivity(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER) + .setPackage(packageName), 0)?.loadLabel(packageManager).toString() /* set application name and package name */ - binding.appName.setText(appNamesPrefs?.getString(packageName, defAppName)) - binding.appName.hint = defAppName + binding.appName.apply { + setText(appNamesPrefs?.getString(packageName, defAppName)) + hint = defAppName + + } binding.appPackage.text = packageName /* favorite apps */ favoriteApps() @@ -146,7 +148,7 @@ internal class AppMenu : BottomSheetDialogFragment() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) packageManager.getPackageInfo(savedPackageName!!, PackageManager.PackageInfoFlags.of(0)) else - @Suppress("DEPRECATION") packageManager.getPackageInfo(savedPackageName!!, 0) + packageManager.getPackageInfo(savedPackageName!!, 0) } catch (e: PackageManager.NameNotFoundException) { requireContext().getSharedPreferences(PREFS_FAVORITE_APPS, 0) .edit().remove(KEY_APP_NO_ + position).apply() @@ -176,18 +178,16 @@ internal class AppMenu : BottomSheetDialogFragment() { } } - private fun appName() - { + private fun appName() { binding.appName.setOnFocusChangeListener { _, hasFocus -> - if (hasFocus) { - binding.appName.minWidth = resources.getDimensionPixelOffset(R.dimen.twoSeventySix) - } + if (hasFocus) binding.appName.minWidth = resources.getDimensionPixelOffset(R.dimen.twoSeventySix) else { - val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - imm.hideSoftInputFromWindow(binding.appName.windowToken, 0) + (requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager) + .hideSoftInputFromWindow(binding.appName.windowToken, 0) - binding.appName.minWidth = resources.getDimensionPixelOffset(R.dimen.zero) binding.appName.apply { + minWidth = resources.getDimensionPixelOffset(R.dimen.zero) + if (text!!.isBlank()) setText(defAppName) else setText(text!!.trim()) @@ -200,8 +200,7 @@ internal class AppMenu : BottomSheetDialogFragment() { } binding.appName.setOnKeyListener { _, keyCode, event -> - if (event.action == KeyEvent.ACTION_DOWN) - { + if (event.action == KeyEvent.ACTION_DOWN) { if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_BACK) { binding.appName.clearFocus() return@setOnKeyListener true @@ -227,7 +226,7 @@ internal class AppMenu : BottomSheetDialogFragment() { val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0)) } else { - @Suppress("DEPRECATION") packageManager.getPackageInfo(packageName, 0) + packageManager.getPackageInfo(packageName, 0) } /* show infos */ @@ -259,7 +258,7 @@ internal class AppMenu : BottomSheetDialogFragment() { packageName, PackageManager.PackageInfoFlags.of(PackageManager.GET_ACTIVITIES.toLong()) ) } else { - @Suppress("DEPRECATION") packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES) + packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES) } /* show activity list */ @@ -398,7 +397,7 @@ internal class AppMenu : BottomSheetDialogFragment() { val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS.toLong())) } else { - @Suppress("DEPRECATION") packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS) + packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS) } return if (packageInfo.requestedPermissions.isNotEmpty()) { diff --git a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppsAdapter.kt b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppsAdapter.kt index 736f3be..cf14fb7 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/apps/AppsAdapter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/apps/AppsAdapter.kt @@ -26,7 +26,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.core.view.updatePadding -import androidx.fragment.app.DialogFragment import androidx.fragment.app.FragmentManager import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView @@ -104,10 +103,7 @@ internal class AppsAdapter( /* on long click - open app menu */ setOnLongClickListener { - AppMenu().let { - it.setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialog) - it.show(fragmentManager, item.packageName) - } + AppMenu().show(fragmentManager, item.packageName) true } } @@ -119,8 +115,7 @@ internal class AppsAdapter( /* update app list */ fun updateData(newList: List) { - val diffUtil = AppsDiffUtil(oldList, newList) - val diffUtilResult = DiffUtil.calculateDiff(diffUtil) + val diffUtilResult = DiffUtil.calculateDiff(AppsDiffUtil(oldList, newList)) oldList.clear() oldList.addAll(newList) diff --git a/app/src/main/kotlin/rasel/lunar/launcher/helpers/UniUtils.kt b/app/src/main/kotlin/rasel/lunar/launcher/helpers/UniUtils.kt index 6c399e3..e6d785a 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/helpers/UniUtils.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/helpers/UniUtils.kt @@ -194,7 +194,6 @@ internal class UniUtils { /* favorite apps */ private fun populateFavApps(context: Context, linearLayoutCompat: LinearLayoutCompat) { val prefsFavApps = context.getSharedPreferences(PREFS_FAVORITE_APPS, 0) - val useIconPack = context.getSharedPreferences(PREFS_SETTINGS, 0).getInt(KEY_APPS_LAYOUT, 0) != 0 if (linearLayoutCompat.isVisible || prefsFavApps.all.toString().length < 3) { linearLayoutCompat.visibility = View.GONE } else { diff --git a/app/src/main/res/layout/app_menu.xml b/app/src/main/res/layout/app_menu.xml index b601e51..38c0687 100644 --- a/app/src/main/res/layout/app_menu.xml +++ b/app/src/main/res/layout/app_menu.xml @@ -26,9 +26,7 @@ android:minWidth="@dimen/zero" android:gravity="center" android:padding="@dimen/eight" - android:inputType="textNoSuggestions" android:textAppearance="@style/TextAppearance.Material3.TitleLarge" /> -