多屏联动 - 非动画

基于Android R版本分析

新增 DoubleScreenMovePointerEventListener 类:

java 复制代码
package com.android.server.wm;
​
import android.util.Log;
import android.view.MotionEvent;
import android.view.WindowManagerPolicyConstants;
​
public class DoubleScreenMovePointerEventListener implements WindowManagerPolicyConstants.PointerEventListener {
    private static final String TAG = DoubleScreenMovePointerEventListener.class.getName();
​
    private boolean shouldBeginMove = false;
    private int mPoint0FirstX = 0;
    private int mPoint1FirstX = 0;
    private int mPoint0LastX = 0;
    private int mPoint1LastX = 0;
    int START_GAP = 20; //动作触发阈值,最少移动为20个像素才可以
    private final WindowManagerService mService;
    private final DisplayContent mDisplayContent;
​
    public DoubleScreenMovePointerEventListener(WindowManagerService service, DisplayContent displayContent) {
        mService = service;
        mDisplayContent = displayContent;
    }
​
    @Override
    public void onPointerEvent(MotionEvent motionEvent) {
        Log.d(TAG, "onPointerEvent: motionEvent = " + motionEvent);
        switch (motionEvent.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                if (motionEvent.getPointerCount() > 1) {
                    shouldBeginMove = false;
                    Log.d(TAG, "onPointerEvent: motionEvent.getPointerCount() > 2 end DoubleScreenMove");
                }
                if (motionEvent.getPointerCount() == 1) {
                    if (mPoint0FirstX == 0) {
                        //if (mPoint0FirstX == 0 && mPoint1FirstX == 0) {
                        mPoint0FirstX = (int) motionEvent.getX(0);
//                        mPoint1FirstX = (int) motionEvent.getX(1);
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (motionEvent.getPointerCount() == 1) {
                    if (!shouldBeginMove && (motionEvent.getX(0) - mPoint0FirstX) > START_GAP) {
                        // && (motionEvent.getX(1) - mPoint1FirstX) > START_GAP
                        //识别了双指动作达到触发task移动条件,则调用对应mDisplayContent.doTestMoveTaskToOtherDisplay方法
                        Log.d(TAG, "onPointerEvent: start DoubleScreenMove");
                        shouldBeginMove = true;
                        mDisplayContent.doMoveStackToDisplay();
                    }
                    mPoint0LastX = (int) motionEvent.getX(0);
//                    mPoint1LastX = (int) motionEvent.getX(1);
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP:
                shouldBeginMove = false;
                mPoint0FirstX = mPoint1FirstX = 0;
                Log.d(TAG, "onPointerEvent: ACTION_UP end DoubleScreenMove");
                break;
        }
    }
}

新增DoubleScreenMovePointerEventListener初始化(在DisplayContent中):

scss 复制代码
final DoubleScreenMovePointerEventListener mPointerEventListener;
​
DisplayContent(Display display, RootWindowContainer root) {
    ........................
    mTapDetector = new TaskTapPointerEventListener(mWmService, this);
    mPointerEventListener = new DoubleScreenMovePointerEventListener(mWmService,this);
    registerPointerEventListener(mTapDetector);
    registerPointerEventListener(mPointerEventListener);
    registerPointerEventListener(mWmService.mMousePositionTracker);
    ........................
}

修改DisplayContent,新增doMoveStackToDisplay()方法:

ini 复制代码
public void doMoveStackToDisplay() {
    DisplayContent otherDisplay = null;
    if (mRootWindowContainer.getChildCount() >= 2) {
        otherDisplay = (mRootWindowContainer.getChildAt(0) == this)
            ? mRootWindowContainer.getChildAt(1) : mRootWindowContainer.getChildAt(0);
    }
    if (otherDisplay != this && otherDisplay != null) {
        int topStackId = 0;
        try {
            Task topStack = getTopStack();
            if (topStack.isActivityTypeHome()) {
                Log.d(TAG, "doMoveStackToDisplay: isActivityTypeHome");
                return;
            }
            topStackId = topStack.mTaskId;
            mRootWindowContainer.moveStackToDisplay(topStackId, otherDisplay.mDisplayId, true);
        } catch (Exception ex) {
            Log.d(TAG, "doMoveStackToDisplay: exception = " + ex);
        }
    }
}

参考:blog.csdn.net/learnframew...

相关推荐
Light6036 分钟前
云途领航:现代应用架构助力企业转型新篇
微服务·架构·saas·paas·iaas·ipaas·apaas
zhangphil1 小时前
Android绘图Path基于LinearGradient线性动画渐变,Kotlin(2)
android·kotlin
watl02 小时前
【Android】unzip aar删除冲突classes再zip
android·linux·运维
键盘上的蚂蚁-2 小时前
PHP爬虫类的并发与多线程处理技巧
android
喜欢猪猪3 小时前
Java技术专家视角解读:SQL优化与批处理在大数据处理中的应用及原理
android·python·adb
言之。3 小时前
【面试题】构建高并发、高可用服务架构:技术选型与设计
架构
JasonYin~4 小时前
HarmonyOS NEXT 实战之元服务:静态案例效果---手机查看电量
android·华为·harmonyos
zhangphil4 小时前
Android adb查看某个进程的总线程数
android·adb
抛空5 小时前
Android14 - SystemServer进程的启动与工作流程分析
android