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;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值