| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.ui.gfx; | |
| 6 | |
| 7 import android.annotation.TargetApi; | |
| 8 import android.content.Context; | |
| 9 import android.graphics.PixelFormat; | |
| 10 import android.graphics.Point; | |
| 11 import android.os.Build; | |
| 12 import android.util.DisplayMetrics; | |
| 13 import android.view.Display; | |
| 14 import android.view.Surface; | |
| 15 import android.view.WindowManager; | |
| 16 | |
| 17 import org.chromium.base.annotations.CalledByNative; | |
| 18 import org.chromium.base.annotations.JNINamespace; | |
| 19 import org.chromium.base.annotations.MainDex; | |
| 20 | |
| 21 /** | |
| 22 * This class facilitates access to android information typically only | |
| 23 * available using the Java SDK, including {@link Display} properties. | |
| 24 * | |
| 25 * Currently the information consists of very raw display information (height, w
idth, DPI scale) | |
| 26 * regarding the main display. | |
| 27 * | |
| 28 * This class is being Deprecated. Use DisplayAndroid instead where possible. | |
| 29 */ | |
| 30 @JNINamespace("gfx") | |
| 31 @MainDex | |
| 32 public class DeviceDisplayInfo { | |
| 33 | |
| 34 private final Context mAppContext; | |
| 35 private final WindowManager mWinManager; | |
| 36 private Point mTempPoint = new Point(); | |
| 37 private DisplayMetrics mTempMetrics = new DisplayMetrics(); | |
| 38 | |
| 39 private DeviceDisplayInfo(Context context) { | |
| 40 mAppContext = context.getApplicationContext(); | |
| 41 mWinManager = (WindowManager) mAppContext.getSystemService(Context.WINDO
W_SERVICE); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * @return Display height in physical pixels. | |
| 46 */ | |
| 47 @CalledByNative | |
| 48 public int getDisplayHeight() { | |
| 49 getDisplay().getSize(mTempPoint); | |
| 50 return mTempPoint.y; | |
| 51 } | |
| 52 | |
| 53 /** | |
| 54 * @return Display width in physical pixels. | |
| 55 */ | |
| 56 @CalledByNative | |
| 57 public int getDisplayWidth() { | |
| 58 getDisplay().getSize(mTempPoint); | |
| 59 return mTempPoint.x; | |
| 60 } | |
| 61 | |
| 62 /** | |
| 63 * @return Real physical display height in physical pixels. | |
| 64 */ | |
| 65 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | |
| 66 @CalledByNative | |
| 67 public int getPhysicalDisplayHeight() { | |
| 68 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
| 69 return 0; | |
| 70 } | |
| 71 getDisplay().getRealSize(mTempPoint); | |
| 72 return mTempPoint.y; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * @return Real physical display width in physical pixels. | |
| 77 */ | |
| 78 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) | |
| 79 @CalledByNative | |
| 80 public int getPhysicalDisplayWidth() { | |
| 81 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
| 82 return 0; | |
| 83 } | |
| 84 getDisplay().getRealSize(mTempPoint); | |
| 85 return mTempPoint.x; | |
| 86 } | |
| 87 | |
| 88 @SuppressWarnings("deprecation") | |
| 89 private int getPixelFormat() { | |
| 90 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
| 91 return getDisplay().getPixelFormat(); | |
| 92 } | |
| 93 // JellyBean MR1 and later always uses RGBA_8888. | |
| 94 return PixelFormat.RGBA_8888; | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * @return Bits per pixel. | |
| 99 */ | |
| 100 @CalledByNative | |
| 101 private int getBitsPerPixel() { | |
| 102 int format = getPixelFormat(); | |
| 103 PixelFormat info = new PixelFormat(); | |
| 104 PixelFormat.getPixelFormatInfo(format, info); | |
| 105 return info.bitsPerPixel; | |
| 106 } | |
| 107 | |
| 108 /** | |
| 109 * @return Bits per component. | |
| 110 */ | |
| 111 @SuppressWarnings("deprecation") | |
| 112 @CalledByNative | |
| 113 private int getBitsPerComponent() { | |
| 114 int format = getPixelFormat(); | |
| 115 switch (format) { | |
| 116 case PixelFormat.RGBA_4444: | |
| 117 return 4; | |
| 118 | |
| 119 case PixelFormat.RGBA_5551: | |
| 120 return 5; | |
| 121 | |
| 122 case PixelFormat.RGBA_8888: | |
| 123 case PixelFormat.RGBX_8888: | |
| 124 case PixelFormat.RGB_888: | |
| 125 return 8; | |
| 126 | |
| 127 case PixelFormat.RGB_332: | |
| 128 return 2; | |
| 129 | |
| 130 case PixelFormat.RGB_565: | |
| 131 return 5; | |
| 132 | |
| 133 // Non-RGB formats. | |
| 134 case PixelFormat.A_8: | |
| 135 case PixelFormat.LA_88: | |
| 136 case PixelFormat.L_8: | |
| 137 return 0; | |
| 138 | |
| 139 // Unknown format. Use 8 as a sensible default. | |
| 140 default: | |
| 141 return 8; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 /** | |
| 146 * @return A scaling factor for the Density Independent Pixel unit. 1.0 is | |
| 147 * 160dpi, 0.75 is 120dpi, 2.0 is 320dpi. | |
| 148 */ | |
| 149 @CalledByNative | |
| 150 public double getDIPScale() { | |
| 151 getDisplay().getMetrics(mTempMetrics); | |
| 152 return mTempMetrics.density; | |
| 153 } | |
| 154 | |
| 155 /** | |
| 156 * @return Smallest screen size in density-independent pixels that the | |
| 157 * application will see, regardless of orientation. | |
| 158 */ | |
| 159 @CalledByNative | |
| 160 private int getSmallestDIPWidth() { | |
| 161 return mAppContext.getResources().getConfiguration().smallestScreenWidth
Dp; | |
| 162 } | |
| 163 | |
| 164 /** | |
| 165 * @return the screen's rotation angle from its 'natural' orientation. | |
| 166 * Expected values are one of { 0, 90, 180, 270 }. | |
| 167 * See http://developer.android.com/reference/android/view/Display.html#getR
otation() | |
| 168 * for more information about Display.getRotation() behavior. | |
| 169 */ | |
| 170 @CalledByNative | |
| 171 public int getRotationDegrees() { | |
| 172 switch (getDisplay().getRotation()) { | |
| 173 case Surface.ROTATION_0: | |
| 174 return 0; | |
| 175 case Surface.ROTATION_90: | |
| 176 return 90; | |
| 177 case Surface.ROTATION_180: | |
| 178 return 180; | |
| 179 case Surface.ROTATION_270: | |
| 180 return 270; | |
| 181 } | |
| 182 | |
| 183 // This should not happen. | |
| 184 assert false; | |
| 185 return 0; | |
| 186 } | |
| 187 | |
| 188 /** | |
| 189 * Inform the native implementation to update its cached representation of | |
| 190 * the DeviceDisplayInfo values. | |
| 191 */ | |
| 192 public void updateNativeSharedDisplayInfo() { | |
| 193 nativeUpdateSharedDeviceDisplayInfo( | |
| 194 getDisplayHeight(), getDisplayWidth(), | |
| 195 getPhysicalDisplayHeight(), getPhysicalDisplayWidth(), | |
| 196 getBitsPerPixel(), getBitsPerComponent(), | |
| 197 getDIPScale(), getSmallestDIPWidth(), getRotationDegrees()); | |
| 198 } | |
| 199 | |
| 200 private Display getDisplay() { | |
| 201 return mWinManager.getDefaultDisplay(); | |
| 202 } | |
| 203 | |
| 204 /** | |
| 205 * Creates DeviceDisplayInfo for a given Context. | |
| 206 * | |
| 207 * @param context A context to use. | |
| 208 * @return DeviceDisplayInfo associated with a given Context. | |
| 209 */ | |
| 210 @CalledByNative | |
| 211 public static DeviceDisplayInfo create(Context context) { | |
| 212 return new DeviceDisplayInfo(context); | |
| 213 } | |
| 214 | |
| 215 private native void nativeUpdateSharedDeviceDisplayInfo( | |
| 216 int displayHeight, int displayWidth, | |
| 217 int physicalDisplayHeight, int physicalDisplayWidth, | |
| 218 int bitsPerPixel, int bitsPerComponent, double dipScale, | |
| 219 int smallestDIPWidth, int rotationDegrees); | |
| 220 | |
| 221 } | |
| OLD | NEW |