系统默认按名称进行排列
Launcher3/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
AllAppsRecyclerView
继承自FastScrollRecyclerView
protected AlphabeticalAppsList<?> mApps;
public void setApps(AlphabeticalAppsList<?> apps) {
mApps = apps;
}
AlphabeticalAppsList
Launcher3/src/com/android/launcher3/allapps/AlphabeticalAppsList.java
public class AlphabeticalAppsList<T extends Context & ActivityContext> implements
AllAppsStore.OnUpdateListener {
private AppInfoComparator mAppNameComparator;
@Override
public void onAppsUpdated() {
if (mAllAppsStore == null) {
return;
}
// Sort the list of apps
mApps.clear();
Stream<AppInfo> appSteam = Stream.of(mAllAppsStore.getApps());
if (!hasSearchResults() && mItemFilter != null) {
appSteam = appSteam.filter(mItemFilter);
}
appSteam = appSteam.sorted(mAppNameComparator);
// As a special case for some languages (currently only Simplified Chinese), we may need to
// coalesce sections
Locale curLocale = mActivityContext.getResources().getConfiguration().locale;
boolean localeRequiresSectionSorting = curLocale.equals(Locale.SIMPLIFIED_CHINESE);
if (localeRequiresSectionSorting) {
// Compute the section headers. We use a TreeMap with the section name comparator to
// ensure that the sections are ordered when we iterate over it later
appSteam = appSteam.collect(Collectors.groupingBy(
info -> info.sectionName,
() -> new TreeMap<>(new LabelComparator()),
Collectors.toCollection(ArrayList::new)))
.values()
.stream()
.flatMap(ArrayList::stream);
}
appSteam.forEachOrdered(mApps::add);
// Recompose the set of adapter items from the current set of apps
if (mSearchResults.isEmpty()) {
updateAdapterItems();
}
}
AlphabeticalAppsList在更新时会调用appSteam.sorted(mAppNameComparator)进行排序,接着看
AppInfoComparator
Launcher3/src/com/android/launcher3/allapps/AppInfoComparator.java
AppInfoComparator
@Override
public int compare(AppInfo a, AppInfo b) {
// Order by the title in the current locale
int result = mLabelComparator.compare(
a.title == null ? "" : a.title.toString(),
b.title == null ? "" : b.title.toString());
if (result != 0) {
return result;
}
// If labels are same, compare component names
result = a.componentName.compareTo(b.componentName);
if (result != 0) {
return result;
}
if (mMyUser.equals(a.user)) {
return -1;
} else {
Long aUserSerial = mUserManager.getSerialNumberForUser(a.user);
Long bUserSerial = mUserManager.getSerialNumberForUser(b.user);
return aUserSerial.compareTo(bUserSerial);
}
}
compaire()方法的逻辑修改为安装时间
@Override
public int compare(AppInfo a, AppInfo b) {
int result = 0;
//按安装时间进行排序
long aFirstInstallTime = getFirstInstallTime(a);
long bFirstInstallTime = getFirstInstallTime(b);
if(aFirstInstallTime < bFirstInstallTime){
result = 1;
}else if(aFirstInstallTime == bFirstInstallTime){
result = 0;
}else {
result = -1;
}
if(result !=0){
return result;
}
//保留之前的逻辑
// Order by the title in the current locale
result = mLabelComparator.compare(
a.title == null ? "" : a.title.toString(),
b.title == null ? "" : b.title.toString());
if (result != 0) {
return result;
}
// If labels are same, compare component names
result = a.componentName.compareTo(b.componentName);
if (result != 0) {
return result;
}
if (mMyUser.equals(a.user)) {
return -1;
} else {
Long aUserSerial = mUserManager.getSerialNumberForUser(a.user);
Long bUserSerial = mUserManager.getSerialNumberForUser(b.user);
return aUserSerial.compareTo(bUserSerial);
}
}
private long getFirstInstallTime(AppInfo a) {
long firstInstallTime = 0;
try {
PackageManager packageManager = mContext.getPackageManager();
PackageInfo packageInfo = packageManager.getPackageInfo(a.componentName.getPackageName(), 0);
firstInstallTime = packageInfo.firstInstallTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
return firstInstallTime;
}
调整后