背景:桌面去掉了谷歌搜索框,底部快捷图标按要求排了五个。
把小部件拖到桌面后,加宽加高重启后会消失,一开始以为是不是图标太大,导致重启后系统自动把小部件给干掉了。瞎猜是没用,于是打开mtklog,发现如下日志:
LoaderCursor: Error loading shortcut LauncherAppWidgetInfo(id=9 type=WIDGET container=desktop targetComponent=ComponentInfo{com.xc.r3/com.xc.r3.AppWidget} screen=0 cell(1,0) span(3,4) minSpan(1,1) rank=0 user=UserHandle{0} title=null providerName=ComponentInfo{com.xc.r3/com.xc.r3.AppWidget} appWidgetId=5) into cell (0-0:1,1,3,4) already occupied
找到打印日志的代码如下:
packages\apps\Launcher3\src\com\android\launcher3\model\LoaderCursor.java

    /**
     * check & update map of what's occupied; used to discard overlapping/invalid items
     */
    protected boolean checkItemPlacement(ItemInfo item) {
        int containerIndex = item.screenId;
        if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
            final GridOccupancy hotseatOccupancy =
                    occupied.get(LauncherSettings.Favorites.CONTAINER_HOTSEAT);

            if (item.screenId >= mIDP.numHotseatIcons) {
                Log.e(TAG, "Error loading shortcut " + item
                        + " into hotseat position " + item.screenId
                        + ", position out of bounds: (0 to " + (mIDP.numHotseatIcons - 1)
                        + ")");
                return false;
            }

            if (hotseatOccupancy != null) {
                if (hotseatOccupancy.cells[(int) item.screenId][0]) {
                    Log.e(TAG, "Error loading shortcut into hotseat " + item
                            + " into position (" + item.screenId + ":" + item.cellX + ","
                            + item.cellY + ") already occupied");
                    return false;
                } else {
                    hotseatOccupancy.cells[item.screenId][0] = true;
                    return true;
                }
            } else {
                final GridOccupancy occupancy = new GridOccupancy(mIDP.numHotseatIcons, 1);
                occupancy.cells[item.screenId][0] = true;
                occupied.put(LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
                return true;
            }
        } else if (item.container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
            // Skip further checking if it is not the hotseat or workspace container
            return true;
        }

        final int countX = mIDP.numColumns;
        final int countY = mIDP.numRows;
        if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
                item.cellX < 0 || item.cellY < 0 ||
                item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
            Log.e(TAG, "Error loading shortcut " + item
                    + " into cell (" + containerIndex + "-" + item.screenId + ":"
                    + item.cellX + "," + item.cellY
                    + ") out of screen bounds ( " + countX + "x" + countY + ")");
            return false;
        }

        if (!occupied.containsKey(item.screenId)) {
            GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
            if (item.screenId == Workspace.FIRST_SCREEN_ID) {
                // Mark the first row as occupied (if the feature is enabled)
                // in order to account for the QSB.
                screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
            }
            occupied.put(item.screenId, screen);
        }
        final GridOccupancy occupancy = occupied.get(item.screenId);

        // Check if any workspace icons overlap with each other
        if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
            occupancy.markCells(item, true);
            return true;
        } else {
            Log.e(TAG, "Error loading shortcut " + item
                    + " into cell (" + containerIndex + "-" + item.screenId + ":"
                    + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
                    + ") already occupied");
            return false;
        }
    }

看这段代码和注释

        // Check if any workspace icons overlap with each other
        if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
            occupancy.markCells(item, true);
            return true;
        } else {
            Log.e(TAG, "Error loading shortcut " + item
                    + " into cell (" + containerIndex + "-" + item.screenId + ":"
                    + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
                    + ") already occupied");
            return false;
        }

很明显这段代码是对图标啥的是否重叠进行检查,既然打印了这段日志,那么可以判断我拖拽到桌面上的小部件就是与某个图标或者部件重叠了,导致重启后小部件自己消失了。但是桌面workspace啥都没有啊,又怎么会重叠呢。突然想到谷歌搜索框这玩意,我将谷歌搜索框去掉的修改如下:

packages/apps/Launcher3/src/com/android/launcher3/Workspace.java
    /* if (qsb == null) {    // 该段代码注释
            // In transposed layout, we add the QSB in the Grid. As workspace does not touch the
            // edges, we do not need a full width QSB.
            qsb = LayoutInflater.from(getContext())
                    .inflate(R.layout.search_container_workspace, firstPage, false);
        }
*/

从结果看,这样改谷歌搜索框去是能去掉,但是搜索框仍然会占据原有位置,小部件拖拽到桌面后又加宽加高与谷歌搜索框重叠了,所以小部件重启后就消失了。
于是去掉谷歌搜索框换一种修改方式:

+++ b/packages/apps/Launcher3/src/com/android/launcher3/config/FeatureFlags.java
@@ -52,7 +52,7 @@ public final class FeatureFlags {
      * Enable moving the QSB on the 0th screen of the workspace. This is not a configuration feature
      * and should be modified at a project level.
      */
-    public static final boolean QSB_ON_FIRST_SCREEN = true;
+    public static final boolean QSB_ON_FIRST_SCREEN = false;

     /**
      * Feature flag to handle define config changes dynamically instead of killing the process.

这样修改可以去掉谷歌搜索框,而界面上搜索框也不会继续占用位置
最后编译刷机,把小部件不管拉多大,重启后都不会消失了。

总结:有些bug是自己改动了某些地方造成的,所以修改某个功能时同时也需要考虑下这样修改会不会影响其他功能模块,会不会造成意想不到的结果

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐