Skip to content

Commit

Permalink
feat: AND/OR flag for apps filter #378
Browse files Browse the repository at this point in the history
- Added as a replacement for combine flags
  • Loading branch information
Hamza417 committed Dec 28, 2024
1 parent c5832eb commit fda68e9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 72 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/app/simple/inure/apk/utils/PackageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ object PackageUtils {
.getLaunchIntentForPackage(packageName) != null
}

fun PackageInfo.isAppLaunchable(context: Context): Boolean {
return context.packageManager
.getLaunchIntentForPackage(this.packageName) != null
}

fun ApplicationInfo.isAppLaunchable(context: Context): Boolean {
return context.packageManager
.getLaunchIntentForPackage(this.packageName) != null
}

@Throws(NameNotFoundException::class, NullPointerException::class)
fun PackageInfo.launchThisPackage(context: Context) {
val intent = context.packageManager.getLaunchIntentForPackage(this.packageName)
Expand Down
120 changes: 60 additions & 60 deletions app/src/main/java/app/simple/inure/dialogs/apps/AppsSort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,66 +153,6 @@ class AppsSort : ScopedBottomSheetFragment() {
}
}

filterChipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
var sourceFlags = AppsPreferences.getAppsFilter()

sourceFlags = if (checkedIds.contains(R.id.disabled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.DISABLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.DISABLED)
}

sourceFlags = if (checkedIds.contains(R.id.enabled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.ENABLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.ENABLED)
}

sourceFlags = if (checkedIds.contains(R.id.apk)) {
FlagUtils.setFlag(sourceFlags, SortConstant.APK)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.APK)
}

sourceFlags = if (checkedIds.contains(R.id.split)) {
FlagUtils.setFlag(sourceFlags, SortConstant.SPLIT)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.SPLIT)
}

sourceFlags = if (checkedIds.contains(R.id.uninstalled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.UNINSTALLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.UNINSTALLED)
}

sourceFlags = if (checkedIds.contains(R.id.foss)) {
FlagUtils.setFlag(sourceFlags, SortConstant.FOSS)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.FOSS)
}

sourceFlags = if (checkedIds.contains(R.id.large_heap)) {
FlagUtils.setFlag(sourceFlags, SortConstant.LARGE_HEAP)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.LARGE_HEAP)
}

sourceFlags = if (checkedIds.contains(R.id.launchable)) {
FlagUtils.setFlag(sourceFlags, SortConstant.LAUNCHABLE)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.LAUNCHABLE)
}

sourceFlags = if (checkedIds.contains(R.id.stopped)) {
FlagUtils.setFlag(sourceFlags, SortConstant.STOPPED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.STOPPED)
}

AppsPreferences.setAppsFilter(sourceFlags)
}

categoryChipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
var categoryFlags = SortConstant.ALL_CATEGORIES

Expand Down Expand Up @@ -281,6 +221,66 @@ class AppsSort : ScopedBottomSheetFragment() {
AppsPreferences.setAppsCategory(categoryFlags)
}

filterChipGroup.setOnCheckedStateChangeListener { _, checkedIds ->
var sourceFlags = AppsPreferences.getAppsFilter()

sourceFlags = if (checkedIds.contains(R.id.disabled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.DISABLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.DISABLED)
}

sourceFlags = if (checkedIds.contains(R.id.enabled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.ENABLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.ENABLED)
}

sourceFlags = if (checkedIds.contains(R.id.apk)) {
FlagUtils.setFlag(sourceFlags, SortConstant.APK)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.APK)
}

sourceFlags = if (checkedIds.contains(R.id.split)) {
FlagUtils.setFlag(sourceFlags, SortConstant.SPLIT)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.SPLIT)
}

sourceFlags = if (checkedIds.contains(R.id.uninstalled)) {
FlagUtils.setFlag(sourceFlags, SortConstant.UNINSTALLED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.UNINSTALLED)
}

sourceFlags = if (checkedIds.contains(R.id.foss)) {
FlagUtils.setFlag(sourceFlags, SortConstant.FOSS)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.FOSS)
}

sourceFlags = if (checkedIds.contains(R.id.large_heap)) {
FlagUtils.setFlag(sourceFlags, SortConstant.LARGE_HEAP)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.LARGE_HEAP)
}

sourceFlags = if (checkedIds.contains(R.id.launchable)) {
FlagUtils.setFlag(sourceFlags, SortConstant.LAUNCHABLE)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.LAUNCHABLE)
}

sourceFlags = if (checkedIds.contains(R.id.stopped)) {
FlagUtils.setFlag(sourceFlags, SortConstant.STOPPED)
} else {
FlagUtils.unsetFlag(sourceFlags, SortConstant.STOPPED)
}

AppsPreferences.setAppsFilter(sourceFlags)
}

logicalOperatorGroup.addOnButtonCheckedListener { group, _, _ ->
val checkedButtonIds = group.checkedButtonIds

Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/app/simple/inure/util/ToggleUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app.simple.inure.util

import app.simple.inure.decorations.views.Chip
import com.google.android.material.chip.ChipGroup

object ToggleUtils {

fun ChipGroup.uncheck(id: Int) {
this.findViewById<Chip>(id).isChecked = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import app.simple.inure.apk.parsers.FOSSParser
import app.simple.inure.apk.utils.PackageUtils
import app.simple.inure.apk.utils.PackageUtils.isAppLargeHeap
import app.simple.inure.apk.utils.PackageUtils.isAppLaunchable
import app.simple.inure.apk.utils.PackageUtils.isAppStopped
import app.simple.inure.apk.utils.PackageUtils.isInstalled
import app.simple.inure.apk.utils.PackageUtils.safeApplicationInfo
Expand Down Expand Up @@ -117,21 +117,21 @@ class AppsViewModel(application: Application) : DataGeneratorViewModel(applicati
val appInfo = packageInfo.safeApplicationInfo

val conditions = listOf(
FlagUtils.isFlagSet(filter, SortConstant.UNINSTALLED) && !packageInfo.isInstalled(),
FlagUtils.isFlagSet(filter, SortConstant.SPLIT) && !appInfo.splitSourceDirs.isNullOrEmpty(),
FlagUtils.isFlagSet(filter, SortConstant.DISABLED) && !appInfo.enabled && packageInfo.isInstalled(),
FlagUtils.isFlagSet(filter, SortConstant.APK) && appInfo.splitSourceDirs.isNullOrEmpty(),
FlagUtils.isFlagSet(filter, SortConstant.ENABLED) && appInfo.enabled && packageInfo.isInstalled(),
FlagUtils.isFlagSet(filter, SortConstant.FOSS) && FOSSParser.isPackageFOSS(packageInfo),
FlagUtils.isFlagSet(filter, SortConstant.LARGE_HEAP) && packageInfo.isAppLargeHeap(),
FlagUtils.isFlagSet(filter, SortConstant.LAUNCHABLE) && PackageUtils.isAppLaunchable(applicationContext(), packageInfo.packageName),
FlagUtils.isFlagSet(filter, SortConstant.STOPPED) && packageInfo.isAppStopped()
Pair(FlagUtils.isFlagSet(filter, SortConstant.UNINSTALLED), !packageInfo.isInstalled()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.SPLIT), !appInfo.splitSourceDirs.isNullOrEmpty()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.DISABLED), !appInfo.enabled && packageInfo.isInstalled()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.APK), appInfo.splitSourceDirs.isNullOrEmpty()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.ENABLED), appInfo.enabled && packageInfo.isInstalled()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.FOSS), FOSSParser.isPackageFOSS(packageInfo)),
Pair(FlagUtils.isFlagSet(filter, SortConstant.LARGE_HEAP), packageInfo.isAppLargeHeap()),
Pair(FlagUtils.isFlagSet(filter, SortConstant.LAUNCHABLE), packageInfo.isAppLaunchable(applicationContext())),
Pair(FlagUtils.isFlagSet(filter, SortConstant.STOPPED), packageInfo.isAppStopped())
)

return if (AppsPreferences.isFilterStyleAnd()) {
conditions.all { it }
conditions.filter { it.first }.all { it.second }
} else {
conditions.any { it }
conditions.any { it.first && it.second }
}
}

Expand Down

0 comments on commit fda68e9

Please sign in to comment.