DisplayDeviceInfo.java revision 77aebfdbae489c3712ae3f9bca29d01fb1f09dc2
1/*
2 * Copyright (C) 2012 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 com.android.server.display;
18
19import android.util.DisplayMetrics;
20
21import libcore.util.Objects;
22
23/**
24 * Describes the characteristics of a physical display device.
25 */
26final class DisplayDeviceInfo {
27    /**
28     * Flag: Indicates that this display device should be considered the default display
29     * device of the system.
30     */
31    public static final int FLAG_DEFAULT_DISPLAY = 1 << 0;
32
33    /**
34     * Flag: Indicates that this display device can rotate to show contents in a
35     * different orientation.  Otherwise the rotation is assumed to be fixed in the
36     * natural orientation and the display manager should transform the content to fit.
37     */
38    public static final int FLAG_SUPPORTS_ROTATION = 1 << 1;
39
40    /**
41     * Flag: Indicates that this display device has secure video output, such as HDCP.
42     */
43    public static final int FLAG_SECURE = 1 << 2;
44
45    /**
46     * Flag: Indicates that this display device supports compositing
47     * from gralloc protected buffers.
48     */
49    public static final int FLAG_SUPPORTS_PROTECTED_BUFFERS = 1 << 3;
50
51    /**
52     * Touch attachment: Display does not receive touch.
53     */
54    public static final int TOUCH_NONE = 0;
55
56    /**
57     * Touch attachment: Touch input is via the internal interface.
58     */
59    public static final int TOUCH_INTERNAL = 1;
60
61    /**
62     * Touch attachment: Touch input is via an external interface, such as USB.
63     */
64    public static final int TOUCH_EXTERNAL = 2;
65
66    /**
67     * Gets the name of the display device, which may be derived from
68     * EDID or other sources.  The name may be displayed to the user.
69     */
70    public String name;
71
72    /**
73     * The width of the display in its natural orientation, in pixels.
74     * This value is not affected by display rotation.
75     */
76    public int width;
77
78    /**
79     * The height of the display in its natural orientation, in pixels.
80     * This value is not affected by display rotation.
81     */
82    public int height;
83
84    /**
85     * The refresh rate of the display.
86     */
87    public float refreshRate;
88
89    /**
90     * The nominal apparent density of the display in DPI used for layout calculations.
91     * This density is sensitive to the viewing distance.  A big TV and a tablet may have
92     * the same apparent density even though the pixels on the TV are much bigger than
93     * those on the tablet.
94     */
95    public int densityDpi;
96
97    /**
98     * The physical density of the display in DPI in the X direction.
99     * This density should specify the physical size of each pixel.
100     */
101    public float xDpi;
102
103    /**
104     * The physical density of the display in DPI in the X direction.
105     * This density should specify the physical size of each pixel.
106     */
107    public float yDpi;
108
109    /**
110     * Display flags.
111     */
112    public int flags;
113
114    /**
115     * The touch attachment, per {@link DisplayViewport#touch}.
116     */
117    public int touch;
118
119    public void setAssumedDensityForExternalDisplay(int width, int height) {
120        densityDpi = Math.min(width, height) * DisplayMetrics.DENSITY_XHIGH / 1080;
121        // Technically, these values should be smaller than the apparent density
122        // but we don't know the physical size of the display.
123        xDpi = densityDpi;
124        yDpi = densityDpi;
125    }
126
127    @Override
128    public boolean equals(Object o) {
129        return o instanceof DisplayDeviceInfo && equals((DisplayDeviceInfo)o);
130    }
131
132    public boolean equals(DisplayDeviceInfo other) {
133        return other != null
134                && Objects.equal(name, other.name)
135                && width == other.width
136                && height == other.height
137                && refreshRate == other.refreshRate
138                && densityDpi == other.densityDpi
139                && xDpi == other.xDpi
140                && yDpi == other.yDpi
141                && flags == other.flags
142                && touch == other.touch;
143    }
144
145    @Override
146    public int hashCode() {
147        return 0; // don't care
148    }
149
150    public void copyFrom(DisplayDeviceInfo other) {
151        name = other.name;
152        width = other.width;
153        height = other.height;
154        refreshRate = other.refreshRate;
155        densityDpi = other.densityDpi;
156        xDpi = other.xDpi;
157        yDpi = other.yDpi;
158        flags = other.flags;
159        touch = other.touch;
160    }
161
162    // For debugging purposes
163    @Override
164    public String toString() {
165        return "DisplayDeviceInfo{\"" + name + "\": " + width + " x " + height + ", " + refreshRate + " fps, "
166                + "density " + densityDpi + ", " + xDpi + " x " + yDpi + " dpi"
167                + ", touch " + touchToString(touch) + flagsToString(flags) + "}";
168    }
169
170    private static String touchToString(int touch) {
171        switch (touch) {
172            case TOUCH_NONE:
173                return "NONE";
174            case TOUCH_INTERNAL:
175                return "INTERNAL";
176            case TOUCH_EXTERNAL:
177                return "EXTERNAL";
178            default:
179                return Integer.toString(touch);
180        }
181    }
182
183    private static String flagsToString(int flags) {
184        StringBuilder msg = new StringBuilder();
185        if ((flags & FLAG_DEFAULT_DISPLAY) != 0) {
186            msg.append(", FLAG_DEFAULT_DISPLAY");
187        }
188        if ((flags & FLAG_SUPPORTS_ROTATION) != 0) {
189            msg.append(", FLAG_SUPPORTS_ROTATION");
190        }
191        if ((flags & FLAG_SECURE) != 0) {
192            msg.append(", FLAG_SECURE");
193        }
194        if ((flags & FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
195            msg.append(", FLAG_SUPPORTS_PROTECTED_BUFFERS");
196        }
197        return msg.toString();
198    }
199}
200