1. 在 LauncherModel.java 中加入如下代碼, 如果是KK Launcher3 ApplicationInfo要替換為AppInfo
public static final Comparator getInstallTimeComparator() {
return new Comparator() {
public final int compare(ApplicationInfo a, ApplicationInfo b) {
return a.firstInstallTime == b.firstInstallTime ? 0 : a.firstInstallTime > b.firstInstallTime ? 1:-1;
}
};
}
public static class InstallTimeComparator implements Comparator
{
private PackageManager mPackageManager;
List mapps;
InstallTimeComparator(PackageManager pm, List apps)
{
mPackageManager = pm;
mapps = apps;
}
public final int compare(ResolveInfo a, ResolveInfo b) {
String packageNameA = a.activityInfo.applicationInfo.packageName;
String packageNameB = b.activityInfo.applicationInfo.packageName;
long firstInstallTimeA = 0, firstInstallTimeB = 0;
try{
firstInstallTimeA = mPackageManager.getPackageInfo(packageNameA, 0).firstInstallTime;
firstInstallTimeB = mPackageManager.getPackageInfo(packageNameB, 0).firstInstallTime;
}catch(Exception e){
e.printStackTrace();
return 0;
}
return firstInstallTimeA == firstInstallTimeB ? 0 : firstInstallTimeA > firstInstallTimeB ? 1:-1;
}
};
2. 修改 LauncherModel.java 的 loadAllAppsByBatch() 方法:
將
Collections.sort(apps,new LauncherModel.ShortcutNameComparator(packageManager, mLabelCache));
替換成:
Collections.sort(apps,new InstallTimeComparator(packageManager, apps));
3. 修改 AppsCustomizePagedview.java 的 setApps 方法
將
Collections.sort(mApps, LauncherModel.getAppNameComparator());
替換成:
Collections.sort(mApps, LauncherModel.getInstallTimeComparator());
4. 系統排序會調用reorderApps方法,因此您在其他條件下也要達到安裝時間排序,請在對應的reorderApps方法調用前增加
Collections.sort(mApps, LauncherModel.getInstallTimeComparator());
例如:安裝的應用後也想安裝新的排序方式排列,需要在AppsCustomizePagedview.java 的 addApps 方法前增加sort的調用