1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.view;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.res.CompatibilityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.DisplayMetrics;
import java.util.Objects;
/** @hide */
public class DisplayAdjustments {
public static final DisplayAdjustments DEFAULT_DISPLAY_ADJUSTMENTS = new DisplayAdjustments();
private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
private final Configuration mConfiguration = new Configuration(Configuration.EMPTY);
private FixedRotationAdjustments mFixedRotationAdjustments;
@UnsupportedAppUsage
public DisplayAdjustments() {
}
public DisplayAdjustments(@Nullable Configuration configuration) {
if (configuration != null) {
mConfiguration.setTo(configuration);
}
}
public DisplayAdjustments(@NonNull DisplayAdjustments daj) {
setCompatibilityInfo(daj.mCompatInfo);
mConfiguration.setTo(daj.getConfiguration());
mFixedRotationAdjustments = daj.mFixedRotationAdjustments;
}
@UnsupportedAppUsage
public void setCompatibilityInfo(@Nullable CompatibilityInfo compatInfo) {
if (this == DEFAULT_DISPLAY_ADJUSTMENTS) {
throw new IllegalArgumentException(
"setCompatbilityInfo: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS");
}
if (compatInfo != null && (compatInfo.isScalingRequired()
|| !compatInfo.supportsScreen())) {
mCompatInfo = compatInfo;
} else {
mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
}
}
public CompatibilityInfo getCompatibilityInfo() {
return mCompatInfo;
}
/**
* Updates the configuration for the DisplayAdjustments with new configuration.
* Default to EMPTY configuration if new configuration is {@code null}
* @param configuration new configuration
* @throws IllegalArgumentException if trying to modify DEFAULT_DISPLAY_ADJUSTMENTS
*/
public void setConfiguration(@Nullable Configuration configuration) {
if (this == DEFAULT_DISPLAY_ADJUSTMENTS) {
throw new IllegalArgumentException(
"setConfiguration: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS");
}
mConfiguration.setTo(configuration != null ? configuration : Configuration.EMPTY);
}
@UnsupportedAppUsage
@NonNull
public Configuration getConfiguration() {
return mConfiguration;
}
public void setFixedRotationAdjustments(FixedRotationAdjustments fixedRotationAdjustments) {
mFixedRotationAdjustments = fixedRotationAdjustments;
}
public FixedRotationAdjustments getFixedRotationAdjustments() {
return mFixedRotationAdjustments;
}
/** Returns {@code false} if the width and height of display should swap. */
private boolean noFlip(@Surface.Rotation int realRotation) {
final FixedRotationAdjustments rotationAdjustments = mFixedRotationAdjustments;
if (rotationAdjustments == null) {
return true;
}
// Check if the delta is rotated by 90 degrees.
return (realRotation - rotationAdjustments.mRotation + 4) % 2 == 0;
}
/** Adjusts the given size if possible. */
public void adjustSize(@NonNull Point size, @Surface.Rotation int realRotation) {
if (noFlip(realRotation)) {
return;
}
final int w = size.x;
size.x = size.y;
size.y = w;
}
/** Adjusts the given metrics if possible. */
public void adjustMetrics(@NonNull DisplayMetrics metrics, @Surface.Rotation int realRotation) {
if (noFlip(realRotation)) {
return;
}
int w = metrics.widthPixels;
metrics.widthPixels = metrics.heightPixels;
metrics.heightPixels = w;
w = metrics.noncompatWidthPixels;
metrics.noncompatWidthPixels = metrics.noncompatHeightPixels;
metrics.noncompatHeightPixels = w;
}
/** Adjusts global display metrics that is available to applications. */
public void adjustGlobalAppMetrics(@NonNull DisplayMetrics metrics) {
final FixedRotationAdjustments rotationAdjustments = mFixedRotationAdjustments;
if (rotationAdjustments == null) {
return;
}
metrics.noncompatWidthPixels = metrics.widthPixels = rotationAdjustments.mAppWidth;
metrics.noncompatHeightPixels = metrics.heightPixels = rotationAdjustments.mAppHeight;
}
/** Returns the adjusted cutout if available. Otherwise the original cutout is returned. */
@Nullable
public DisplayCutout getDisplayCutout(@Nullable DisplayCutout realCutout) {
final FixedRotationAdjustments rotationAdjustments = mFixedRotationAdjustments;
return rotationAdjustments != null && rotationAdjustments.mRotatedDisplayCutout != null
? rotationAdjustments.mRotatedDisplayCutout
: realCutout;
}
/**
* Returns the adjusted {@link RoundedCorners} if available. Otherwise the original
* {@link RoundedCorners} is returned.
*/
@Nullable
public RoundedCorners adjustRoundedCorner(@Nullable RoundedCorners realRoundedCorners,
@Surface.Rotation int realRotation, int displayWidth, int displayHeight) {
final FixedRotationAdjustments rotationAdjustments = mFixedRotationAdjustments;
if (realRoundedCorners == null || rotationAdjustments == null
|| rotationAdjustments.mRotation == realRotation) {
return realRoundedCorners;
}
return realRoundedCorners.rotate(
rotationAdjustments.mRotation, displayWidth, displayHeight);
}
/** Returns the adjusted rotation if available. Otherwise the original rotation is returned. */
@Surface.Rotation
public int getRotation(@Surface.Rotation int realRotation) {
final FixedRotationAdjustments rotationAdjustments = mFixedRotationAdjustments;
return rotationAdjustments != null ? rotationAdjustments.mRotation : realRotation;
}
@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + Objects.hashCode(mCompatInfo);
hash = hash * 31 + Objects.hashCode(mConfiguration);
hash = hash * 31 + Objects.hashCode(mFixedRotationAdjustments);
return hash;
}
@Override
public boolean equals(@Nullable Object o) {
if (!(o instanceof DisplayAdjustments)) {
return false;
}
DisplayAdjustments daj = (DisplayAdjustments)o;
return Objects.equals(daj.mCompatInfo, mCompatInfo)
&& Objects.equals(daj.mConfiguration, mConfiguration)
&& Objects.equals(daj.mFixedRotationAdjustments, mFixedRotationAdjustments);
}
/**
* An application can be launched in different rotation than the real display. This class
* provides the information to adjust the values returned by {@link Display}.
* @hide
*/
public static class FixedRotationAdjustments implements Parcelable {
/** The application-based rotation. */
@Surface.Rotation
final int mRotation;
/**
* The rotated {@link DisplayInfo#appWidth}. The value cannot be simply swapped according
* to rotation because it minus the region of screen decorations.
*/
final int mAppWidth;
/** The rotated {@link DisplayInfo#appHeight}. */
final int mAppHeight;
/** Non-null if the device has cutout. */
@Nullable
final DisplayCutout mRotatedDisplayCutout;
public FixedRotationAdjustments(@Surface.Rotation int rotation, int appWidth, int appHeight,
DisplayCutout cutout) {
mRotation = rotation;
mAppWidth = appWidth;
mAppHeight = appHeight;
mRotatedDisplayCutout = cutout;
}
@Override
public int hashCode() {
int hash = 17;
hash = hash * 31 + mRotation;
hash = hash * 31 + mAppWidth;
hash = hash * 31 + mAppHeight;
hash = hash * 31 + Objects.hashCode(mRotatedDisplayCutout);
return hash;
}
@Override
public boolean equals(@Nullable Object o) {
if (!(o instanceof FixedRotationAdjustments)) {
return false;
}
final FixedRotationAdjustments other = (FixedRotationAdjustments) o;
return mRotation == other.mRotation
&& mAppWidth == other.mAppWidth && mAppHeight == other.mAppHeight
&& Objects.equals(mRotatedDisplayCutout, other.mRotatedDisplayCutout);
}
@Override
public String toString() {
return "FixedRotationAdjustments{rotation=" + Surface.rotationToString(mRotation)
+ " appWidth=" + mAppWidth + " appHeight=" + mAppHeight
+ " cutout=" + mRotatedDisplayCutout + "}";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mRotation);
dest.writeInt(mAppWidth);
dest.writeInt(mAppHeight);
dest.writeTypedObject(
new DisplayCutout.ParcelableWrapper(mRotatedDisplayCutout), flags);
}
private FixedRotationAdjustments(Parcel in) {
mRotation = in.readInt();
mAppWidth = in.readInt();
mAppHeight = in.readInt();
final DisplayCutout.ParcelableWrapper cutoutWrapper =
in.readTypedObject(DisplayCutout.ParcelableWrapper.CREATOR);
mRotatedDisplayCutout = cutoutWrapper != null ? cutoutWrapper.get() : null;
}
public static final Creator<FixedRotationAdjustments> CREATOR =
new Creator<FixedRotationAdjustments>() {
public FixedRotationAdjustments createFromParcel(Parcel in) {
return new FixedRotationAdjustments(in);
}
public FixedRotationAdjustments[] newArray(int size) {
return new FixedRotationAdjustments[size];
}
};
}
}
|