1 /* 2 * Copyright (C) 2022 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.launcher3.util; 17 18 import static android.view.Surface.ROTATION_0; 19 import static android.view.Surface.ROTATION_180; 20 import static android.view.Surface.ROTATION_270; 21 import static android.view.Surface.ROTATION_90; 22 23 import android.graphics.Point; 24 import android.graphics.Rect; 25 26 /** 27 * Utility methods based on {@code frameworks/base/core/java/android/util/RotationUtils.java} 28 */ 29 public class RotationUtils { 30 31 /** 32 * Rotates an Rect according to the given rotation. 33 */ rotateRect(Rect rect, int rotation)34 public static void rotateRect(Rect rect, int rotation) { 35 switch (rotation) { 36 case ROTATION_0: 37 return; 38 case ROTATION_90: 39 rect.set(rect.top, rect.right, rect.bottom, rect.left); 40 return; 41 case ROTATION_180: 42 rect.set(rect.right, rect.bottom, rect.left, rect.top); 43 return; 44 case ROTATION_270: 45 rect.set(rect.bottom, rect.left, rect.top, rect.right); 46 return; 47 default: 48 throw new IllegalArgumentException("unknown rotation: " + rotation); 49 } 50 } 51 52 /** 53 * Rotates an size according to the given rotation. 54 */ rotateSize(Point size, int rotation)55 public static void rotateSize(Point size, int rotation) { 56 switch (rotation) { 57 case ROTATION_0: 58 case ROTATION_180: 59 return; 60 case ROTATION_90: 61 case ROTATION_270: 62 size.set(size.y, size.x); 63 return; 64 default: 65 throw new IllegalArgumentException("unknown rotation: " + rotation); 66 } 67 } 68 69 /** @return the rotation needed to rotate from oldRotation to newRotation. */ deltaRotation(int oldRotation, int newRotation)70 public static int deltaRotation(int oldRotation, int newRotation) { 71 int delta = newRotation - oldRotation; 72 if (delta < 0) delta += 4; 73 return delta; 74 } 75 } 76