• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package com.google.android.apps.common.testing.accessibility.framework.uielement;
15 
16 import static com.google.common.base.Preconditions.checkNotNull;
17 
18 import com.google.android.apps.common.testing.accessibility.framework.uielement.proto.AccessibilityHierarchyProtos.DisplayInfoMetricsProto;
19 import com.google.android.apps.common.testing.accessibility.framework.uielement.proto.AccessibilityHierarchyProtos.DisplayInfoProto;
20 import com.google.errorprone.annotations.Immutable;
21 import org.checkerframework.checker.nullness.qual.Nullable;
22 
23 /**
24  * Representation of a {@link android.view.Display}
25  *
26  * <p>NOTE: Currently, this class holds only {@link Metrics}, but will likely have additional fields
27  * in the future.
28  */
29 @Immutable
30 public class DisplayInfo {
31 
32   private final @Nullable Metrics metricsWithoutDecoration;
33   private final @Nullable Metrics realMetrics;
34 
DisplayInfo(DisplayInfoProto fromProto)35   DisplayInfo(DisplayInfoProto fromProto) {
36     this.metricsWithoutDecoration = new Metrics(fromProto.getMetricsWithoutDecoration());
37     this.realMetrics = fromProto.hasRealMetrics() ? new Metrics(fromProto.getRealMetrics()) : null;
38   }
39 
DisplayInfo()40   protected DisplayInfo() {
41     this.metricsWithoutDecoration = null;
42     this.realMetrics = null;
43   }
44 
45   /**
46    * @return a {@link Metrics} representing the display's metrics excluding certain system
47    *     decorations.
48    * @see android.view.Display#getMetrics(android.util.DisplayMetrics)
49    */
getMetricsWithoutDecoration()50   public Metrics getMetricsWithoutDecoration() {
51     checkNotNull(metricsWithoutDecoration);
52     return metricsWithoutDecoration;
53   }
54 
55   /**
56    * @return a {@link Metrics} representing the display's real metrics, which include system
57    *     decorations. This value can be {@code null} for instances created on platform versions that
58    *     don't support resolution of real metrics.
59    * @see android.view.Display#getRealMetrics(android.util.DisplayMetrics)
60    */
getRealMetrics()61   public @Nullable Metrics getRealMetrics() {
62     return realMetrics;
63   }
64 
toProto()65   DisplayInfoProto toProto() {
66     checkNotNull(metricsWithoutDecoration);
67     DisplayInfoProto.Builder builder = DisplayInfoProto.newBuilder();
68     builder.setMetricsWithoutDecoration(metricsWithoutDecoration.toProto());
69     if (realMetrics != null) {
70       builder.setRealMetrics(realMetrics.toProto());
71     }
72     return builder.build();
73   }
74 
75   /** Representation of a {@link android.util.DisplayMetrics} */
76   @Immutable
77   public static class Metrics {
78 
79     protected final float density;
80     protected final float scaledDensity;
81     protected final float xDpi;
82     protected final float yDpi;
83     protected final int densityDpi;
84     protected final int heightPixels;
85     protected final int widthPixels;
86 
Metrics( float density, float scaledDensity, float xDpi, float yDpi, int densityDpi, int heightPixels, int widthPixels)87     Metrics(
88         float density,
89         float scaledDensity,
90         float xDpi,
91         float yDpi,
92         int densityDpi,
93         int heightPixels,
94         int widthPixels) {
95       this.density = density;
96       this.scaledDensity = scaledDensity;
97       this.xDpi = xDpi;
98       this.yDpi = yDpi;
99       this.densityDpi = densityDpi;
100       this.heightPixels = heightPixels;
101       this.widthPixels = widthPixels;
102     }
103 
Metrics(DisplayInfoMetricsProto fromProto)104     Metrics(DisplayInfoMetricsProto fromProto) {
105       this.density = fromProto.getDensity();
106       this.scaledDensity = fromProto.getScaledDensity();
107       this.xDpi = fromProto.getXDpi();
108       this.yDpi = fromProto.getYDpi();
109       this.densityDpi = fromProto.getDensityDpi();
110       this.heightPixels = fromProto.getHeightPixels();
111       this.widthPixels = fromProto.getWidthPixels();
112     }
113 
114     /** See {@link android.util.DisplayMetrics#density}. */
getDensity()115     public float getDensity() {
116       return density;
117     }
118 
119     /** See {@link android.util.DisplayMetrics#scaledDensity}. */
getScaledDensity()120     public float getScaledDensity() {
121       return scaledDensity;
122     }
123 
124     /** See {@link android.util.DisplayMetrics#xdpi}. */
getxDpi()125     public float getxDpi() {
126       return xDpi;
127     }
128 
129     /** See {@link android.util.DisplayMetrics#ydpi}. */
getyDpi()130     public float getyDpi() {
131       return yDpi;
132     }
133 
134     /** See {@link android.util.DisplayMetrics#densityDpi}. */
getDensityDpi()135     public int getDensityDpi() {
136       return densityDpi;
137     }
138 
139     /** See {@link android.util.DisplayMetrics#heightPixels}. */
getHeightPixels()140     public int getHeightPixels() {
141       return heightPixels;
142     }
143 
144     /** See {@link android.util.DisplayMetrics#widthPixels}. */
getWidthPixels()145     public int getWidthPixels() {
146       return widthPixels;
147     }
148 
toProto()149     DisplayInfoMetricsProto toProto() {
150       DisplayInfoMetricsProto.Builder builder = DisplayInfoMetricsProto.newBuilder();
151       builder.setDensity(density);
152       builder.setScaledDensity(scaledDensity);
153       builder.setXDpi(xDpi);
154       builder.setYDpi(yDpi);
155       builder.setDensityDpi(densityDpi);
156       builder.setHeightPixels(heightPixels);
157       builder.setWidthPixels(widthPixels);
158       return builder.build();
159     }
160   }
161 }
162