blob: 27c2d5c5cdc3a62dc22d07e90bcac54518d7b04b [file] [log] [blame]
Craig Mautner48d0d182013-06-11 07:53:06 -07001/*
2 * Copyright (C) 2011 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
Diego Veladd184002020-04-03 16:33:46 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Artur Satayevad9254c2019-12-10 17:47:54 +000021import android.compat.annotation.UnsupportedAppUsage;
Craig Mautner48d0d182013-06-11 07:53:06 -070022import android.content.res.CompatibilityInfo;
Wale Ogunwale7c726682015-02-06 17:34:28 -080023import android.content.res.Configuration;
Craig Mautner48d0d182013-06-11 07:53:06 -070024
Kenny Roote6585b32013-12-13 12:00:26 -080025import java.util.Objects;
Craig Mautner48d0d182013-06-11 07:53:06 -070026
27/** @hide */
28public class DisplayAdjustments {
Craig Mautner48d0d182013-06-11 07:53:06 -070029 public static final DisplayAdjustments DEFAULT_DISPLAY_ADJUSTMENTS = new DisplayAdjustments();
30
31 private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
Diego Veladd184002020-04-03 16:33:46 -070032 private final Configuration mConfiguration = new Configuration(Configuration.EMPTY);
Craig Mautner48d0d182013-06-11 07:53:06 -070033
Mathew Inwooda570dee2018-08-17 14:56:00 +010034 @UnsupportedAppUsage
Craig Mautner48d0d182013-06-11 07:53:06 -070035 public DisplayAdjustments() {
36 }
37
Diego Veladd184002020-04-03 16:33:46 -070038 public DisplayAdjustments(@Nullable Configuration configuration) {
39 if (configuration != null) {
40 mConfiguration.setTo(configuration);
41 }
Craig Mautner48d0d182013-06-11 07:53:06 -070042 }
43
Diego Veladd184002020-04-03 16:33:46 -070044 public DisplayAdjustments(@NonNull DisplayAdjustments daj) {
Wale Ogunwale7c726682015-02-06 17:34:28 -080045 setCompatibilityInfo(daj.mCompatInfo);
Diego Veladd184002020-04-03 16:33:46 -070046 mConfiguration.setTo(daj.getConfiguration());
Craig Mautner48d0d182013-06-11 07:53:06 -070047 }
48
Mathew Inwooda570dee2018-08-17 14:56:00 +010049 @UnsupportedAppUsage
Diego Veladd184002020-04-03 16:33:46 -070050 public void setCompatibilityInfo(@Nullable CompatibilityInfo compatInfo) {
Craig Mautner48d0d182013-06-11 07:53:06 -070051 if (this == DEFAULT_DISPLAY_ADJUSTMENTS) {
52 throw new IllegalArgumentException(
53 "setCompatbilityInfo: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS");
54 }
55 if (compatInfo != null && (compatInfo.isScalingRequired()
56 || !compatInfo.supportsScreen())) {
57 mCompatInfo = compatInfo;
58 } else {
59 mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO;
60 }
61 }
62
63 public CompatibilityInfo getCompatibilityInfo() {
64 return mCompatInfo;
65 }
66
Diego Veladd184002020-04-03 16:33:46 -070067 /**
68 * Updates the configuration for the DisplayAdjustments with new configuration.
69 * Default to EMPTY configuration if new configuration is {@code null}
70 * @param configuration new configuration
71 * @throws IllegalArgumentException if trying to modify DEFAULT_DISPLAY_ADJUSTMENTS
72 */
73 public void setConfiguration(@Nullable Configuration configuration) {
Craig Mautner48d0d182013-06-11 07:53:06 -070074 if (this == DEFAULT_DISPLAY_ADJUSTMENTS) {
75 throw new IllegalArgumentException(
Wale Ogunwale7c726682015-02-06 17:34:28 -080076 "setConfiguration: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS");
Craig Mautner48d0d182013-06-11 07:53:06 -070077 }
Andrii Kuliana8a9bc52016-10-14 11:00:13 -070078 mConfiguration.setTo(configuration != null ? configuration : Configuration.EMPTY);
Craig Mautner48d0d182013-06-11 07:53:06 -070079 }
80
Mathew Inwooda570dee2018-08-17 14:56:00 +010081 @UnsupportedAppUsage
Diego Veladd184002020-04-03 16:33:46 -070082 @NonNull
Wale Ogunwale7c726682015-02-06 17:34:28 -080083 public Configuration getConfiguration() {
84 return mConfiguration;
Craig Mautner48d0d182013-06-11 07:53:06 -070085 }
86
87 @Override
88 public int hashCode() {
89 int hash = 17;
Adam Lesinski4ece3d62016-06-16 18:05:41 -070090 hash = hash * 31 + Objects.hashCode(mCompatInfo);
91 hash = hash * 31 + Objects.hashCode(mConfiguration);
Craig Mautner48d0d182013-06-11 07:53:06 -070092 return hash;
93 }
94
95 @Override
96 public boolean equals(Object o) {
97 if (!(o instanceof DisplayAdjustments)) {
98 return false;
99 }
100 DisplayAdjustments daj = (DisplayAdjustments)o;
Kenny Roote6585b32013-12-13 12:00:26 -0800101 return Objects.equals(daj.mCompatInfo, mCompatInfo) &&
Wale Ogunwale7c726682015-02-06 17:34:28 -0800102 Objects.equals(daj.mConfiguration, mConfiguration);
Craig Mautner48d0d182013-06-11 07:53:06 -0700103 }
104}