diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppRunningStateComparator.java b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppRunningStateComparator.java new file mode 100644 index 000000000..00a7b096f --- /dev/null +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppRunningStateComparator.java @@ -0,0 +1,38 @@ +package github.tornaco.android.thanos.common.sort; + +import android.content.Context; + +import com.elvishew.xlog.XLog; + +import java.util.Comparator; + +import github.tornaco.android.thanos.common.AppListModel; +import github.tornaco.android.thanos.core.app.ActivityManager; +import github.tornaco.android.thanos.core.app.ThanosManager; +import util.PinyinComparatorUtils; + +public class AppRunningStateComparator implements Comparator { + private final ActivityManager activityManager; + + public AppRunningStateComparator(Context context) { + this.activityManager = ThanosManager.from(context).getActivityManager(); + } + + @Override + public int compare(AppListModel o1, AppListModel o2) { + try { + boolean isO1Running = activityManager.isPackageRunning(o1.appInfo.getPkgName()); + boolean isO2Running = activityManager.isPackageRunning(o2.appInfo.getPkgName()); + + if (isO1Running != isO2Running) { + if (isO1Running) return -1; + else return 1; + } + + } catch (Throwable e) { + XLog.e("AppLabelComparator error", e); + } + + return PinyinComparatorUtils.compare(o1.appInfo.getAppLabel(), o2.appInfo.getAppLabel()); + } +} diff --git a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppSort.java b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppSort.java index 24c633b7d..aefacbf46 100644 --- a/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppSort.java +++ b/android/modules/module_common/src/main/java/github/tornaco/android/thanos/common/sort/AppSort.java @@ -33,6 +33,18 @@ public String getAppSortDescription(@NonNull Context context, @NonNull AppListMo return null; } }), + Running(R.string.chip_title_app_only_running, new AppSorterProvider() { + @NonNull + @Override + public Comparator comparator(@NonNull Context context) { + return new AppRunningStateComparator(context); + } + + @Override + public String getAppSortDescription(@NonNull Context context, @NonNull AppListModel model) { + return null; + } + }), AppLabel(R.string.common_sort_by_install_app_label, new AppSorterProvider() { @NonNull @Override