| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.util; |
| 18 |
|
| 19 |
import static android.view.Surface.ROTATION_0; |
| 20 |
import static android.view.Surface.ROTATION_180; |
| 21 |
import static android.view.Surface.ROTATION_270; |
| 22 |
import static android.view.Surface.ROTATION_90; |
| 23 |
|
| 24 |
import android.app.WindowConfiguration; |
| 25 |
import android.content.Context; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.hardware.display.DisplayManagerGlobal; |
| 28 |
import android.view.DisplayInfo; |
| 29 |
import android.view.Surface; |
| 30 |
import android.view.WindowManager; |
| 31 |
|
| 32 |
import androidx.annotation.NonNull; |
| 33 |
import androidx.annotation.UiContext; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public final class ExtensionHelper { |
| 39 |
|
| 40 |
private ExtensionHelper() { |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static void rotateRectToDisplayRotation(int displayId, Rect inOutRect) { |
| 49 |
DisplayManagerGlobal dmGlobal = DisplayManagerGlobal.getInstance(); |
| 50 |
DisplayInfo displayInfo = dmGlobal.getDisplayInfo(displayId); |
| 51 |
int rotation = displayInfo.rotation; |
| 52 |
|
| 53 |
boolean isSideRotation = rotation == ROTATION_90 || rotation == ROTATION_270; |
| 54 |
int displayWidth = isSideRotation ? displayInfo.logicalHeight : displayInfo.logicalWidth; |
| 55 |
int displayHeight = isSideRotation ? displayInfo.logicalWidth : displayInfo.logicalHeight; |
| 56 |
|
| 57 |
inOutRect.intersect(0, 0, displayWidth, displayHeight); |
| 58 |
|
| 59 |
rotateBounds(inOutRect, displayWidth, displayHeight, rotation); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private static void rotateBounds(Rect inOutRect, int parentWidth, int parentHeight, |
| 66 |
@Surface.Rotation int delta) { |
| 67 |
int origLeft = inOutRect.left; |
| 68 |
switch (delta) { |
| 69 |
case ROTATION_0: |
| 70 |
return; |
| 71 |
case ROTATION_90: |
| 72 |
inOutRect.left = inOutRect.top; |
| 73 |
inOutRect.top = parentWidth - inOutRect.right; |
| 74 |
inOutRect.right = inOutRect.bottom; |
| 75 |
inOutRect.bottom = parentWidth - origLeft; |
| 76 |
return; |
| 77 |
case ROTATION_180: |
| 78 |
inOutRect.left = parentWidth - inOutRect.right; |
| 79 |
inOutRect.right = parentWidth - origLeft; |
| 80 |
return; |
| 81 |
case ROTATION_270: |
| 82 |
inOutRect.left = parentHeight - inOutRect.bottom; |
| 83 |
inOutRect.bottom = inOutRect.right; |
| 84 |
inOutRect.right = parentHeight - inOutRect.top; |
| 85 |
inOutRect.top = origLeft; |
| 86 |
return; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public static void transformToWindowSpaceRect(@NonNull @UiContext Context context, |
| 92 |
Rect inOutRect) { |
| 93 |
transformToWindowSpaceRect(getWindowBounds(context), inOutRect); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public static void transformToWindowSpaceRect(@NonNull WindowConfiguration windowConfiguration, |
| 98 |
Rect inOutRect) { |
| 99 |
transformToWindowSpaceRect(windowConfiguration.getBounds(), inOutRect); |
| 100 |
} |
| 101 |
|
| 102 |
private static void transformToWindowSpaceRect(@NonNull Rect bounds, @NonNull Rect inOutRect) { |
| 103 |
if (!inOutRect.intersect(bounds)) { |
| 104 |
inOutRect.setEmpty(); |
| 105 |
return; |
| 106 |
} |
| 107 |
inOutRect.offset(-bounds.left, -bounds.top); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@NonNull |
| 114 |
private static Rect getWindowBounds(@NonNull @UiContext Context context) { |
| 115 |
return context.getSystemService(WindowManager.class).getCurrentWindowMetrics().getBounds(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static boolean isZero(@NonNull Rect rect) { |
| 122 |
return rect.height() == 0 && rect.width() == 0; |
| 123 |
} |
| 124 |
} |