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 17 package android.view; 18 19 import android.util.DisplayMetrics; 20 21 public 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 */ Display(int display)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 * @return index of this display. 49 */ getDisplayId()50 public int getDisplayId() { 51 return mDisplay; 52 } 53 54 /** 55 * @return the number of displays connected to the device. 56 */ getDisplayCount()57 native static int getDisplayCount(); 58 59 /** 60 * @return width of this display in pixels. 61 */ getWidth()62 native public int getWidth(); 63 64 /** 65 * @return height of this display in pixels. 66 */ getHeight()67 native public int getHeight(); 68 69 /** 70 * @return orientation of this display. 71 */ getOrientation()72 native public int getOrientation(); 73 74 /** 75 * @return pixel format of this display. 76 */ getPixelFormat()77 public int getPixelFormat() { 78 return mPixelFormat; 79 } 80 81 /** 82 * @return refresh rate of this display in frames per second. 83 */ getRefreshRate()84 public float getRefreshRate() { 85 return mRefreshRate; 86 } 87 88 /** 89 * Initialize a DisplayMetrics object from this display's data. 90 * 91 * @param outMetrics 92 */ getMetrics(DisplayMetrics outMetrics)93 public void getMetrics(DisplayMetrics outMetrics) { 94 outMetrics.widthPixels = getWidth(); 95 outMetrics.heightPixels = getHeight(); 96 outMetrics.density = mDensity; 97 outMetrics.densityDpi = (int)((mDensity*DisplayMetrics.DENSITY_DEFAULT)+.5f); 98 outMetrics.scaledDensity= outMetrics.density; 99 outMetrics.xdpi = mDpiX; 100 outMetrics.ydpi = mDpiY; 101 } 102 103 /* 104 * We use a class initializer to allow the native code to cache some 105 * field offsets. 106 */ nativeClassInit()107 native private static void nativeClassInit(); 108 init(int display)109 private native void init(int display); 110 111 private int mDisplay; 112 // Following fields are initialized from native code 113 private int mPixelFormat; 114 private float mRefreshRate; 115 private float mDensity; 116 private float mDpiX; 117 private float mDpiY; 118 119 private static final Object mStaticInit = new Object(); 120 private static boolean mInitialized = false; 121 122 /** 123 * Returns a display object which uses the metric's width/height instead. 124 * @hide 125 */ createMetricsBasedDisplay(int displayId, DisplayMetrics metrics)126 public static Display createMetricsBasedDisplay(int displayId, DisplayMetrics metrics) { 127 return new CompatibleDisplay(displayId, metrics); 128 } 129 130 private static class CompatibleDisplay extends Display { 131 private final DisplayMetrics mMetrics; 132 CompatibleDisplay(int displayId, DisplayMetrics metrics)133 private CompatibleDisplay(int displayId, DisplayMetrics metrics) { 134 super(displayId); 135 mMetrics = metrics; 136 } 137 138 @Override getWidth()139 public int getWidth() { 140 return mMetrics.widthPixels; 141 } 142 143 @Override getHeight()144 public int getHeight() { 145 return mMetrics.heightPixels; 146 } 147 } 148 } 149 150