Android 11.0 Launcher定制桌面图片按照安装时间顺序排列

Posted Mrsongs的心情杂货铺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 11.0 Launcher定制桌面图片按照安装时间顺序排列相关的知识,希望对你有一定的参考价值。

创作不易,请尊重原创,转载须注明出处:https://blog.csdn.net/An_Times/article/details/122133464

一、前言

原生代码中Launcher 桌面应用图标都是按照应用首字母顺序排列的,那这样会带来一个问题,后面安装的应用图标会插入到原有的图标中打乱排列顺序。刚好产品经理提了一个需求希望后安装的应用在原有的顺序后继续排列图标。那么这个需求怎么实现呢,我们需要找到这块的代码看看他的逻辑,本文就介绍一下这个部分。

环境:本文基于MTK 6762 Android 11.0

二、Launcher3的图标排列逻辑


  • 代码路径:packages\\apps\\Launcher3\\src\\com\\android\\launcher3\\allapps\\AlphabeticalAppsList.java
  • onAppsUpdated() 方法中 有应用图标刷新被调用,其中Collections.sort(mApps, mAppNameComparator)就是根据mAppNameComparator 这个APP名称比较器是比较排序的
    /**
     * Updates internals when the set of apps are updated.
     */
    @Override
    public void onAppsUpdated() 
        // Sort the list of apps
        mApps.clear();

...此处省略部分代码
        Collections.sort(mApps, mAppNameComparator);
        // As a special case for some languages (currently only Simplified Chinese), we may need to
        // coalesce sections
        Locale curLocale = mLauncher.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
            TreeMap<String, ArrayList<AppInfo>> sectionMap = new TreeMap<>(new LabelComparator());
            for (AppInfo info : mApps) 
                // Add the section to the cache
                String sectionName = info.sectionName;

                // Add it to the mapping
                ArrayList<AppInfo> sectionApps = sectionMap.get(sectionName);
                if (sectionApps == null) 
                    sectionApps = new ArrayList<>();
                    sectionMap.put(sectionName, sectionApps);
                
                sectionApps.add(info);
            

            // Add each of the section apps to the list in order
            mApps.clear();
            for (Map.Entry<String, ArrayList<AppInfo>> entry : sectionMap.entrySet()) 
                mApps.addAll(entry.getValue());
            
        

        // Recompose the set of adapter items from the current set of apps
        updateAdapterItems();
    
  • 上述中 mAppNameComparator 比较器实际上是使用LabelComparator ,我们可以看到它采用的是比较首字母的来排序的。
public class LabelComparator implements Comparator<String> 

    private final Collator mCollator = Collator.getInstance();

    @Override
    public int compare(String titleA, String titleB) 
        // Ensure that we de-prioritize any titles that don't start with a
        // linguistic letter or digit
        boolean aStartsWithLetter = (titleA.length() > 0) &&
                Character.isLetterOrDigit(titleA.codePointAt(0));
        boolean bStartsWithLetter = (titleB.length() > 0) &&
                Character.isLetterOrDigit(titleB.codePointAt(0));
        if (aStartsWithLetter && !bStartsWithLetter) 
            return -1;
         else if (!aStartsWithLetter && bStartsWithLetter) 
            return 1;
        

        // Order by the title in the current locale
        return mCollator.compare(titleA, titleB);
    


三、Launcher3的图标按照应用安装时间排序


  • 知道前面的逻辑改起来就很简单了,我们之间用一个比较安装时间的比较器就行了安装时间可以从PKMS获取,还不清楚的小伙伴可以私聊我。

以上是关于Android 11.0 Launcher定制桌面图片按照安装时间顺序排列的主要内容,如果未能解决你的问题,请参考以下文章

一个效果非常华丽的仿桌面APP,却胜似Launcher

Android Launcher3简介

Android Launcher3简介

Android Launcher3简介

Android 11.0 删除上拉,进入全部应用界面

Android 11.0 删除全部应用界面的搜索框