Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 22 | import android.content.res.CompatibilityInfo; |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 24 | |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 25 | import java.util.Objects; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 26 | |
| 27 | /** @hide */ |
| 28 | public class DisplayAdjustments { |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 29 | public static final DisplayAdjustments DEFAULT_DISPLAY_ADJUSTMENTS = new DisplayAdjustments(); |
| 30 | |
| 31 | private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 32 | private final Configuration mConfiguration = new Configuration(Configuration.EMPTY); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 33 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 34 | @UnsupportedAppUsage |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 35 | public DisplayAdjustments() { |
| 36 | } |
| 37 | |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 38 | public DisplayAdjustments(@Nullable Configuration configuration) { |
| 39 | if (configuration != null) { |
| 40 | mConfiguration.setTo(configuration); |
| 41 | } |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 44 | public DisplayAdjustments(@NonNull DisplayAdjustments daj) { |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 45 | setCompatibilityInfo(daj.mCompatInfo); |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 46 | mConfiguration.setTo(daj.getConfiguration()); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 49 | @UnsupportedAppUsage |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 50 | public void setCompatibilityInfo(@Nullable CompatibilityInfo compatInfo) { |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 51 | 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 Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 67 | /** |
| 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 Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 74 | if (this == DEFAULT_DISPLAY_ADJUSTMENTS) { |
| 75 | throw new IllegalArgumentException( |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 76 | "setConfiguration: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS"); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 77 | } |
Andrii Kulian | a8a9bc5 | 2016-10-14 11:00:13 -0700 | [diff] [blame] | 78 | mConfiguration.setTo(configuration != null ? configuration : Configuration.EMPTY); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 81 | @UnsupportedAppUsage |
Diego Vela | dd18400 | 2020-04-03 16:33:46 -0700 | [diff] [blame] | 82 | @NonNull |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 83 | public Configuration getConfiguration() { |
| 84 | return mConfiguration; |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int hashCode() { |
| 89 | int hash = 17; |
Adam Lesinski | 4ece3d6 | 2016-06-16 18:05:41 -0700 | [diff] [blame] | 90 | hash = hash * 31 + Objects.hashCode(mCompatInfo); |
| 91 | hash = hash * 31 + Objects.hashCode(mConfiguration); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 92 | 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 Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 101 | return Objects.equals(daj.mCompatInfo, mCompatInfo) && |
Wale Ogunwale | 7c72668 | 2015-02-06 17:34:28 -0800 | [diff] [blame] | 102 | Objects.equals(daj.mConfiguration, mConfiguration); |
Craig Mautner | 48d0d18 | 2013-06-11 07:53:06 -0700 | [diff] [blame] | 103 | } |
| 104 | } |