1 /*
2  * Copyright (C) 2021 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 package com.android.quickstep;
17 
18 import static com.android.launcher3.states.RotationHelper.deltaRotation;
19 import static com.android.quickstep.util.RecentsOrientedState.postDisplayRotation;
20 
21 import android.graphics.Matrix;
22 import android.graphics.RectF;
23 import android.util.Log;
24 import android.view.MotionEvent;
25 
26 public class OrientationRectF extends RectF {
27 
28     private static final String TAG = "OrientationRectF";
29     private static final boolean DEBUG = false;
30 
31     private final int mRotation;
32     private final float mHeight;
33     private final float mWidth;
34 
35     private final Matrix mTmpMatrix = new Matrix();
36     private final float[] mTmpPoint = new float[2];
37 
OrientationRectF(float left, float top, float right, float bottom, int rotation)38     public OrientationRectF(float left, float top, float right, float bottom, int rotation) {
39         super(left, top, right, bottom);
40         mRotation = rotation;
41         mHeight = bottom;
42         mWidth = right;
43     }
44 
45     @Override
toString()46     public String toString() {
47         String s = super.toString();
48         s += " rotation: " + mRotation;
49         return s;
50     }
51 
52     @Override
contains(float x, float y)53     public boolean contains(float x, float y) {
54         // Mark bottom right as included in the Rect (copied from Rect src, added "=" in "<=")
55         return left < right && top < bottom  // check for empty first
56                 && x >= left && x <= right && y >= top && y <= bottom;
57     }
58 
applyTransformFromRotation(MotionEvent event, int currentRotation, boolean forceTransform)59     public boolean applyTransformFromRotation(MotionEvent event, int currentRotation,
60             boolean forceTransform) {
61         return applyTransform(event, deltaRotation(currentRotation, mRotation), forceTransform);
62     }
63 
applyTransformToRotation(MotionEvent event, int currentRotation, boolean forceTransform)64     public boolean applyTransformToRotation(MotionEvent event, int currentRotation,
65             boolean forceTransform) {
66         return applyTransform(event, deltaRotation(mRotation, currentRotation), forceTransform);
67     }
68 
applyTransform(MotionEvent event, int deltaRotation, boolean forceTransform)69     public boolean applyTransform(MotionEvent event, int deltaRotation, boolean forceTransform) {
70         mTmpMatrix.reset();
71         postDisplayRotation(deltaRotation, mHeight, mWidth, mTmpMatrix);
72         if (forceTransform) {
73             if (DEBUG) {
74                 Log.d(TAG, "Transforming rotation due to forceTransform, "
75                         + "deltaRotation: " + deltaRotation
76                         + "mRotation: " + mRotation
77                         + " this: " + this);
78             }
79             event.applyTransform(mTmpMatrix);
80             return true;
81         }
82         mTmpPoint[0] = event.getX();
83         mTmpPoint[1] = event.getY();
84         mTmpMatrix.mapPoints(mTmpPoint);
85 
86         if (DEBUG) {
87             Log.d(TAG, "original: " + event.getX() + ", " + event.getY()
88                     + " new: " + mTmpPoint[0] + ", " + mTmpPoint[1]
89                     + " rect: " + this + " forceTransform: " + forceTransform
90                     + " contains: " + contains(mTmpPoint[0], mTmpPoint[1])
91                     + " this: " + this);
92         }
93 
94         if (contains(mTmpPoint[0], mTmpPoint[1])) {
95             event.applyTransform(mTmpMatrix);
96             return true;
97         }
98         return false;
99     }
100 
getRotation()101     int getRotation() {
102         return mRotation;
103     }
104 }
105