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