Skip to content

Commit

Permalink
[app] app list: sort with running state
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Sep 12, 2022
1 parent 5f2726e commit 26ed6b1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<AppListModel> {
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppListModel> 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
Expand Down

0 comments on commit 26ed6b1

Please sign in to comment.