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 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.compat.annotation.UnsupportedAppUsage; 22 import android.content.res.CompatibilityInfo; 23 import android.content.res.Configuration; 24 25 import java.util.Objects; 26 27 /** @hide */ 28 public class DisplayAdjustments { 29 public static final DisplayAdjustments DEFAULT_DISPLAY_ADJUSTMENTS = new DisplayAdjustments(); 30 31 private volatile CompatibilityInfo mCompatInfo = CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO; 32 private final Configuration mConfiguration = new Configuration(Configuration.EMPTY); 33 34 @UnsupportedAppUsage DisplayAdjustments()35 public DisplayAdjustments() { 36 } 37 DisplayAdjustments(@ullable Configuration configuration)38 public DisplayAdjustments(@Nullable Configuration configuration) { 39 if (configuration != null) { 40 mConfiguration.setTo(configuration); 41 } 42 } 43 DisplayAdjustments(@onNull DisplayAdjustments daj)44 public DisplayAdjustments(@NonNull DisplayAdjustments daj) { 45 setCompatibilityInfo(daj.mCompatInfo); 46 mConfiguration.setTo(daj.getConfiguration()); 47 } 48 49 @UnsupportedAppUsage setCompatibilityInfo(@ullable CompatibilityInfo compatInfo)50 public void setCompatibilityInfo(@Nullable CompatibilityInfo compatInfo) { 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 getCompatibilityInfo()63 public CompatibilityInfo getCompatibilityInfo() { 64 return mCompatInfo; 65 } 66 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 */ setConfiguration(@ullable Configuration configuration)73 public void setConfiguration(@Nullable Configuration configuration) { 74 if (this == DEFAULT_DISPLAY_ADJUSTMENTS) { 75 throw new IllegalArgumentException( 76 "setConfiguration: Cannot modify DEFAULT_DISPLAY_ADJUSTMENTS"); 77 } 78 mConfiguration.setTo(configuration != null ? configuration : Configuration.EMPTY); 79 } 80 81 @UnsupportedAppUsage 82 @NonNull getConfiguration()83 public Configuration getConfiguration() { 84 return mConfiguration; 85 } 86 87 @Override hashCode()88 public int hashCode() { 89 int hash = 17; 90 hash = hash * 31 + Objects.hashCode(mCompatInfo); 91 hash = hash * 31 + Objects.hashCode(mConfiguration); 92 return hash; 93 } 94 95 @Override equals(@ullable Object o)96 public boolean equals(@Nullable Object o) { 97 if (!(o instanceof DisplayAdjustments)) { 98 return false; 99 } 100 DisplayAdjustments daj = (DisplayAdjustments)o; 101 return Objects.equals(daj.mCompatInfo, mCompatInfo) 102 && Objects.equals(daj.mConfiguration, mConfiguration); 103 } 104 } 105