blob: 9a9c4cd3b4c02d8a7e7aa6db24e80113159b0501 [file] [log] [blame]
Jeff Brownfa25bf52012-07-23 19:26:30 -07001/*
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 android.view;
18
19import android.content.res.CompatibilityInfo;
Craig Mautner48d0d182013-06-11 07:53:06 -070020import android.os.IBinder;
Jeff Brownfa25bf52012-07-23 19:26:30 -070021import android.os.Parcel;
22import android.os.Parcelable;
Jeff Browna506a6e2013-06-04 00:02:38 -070023import android.os.Process;
Jeff Brownfa25bf52012-07-23 19:26:30 -070024import android.util.DisplayMetrics;
25
Jeff Brown4ed8fe72012-08-30 18:18:29 -070026import libcore.util.Objects;
27
Jeff Brownfa25bf52012-07-23 19:26:30 -070028/**
29 * Describes the characteristics of a particular logical display.
30 * @hide
31 */
32public final class DisplayInfo implements Parcelable {
33 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -070034 * The surface flinger layer stack associated with this logical display.
35 */
36 public int layerStack;
37
38 /**
Jeff Brownc5df37c2012-09-13 11:45:07 -070039 * Display flags.
40 */
41 public int flags;
42
43 /**
Jeff Brown92130f62012-10-24 21:28:33 -070044 * Display type.
45 */
46 public int type;
47
48 /**
49 * Display address, or null if none.
50 * Interpretation varies by display type.
51 */
52 public String address;
53
54 /**
Jeff Brown4ed8fe72012-08-30 18:18:29 -070055 * The human-readable name of the display.
56 */
57 public String name;
58
59 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -070060 * The width of the portion of the display that is available to applications, in pixels.
61 * Represents the size of the display minus any system decorations.
62 */
63 public int appWidth;
64
65 /**
66 * The height of the portion of the display that is available to applications, in pixels.
67 * Represents the size of the display minus any system decorations.
68 */
69 public int appHeight;
70
71 /**
72 * The smallest value of {@link #appWidth} that an application is likely to encounter,
73 * in pixels, excepting cases where the width may be even smaller due to the presence
74 * of a soft keyboard, for example.
75 */
76 public int smallestNominalAppWidth;
77
78 /**
79 * The smallest value of {@link #appHeight} that an application is likely to encounter,
80 * in pixels, excepting cases where the height may be even smaller due to the presence
81 * of a soft keyboard, for example.
82 */
83 public int smallestNominalAppHeight;
84
85 /**
86 * The largest value of {@link #appWidth} that an application is likely to encounter,
87 * in pixels, excepting cases where the width may be even larger due to system decorations
88 * such as the status bar being hidden, for example.
89 */
90 public int largestNominalAppWidth;
91
92 /**
93 * The largest value of {@link #appHeight} that an application is likely to encounter,
94 * in pixels, excepting cases where the height may be even larger due to system decorations
95 * such as the status bar being hidden, for example.
96 */
97 public int largestNominalAppHeight;
98
99 /**
100 * The logical width of the display, in pixels.
101 * Represents the usable size of the display which may be smaller than the
102 * physical size when the system is emulating a smaller display.
103 */
104 public int logicalWidth;
105
106 /**
107 * The logical height of the display, in pixels.
108 * Represents the usable size of the display which may be smaller than the
109 * physical size when the system is emulating a smaller display.
110 */
111 public int logicalHeight;
112
113 /**
Dianne Hackbornc652de82013-02-15 16:32:56 -0800114 * @hide
115 * Number of overscan pixels on the left side of the display.
116 */
117 public int overscanLeft;
118
119 /**
120 * @hide
121 * Number of overscan pixels on the top side of the display.
122 */
123 public int overscanTop;
124
125 /**
126 * @hide
127 * Number of overscan pixels on the right side of the display.
128 */
129 public int overscanRight;
130
131 /**
132 * @hide
133 * Number of overscan pixels on the bottom side of the display.
134 */
135 public int overscanBottom;
136
137 /**
Jeff Brownfa25bf52012-07-23 19:26:30 -0700138 * The rotation of the display relative to its natural orientation.
139 * May be one of {@link android.view.Surface#ROTATION_0},
140 * {@link android.view.Surface#ROTATION_90}, {@link android.view.Surface#ROTATION_180},
141 * {@link android.view.Surface#ROTATION_270}.
142 * <p>
143 * The value of this field is indeterminate if the logical display is presented on
144 * more than one physical display.
145 * </p>
146 */
147 public int rotation;
148
149 /**
150 * The refresh rate of this display in frames per second.
151 * <p>
152 * The value of this field is indeterminate if the logical display is presented on
153 * more than one physical display.
154 * </p>
155 */
156 public float refreshRate;
157
158 /**
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700159 * The logical display density which is the basis for density-independent
160 * pixels.
Jeff Brownfa25bf52012-07-23 19:26:30 -0700161 */
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700162 public int logicalDensityDpi;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700163
164 /**
165 * The exact physical pixels per inch of the screen in the X dimension.
166 * <p>
167 * The value of this field is indeterminate if the logical display is presented on
168 * more than one physical display.
169 * </p>
170 */
171 public float physicalXDpi;
172
173 /**
174 * The exact physical pixels per inch of the screen in the Y dimension.
175 * <p>
176 * The value of this field is indeterminate if the logical display is presented on
177 * more than one physical display.
178 * </p>
179 */
180 public float physicalYDpi;
181
Jeff Browna506a6e2013-06-04 00:02:38 -0700182 /**
183 * The UID of the application that owns this display, or zero if it is owned by the system.
184 * <p>
185 * If the display is private, then only the owner can use it.
186 * </p>
187 */
188 public int ownerUid;
189
190 /**
191 * The package name of the application that owns this display, or null if it is
192 * owned by the system.
193 * <p>
194 * If the display is private, then only the owner can use it.
195 * </p>
196 */
197 public String ownerPackageName;
198
Jeff Brownfa25bf52012-07-23 19:26:30 -0700199 public static final Creator<DisplayInfo> CREATOR = new Creator<DisplayInfo>() {
Jeff Brown92130f62012-10-24 21:28:33 -0700200 @Override
Jeff Brownfa25bf52012-07-23 19:26:30 -0700201 public DisplayInfo createFromParcel(Parcel source) {
202 return new DisplayInfo(source);
203 }
204
Jeff Brown92130f62012-10-24 21:28:33 -0700205 @Override
Jeff Brownfa25bf52012-07-23 19:26:30 -0700206 public DisplayInfo[] newArray(int size) {
207 return new DisplayInfo[size];
208 }
209 };
210
211 public DisplayInfo() {
212 }
213
Jeff Brownbd6e1502012-08-28 03:27:37 -0700214 public DisplayInfo(DisplayInfo other) {
215 copyFrom(other);
216 }
217
Jeff Brownfa25bf52012-07-23 19:26:30 -0700218 private DisplayInfo(Parcel source) {
219 readFromParcel(source);
220 }
221
222 @Override
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700223 public boolean equals(Object o) {
224 return o instanceof DisplayInfo && equals((DisplayInfo)o);
225 }
226
227 public boolean equals(DisplayInfo other) {
228 return other != null
229 && layerStack == other.layerStack
Jeff Brown92130f62012-10-24 21:28:33 -0700230 && flags == other.flags
231 && type == other.type
232 && Objects.equal(address, other.address)
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700233 && Objects.equal(name, other.name)
234 && appWidth == other.appWidth
235 && appHeight == other.appHeight
236 && smallestNominalAppWidth == other.smallestNominalAppWidth
237 && smallestNominalAppHeight == other.smallestNominalAppHeight
238 && largestNominalAppWidth == other.largestNominalAppWidth
239 && largestNominalAppHeight == other.largestNominalAppHeight
240 && logicalWidth == other.logicalWidth
241 && logicalHeight == other.logicalHeight
Dianne Hackbornc652de82013-02-15 16:32:56 -0800242 && overscanLeft == other.overscanLeft
243 && overscanTop == other.overscanTop
244 && overscanRight == other.overscanRight
245 && overscanBottom == other.overscanBottom
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700246 && rotation == other.rotation
247 && refreshRate == other.refreshRate
248 && logicalDensityDpi == other.logicalDensityDpi
249 && physicalXDpi == other.physicalXDpi
Jeff Browna506a6e2013-06-04 00:02:38 -0700250 && physicalYDpi == other.physicalYDpi
251 && ownerUid == other.ownerUid
252 && Objects.equal(ownerPackageName, other.ownerPackageName);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700253 }
254
255 @Override
256 public int hashCode() {
257 return 0; // don't care
Jeff Brownfa25bf52012-07-23 19:26:30 -0700258 }
259
260 public void copyFrom(DisplayInfo other) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700261 layerStack = other.layerStack;
Jeff Brownc5df37c2012-09-13 11:45:07 -0700262 flags = other.flags;
Jeff Brown92130f62012-10-24 21:28:33 -0700263 type = other.type;
264 address = other.address;
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700265 name = other.name;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700266 appWidth = other.appWidth;
267 appHeight = other.appHeight;
268 smallestNominalAppWidth = other.smallestNominalAppWidth;
269 smallestNominalAppHeight = other.smallestNominalAppHeight;
270 largestNominalAppWidth = other.largestNominalAppWidth;
271 largestNominalAppHeight = other.largestNominalAppHeight;
272 logicalWidth = other.logicalWidth;
273 logicalHeight = other.logicalHeight;
Dianne Hackbornc652de82013-02-15 16:32:56 -0800274 overscanLeft = other.overscanLeft;
275 overscanTop = other.overscanTop;
276 overscanRight = other.overscanRight;
277 overscanBottom = other.overscanBottom;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700278 rotation = other.rotation;
279 refreshRate = other.refreshRate;
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700280 logicalDensityDpi = other.logicalDensityDpi;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700281 physicalXDpi = other.physicalXDpi;
282 physicalYDpi = other.physicalYDpi;
Jeff Browna506a6e2013-06-04 00:02:38 -0700283 ownerUid = other.ownerUid;
284 ownerPackageName = other.ownerPackageName;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700285 }
286
287 public void readFromParcel(Parcel source) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700288 layerStack = source.readInt();
Jeff Brownc5df37c2012-09-13 11:45:07 -0700289 flags = source.readInt();
Jeff Brown92130f62012-10-24 21:28:33 -0700290 type = source.readInt();
291 address = source.readString();
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700292 name = source.readString();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700293 appWidth = source.readInt();
294 appHeight = source.readInt();
295 smallestNominalAppWidth = source.readInt();
296 smallestNominalAppHeight = source.readInt();
297 largestNominalAppWidth = source.readInt();
298 largestNominalAppHeight = source.readInt();
299 logicalWidth = source.readInt();
300 logicalHeight = source.readInt();
Dianne Hackbornc652de82013-02-15 16:32:56 -0800301 overscanLeft = source.readInt();
302 overscanTop = source.readInt();
303 overscanRight = source.readInt();
304 overscanBottom = source.readInt();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700305 rotation = source.readInt();
306 refreshRate = source.readFloat();
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700307 logicalDensityDpi = source.readInt();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700308 physicalXDpi = source.readFloat();
309 physicalYDpi = source.readFloat();
Jeff Browna506a6e2013-06-04 00:02:38 -0700310 ownerUid = source.readInt();
311 ownerPackageName = source.readString();
Jeff Brownfa25bf52012-07-23 19:26:30 -0700312 }
313
314 @Override
315 public void writeToParcel(Parcel dest, int flags) {
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700316 dest.writeInt(layerStack);
Jeff Brown3f2ba622012-10-04 14:57:44 -0700317 dest.writeInt(this.flags);
Jeff Brown92130f62012-10-24 21:28:33 -0700318 dest.writeInt(type);
319 dest.writeString(address);
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700320 dest.writeString(name);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700321 dest.writeInt(appWidth);
322 dest.writeInt(appHeight);
323 dest.writeInt(smallestNominalAppWidth);
324 dest.writeInt(smallestNominalAppHeight);
325 dest.writeInt(largestNominalAppWidth);
326 dest.writeInt(largestNominalAppHeight);
327 dest.writeInt(logicalWidth);
328 dest.writeInt(logicalHeight);
Dianne Hackbornc652de82013-02-15 16:32:56 -0800329 dest.writeInt(overscanLeft);
330 dest.writeInt(overscanTop);
331 dest.writeInt(overscanRight);
332 dest.writeInt(overscanBottom);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700333 dest.writeInt(rotation);
334 dest.writeFloat(refreshRate);
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700335 dest.writeInt(logicalDensityDpi);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700336 dest.writeFloat(physicalXDpi);
337 dest.writeFloat(physicalYDpi);
Jeff Browna506a6e2013-06-04 00:02:38 -0700338 dest.writeInt(ownerUid);
339 dest.writeString(ownerPackageName);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700340 }
341
Jeff Brown4ed8fe72012-08-30 18:18:29 -0700342 @Override
343 public int describeContents() {
344 return 0;
345 }
346
Craig Mautner48d0d182013-06-11 07:53:06 -0700347 public void getAppMetrics(DisplayMetrics outMetrics) {
348 getAppMetrics(outMetrics, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700349 }
350
Craig Mautner48d0d182013-06-11 07:53:06 -0700351 public void getAppMetrics(DisplayMetrics outMetrics, DisplayAdjustments displayAdjustments) {
352 getMetricsWithSize(outMetrics, displayAdjustments.getCompatibilityInfo(),
353 displayAdjustments.getActivityToken(), appWidth, appHeight);
354 }
355
356 public void getAppMetrics(DisplayMetrics outMetrics, CompatibilityInfo ci, IBinder token) {
357 getMetricsWithSize(outMetrics, ci, token, appWidth, appHeight);
358 }
359
360 public void getLogicalMetrics(DisplayMetrics outMetrics, CompatibilityInfo compatInfo,
361 IBinder token) {
362 getMetricsWithSize(outMetrics, compatInfo, token, logicalWidth, logicalHeight);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700363 }
364
Jeff Brown7f3994e2012-12-04 14:04:28 -0800365 public int getNaturalWidth() {
366 return rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 ?
367 logicalWidth : logicalHeight;
368 }
369
370 public int getNaturalHeight() {
371 return rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180 ?
372 logicalHeight : logicalWidth;
373 }
374
Jeff Browna506a6e2013-06-04 00:02:38 -0700375 /**
376 * Returns true if the specified UID has access to this display.
377 */
378 public boolean hasAccess(int uid) {
379 return Display.hasAccess(uid, flags, ownerUid);
380 }
381
Craig Mautner48d0d182013-06-11 07:53:06 -0700382 private void getMetricsWithSize(DisplayMetrics outMetrics, CompatibilityInfo compatInfo,
383 IBinder token, int width, int height) {
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700384 outMetrics.densityDpi = outMetrics.noncompatDensityDpi = logicalDensityDpi;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700385 outMetrics.noncompatWidthPixels = outMetrics.widthPixels = width;
386 outMetrics.noncompatHeightPixels = outMetrics.heightPixels = height;
387
Dianne Hackborn908aecc2012-07-31 16:37:34 -0700388 outMetrics.density = outMetrics.noncompatDensity =
389 logicalDensityDpi * DisplayMetrics.DENSITY_DEFAULT_SCALE;
Jeff Brownfa25bf52012-07-23 19:26:30 -0700390 outMetrics.scaledDensity = outMetrics.noncompatScaledDensity = outMetrics.density;
391 outMetrics.xdpi = outMetrics.noncompatXdpi = physicalXDpi;
392 outMetrics.ydpi = outMetrics.noncompatYdpi = physicalYDpi;
393
Craig Mautner48d0d182013-06-11 07:53:06 -0700394 if (!compatInfo.equals(CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO)) {
395 compatInfo.applyToDisplayMetrics(outMetrics);
Jeff Brownfa25bf52012-07-23 19:26:30 -0700396 }
397 }
Jeff Brownbf5740e2012-08-19 23:20:02 -0700398
399 // For debugging purposes
400 @Override
401 public String toString() {
Dianne Hackbornc652de82013-02-15 16:32:56 -0800402 StringBuilder sb = new StringBuilder();
403 sb.append("DisplayInfo{\"");
404 sb.append(name);
405 sb.append("\", app ");
406 sb.append(appWidth);
407 sb.append(" x ");
408 sb.append(appHeight);
409 sb.append(", real ");
410 sb.append(logicalWidth);
411 sb.append(" x ");
412 sb.append(logicalHeight);
413 if (overscanLeft != 0 || overscanTop != 0 || overscanRight != 0 || overscanBottom != 0) {
414 sb.append(", overscan (");
415 sb.append(overscanLeft);
416 sb.append(",");
417 sb.append(overscanTop);
418 sb.append(",");
419 sb.append(overscanRight);
420 sb.append(",");
421 sb.append(overscanBottom);
422 sb.append(")");
423 }
424 sb.append(", largest app ");
425 sb.append(largestNominalAppWidth);
426 sb.append(" x ");
427 sb.append(largestNominalAppHeight);
428 sb.append(", smallest app ");
429 sb.append(smallestNominalAppWidth);
430 sb.append(" x ");
431 sb.append(smallestNominalAppHeight);
432 sb.append(", ");
433 sb.append(refreshRate);
434 sb.append(" fps, rotation");
435 sb.append(rotation);
436 sb.append(", density ");
437 sb.append(logicalDensityDpi);
438 sb.append(" (");
439 sb.append(physicalXDpi);
440 sb.append(" x ");
441 sb.append(physicalYDpi);
442 sb.append(") dpi, layerStack ");
443 sb.append(layerStack);
444 sb.append(", type ");
445 sb.append(Display.typeToString(type));
Jeff Browna506a6e2013-06-04 00:02:38 -0700446 if (address != null) {
447 sb.append(", address ").append(address);
448 }
449 if (ownerUid != 0 || ownerPackageName != null) {
450 sb.append(", owner ").append(ownerPackageName);
451 sb.append(" (uid ").append(ownerUid).append(")");
452 }
Dianne Hackbornc652de82013-02-15 16:32:56 -0800453 sb.append(flagsToString(flags));
454 sb.append("}");
455 return sb.toString();
Jeff Brownc5df37c2012-09-13 11:45:07 -0700456 }
457
458 private static String flagsToString(int flags) {
459 StringBuilder result = new StringBuilder();
Jeff Brownf0681b32012-10-23 17:35:57 -0700460 if ((flags & Display.FLAG_SECURE) != 0) {
461 result.append(", FLAG_SECURE");
462 }
Jeff Brown77aebfd2012-10-01 21:07:03 -0700463 if ((flags & Display.FLAG_SUPPORTS_PROTECTED_BUFFERS) != 0) {
464 result.append(", FLAG_SUPPORTS_PROTECTED_BUFFERS");
Jeff Brownc5df37c2012-09-13 11:45:07 -0700465 }
Jeff Browna506a6e2013-06-04 00:02:38 -0700466 if ((flags & Display.FLAG_PRIVATE) != 0) {
467 result.append(", FLAG_PRIVATE");
468 }
Jeff Brownc5df37c2012-09-13 11:45:07 -0700469 return result.toString();
Jeff Brownbf5740e2012-08-19 23:20:02 -0700470 }
Jeff Brownfa25bf52012-07-23 19:26:30 -0700471}