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获取,还不清楚的小伙伴可以私聊我。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mrsongs的心情杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值