From ecfa08a4750f61dee75a1a109c6b3bb5e6798cdb Mon Sep 17 00:00:00 2001 From: Y-trie <150873944+Y-trie@users.noreply.github.com> Date: Tue, 14 Nov 2023 17:59:31 -0800 Subject: [PATCH] Fix issue #304 Removed `getHiddenAppsList` and refactored `getAppsList` in Utils to serve the function both. `getAppsList` can now be used to show only hidden apps, only non-hidden applications, or to show both hidden and non-hidden applications. --- .../main/java/app/olauncher/MainViewModel.kt | 5 +- .../main/java/app/olauncher/helper/Utils.kt | 78 ++++++------------- 2 files changed, 24 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/app/olauncher/MainViewModel.kt b/app/src/main/java/app/olauncher/MainViewModel.kt index d02edacf..0b7449bb 100644 --- a/app/src/main/java/app/olauncher/MainViewModel.kt +++ b/app/src/main/java/app/olauncher/MainViewModel.kt @@ -20,7 +20,6 @@ import app.olauncher.data.Prefs import app.olauncher.helper.SingleLiveEvent import app.olauncher.helper.WallpaperWorker import app.olauncher.helper.getAppsList -import app.olauncher.helper.getHiddenAppsList import app.olauncher.helper.isOlauncherDefault import app.olauncher.helper.showToast import kotlinx.coroutines.launch @@ -196,13 +195,13 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun getAppList(includeHiddenApps: Boolean = false) { viewModelScope.launch { - appList.value = getAppsList(appContext, prefs, includeHiddenApps) + appList.value = getAppsList(appContext, prefs, includeRegularApps = true, includeHiddenApps) } } fun getHiddenApps() { viewModelScope.launch { - hiddenApps.value = getHiddenAppsList(appContext, prefs) + hiddenApps.value = getAppsList(appContext, prefs, includeRegularApps = false, includeHiddenApps = true) } } diff --git a/app/src/main/java/app/olauncher/helper/Utils.kt b/app/src/main/java/app/olauncher/helper/Utils.kt index 9ee2acdb..cb254198 100644 --- a/app/src/main/java/app/olauncher/helper/Utils.kt +++ b/app/src/main/java/app/olauncher/helper/Utils.kt @@ -65,7 +65,7 @@ fun Context.showToast(stringResource: Int, duration: Int = Toast.LENGTH_SHORT) { Toast.makeText(this, getString(stringResource), duration).show() } -suspend fun getAppsList(context: Context, prefs: Prefs, includeHiddenApps: Boolean = false): MutableList { +suspend fun getAppsList(context: Context, prefs: Prefs, includeRegularApps: Boolean = true, includeHiddenApps: Boolean = false): MutableList { return withContext(Dispatchers.IO) { val appList: MutableList = mutableListOf() @@ -81,29 +81,28 @@ suspend fun getAppsList(context: Context, prefs: Prefs, includeHiddenApps: Boole for (app in launcherApps.getActivityList(null, profile)) { val appLabelShown = prefs.getAppRenameLabel(app.applicationInfo.packageName).ifBlank { app.label.toString() } - - if (includeHiddenApps && app.applicationInfo.packageName != BuildConfig.APPLICATION_ID) - appList.add( - AppModel( - appLabelShown, - collator.getCollationKey(app.label.toString()), - app.applicationInfo.packageName, - app.componentName.className, - profile - ) - ) - else if (!hiddenApps.contains(app.applicationInfo.packageName + "|" + profile.toString()) - && app.applicationInfo.packageName != BuildConfig.APPLICATION_ID + val appModel = AppModel( + appLabelShown, + collator.getCollationKey(app.label.toString()), + app.applicationInfo.packageName, + app.componentName.className, + profile ) - appList.add( - AppModel( - appLabelShown, - collator.getCollationKey(app.label.toString()), - app.applicationInfo.packageName, - app.componentName.className, - profile - ) - ) + + // if the current app is not OLauncher + if (app.applicationInfo.packageName != BuildConfig.APPLICATION_ID) { + // is this a hidden app? + if (hiddenApps.contains(app.applicationInfo.packageName + "|" + profile.toString())) { + if (includeHiddenApps) { + appList.add(appModel) + } + } else { + // this is a regular app + if (includeRegularApps) { + appList.add(appModel) + } + } + } } } appList.sortBy { it.appLabel.lowercase() } @@ -115,39 +114,6 @@ suspend fun getAppsList(context: Context, prefs: Prefs, includeHiddenApps: Boole } } -suspend fun getHiddenAppsList(context: Context, prefs: Prefs): MutableList { - return withContext(Dispatchers.IO) { - val pm = context.packageManager - if (!prefs.hiddenAppsUpdated) upgradeHiddenApps(prefs) - - val hiddenAppsSet = prefs.hiddenApps - val appList: MutableList = mutableListOf() - if (hiddenAppsSet.isEmpty()) return@withContext appList - - val userManager = context.getSystemService(Context.USER_SERVICE) as UserManager - val collator = Collator.getInstance() - for (hiddenPackage in hiddenAppsSet) { - try { - val appPackage = hiddenPackage.split("|")[0] - val userString = hiddenPackage.split("|")[1] - var userHandle = android.os.Process.myUserHandle() - for (user in userManager.userProfiles) { - if (user.toString() == userString) userHandle = user - } - - val appInfo = pm.getApplicationInfo(appPackage, 0) - val appLabelShown = prefs.getAppRenameLabel(appPackage).ifBlank { pm.getApplicationLabel(appInfo).toString() } - val appKey = collator.getCollationKey(appLabelShown) - appList.add(AppModel(appLabelShown, appKey, appPackage, null, userHandle)) - } catch (e: Exception) { - e.printStackTrace() - } - } - appList.sortBy { it.appLabel.lowercase() } - appList - } -} - // This is to ensure backward compatibility with older app versions // which did not support multiple user profiles private fun upgradeHiddenApps(prefs: Prefs) {