Debian Codegraph @

sdk-common/src/main/java/com/android/ide/common/resources/configuration/SmallestScreenWidthQualifier.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 SmallestScreenWidthQualifier extends ResourceQualifier {
29 /** Default screen size value. This means the property is not set */
30 static final int DEFAULT_SIZE = -1;
31
32 private static final Pattern sParsePattern = Pattern.compile("^sw(\\d+)dp$"); //$NON-NLS-1$
33 private static final String sPrintPattern = "sw%1$ddp"; //$NON-NLS-1$
34
35 public static final String NAME = "Smallest Screen Width";
36
37 private int mValue = DEFAULT_SIZE;
38
39 public SmallestScreenWidthQualifier() {
40 // pass
41 }
42
43 public SmallestScreenWidthQualifier(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 SmallestScreenWidthQualifier qualifier = getQualifier(v);
83 if (qualifier != null) {
84 config.setSmallestScreenWidthQualifier(qualifier);
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 public static SmallestScreenWidthQualifier getQualifier(String value) {
93 try {
94 int dp = Integer.parseInt(value);
95
96 SmallestScreenWidthQualifier qualifier = new SmallestScreenWidthQualifier();
97 qualifier.mValue = dp;
98 return qualifier;
99
100 } catch (NumberFormatException e) {
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 if (qualifier instanceof SmallestScreenWidthQualifier) {
110 return mValue <= ((SmallestScreenWidthQualifier) qualifier).mValue;
111 }
112
113 return false;
114 }
115
116 @Override
117 public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo,
118 @NonNull ResourceQualifier reference) {
119 if (compareTo == null) {
120 return true;
121 }
122
123 SmallestScreenWidthQualifier compareQ = (SmallestScreenWidthQualifier) compareTo;
124 SmallestScreenWidthQualifier referenceQ = (SmallestScreenWidthQualifier) reference;
125
126 if (compareQ.mValue == referenceQ.mValue) {
127 // what we have is already the best possible match (exact match)
128 return false;
129 } else if (mValue == referenceQ.mValue) {
130 // got new exact value, this is the best!
131 return true;
132 } else {
133 // get the qualifier that has the width that is the closest to the reference, but not
134 // above. (which is guaranteed when this is called as isMatchFor is called first.
135 return mValue > compareQ.mValue;
136 }
137 }
138
139 @Override
140 public String getFolderSegment() {
141 return String.format(sPrintPattern, mValue);
142 }
143
144 @Override
145 public String getShortDisplayValue() {
146 if (isValid()) {
147 return getFolderSegment();
148 }
149
150 return ""; //$NON-NLS-1$
151 }
152
153 @Override
154 public String getLongDisplayValue() {
155 if (isValid()) {
156 return getFolderSegment();
157 }
158
159 return ""; //$NON-NLS-1$
160 }
161
162 @Override
163 public int hashCode() {
164 return mValue;
165 }
166
167 @Override
168 public boolean equals(Object obj) {
169 if (this == obj) {
170 return true;
171 }
172 if (obj == null) {
173 return false;
174 }
175 if (getClass() != obj.getClass()) {
176 return false;
177 }
178 SmallestScreenWidthQualifier other = (SmallestScreenWidthQualifier) obj;
179 if (mValue != other.mValue) {
180 return false;
181 }
182 return true;
183 }
184 }