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