Skip to content

Commit

Permalink
feat: AND/OR flag for apps filter
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 0c29c37 commit 8a47975
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 36 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/app/simple/inure/constants/SortConstant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ object SortConstant {
const val ASCENDING = "ascending"
const val DESCENDING = "descending"

const val FILTER_STYLE_AND = "filter_style_and"
const val FILTER_STYLE_OR = "filter_style_or"

const val DISABLED = 1 shl 1
const val ENABLED = 1 shl 2
const val APK = 1 shl 3
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/app/simple/inure/dialogs/apps/AppsSort.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import app.simple.inure.extensions.fragments.ScopedBottomSheetFragment
import app.simple.inure.preferences.AppsPreferences
import app.simple.inure.util.FlagUtils
import app.simple.inure.util.Sort
import com.google.android.material.button.MaterialButtonToggleGroup
import com.google.android.material.chip.ChipGroup

class AppsSort : ScopedBottomSheetFragment() {
Expand All @@ -21,6 +22,7 @@ class AppsSort : ScopedBottomSheetFragment() {
private lateinit var applicationTypeChipGroup: ChipGroup
private lateinit var categoryChipGroup: ChipGroup
private lateinit var filterChipGroup: ChipGroup
private lateinit var logicalOperatorGroup: MaterialButtonToggleGroup

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.dialog_sort_apps, container, false)
Expand All @@ -30,6 +32,7 @@ class AppsSort : ScopedBottomSheetFragment() {
filterChipGroup = view.findViewById(R.id.filter_chip_group)
categoryChipGroup = view.findViewById(R.id.category_chip_group)
applicationTypeChipGroup = view.findViewById(R.id.application_type_chip_group)
logicalOperatorGroup = view.findViewById(R.id.logical_operator_group)

return view
}
Expand All @@ -47,6 +50,7 @@ class AppsSort : ScopedBottomSheetFragment() {

setAppsCategory()
setAppsFilter()
setLogicalOperator()

when (AppsPreferences.getSortStyle()) {
Sort.NAME -> {
Expand Down Expand Up @@ -276,6 +280,22 @@ class AppsSort : ScopedBottomSheetFragment() {

AppsPreferences.setAppsCategory(categoryFlags)
}

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

when {
checkedButtonIds.contains(R.id.and) -> {
AppsPreferences.setFilterStyle(SortConstant.FILTER_STYLE_AND)
}
checkedButtonIds.contains(R.id.or) -> {
AppsPreferences.setFilterStyle(SortConstant.FILTER_STYLE_OR)
}
else -> {
AppsPreferences.setFilterStyle(SortConstant.FILTER_STYLE_OR)
}
}
}
}

private fun setAppsCategory() {
Expand Down Expand Up @@ -360,6 +380,14 @@ class AppsSort : ScopedBottomSheetFragment() {
}
}

private fun setLogicalOperator() {
if (AppsPreferences.isFilterStyleAnd()) {
logicalOperatorGroup.check(R.id.and)
} else {
logicalOperatorGroup.check(R.id.or)
}
}

companion object {
fun newInstance(): AppsSort {
val args = Bundle()
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/app/simple/inure/preferences/AppsPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ object AppsPreferences {
const val COMBINE_FILTER = "combine_filter"
const val APPS_CATEGORY = "apps_category_flags"
const val INFO_CUSTOM_FILTER = "info_custom_filter"
const val FILTER_STYLE = "filter_style"

// ---------------------------------------------------------------------------------------------------------- //

Expand Down Expand Up @@ -82,4 +83,25 @@ object AppsPreferences {
fun getInfoCustomFilter(): Int {
return SharedPreferences.getSharedPreferences().getInt(INFO_CUSTOM_FILTER, SortConstant.INFO_DEFAULT)
}

// ---------------------------------------------------------------------------------------------------------- //

fun setFilterStyle(value: String) {
SharedPreferences.getSharedPreferences().edit().putString(FILTER_STYLE, value).apply()
}

fun getFilterStyle(): String {
return SharedPreferences.getSharedPreferences()
.getString(FILTER_STYLE, SortConstant.FILTER_STYLE_OR)!!
}

fun isFilterStyleAnd(): Boolean {
return SharedPreferences.getSharedPreferences()
.getString(FILTER_STYLE, SortConstant.FILTER_STYLE_OR) == SortConstant.FILTER_STYLE_AND
}

fun isFilterStyleOr(): Boolean {
return SharedPreferences.getSharedPreferences()
.getString(FILTER_STYLE, SortConstant.FILTER_STYLE_OR) == SortConstant.FILTER_STYLE_OR
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/app/simple/inure/ui/panels/Apps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import app.simple.inure.adapters.ui.AdapterApps
import app.simple.inure.constants.BottomMenuConstants
import app.simple.inure.constants.BundleConstants
import app.simple.inure.decorations.overscroll.CustomVerticalRecyclerView
import app.simple.inure.dialogs.app.AppMenu
import app.simple.inure.dialogs.app.AppMenu.Companion.showAppMenu
import app.simple.inure.dialogs.apps.AppsMenu.Companion.newAppsMenuInstance
import app.simple.inure.dialogs.apps.AppsSort.Companion.showAppsSortDialog
import app.simple.inure.dialogs.miscellaneous.GenerateAppData.Companion.showGeneratedDataTypeSelector
Expand Down Expand Up @@ -74,8 +74,7 @@ class Apps : ScopedFragment() {
}

override fun onAppLongPressed(packageInfo: PackageInfo, icon: ImageView) {
AppMenu.newInstance(packageInfo)
.show(childFragmentManager, "apps_menu")
childFragmentManager.showAppMenu(packageInfo)
}
})

Expand Down Expand Up @@ -159,6 +158,7 @@ class Apps : ScopedFragment() {
AppsPreferences.APPS_CATEGORY,
AppsPreferences.APPS_FILTER,
AppsPreferences.COMBINE_FILTER,
AppsPreferences.FILTER_STYLE,
AppsPreferences.APPS_TYPE -> {
appsViewModel.loadAppData()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.isAppStopped
import app.simple.inure.apk.utils.PackageUtils.isInstalled
import app.simple.inure.apk.utils.PackageUtils.safeApplicationInfo
import app.simple.inure.constants.SortConstant
import app.simple.inure.extensions.viewmodels.DataGeneratorViewModel
Expand Down Expand Up @@ -91,6 +92,7 @@ class AppsViewModel(application: Application) : DataGeneratorViewModel(applicati
}
}
}

return categoryList
}

Expand All @@ -111,39 +113,25 @@ class AppsViewModel(application: Application) : DataGeneratorViewModel(applicati
}

private fun shouldAddApp(packageInfo: PackageInfo): Boolean {
return when {
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.UNINSTALLED) -> {
packageInfo.safeApplicationInfo.flags and ApplicationInfo.FLAG_INSTALLED == 0
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.SPLIT) -> {
packageInfo.safeApplicationInfo.splitSourceDirs?.isNotEmpty() == true
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.DISABLED) -> {
!packageInfo.safeApplicationInfo.enabled &&
packageInfo.safeApplicationInfo.flags and ApplicationInfo.FLAG_INSTALLED != 0
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.APK) -> {
packageInfo.safeApplicationInfo.splitSourceDirs.isNullOrEmpty()
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.ENABLED) -> {
packageInfo.safeApplicationInfo.enabled &&
packageInfo.safeApplicationInfo.flags and ApplicationInfo.FLAG_INSTALLED != 0
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.FOSS) -> {
FOSSParser.isPackageFOSS(packageInfo)
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.LARGE_HEAP) -> {
packageInfo.isAppLargeHeap()
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.LAUNCHABLE) -> {
PackageUtils.isAppLaunchable(applicationContext(), packageInfo.packageName)
}
FlagUtils.isFlagSet(AppsPreferences.getAppsFilter(), SortConstant.STOPPED) -> {
packageInfo.isAppStopped()
}
else -> {
false
}
val filter = AppsPreferences.getAppsFilter()
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()
)

return if (AppsPreferences.isFilterStyleAnd()) {
conditions.all { it }
} else {
conditions.any { it }
}
}

Expand Down
28 changes: 28 additions & 0 deletions app/src/main/res/layout/dialog_sort_apps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,34 @@
app:appFontStyle="bold"
app:textColorStyle="primary" />

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/logical_operator_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
app:selectionRequired="true"
app:singleSelection="true">

<app.simple.inure.decorations.views.Button
android:id="@+id/and"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/and" />

<app.simple.inure.decorations.views.Button
android:id="@+id/or"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/or" />

</com.google.android.material.button.MaterialButtonToggleGroup>

<com.google.android.material.chip.ChipGroup
android:id="@+id/filter_chip_group"
android:layout_width="match_parent"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/non_translatable_string.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,9 @@
<string name="foss" translatable="false">FOSS</string>
<string name="aosp" translatable="false">AOSP</string>
<string name="click_to_participate" translatable="false">Click to participate in the translation on Crowdin.</string>
<string name="and" translatable="false">AND</string>
<string name="or" translatable="false">OR</string>
<string name="not" translatable="false">NOT</string>
<string name="xor" translatable="false">XOR</string>

</resources>

0 comments on commit 8a47975

Please sign in to comment.