1/* 2 * Copyright (C) 2006 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 17package android.view; 18 19import android.util.DisplayMetrics; 20 21public class Display 22{ 23 /** 24 * Specify the default Display 25 */ 26 public static final int DEFAULT_DISPLAY = 0; 27 28 29 /** 30 * Use the WindowManager interface to create a Display object. 31 * Display gives you access to some information about a particular display 32 * connected to the device. 33 */ 34 Display(int display) { 35 // initalize the statics when this class is first instansiated. This is 36 // done here instead of in the static block because Zygote 37 synchronized (mStaticInit) { 38 if (!mInitialized) { 39 nativeClassInit(); 40 mInitialized = true; 41 } 42 } 43 mDisplay = display; 44 init(display); 45 } 46 47 /** 48 * Returns the index of this display. This is currently undefined; do 49 * not use. 50 */ 51 public int getDisplayId() { 52 return mDisplay; 53 } 54 55 /** 56 * Returns the number of displays connected to the device. This is 57 * currently undefined; do not use. 58 */ 59 native static int getDisplayCount(); 60 61 /** 62 * Returns the raw width of the display, in pixels. Note that this 63 * should <em>not</em> generally be used for computing layouts, since 64 * a device will typically have screen decoration (such as a status bar) 65 * along the edges of the display that reduce the amount of application 66 * space available from the raw size returned here. This value is 67 * adjusted for you based on the current rotation of the display. 68 */ 69 native public int getWidth(); 70 71 /** 72 * Returns the raw height of the display, in pixels. Note that this 73 * should <em>not</em> generally be used for computing layouts, since 74 * a device will typically have screen decoration (such as a status bar) 75 * along the edges of the display that reduce the amount of application 76 * space available from the raw size returned here. This value is 77 * adjusted for you based on the current rotation of the display. 78 */ 79 native public int getHeight(); 80 81 /** 82 * Returns the rotation of the screen from its "natural" orientation. 83 * The returned value may be {@link Surface#ROTATION_0 Surface.ROTATION_0} 84 * (no rotation), {@link Surface#ROTATION_90 Surface.ROTATION_90}, 85 * {@link Surface#ROTATION_180 Surface.ROTATION_180}, or 86 * {@link Surface#ROTATION_270 Surface.ROTATION_270}. For 87 * example, if a device has a naturally tall screen, and the user has 88 * turned it on its side to go into a landscape orientation, the value 89 * returned here may be either {@link Surface#ROTATION_90 Surface.ROTATION_90} 90 * or {@link Surface#ROTATION_270 Surface.ROTATION_270} depending on 91 * the direction it was turned. The angle is the rotation of the drawn 92 * graphics on the screen, which is the opposite direction of the physical 93 * rotation of the device. For example, if the device is rotated 90 94 * degrees counter-clockwise, to compensate rendering will be rotated by 95 * 90 degrees clockwise and thus the returned value here will be 96 * {@link Surface#ROTATION_90 Surface.ROTATION_90}. 97 */ 98 public int getRotation() { 99 return getOrientation(); 100 } 101 102 /** 103 * @deprecated use {@link #getRotation} 104 * @return orientation of this display. 105 */ 106 @Deprecated native public int getOrientation(); 107 108 /** 109 * Return the native pixel format of the display. The returned value 110 * may be one of the constants int {@link android.graphics.PixelFormat}. 111 */ 112 public int getPixelFormat() { 113 return mPixelFormat; 114 } 115 116 /** 117 * Return the refresh rate of this display in frames per second. 118 */ 119 public float getRefreshRate() { 120 return mRefreshRate; 121 } 122 123 /** 124 * Initialize a DisplayMetrics object from this display's data. 125 * 126 * @param outMetrics 127 */ 128 public void getMetrics(DisplayMetrics outMetrics) { 129 outMetrics.widthPixels = getWidth(); 130 outMetrics.heightPixels = getHeight(); 131 outMetrics.density = mDensity; 132 outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f); 133 outMetrics.scaledDensity= outMetrics.density; 134 outMetrics.xdpi = mDpiX; 135 outMetrics.ydpi = mDpiY; 136 } 137 138 /* 139 * We use a class initializer to allow the native code to cache some 140 * field offsets. 141 */ 142 native private static void nativeClassInit(); 143 144 private native void init(int display); 145 146 private int mDisplay; 147 // Following fields are initialized from native code 148 private int mPixelFormat; 149 private float mRefreshRate; 150 private float mDensity; 151 private float mDpiX; 152 private float mDpiY; 153 154 private static final Object mStaticInit = new Object(); 155 private static boolean mInitialized = false; 156 157 /** 158 * Returns a display object which uses the metric's width/height instead. 159 * @hide 160 */ 161 public static Display createMetricsBasedDisplay(int displayId, DisplayMetrics metrics) { 162 return new CompatibleDisplay(displayId, metrics); 163 } 164 165 private static class CompatibleDisplay extends Display { 166 private final DisplayMetrics mMetrics; 167 168 private CompatibleDisplay(int displayId, DisplayMetrics metrics) { 169 super(displayId); 170 mMetrics = metrics; 171 } 172 173 @Override 174 public int getWidth() { 175 return mMetrics.widthPixels; 176 } 177 178 @Override 179 public int getHeight() { 180 return mMetrics.heightPixels; 181 } 182 } 183} 184 185