Android Launcher3在Hotseat中将AllappsButton 替换成普通shortcut

我们首先来简单介绍Hotseat:

1、Hotseat 配置文件

hotseat默认是有5个按钮,其中中间一个是进入AllApp列表的按钮,这个是程序里面设置。其他的默认按钮需要在default_workspace.xml里面配置。

<!-- Hotseat (We use the screen as the position of the item in the hotseat) -->
<!-- 使用screen作为按钮位置标识-->
<favorite
launcher:packageName="com.example.naviback"
launcher:className="com.example.naviback.MainActivity"
launcher:container="-101"
launcher:screen="0"
launcher:x="0"
launcher:y="0" />
<favorite
launcher:packageName="com.csr.dvd"
launcher:className="com.csr.dvd.LoadDVD"
launcher:container="-101"
launcher:screen="1"
launcher:x="1"
launcher:y="0" />
<favorite
launcher:packageName="com.apical.apicalradio"
launcher:className="com.apical.apicalradio.RadioMainActivity"
launcher:container="-101"
launcher:screen="3"
launcher:x="3"
launcher:y="0" />
<favorite
launcher:packageName="com.csr.BTApp"
launcher:className="com.csr.BTApp.CSRBluetoothDemoActivity"
launcher:container="-101"
launcher:screen="4"
launcher:x="4"
launcher:y="0" />
launcher:container:需要标识为-101 ,代表是hotseat的默认按钮。
launcher:screen:代表按钮的位置,0是第一个位置。ALlApp按钮默认是2,所以上面并没有
screen为2的标签。


2、Hotseat加载数据

Hotseat加载数据可以分为两部分,AllApp按钮和其他默认按钮。其他默认按钮加载跟workspace的默认数据加载一样,都是在LauncherModel加载。我们着重来看AllAPP加载的过程。

Hotseat.java

void resetLayout() {
        mContent.removeAllViewsInLayout();

        if (!AppsCustomizePagedView.DISABLE_ALL_APPS) {
            // Add the Apps button
            Context context = getContext();

            LayoutInflater inflater = LayoutInflater.from(context);
            TextView allAppsButton = (TextView)
                    inflater.inflate(R.layout.all_apps_button, mContent, false);
            Drawable d = context.getResources().getDrawable(R.drawable.all_apps_button_icon);
            Utilities.resizeIconDrawable(d);
            allAppsButton.setCompoundDrawables(null, d, null, null);

            allAppsButton.setContentDescription(context.getString(R.string.all_apps_button_label));
            if (mLauncher != null) {
                allAppsButton.setOnTouchListener(mLauncher.getHapticFeedbackTouchListener());
            }
            allAppsButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(android.view.View v) {
                    if (mLauncher != null) {
                        mLauncher.onClickAllAppsButton(v);
                    }
                }
            });

            // Note: We do this to ensure that the hotseat is always laid out in the orientation of
            // the hotseat in order regardless of which orientation they were added
            int x = getCellXFromOrder(mAllAppsButtonRank);
            int y = getCellYFromOrder(mAllAppsButtonRank);
            CellLayout.LayoutParams lp = new CellLayout.LayoutParams(x,y,1,1);
            lp.canReorder = false;
            mContent.addViewToCellLayout(allAppsButton, -1, 0, lp, true);
        }
    }

我们再来讨论另一种常见去除AllappsButton的场景


去除Hotseat中的AllAppsButton常见的场景是,将所有快捷方式都放在workspace中,就像国产手机中常见的。

packages\apps\Launcher3\src\com\Android\launcher3\AppsCustomizePagedView.Java

    public static boolean DISABLE_ALL_APPS = true;


最后我们来讲如何将AllAPP按钮替换成普通的shortcut而不改变其他。

① 屏蔽上面resetLayout()函数函数中用该条件(!AppsCustomizePagedView.DISABLE_ALL_APPS)包括的所有内容,这样我们的我们AllAPP的按钮就不再显示了


② 在default_workspace.xml配置文件中将原来是为AllAPP按钮的位置预留的没写screen为2的部分换成其他Shortcut。到了这一步我们以为OK了,但是当我们编译后查看原来的位置还是没有加上新的Shortcut这是为什么呢?


在加载普通shortcut时,当加载位置是Hotseat时会有一个判断,当前位置是否是AllAPP的位置。在loadWorkspace中调用checkItemPlacement判断,我们来查看相应代码。

 private boolean checkItemPlacement(HashMap<Long, ItemInfo[][]> occupied, ItemInfo item,
                                           AtomicBoolean deleteOnItemOverlap) {
            LauncherAppState app = LauncherAppState.getInstance();
            DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
            int countX = (int) grid.numColumns;
            int countY = (int) grid.numRows;

            long containerIndex = item.screenId;
            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
                // Return early if we detect that an item is under the hotseat button
                if (mCallbacks == null ||
                        mCallbacks.get().isAllAppsButtonRank((int) item.screenId)) {
                    deleteOnItemOverlap.set(true);
                    return false;
                }

                if (occupied.containsKey(LauncherSettings.Favorites.CONTAINER_HOTSEAT)) {
                    if (occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
                            [(int) item.screenId][0] != null) {
                        Log.e(TAG, "Error loading shortcut into hotseat " + item
                                + " into position (" + item.screenId + ":" + item.cellX + ","
                                + item.cellY + ") occupied by "
                                + occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT)
                                [(int) item.screenId][0]);
                            return false;
                    }
                } else {
                    ItemInfo[][] items = new ItemInfo[countX + 1][countY + 1];
                    items[(int) item.screenId][0] = item;
                    occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, items);
                    return true;
                }
            } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
                // Skip further checking if it is not the hotseat or workspace container
                return true;
            }
在Hotseat.java中

    public boolean isAllAppsButtonRank(int rank) {

        if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
            return false;
        } else {
            return rank == mAllAppsButtonRank;
        }
    }
查看代码发现mAllAppsButtonRank值为(int) (numColumns / 2),所以该函数判断的就是当前的ScreenID是否为AllAPPsButton的位置,当你在XML中即使写上 screen为2的Shortcut配置最后也会不会被加入。

③ 将isAllAppsButtonRank函数的返回值改为false。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值