Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/configuration/ScreenHeightQualifier.java

raw
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 com.android.ide.common.resources.configuration;
18
19 import com.android.annotations.NonNull;
20 import com.android.annotations.Nullable;
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 /**
26 * Resource Qualifier for Screen Pixel Density.
27 */
28 public final class ScreenHeightQualifier extends ResourceQualifier {
29 /** Default screen size value. This means the property is not set */
30 private static final int DEFAULT_SIZE = -1;
31
32 private static final Pattern sParsePattern = Pattern.compile("^h(\\d+)dp$");//$NON-NLS-1$
33 private static final String sPrintPattern = "h%1$ddp";
34
35 public static final String NAME = "Screen Height";
36
37 private int mValue = DEFAULT_SIZE;
38
39 public ScreenHeightQualifier() {
40 // pass
41 }
42
43 public ScreenHeightQualifier(int value) {
44 mValue = value;
45 }
46
47 public int getValue() {
48 return mValue;
49 }
50
51 @Override
52 public String getName() {
53 return NAME;
54 }
55
56 @Override
57 public String getShortName() {
58 return NAME;
59 }
60
61 @Override
62 public int since() {
63 return 13;
64 }
65
66 @Override
67 public boolean hasFakeValue() {
68 return false;
69 }
70
71 @Override
72 public boolean isValid() {
73 return mValue != DEFAULT_SIZE;
74 }
75
76 @Override
77 public boolean checkAndSet(String value, FolderConfiguration config) {
78 Matcher m = sParsePattern.matcher(value);
79 if (m.matches()) {
80 String v = m.group(1);
81
82 ScreenHeightQualifier qualifier = getQualifier(v);
83 if (qualifier != null) {
84 config.setScreenHeightQualifier(qualifier);
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 public static ScreenHeightQualifier getQualifier(String value) {
93 try {
94 int dp = Integer.parseInt(value);
95
96 ScreenHeightQualifier qualifier = new ScreenHeightQualifier();
97 qualifier.mValue = dp;
98 return qualifier;
99
100 } catch (NumberFormatException ignored) {
101 }
102
103 return null;
104 }
105
106 @Override
107 public boolean isMatchFor(ResourceQualifier qualifier) {
108 // this is the match only of the current dp value is lower or equal to the
109 return qualifier instanceof ScreenHeightQualifier
110 && mValue <= ((ScreenHeightQualifier) qualifier).mValue;
111
112 }
113
114 @Override
115 public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo,
116 @NonNull ResourceQualifier reference) {
117 if (compareTo == null) {
118 return true;
119 }
120
121 ScreenHeightQualifier compareQ = (ScreenHeightQualifier) compareTo;
122 ScreenHeightQualifier referenceQ = (ScreenHeightQualifier) reference;
123
124 if (compareQ.mValue == referenceQ.mValue) {
125 // what we have is already the best possible match (exact match)
126 return false;
127 } else if (mValue == referenceQ.mValue) {
128 // got new exact value, this is the best!
129 return true;
130 } else {
131 // get the qualifier that has the width that is the closest to the reference, but not
132 // above. (which is guaranteed when this is called as isMatchFor is called first.
133 return mValue > compareQ.mValue;
134 }
135 }
136
137 @Override
138 public String getFolderSegment() {
139 return String.format(sPrintPattern, mValue);
140 }
141
142 @Override
143 public String getShortDisplayValue() {
144 if (isValid()) {
145 return getFolderSegment();
146 }
147
148 return ""; //$NON-NLS-1$
149 }
150
151 @Override
152 public String getLongDisplayValue() {
153 if (isValid()) {
154 return getFolderSegment();
155 }
156
157 return ""; //$NON-NLS-1$
158 }
159
160
161 @Override
162 public int hashCode() {
163 return mValue;
164 }
165
166 @Override
167 public boolean equals(Object obj) {
168 if (this == obj) {
169 return true;
170 }
171 if (obj == null) {
172 return false;
173 }
174 if (getClass() != obj.getClass()) {
175 return false;
176 }
177 ScreenHeightQualifier other = (ScreenHeightQualifier) obj;
178 return mValue == other.mValue;
179 }
180 }