Launcher桌面负一屏实现 第一章

Android Launcher负一屏实现

负一屏的实现主流有两种方式

  1. Launcher自行开发,往Workspace中插入一个自定义的CellLayout来作为负一屏的容器。 这种方式是最常用的方案。
  2. 利用Google的feed屏方案,基于ILauncherOverlay和ILauncherOverlayCallback这两个接口来实现,Launcher作为客户端,负一屏是一个独立应用作为服务端,通过aidl来通信来实现加载负一屏以及支持相互滑页。

这一章节我们主要介绍下方案一,通过往workspace中添加celllayout来实现负一屏

先介绍下桌面的显示结构,Workspace继承自Pageview,用来展示桌面的页面,workspace中可以放入很多CellLayout,而每个CellLayout对应桌面的每一页,CellLayout的一个子View是ShortcutAndWidgetContainer,ShortcutAndWidgetContainer定义实现了应用图标的显示排列。

在了解了Workspace的基本结构后,可以看出,想要新增个负一屏,只需要在workspace中add一个新的CellLayout。 可以看一下代码是如何实现的,主要分三步:

  • 第一步:在Launcher.java中的bindScreens中,除了原来的根据screenIds的集合来bindAddScreens之外,我们新增个业务,用hasCustomContentToLeft()来判断是否需要负一屏,如果需要,我们调用workspace中的createCustomContentContainer()来创建负一屏,populateCustomContentContainer()可以用来将自定义的负一屏view加入到新的CellLayout中,具体代码如下:

···

@Override
public void bindScreens(ArrayList<Long> orderedScreenIds) {
    if (LauncherModel.DEBUG_APPLICATION_ICON) {
        LogUtils.d(LauncherModel.TAG_DEBUG_ICON_INFO, TAG+", "+" bindScreens size "+orderedScreenIds.size());
    }
    bindAddScreens(orderedScreenIds);

    // If there are no screens, we need to have an empty screen
    if (orderedScreenIds.size() == 0) {
        mWorkspace.addExtraEmptyScreen();
    }

    // Create the custom content page (this call updates mDefaultScreen which calls
    // setCurrentPage() so ensure that all pages are added before calling this).
    if (hasCustomContentToLeft()) {
        mWorkspace.createCustomContentContainer();
        populateCustomContentContainer();
    }
}

···

  • 第二步:具体的创建自定义的负一屏celllayout,并addView到workspace的第一页,代码如下:

···

public void createCustomContentContainer() {
    CellLayout customScreen = (CellLayout)
            mLauncher.getLayoutInflater().inflate(R.layout.workspace_screen, null);
    customScreen.disableBackground();
    customScreen.disableDragTarget();
    if (mScreenOrder.size() > 0 && mScreenOrder.get(0) == CUSTOM_CONTENT_SCREEN_ID) {
        LogUtils.d(TAG, "already exist custom screen.");
        return;
    }
    mWorkspaceScreens.put(CUSTOM_CONTENT_SCREEN_ID, customScreen);
    mScreenOrder.add(0, CUSTOM_CONTENT_SCREEN_ID);

    // We want no padding on the custom content
    customScreen.setPadding(0, 0, 0, 0);

    addFullScreenPage(customScreen);

    // Update the custom content hint
    if (mRestorePage != INVALID_RESTORE_PAGE) {
        mRestorePage = mRestorePage + 1;
    } else {
        setCurrentPage(getCurrentPage() + 1);
    }
}

public void addFullScreenPage(View page) {
    LayoutParams lp = generateDefaultLayoutParams();
    lp.isFullScreenPage = true;
    super.addView(page, 0, lp); //index是0,所以是在最左边的一页
}

···

  • 第三步:将自定义的负一屏view添加到创建的celllayout中,可以给CellLayout设置全屏,lp.isFullScreen
    =true,然后将celllayout.removeAllView之后,在add新的负一屏view,代码如下:

···

public void addToCustomContentPage(View customContent, CustomContentCallbacks callbacks,
        String description) {
    if (getPageIndexForScreenId(CUSTOM_CONTENT_SCREEN_ID) < 0) {
        // throw new RuntimeException("Expected custom content screen to exist");
        LogUtils.d(TAG,"there's no custom screen when add left page!!!!!!!!!!!!");
        createCustomContentContainer();
    }

    // Add the custom content to the full screen custom page
    CellLayout customScreen = getScreenWithId(CUSTOM_CONTENT_SCREEN_ID);
    int spanX = customScreen.getCountX();
    int spanY = customScreen.getCountY();
    CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
    lp.canReorder  = false;
    lp.isFullscreen = true;
    if (customContent instanceof Insettable) {
        ((Insettable)customContent).setInsets(mInsets);
    }

    // Verify that the child is removed from any existing parent.
    if (customContent.getParent() instanceof ViewGroup) {
        ViewGroup parent = (ViewGroup) customContent.getParent();
        parent.removeView(customContent);
    }
    customScreen.removeAllViews();
    customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
    mCustomContentDescription = description;

    mCustomContentCallbacks = callbacks;
}

···

以上内容就是如何在launcher中利用workspace来添加负一屏了。如果是一个相对内容简单的负一屏,建议使用这种方式来实现,如果你的负一屏内容很丰富的话,用这种方案来实现,最终代码逻辑运行在launcher进程中,占用launcher进程资源,会拖累launcher进程的运行。

下一章节我们来介绍下独立负一屏的实现,来解决负一屏占用launcher进程的问题。

猜你喜欢

转载自blog.csdn.net/u013032242/article/details/83036747