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