Skip to content

Commit

Permalink
Fix issue tanujnotes#304
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Y-trie committed Nov 15, 2023
1 parent a602e7f commit ecfa08a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 59 deletions.
5 changes: 2 additions & 3 deletions app/src/main/java/app/olauncher/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
78 changes: 22 additions & 56 deletions app/src/main/java/app/olauncher/helper/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppModel> {
suspend fun getAppsList(context: Context, prefs: Prefs, includeRegularApps: Boolean = true, includeHiddenApps: Boolean = false): MutableList<AppModel> {
return withContext(Dispatchers.IO) {
val appList: MutableList<AppModel> = mutableListOf()

Expand All @@ -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() }
Expand All @@ -115,39 +114,6 @@ suspend fun getAppsList(context: Context, prefs: Prefs, includeHiddenApps: Boole
}
}

suspend fun getHiddenAppsList(context: Context, prefs: Prefs): MutableList<AppModel> {
return withContext(Dispatchers.IO) {
val pm = context.packageManager
if (!prefs.hiddenAppsUpdated) upgradeHiddenApps(prefs)

val hiddenAppsSet = prefs.hiddenApps
val appList: MutableList<AppModel> = 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) {
Expand Down

0 comments on commit ecfa08a

Please sign in to comment.