hi,粉丝朋友们!
上一节已经对车载的多屏互动进行了相关的技术方案介绍,以及相关的核心方法
moveRootTaskToDisplay的讲解和使用。
具体可以参考链接:https://blog.csdn.net/learnframework/article/details/130461689
本节就来进行代码实战

1、方案设计

要实现双屏互动,主要就只需要两个步骤:
1、手指动作识别
2、识别动作后触发寻找display,获取顶部task,触发moveTask操作
在这里插入图片描述

2、手势动作识别部分

触发手势设计:因为模拟器实现条件有限,所以这里采用是双指向右移动一定距离触发。
首先要进行对手指全局移动的监听,针对多个手指的触摸移动动作要进行识别。
代码实现:
单独建立一个PointerEventListener的实现子类叫做DoubleScreenMovePointerEventListener,在这里面的onPointerEvent方法即可以实现对应的触摸动作识别,这里实现是双指同时滑动触摸方式:

diff --git a/services/core/java/com/android/server/wm/DoubleScreenMovePointerEventListener.java b/services/core/java/com/android/server/wm/DoubleScreenMovePointerEventListener.java
new file mode 100644
index 000000000000..f9c765476d19
--- /dev/null
+++ b/services/core/java/com/android/server/wm/DoubleScreenMovePointerEventListener.java
@@ -0,0 +1,62 @@
+package com.android.server.wm;
+
+import android.view.MotionEvent;
+import android.view.WindowManagerPolicyConstants;
+
+public class DoubleScreenMovePointerEventListener implements WindowManagerPolicyConstants.PointerEventListener {
+    boolean shouldBeginMove = false;
+    int mPoint0FirstX = 0;
+    int mPoint1FirstX = 0;
+
+    int mPoint0LastX = 0;
+    int mPoint1LastX = 0;
+    int START_GAP = 20;//动作触发阈值,最少移动为20个像素才可以
+    private final WindowManagerService mService;
+
+    public DoubleScreenMovePointerEventListener(WindowManagerService mService, DisplayContent mDisplayContent) {
+        this.mService = mService;
+        this.mDisplayContent = mDisplayContent;
+    }
+
+    private final DisplayContent mDisplayContent;
+
+    @Override
+    public void onPointerEvent(MotionEvent motionEvent) {
+        android.util.Log.i("DoubleScreenTouch","DoubleScreenMovePointerEventListener onPointerEvent motionEvent = "+motionEvent);
+        switch (motionEvent.getActionMasked()) {
+            case MotionEvent.ACTION_DOWN:
+            case MotionEvent.ACTION_POINTER_DOWN:
+                if (motionEvent.getPointerCount() > 2) {
+                    shouldBeginMove = false;
+                    android.util.Log.i("DoubleScreen","DoubleScreenMovePointerEventListener motionEvent.getPointerCount() > 2 end DoubleScreenMove ");
+                }
+                if (motionEvent.getPointerCount() == 2) {
+                    if (mPoint0FirstX == 0 && mPoint1FirstX == 0) {
+                        mPoint0FirstX = (int)motionEvent.getX(0);
+                        mPoint1FirstX = (int)motionEvent.getX(1);
+                    }
+                }
+                break;
+           case MotionEvent.ACTION_MOVE:
+               if (motionEvent.getPointerCount() == 2) {
+                   if (!shouldBeginMove && motionEvent.getX(0)  - mPoint0FirstX > START_GAP &&
+                           motionEvent.getX(1)  - mPoint1FirstX > START_GAP) { //识别了双指动作达到触发task移动条件,则调用对应mDisplayContent.doTestMoveTaskToOtherDisplay方法
+                       android.util.Log.i("DoubleScreen","DoubleScreenMovePointerEventListener start DoubleScreenMove ");
+                       shouldBeginMove = true;
+                       mDisplayContent.doTestMoveTaskToOtherDisplay();
+                   }
+
+                   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;
+               android.util.Log.i("DoubleScreen","DoubleScreenMovePointerEventListener ACTION_UP end DoubleScreenMove ");
+               break;
+       }
+    }
+
+}

同时不要忘记需要把这个PointEventListener让displaycontent注册监听:

@@ -1063,7 +1085,9 @@ class DisplayContent extends RootDisplayArea implements WindowManagerPolicy.Disp
         // 1. All physical displays (multi-display).
         // 2. VirtualDisplays on VR, AA (and everything else).
         mTapDetector = new TaskTapPointerEventListener(mWmService, this);//需要传递WMS和DisplayContent方便调用
+        mDoubleScreenMoveListener = new DoubleScreenMovePointerEventListener(mWmService, this);
         registerPointerEventListener(mTapDetector);
+        registerPointerEventListener(mDoubleScreenMoveListener);
         registerPointerEventListener(mWmService.mMousePositionTracker);
         if (mWmService.mAtmService.getRecentTasks() != null) {
             registerPointerEventListener(

3、寻找当前Display的Task,移动到目标display
触摸动作识别后会调用DisplayContent的doTestMoveTaskToOtherDisplay方法来完成剩下的业务:
1、获取要移动到的目标display对象
2、获取当前display要移动的Task对象
3、调用RootWindowContainer的moveRootTaskToDisplay方法来实现

+//add by doublescreenmove
+    public void doTestMoveTaskToOtherDisplay() {
+        DisplayContent otherDisplay = null;
+        if (mRootWindowContainer.getChildCount() == 2) {//检测是不是双屏
+            otherDisplay = (mRootWindowContainer.getChildAt(0) == this) ? mRootWindowContainer.getChildAt(1):mRootWindowContainer.getChildAt(0);//获取另一个屏幕的DisplayContent
+        }
+        if (otherDisplay!= this && otherDisplay!= null) {
+            int rootTaskId = 0;
+            try {
+                Task rootTask = getTopRootTask();//获取当前display的顶部Task
+                if (rootTask.isActivityTypeHome()) {//home类型的task不支持移动
+                    android.util.Log.i("DoubleScreen","doTestMoveTaskToOtherDisplay isActivityTypeHome");
+                    return;
+                }
+                rootTaskId =rootTask.mTaskId;
+                mRootWindowContainer.moveRootTaskToDisplay(rootTaskId,otherDisplay.mDisplayId,true);//把task移动到另一屏
+            }catch (Exception e) {
+                android.util.Log.i("DoubleScreen","doTestMoveTaskToOtherDisplay Exception",e);
+            }
+        }
+    }
+ //end  by doublescreenmove

成果展示:
在这里插入图片描述

模拟器调试要点:

这个部分请关注bibili的 视频
https://www.bilibili.com/video/BV1Tv4y1J7eb

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐