android 禁止拖动桌面时钟小组件

摘要:本文将介绍一种禁止拖动桌面时钟小组件的方案。首先,通过添加trace log追踪至拖动的唯一入口;其次,获取拖动对象的详细信息并区分是否是对应的小组件;最后,禁止进入拖动流程。

1.trace log

通过android UI分析工具monitor得到长按时的界面,并获取id:workspace_page_container
通过全局搜索Launcher3代码得到出现id:workspace_page_container的代码并在方法入口添加trace log

    public CellLayout insertNewWorkspaceScreen(long screenId, int insertIndex) {
        Log.d(TAG, "insertNewWorkspaceScreen, trace: ", new Exception());
        if (mWorkspaceScreens.containsKey(screenId)) {
            throw new RuntimeException("Screen id " + screenId + " already exists!");
        }

        // Inflate the cell layout, but do not add it automatically so that we can get the newly
        // created CellLayout.
        CellLayout newScreen = (CellLayout) LayoutInflater.from(getContext()).inflate(
                        R.layout.workspace_screen, this, false /* attachToRoot */);
        newScreen.getShortcutsAndWidgets().setId(R.id.workspace_page_container);
        int paddingLeftRight = mLauncher.getDeviceProfile().cellLayoutPaddingLeftRightPx;
        int paddingBottom = mLauncher.getDeviceProfile().cellLayoutBottomPaddingPx;
        newScreen.setPadding(paddingLeftRight, 0, paddingLeftRight, paddingBottom);

        mWorkspaceScreens.put(screenId, newScreen);
        mScreenOrder.add(insertIndex, screenId);
        addView(newScreen, insertIndex);
        mStateTransitionAnimation.applyChildState(
                mLauncher.getStateManager().getState(), newScreen, insertIndex);

        if (mLauncher.getAccessibilityDelegate().isInAccessibleDrag()) {
            newScreen.enableAccessibleDrag(true, CellLayout.WORKSPACE_ACCESSIBILITY_DRAG);
        }

        return newScreen;
    }

分析trace log可以得到入口onWorkspaceItemLongClick(ItemLongClickListener.java:72)

08-23 14:42:03.712 D/Launcher.Workspace( 2947): insertNewWorkspaceScreen, trace: 
08-23 14:42:03.712 D/Launcher.Workspace( 2947): java.lang.Exception
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.insertNewWorkspaceScreen(Workspace.java:544)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.insertNewWorkspaceScreen(Workspace.java:540)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.addExtraEmptyScreenOnDrag(Workspace.java:593)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.onDragStart(Workspace.java:391)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.dragndrop.DragController.callOnDragStart(DragController.java:242)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.dragndrop.DragController.startDrag(DragController.java:224)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.beginDragShared(Workspace.java:1598)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.beginDragShared(Workspace.java:1534)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.Workspace.startDrag(Workspace.java:1523)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.touch.ItemLongClickListener.beginDrag(ItemLongClickListener.java:91)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.touch.ItemLongClickListener.onWorkspaceItemLongClick(ItemLongClickListener.java:72)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.touch.ItemLongClickListener.lambda$n_ku6Bnp7SQn-CFIT2R46R_RyW8(Unknown Source:0)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.touch.-$$Lambda$ItemLongClickListener$n_ku6Bnp7SQn-CFIT2R46R_RyW8.onLongClick(Unknown Source:0)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.view.View.performLongClickInternal(View.java:6677)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.view.View.performLongClick(View.java:6635)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.widget.LauncherAppWidgetHostView.onLongClick(LauncherAppWidgetHostView.java:116)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.launcher3.CheckLongPressHelper$CheckForLongPress.run(CheckLongPressHelper.java:39)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.os.Handler.handleCallback(Handler.java:873)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.os.Looper.loop(Looper.java:193)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at android.app.ActivityThread.main(ActivityThread.java:6746)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at java.lang.reflect.Method.invoke(Native Method)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
08-23 14:42:03.712 D/Launcher.Workspace( 2947): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2.功能实现

    private static boolean onWorkspaceItemLongClick(View v) {
        Launcher launcher = Launcher.getLauncher(v.getContext());
        if (!canStartDrag(launcher)) return false;
        if (!launcher.isInState(NORMAL) && !launcher.isInState(OVERVIEW)) return false;
        if (!(v.getTag() instanceof ItemInfo)) return false;
        // @ + {
        if (v.getTag() instanceof LauncherAppWidgetInfo) {
            LauncherAppWidgetInfo widgetInfo = (LauncherAppWidgetInfo) v.getTag();
            if (widgetInfo.providerName != null) {
                String packageName = widgetInfo.providerName.getPackageName();
                if (packageName.equals("com.android.deskclock")) {
                    Log.d(TAG, "onWorkspaceItemLongClick::disallow widget(" + packageName + ") drag.");
                    return false;
                }
            }
        }
        // @ + }
        launcher.setWaitingForResult(null);
        beginDrag(v, launcher, (ItemInfo) v.getTag(), new DragOptions());
        return true;
    }

你可能感兴趣的:(android)