aboutsummaryrefslogtreecommitdiffstats
path: root/ide_common/src/com/android
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2011-06-09 14:07:57 -0700
committerXavier Ducrohet <xav@android.com>2011-06-09 14:23:18 -0700
commit65d2312eb7cb092b5db7c871231817bd56628d42 (patch)
treee0169ddbf2cd2ddf78516526628ed78ae89d561e /ide_common/src/com/android
parentc1b76da1654a96eafd17f8cdb79feeef5b9f5177 (diff)
downloadsdk-65d2312eb7cb092b5db7c871231817bd56628d42.zip
sdk-65d2312eb7cb092b5db7c871231817bd56628d42.tar.gz
sdk-65d2312eb7cb092b5db7c871231817bd56628d42.tar.bz2
Add support for the new width/height resource qualifiers.
Also add Television to the dock mode (which is really UI Mode). Change-Id: I97e3fbea1806a32d8267d8e462211add2b122ed0
Diffstat (limited to 'ide_common/src/com/android')
-rw-r--r--ide_common/src/com/android/ide/common/resources/configuration/FolderConfiguration.java150
-rw-r--r--ide_common/src/com/android/ide/common/resources/configuration/ScreenDimensionQualifier.java4
-rw-r--r--ide_common/src/com/android/ide/common/resources/configuration/ScreenHeightQualifier.java169
-rw-r--r--ide_common/src/com/android/ide/common/resources/configuration/ScreenWidthQualifier.java169
-rw-r--r--ide_common/src/com/android/ide/common/resources/configuration/SmallestScreenWidthQualifier.java169
5 files changed, 641 insertions, 20 deletions
diff --git a/ide_common/src/com/android/ide/common/resources/configuration/FolderConfiguration.java b/ide_common/src/com/android/ide/common/resources/configuration/FolderConfiguration.java
index 09cf9e4..8b04bac 100644
--- a/ide_common/src/com/android/ide/common/resources/configuration/FolderConfiguration.java
+++ b/ide_common/src/com/android/ide/common/resources/configuration/FolderConfiguration.java
@@ -17,7 +17,9 @@
package com.android.ide.common.resources.configuration;
import com.android.AndroidConstants;
+import com.android.resources.Density;
import com.android.resources.ResourceFolderType;
+import com.android.resources.ScreenOrientation;
import java.util.ArrayList;
import java.util.List;
@@ -41,24 +43,27 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
private final ResourceQualifier[] mQualifiers = new ResourceQualifier[INDEX_COUNT];
- private final static int INDEX_COUNTRY_CODE = 0;
- private final static int INDEX_NETWORK_CODE = 1;
- private final static int INDEX_LANGUAGE = 2;
- private final static int INDEX_REGION = 3;
- private final static int INDEX_SCREEN_SIZE = 4;
- private final static int INDEX_SCREEN_RATIO = 5;
- private final static int INDEX_SCREEN_ORIENTATION = 6;
- private final static int INDEX_DOCK_MODE = 7;
- private final static int INDEX_NIGHT_MODE = 8;
- private final static int INDEX_PIXEL_DENSITY = 9;
- private final static int INDEX_TOUCH_TYPE = 10;
- private final static int INDEX_KEYBOARD_STATE = 11;
- private final static int INDEX_TEXT_INPUT_METHOD = 12;
- private final static int INDEX_NAVIGATION_STATE = 13;
- private final static int INDEX_NAVIGATION_METHOD = 14;
- private final static int INDEX_SCREEN_DIMENSION = 15;
- private final static int INDEX_VERSION = 16;
- private final static int INDEX_COUNT = 17;
+ private final static int INDEX_COUNTRY_CODE = 0;
+ private final static int INDEX_NETWORK_CODE = 1;
+ private final static int INDEX_LANGUAGE = 2;
+ private final static int INDEX_REGION = 3;
+ private final static int INDEX_SMALLEST_SCREEN_WIDTH = 4;
+ private final static int INDEX_SCREEN_WIDTH = 5;
+ private final static int INDEX_SCREEN_HEIGHT = 6;
+ private final static int INDEX_SCREEN_SIZE = 7;
+ private final static int INDEX_SCREEN_RATIO = 8;
+ private final static int INDEX_SCREEN_ORIENTATION = 9;
+ private final static int INDEX_DOCK_MODE = 10;
+ private final static int INDEX_NIGHT_MODE = 11;
+ private final static int INDEX_PIXEL_DENSITY = 12;
+ private final static int INDEX_TOUCH_TYPE = 13;
+ private final static int INDEX_KEYBOARD_STATE = 14;
+ private final static int INDEX_TEXT_INPUT_METHOD = 15;
+ private final static int INDEX_NAVIGATION_STATE = 16;
+ private final static int INDEX_NAVIGATION_METHOD = 17;
+ private final static int INDEX_SCREEN_DIMENSION = 18;
+ private final static int INDEX_VERSION = 19;
+ private final static int INDEX_COUNT = 20;
/**
* Creates a {@link FolderConfiguration} matching the folder segments.
@@ -205,38 +210,64 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
public void addQualifier(ResourceQualifier qualifier) {
if (qualifier instanceof CountryCodeQualifier) {
mQualifiers[INDEX_COUNTRY_CODE] = qualifier;
+
} else if (qualifier instanceof NetworkCodeQualifier) {
mQualifiers[INDEX_NETWORK_CODE] = qualifier;
+
} else if (qualifier instanceof LanguageQualifier) {
mQualifiers[INDEX_LANGUAGE] = qualifier;
+
} else if (qualifier instanceof RegionQualifier) {
mQualifiers[INDEX_REGION] = qualifier;
+
+ } else if (qualifier instanceof SmallestScreenWidthQualifier) {
+ mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = qualifier;
+
+ } else if (qualifier instanceof ScreenWidthQualifier) {
+ mQualifiers[INDEX_SCREEN_WIDTH] = qualifier;
+
+ } else if (qualifier instanceof ScreenHeightQualifier) {
+ mQualifiers[INDEX_SCREEN_HEIGHT] = qualifier;
+
} else if (qualifier instanceof ScreenSizeQualifier) {
mQualifiers[INDEX_SCREEN_SIZE] = qualifier;
+
} else if (qualifier instanceof ScreenRatioQualifier) {
mQualifiers[INDEX_SCREEN_RATIO] = qualifier;
+
} else if (qualifier instanceof ScreenOrientationQualifier) {
mQualifiers[INDEX_SCREEN_ORIENTATION] = qualifier;
+
} else if (qualifier instanceof DockModeQualifier) {
mQualifiers[INDEX_DOCK_MODE] = qualifier;
+
} else if (qualifier instanceof NightModeQualifier) {
mQualifiers[INDEX_NIGHT_MODE] = qualifier;
+
} else if (qualifier instanceof PixelDensityQualifier) {
mQualifiers[INDEX_PIXEL_DENSITY] = qualifier;
+
} else if (qualifier instanceof TouchScreenQualifier) {
mQualifiers[INDEX_TOUCH_TYPE] = qualifier;
+
} else if (qualifier instanceof KeyboardStateQualifier) {
mQualifiers[INDEX_KEYBOARD_STATE] = qualifier;
+
} else if (qualifier instanceof TextInputMethodQualifier) {
mQualifiers[INDEX_TEXT_INPUT_METHOD] = qualifier;
+
} else if (qualifier instanceof NavigationStateQualifier) {
mQualifiers[INDEX_NAVIGATION_STATE] = qualifier;
+
} else if (qualifier instanceof NavigationMethodQualifier) {
mQualifiers[INDEX_NAVIGATION_METHOD] = qualifier;
+
} else if (qualifier instanceof ScreenDimensionQualifier) {
mQualifiers[INDEX_SCREEN_DIMENSION] = qualifier;
+
} else if (qualifier instanceof VersionQualifier) {
mQualifiers[INDEX_VERSION] = qualifier;
+
}
}
@@ -295,6 +326,30 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
return (RegionQualifier)mQualifiers[INDEX_REGION];
}
+ public void setSmallestScreenWidthQualifier(SmallestScreenWidthQualifier qualifier) {
+ mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = qualifier;
+ }
+
+ public SmallestScreenWidthQualifier getSmallestScreenWidthQualifier() {
+ return (SmallestScreenWidthQualifier) mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH];
+ }
+
+ public void setScreenWidthQualifier(ScreenWidthQualifier qualifier) {
+ mQualifiers[INDEX_SCREEN_WIDTH] = qualifier;
+ }
+
+ public ScreenWidthQualifier getScreenWidthQualifier() {
+ return (ScreenWidthQualifier) mQualifiers[INDEX_SCREEN_WIDTH];
+ }
+
+ public void setScreenHeightQualifier(ScreenHeightQualifier qualifier) {
+ mQualifiers[INDEX_SCREEN_HEIGHT] = qualifier;
+ }
+
+ public ScreenHeightQualifier getScreenHeightQualifier() {
+ return (ScreenHeightQualifier) mQualifiers[INDEX_SCREEN_HEIGHT];
+ }
+
public void setScreenSizeQualifier(ScreenSizeQualifier qualifier) {
mQualifiers[INDEX_SCREEN_SIZE] = qualifier;
}
@@ -400,6 +455,61 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
}
/**
+ * Updates the {@link SmallestScreenWidthQualifier}, {@link ScreenWidthQualifier}, and
+ * {@link ScreenHeightQualifier} based on the (required) values of
+ * {@link ScreenDimensionQualifier} {@link PixelDensityQualifier}, and
+ * {@link ScreenOrientationQualifier}.
+ *
+ * Also the density cannot be {@link Density#NODPI} as it's not valid on a device.
+ */
+ public void updateScreenWidthAndHeight() {
+
+ ResourceQualifier sizeQ = mQualifiers[INDEX_SCREEN_DIMENSION];
+ ResourceQualifier densityQ = mQualifiers[INDEX_PIXEL_DENSITY];
+ ResourceQualifier orientQ = mQualifiers[INDEX_SCREEN_ORIENTATION];
+
+ if (sizeQ != null && densityQ != null && orientQ != null) {
+ Density density = ((PixelDensityQualifier) densityQ).getValue();
+ if (density == Density.NODPI) {
+ return;
+ }
+
+ ScreenOrientation orientation = ((ScreenOrientationQualifier) orientQ).getValue();
+
+ int size1 = ((ScreenDimensionQualifier) sizeQ).getValue1();
+ int size2 = ((ScreenDimensionQualifier) sizeQ).getValue2();
+
+ // make sure size1 is the biggest (should be the case, but make sure)
+ if (size1 < size2) {
+ int a = size1;
+ size1 = size2;
+ size2 = a;
+ }
+
+ // compute the dp. round them up since we want -w480dp to match a 480.5dp screen
+ int dp1 = (int) Math.ceil(size1 * Density.DEFAULT_DENSITY / density.getDpiValue());
+ int dp2 = (int) Math.ceil(size2 * Density.DEFAULT_DENSITY / density.getDpiValue());
+
+ setSmallestScreenWidthQualifier(new SmallestScreenWidthQualifier(dp2));
+
+ switch (orientation) {
+ case PORTRAIT:
+ setScreenWidthQualifier(new ScreenWidthQualifier(dp2));
+ setScreenHeightQualifier(new ScreenHeightQualifier(dp1));
+ break;
+ case LANDSCAPE:
+ setScreenWidthQualifier(new ScreenWidthQualifier(dp1));
+ setScreenHeightQualifier(new ScreenHeightQualifier(dp2));
+ break;
+ case SQUARE:
+ setScreenWidthQualifier(new ScreenWidthQualifier(dp2));
+ setScreenHeightQualifier(new ScreenHeightQualifier(dp2));
+ break;
+ }
+ }
+ }
+
+ /**
* Returns whether an object is equals to the receiver.
*/
@Override
@@ -704,6 +814,7 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
return false;
}
}
+
return true;
}
@@ -732,6 +843,9 @@ public final class FolderConfiguration implements Comparable<FolderConfiguration
mQualifiers[INDEX_NETWORK_CODE] = new NetworkCodeQualifier();
mQualifiers[INDEX_LANGUAGE] = new LanguageQualifier();
mQualifiers[INDEX_REGION] = new RegionQualifier();
+ mQualifiers[INDEX_SMALLEST_SCREEN_WIDTH] = new SmallestScreenWidthQualifier();
+ mQualifiers[INDEX_SCREEN_WIDTH] = new ScreenWidthQualifier();
+ mQualifiers[INDEX_SCREEN_HEIGHT] = new ScreenHeightQualifier();
mQualifiers[INDEX_SCREEN_SIZE] = new ScreenSizeQualifier();
mQualifiers[INDEX_SCREEN_RATIO] = new ScreenRatioQualifier();
mQualifiers[INDEX_SCREEN_ORIENTATION] = new ScreenOrientationQualifier();
diff --git a/ide_common/src/com/android/ide/common/resources/configuration/ScreenDimensionQualifier.java b/ide_common/src/com/android/ide/common/resources/configuration/ScreenDimensionQualifier.java
index a58789a..9b42b88 100644
--- a/ide_common/src/com/android/ide/common/resources/configuration/ScreenDimensionQualifier.java
+++ b/ide_common/src/com/android/ide/common/resources/configuration/ScreenDimensionQualifier.java
@@ -144,7 +144,7 @@ public final class ScreenDimensionQualifier extends ResourceQualifier {
@Override
public String getShortDisplayValue() {
- if (mValue1 != -1 && mValue2 != -1) {
+ if (isValid()) {
return String.format("%1$dx%2$d", mValue1, mValue2);
}
@@ -153,7 +153,7 @@ public final class ScreenDimensionQualifier extends ResourceQualifier {
@Override
public String getLongDisplayValue() {
- if (mValue1 != -1 && mValue2 != -1) {
+ if (isValid()) {
return String.format("Screen resolution %1$dx%2$d", mValue1, mValue2);
}
diff --git a/ide_common/src/com/android/ide/common/resources/configuration/ScreenHeightQualifier.java b/ide_common/src/com/android/ide/common/resources/configuration/ScreenHeightQualifier.java
new file mode 100644
index 0000000..2899631
--- /dev/null
+++ b/ide_common/src/com/android/ide/common/resources/configuration/ScreenHeightQualifier.java
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ide.common.resources.configuration;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Resource Qualifier for Screen Pixel Density.
+ */
+public final class ScreenHeightQualifier extends ResourceQualifier {
+ /** Default screen size value. This means the property is not set */
+ final static int DEFAULT_SIZE = -1;
+
+ private final static Pattern sParsePattern = Pattern.compile("^h(\\d+)dp$");//$NON-NLS-1$
+ private final static String sPrintPattern = "h%1$ddp";
+
+ public static final String NAME = "Screen Height";
+
+ private int mValue = DEFAULT_SIZE;
+
+ public ScreenHeightQualifier() {
+ // pass
+ }
+
+ public ScreenHeightQualifier(int value) {
+ mValue = value;
+ }
+
+ public int getValue() {
+ return mValue;
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public String getShortName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean hasFakeValue() {
+ return false;
+ }
+
+ @Override
+ public boolean isValid() {
+ return mValue != DEFAULT_SIZE;
+ }
+
+ @Override
+ public boolean checkAndSet(String value, FolderConfiguration config) {
+ Matcher m = sParsePattern.matcher(value);
+ if (m.matches()) {
+ String v = m.group(1);
+
+ ScreenHeightQualifier qualifier = getQualifier(v);
+ if (qualifier != null) {
+ config.setScreenHeightQualifier(qualifier);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public static ScreenHeightQualifier getQualifier(String value) {
+ try {
+ int dp = Integer.parseInt(value);
+
+ ScreenHeightQualifier qualifier = new ScreenHeightQualifier();
+ qualifier.mValue = dp;
+ return qualifier;
+
+ } catch (NumberFormatException e) {
+ }
+
+ return null;
+ }
+
+ @Override
+ public boolean isMatchFor(ResourceQualifier qualifier) {
+ // this is the match only of the current dp value is lower or equal to the
+ if (qualifier instanceof ScreenHeightQualifier) {
+ return mValue <= ((ScreenHeightQualifier) qualifier).mValue;
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
+ if (compareTo == null) {
+ return true;
+ }
+
+ ScreenHeightQualifier compareQ = (ScreenHeightQualifier)compareTo;
+ ScreenHeightQualifier referenceQ = (ScreenHeightQualifier)reference;
+
+ if (compareQ.mValue == referenceQ.mValue) {
+ // what we have is already the best possible match (exact match)
+ return false;
+ } else if (mValue == referenceQ.mValue) {
+ // got new exact value, this is the best!
+ return true;
+ } else {
+ // get the qualifier that has the width that is the closest to the reference, but not
+ // above. (which is guaranteed when this is called as isMatchFor is called first.
+ return mValue > compareQ.mValue;
+ }
+ }
+
+ @Override
+ public String getFolderSegment() {
+ return String.format(sPrintPattern, mValue);
+ }
+
+ @Override
+ public String getShortDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public String getLongDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public int hashCode() {
+ return mValue;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (getClass() != obj.getClass())
+ return false;
+ ScreenHeightQualifier other = (ScreenHeightQualifier) obj;
+ if (mValue != other.mValue)
+ return false;
+ return true;
+ }
+}
diff --git a/ide_common/src/com/android/ide/common/resources/configuration/ScreenWidthQualifier.java b/ide_common/src/com/android/ide/common/resources/configuration/ScreenWidthQualifier.java
new file mode 100644
index 0000000..8748864
--- /dev/null
+++ b/ide_common/src/com/android/ide/common/resources/configuration/ScreenWidthQualifier.java
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ide.common.resources.configuration;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Resource Qualifier for Screen Pixel Density.
+ */
+public final class ScreenWidthQualifier extends ResourceQualifier {
+ /** Default screen size value. This means the property is not set */
+ final static int DEFAULT_SIZE = -1;
+
+ private final static Pattern sParsePattern = Pattern.compile("^w(\\d+)dp$"); //$NON-NLS-1$
+ private final static String sPrintPattern = "w%1$ddp"; //$NON-NLS-1$
+
+ public static final String NAME = "Screen Width";
+
+ private int mValue = DEFAULT_SIZE;
+
+ public ScreenWidthQualifier() {
+ // pass
+ }
+
+ public ScreenWidthQualifier(int value) {
+ mValue = value;
+ }
+
+ public int getValue() {
+ return mValue;
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public String getShortName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean hasFakeValue() {
+ return false;
+ }
+
+ @Override
+ public boolean isValid() {
+ return mValue != DEFAULT_SIZE;
+ }
+
+ @Override
+ public boolean checkAndSet(String value, FolderConfiguration config) {
+ Matcher m = sParsePattern.matcher(value);
+ if (m.matches()) {
+ String v = m.group(1);
+
+ ScreenWidthQualifier qualifier = getQualifier(v);
+ if (qualifier != null) {
+ config.setScreenWidthQualifier(qualifier);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public static ScreenWidthQualifier getQualifier(String value) {
+ try {
+ int dp = Integer.parseInt(value);
+
+ ScreenWidthQualifier qualifier = new ScreenWidthQualifier();
+ qualifier.mValue = dp;
+ return qualifier;
+
+ } catch (NumberFormatException e) {
+ }
+
+ return null;
+ }
+
+ @Override
+ public boolean isMatchFor(ResourceQualifier qualifier) {
+ // this is the match only of the current dp value is lower or equal to the
+ if (qualifier instanceof ScreenWidthQualifier) {
+ return mValue <= ((ScreenWidthQualifier) qualifier).mValue;
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
+ if (compareTo == null) {
+ return true;
+ }
+
+ ScreenWidthQualifier compareQ = (ScreenWidthQualifier)compareTo;
+ ScreenWidthQualifier referenceQ = (ScreenWidthQualifier)reference;
+
+ if (compareQ.mValue == referenceQ.mValue) {
+ // what we have is already the best possible match (exact match)
+ return false;
+ } else if (mValue == referenceQ.mValue) {
+ // got new exact value, this is the best!
+ return true;
+ } else {
+ // get the qualifier that has the width that is the closest to the reference, but not
+ // above. (which is guaranteed when this is called as isMatchFor is called first.
+ return mValue > compareQ.mValue;
+ }
+ }
+
+ @Override
+ public String getFolderSegment() {
+ return String.format(sPrintPattern, mValue);
+ }
+
+ @Override
+ public String getShortDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public String getLongDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public int hashCode() {
+ return mValue;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (getClass() != obj.getClass())
+ return false;
+ ScreenWidthQualifier other = (ScreenWidthQualifier) obj;
+ if (mValue != other.mValue)
+ return false;
+ return true;
+ }
+}
diff --git a/ide_common/src/com/android/ide/common/resources/configuration/SmallestScreenWidthQualifier.java b/ide_common/src/com/android/ide/common/resources/configuration/SmallestScreenWidthQualifier.java
new file mode 100644
index 0000000..e151805
--- /dev/null
+++ b/ide_common/src/com/android/ide/common/resources/configuration/SmallestScreenWidthQualifier.java
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.ide.common.resources.configuration;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Resource Qualifier for Screen Pixel Density.
+ */
+public final class SmallestScreenWidthQualifier extends ResourceQualifier {
+ /** Default screen size value. This means the property is not set */
+ final static int DEFAULT_SIZE = -1;
+
+ private final static Pattern sParsePattern = Pattern.compile("^sw(\\d+)dp$"); //$NON-NLS-1$
+ private final static String sPrintPattern = "sw%1$ddp"; //$NON-NLS-1$
+
+ public static final String NAME = "Smallest Screen Width";
+
+ private int mValue = DEFAULT_SIZE;
+
+ public SmallestScreenWidthQualifier() {
+ // pass
+ }
+
+ public SmallestScreenWidthQualifier(int value) {
+ mValue = value;
+ }
+
+ public int getValue() {
+ return mValue;
+ }
+
+ @Override
+ public String getName() {
+ return NAME;
+ }
+
+ @Override
+ public String getShortName() {
+ return NAME;
+ }
+
+ @Override
+ public boolean hasFakeValue() {
+ return false;
+ }
+
+ @Override
+ public boolean isValid() {
+ return mValue != DEFAULT_SIZE;
+ }
+
+ @Override
+ public boolean checkAndSet(String value, FolderConfiguration config) {
+ Matcher m = sParsePattern.matcher(value);
+ if (m.matches()) {
+ String v = m.group(1);
+
+ SmallestScreenWidthQualifier qualifier = getQualifier(v);
+ if (qualifier != null) {
+ config.setSmallestScreenWidthQualifier(qualifier);
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ public static SmallestScreenWidthQualifier getQualifier(String value) {
+ try {
+ int dp = Integer.parseInt(value);
+
+ SmallestScreenWidthQualifier qualifier = new SmallestScreenWidthQualifier();
+ qualifier.mValue = dp;
+ return qualifier;
+
+ } catch (NumberFormatException e) {
+ }
+
+ return null;
+ }
+
+ @Override
+ public boolean isMatchFor(ResourceQualifier qualifier) {
+ // this is the match only of the current dp value is lower or equal to the
+ if (qualifier instanceof SmallestScreenWidthQualifier) {
+ return mValue <= ((SmallestScreenWidthQualifier) qualifier).mValue;
+ }
+
+ return false;
+ }
+
+ @Override
+ public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
+ if (compareTo == null) {
+ return true;
+ }
+
+ SmallestScreenWidthQualifier compareQ = (SmallestScreenWidthQualifier)compareTo;
+ SmallestScreenWidthQualifier referenceQ = (SmallestScreenWidthQualifier)reference;
+
+ if (compareQ.mValue == referenceQ.mValue) {
+ // what we have is already the best possible match (exact match)
+ return false;
+ } else if (mValue == referenceQ.mValue) {
+ // got new exact value, this is the best!
+ return true;
+ } else {
+ // get the qualifier that has the width that is the closest to the reference, but not
+ // above. (which is guaranteed when this is called as isMatchFor is called first.
+ return mValue > compareQ.mValue;
+ }
+ }
+
+ @Override
+ public String getFolderSegment() {
+ return String.format(sPrintPattern, mValue);
+ }
+
+ @Override
+ public String getShortDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public String getLongDisplayValue() {
+ if (isValid()) {
+ return getFolderSegment();
+ }
+
+ return ""; //$NON-NLS-1$
+ }
+
+ @Override
+ public int hashCode() {
+ return mValue;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (getClass() != obj.getClass())
+ return false;
+ SmallestScreenWidthQualifier other = (SmallestScreenWidthQualifier) obj;
+ if (mValue != other.mValue)
+ return false;
+ return true;
+ }
+}