1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.wm; 18 19 import android.graphics.Rect; 20 import android.graphics.Region; 21 import android.hardware.input.InputManager; 22 import android.view.MotionEvent; 23 import android.view.WindowManagerPolicyConstants.PointerEventListener; 24 25 import com.android.server.wm.WindowManagerService.H; 26 27 import static android.view.Display.DEFAULT_DISPLAY; 28 import static android.view.PointerIcon.TYPE_NOT_SPECIFIED; 29 import static android.view.PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW; 30 import static android.view.PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW; 31 import static android.view.PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW; 32 import static android.view.PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW; 33 34 public class TaskTapPointerEventListener implements PointerEventListener { 35 36 final private Region mTouchExcludeRegion = new Region(); 37 private final WindowManagerService mService; 38 private final DisplayContent mDisplayContent; 39 private final Rect mTmpRect = new Rect(); 40 private int mPointerIconType = TYPE_NOT_SPECIFIED; 41 42 public TaskTapPointerEventListener(WindowManagerService service, 43 DisplayContent displayContent) { 44 mService = service; 45 mDisplayContent = displayContent; 46 } 47 48 @Override 49 public void onPointerEvent(MotionEvent motionEvent, int displayId) { 50 if (displayId == getDisplayId()) { 51 onPointerEvent(motionEvent); 52 } 53 } 54 55 @Override 56 public void onPointerEvent(MotionEvent motionEvent) { 57 final int action = motionEvent.getAction(); 58 switch (action & MotionEvent.ACTION_MASK) { 59 case MotionEvent.ACTION_DOWN: { 60 final int x = (int) motionEvent.getX(); 61 final int y = (int) motionEvent.getY(); 62 63 synchronized (this) { 64 if (!mTouchExcludeRegion.contains(x, y)) { 65 mService.mTaskPositioningController.handleTapOutsideTask( 66 mDisplayContent, x, y); 67 } 68 } 69 } 70 break; 71 72 case MotionEvent.ACTION_HOVER_MOVE: { 73 final int x = (int) motionEvent.getX(); 74 final int y = (int) motionEvent.getY(); 75 final Task task = mDisplayContent.findTaskForResizePoint(x, y); 76 int iconType = TYPE_NOT_SPECIFIED; 77 if (task != null) { 78 task.getDimBounds(mTmpRect); 79 if (!mTmpRect.isEmpty() && !mTmpRect.contains(x, y)) { 80 if (x < mTmpRect.left) { 81 iconType = 82 (y < mTmpRect.top) ? TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW : 83 (y > mTmpRect.bottom) ? TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW : 84 TYPE_HORIZONTAL_DOUBLE_ARROW; 85 } else if (x > mTmpRect.right) { 86 iconType = 87 (y < mTmpRect.top) ? TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW : 88 (y > mTmpRect.bottom) ? TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW : 89 TYPE_HORIZONTAL_DOUBLE_ARROW; 90 } else if (y < mTmpRect.top || y > mTmpRect.bottom) { 91 iconType = TYPE_VERTICAL_DOUBLE_ARROW; 92 } 93 } 94 } 95 if (mPointerIconType != iconType) { 96 mPointerIconType = iconType; 97 if (mPointerIconType == TYPE_NOT_SPECIFIED) { 98 // Find the underlying window and ask it restore the pointer icon. 99 mService.mH.obtainMessage(H.RESTORE_POINTER_ICON, 100 x, y, mDisplayContent).sendToTarget(); 101 } else { 102 InputManager.getInstance().setPointerIconType(mPointerIconType); 103 } 104 } 105 } 106 break; 107 } 108 } 109 110 void setTouchExcludeRegion(Region newRegion) { 111 synchronized (this) { 112 mTouchExcludeRegion.set(newRegion); 113 } 114 } 115 116 private int getDisplayId() { 117 return mDisplayContent.getDisplayId(); 118 } 119 } 120