1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.util.leak; 16 17 import static android.content.res.Configuration.ORIENTATION_LANDSCAPE; 18 import static android.content.res.Configuration.ORIENTATION_PORTRAIT; 19 20 import android.annotation.IntDef; 21 import android.content.Context; 22 import android.content.res.Configuration; 23 import android.content.res.Resources; 24 import android.view.Surface; 25 26 import java.lang.annotation.Retention; 27 import java.lang.annotation.RetentionPolicy; 28 29 public class RotationUtils { 30 31 public static final int ROTATION_NONE = 0; 32 public static final int ROTATION_LANDSCAPE = 1; 33 public static final int ROTATION_UPSIDE_DOWN = 2; 34 public static final int ROTATION_SEASCAPE = 3; 35 36 // Not to be confused with Surface.Rotation 37 @IntDef(prefix = { "ROTATION_" }, value = { 38 ROTATION_NONE, 39 ROTATION_LANDSCAPE, 40 ROTATION_SEASCAPE, 41 ROTATION_UPSIDE_DOWN, 42 }) 43 @Retention(RetentionPolicy.SOURCE) 44 public @interface Rotation {}; 45 46 /** 47 * @return the current rotation, differentiating between none (rot_0), landscape (rot_90), and 48 * seascape (rot_180). upside down is not distinguished here 49 */ 50 @Rotation getRotation(Context context)51 public static int getRotation(Context context) { 52 int rot = context.getDisplay().getRotation(); 53 if (rot == Surface.ROTATION_90) { 54 return ROTATION_LANDSCAPE; 55 } else if (rot == Surface.ROTATION_270) { 56 return ROTATION_SEASCAPE; 57 } else { 58 return ROTATION_NONE; 59 } 60 } 61 62 /** 63 * @return the current rotation, differentiating between landscape (rot_90), seascape 64 * (rot_270), and upside down (rot_180) 65 */ 66 @Rotation getExactRotation(Context context)67 public static int getExactRotation(Context context) { 68 int rot = context.getDisplay().getRotation(); 69 if (rot == Surface.ROTATION_90) { 70 return ROTATION_LANDSCAPE; 71 } else if (rot == Surface.ROTATION_270) { 72 return ROTATION_SEASCAPE; 73 } else if (rot == Surface.ROTATION_180) { 74 return ROTATION_UPSIDE_DOWN; 75 } else { 76 return ROTATION_NONE; 77 } 78 } 79 80 /** * To string */ toString(@otation int rot)81 public static String toString(@Rotation int rot) { 82 switch (rot) { 83 case ROTATION_NONE: 84 return "None (0)"; 85 case ROTATION_LANDSCAPE: 86 return "Landscape (1)"; 87 case ROTATION_UPSIDE_DOWN: 88 return "Upside down (2)"; 89 case ROTATION_SEASCAPE: 90 return "Seascape (3)"; 91 default: 92 return "Unknown (" + rot + ")"; 93 } 94 } 95 96 /** 97 * Create a Resources using the specified rotation for the configuration. Use this to retrieve 98 * resources in values or values-land without needing an actual rotation to happen. 99 * 100 * @param rot the target rotation for which to create the resources 101 * @param context a context 102 * @return a Resources object configured for the given orientation 103 */ getResourcesForRotation(@otation int rot, Context context)104 public static Resources getResourcesForRotation(@Rotation int rot, Context context) { 105 int orientation; 106 switch (rot) { 107 case ROTATION_NONE: 108 case ROTATION_UPSIDE_DOWN: 109 orientation = ORIENTATION_PORTRAIT; 110 break; 111 case ROTATION_LANDSCAPE: 112 case ROTATION_SEASCAPE: 113 orientation = ORIENTATION_LANDSCAPE; 114 break; 115 116 default: 117 throw new IllegalArgumentException("Unknown rotation: " + rot); 118 } 119 Configuration c = new Configuration(context.getResources().getConfiguration()); 120 c.orientation = orientation; 121 Context rotated = context.createConfigurationContext(c); 122 return rotated.getResources(); 123 } 124 } 125