1,Launcher.java
通过log打印,onCreate里面没有加载列表,在onResume里面:
if (mOnResumeState != State.APPS) {
Log.d(TAG, "onResume() 4");
tryAndUpdatePredictedApps();---------这里更新APP
}
继续看tryAndUpdatePredictedApps
List<ComponentKeyMapper<AppInfo>> apps = mLauncherCallbacks.getPredictedApps();
if (apps != null) {
mAppsView.setPredictedApps(apps);------mAppsView这个就是加载的APP列表了
Log.d(TAG, "tryAndUpdatePredictedApps csh 3");
}
继续看mAppsView:@Thunk AllAppsContainerView mAppsView;
查看mAppsView.setPredictedApps:
public void setPredictedApps(List<ComponentKeyMapper<AppInfo>> apps) {
mApps.setPredictedApps(apps);------继续跟踪
}
关键的地方来了:
public void setPredictedApps(List<ComponentKeyMapper<AppInfo>> apps) {
mPredictedAppComponents.clear();
mPredictedAppComponents.addAll(apps);
List<AppInfo> newPredictedApps = processPredictedAppComponents(apps);
// We only need to do work if any of the visible predicted apps have changed.
if (!newPredictedApps.equals(mPredictedApps)) {
if (newPredictedApps.size() == mPredictedApps.size()) {
swapInNewPredictedApps(newPredictedApps);
} else {
// We need to update the appIndex of all the items.
onAppsUpdated();--------------此处就是我们要重点看的
}
}
}
/**
* Updates internals when the set of apps are updated.*更新应用程序集时更新内部。
*/
private void onAppsUpdated() {
// Sort the list of apps
mApps.clear();
mApps.addAll(mComponentToAppMap.values());
Collections.sort(mApps, mAppNameComparator);
注解:mApps是一个list集合,private final List<AppInfo> mApps = new ArrayList<>();
在onAppsUpdated里面,我们看到针对mApps列表的操作,先clear,再添加了.addAll一个mComponentToAppMap.values()
集合,这个怎么来的,我们看,算了不看mComponentToAppMap了,它就是获取的系统的APP值,.values()就是APP的包名
,也就是在这,mApps被添加了一个APP的集合,然后对mApps进行了排序:Collections.sort(mApps, mAppNameComparator);其中mAppNameComparator就是排序方式,大概就是按abcd拼音排序。
那么我们怎么自定义自己的APP排序呢,一个很简单的方法,我们自己做一个list集合列表
把系统获取过来要加载的app都加到我们的list,然后把mApps清空,再直接导入mApps.addAll(我们自己的list),我们自己的list怎么组合,可以举例:
private static String[] UESR_APPS_PKGS = {A;B;C;D};
/**
* Adds or updates existing apps in the list
*/
public void addOrUpdateApps(List<AppInfo> apps) {------这里是遍历添加系统APP的地方,我们在这来获取AppInfo app存到我们自己的list里面去。
for (AppInfo app : apps) {
mComponentToAppMap.put(app.toComponentKey(), app);
String str = app.componentName.getPackageName();
if(str.contains("com.android.contacts")){
list_csh[0] = app;
}else if(str.contains("org.codeaurora.snapcam")){
list_csh[1] = app;
}else if(str.contains("com.android.settings")){
list_csh[2] = app;
}
}
onAppsUpdated();
}
然后把list直接添加到系统的要加载的list mApps:
在private void onAppsUpdated() {里面修改:
int i = 0;
while(i < list_csh.length)
{
if (list_csh[i] != null)
mApps.add(list_csh[i]);
i++;
}
把//Collections.sort(mApps, mAppNameComparator);注释掉
怎么弄自己看着办,关键点就这样
下面是修改全部内容:
import java.util.TreeMap;
+import java.util.Arrays;
/**
* The alphabetically sorted list of applications.
@@ -166,6 +167,8 @@ public class AlphabeticalAppsList {
// The set of apps from the system not including predictions
private final List<AppInfo> mApps = new ArrayList<>();
private final HashMap<ComponentKey, AppInfo> mComponentToAppMap = new HashMap<>();
+ //private static String[] list_csh = ("com.android.contacts","org.codeaurora.snapcam","com.android.settings");
+ private AppInfo[] list_csh = new AppInfo[3];
// The set of filtered apps with the current filter
private final List<AppInfo> mFilteredApps = new ArrayList<>();
@@ -384,6 +387,14 @@ public class AlphabeticalAppsList {
public void addOrUpdateApps(List<AppInfo> apps) {
for (AppInfo app : apps) {
mComponentToAppMap.put(app.toComponentKey(), app);
+ String str = app.componentName.getPackageName();
+ if(str.contains("com.android.contacts")){
+ list_csh[0] = app;
+ }else if(str.contains("org.codeaurora.snapcam")){
+ list_csh[1] = app;
+ }else if(str.contains("com.android.settings")){
+ list_csh[2] = app;
+ }
}
onAppsUpdated();
}
@@ -404,8 +415,16 @@ public class AlphabeticalAppsList {
private void onAppsUpdated() {
// Sort the list of apps
mApps.clear();
- mApps.addAll(mComponentToAppMap.values());
- Collections.sort(mApps, mAppNameComparator);
+ Arrays.asList(list_csh);
+ int i = 0;
+ while(i < list_csh.length)
+ {
+ if (list_csh[i] != null)
+ mApps.add(list_csh[i]);
+ i++;
+ }
+ //mApps.addAll(list_csh);
+ //Collections.sort(mApps, mAppNameComparator);
// As a special case for some languages (currently only Simplified Chinese), we may need to
// coalesce sections
(END)