1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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.eclipse.adt.internal.resources.configurations; 18 19 import com.android.ide.eclipse.adt.internal.editors.IconFactory; 20 import com.android.sdklib.resources.Density; 21 import com.android.sdklib.resources.ResourceEnum; 22 23 import org.eclipse.swt.graphics.Image; 24 25 import java.util.regex.Matcher; 26 import java.util.regex.Pattern; 27 28 /** 29 * Resource Qualifier for Screen Pixel Density. 30 */ 31 public final class PixelDensityQualifier extends EnumBasedResourceQualifier { 32 private final static Pattern sDensityLegacyPattern = Pattern.compile("^(\\d+)dpi$");//$NON-NLS-1$ 33 34 public static final String NAME = "Pixel Density"; 35 36 private Density mValue = Density.MEDIUM; 37 38 public PixelDensityQualifier() { 39 // pass 40 } 41 42 public PixelDensityQualifier(Density value) { 43 mValue = value; 44 } 45 46 public Density getValue() { 47 return mValue; 48 } 49 50 @Override 51 ResourceEnum getEnumValue() { 52 return mValue; 53 } 54 55 @Override 56 public String getName() { 57 return NAME; 58 } 59 60 @Override 61 public String getShortName() { 62 return NAME; 63 } 64 65 @Override 66 public Image getIcon() { 67 return IconFactory.getInstance().getIcon("dpi"); //$NON-NLS-1$ 68 } 69 70 @Override 71 public boolean checkAndSet(String value, FolderConfiguration config) { 72 Density density = Density.getEnum(value); 73 if (density == null) { 74 75 // attempt to read a legacy value. 76 Matcher m = sDensityLegacyPattern.matcher(value); 77 if (m.matches()) { 78 String v = m.group(1); 79 80 try { 81 density = Density.getEnum(Integer.parseInt(v)); 82 } catch (NumberFormatException e) { 83 // looks like the string we extracted wasn't a valid number 84 // which really shouldn't happen since the regexp would have failed. 85 } 86 } 87 } 88 89 if (density != null) { 90 PixelDensityQualifier qualifier = new PixelDensityQualifier(); 91 qualifier.mValue = density; 92 config.setPixelDensityQualifier(qualifier); 93 return true; 94 } 95 96 return false; 97 } 98 99 @Override 100 public boolean isMatchFor(ResourceQualifier qualifier) { 101 if (qualifier instanceof PixelDensityQualifier) { 102 // as long as there's a density qualifier, it's always a match. 103 // The best match will be found later. 104 return true; 105 } 106 107 return false; 108 } 109 110 @Override 111 public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) { 112 if (compareTo == null) { 113 return true; 114 } 115 116 PixelDensityQualifier compareQ = (PixelDensityQualifier)compareTo; 117 PixelDensityQualifier referenceQ = (PixelDensityQualifier)reference; 118 119 if (mValue == referenceQ.mValue && compareQ.mValue != referenceQ.mValue) { 120 // got exact value, this is the best! 121 return true; 122 } else { 123 // in all case we're going to prefer the higher dpi. 124 // if reference is high, we want highest dpi. 125 // if reference is medium, we'll prefer to scale down high dpi, than scale up low dpi 126 // if reference if low, we'll prefer to scale down high than medium (2:1 over 4:3) 127 return mValue.getDpiValue() > compareQ.mValue.getDpiValue(); 128 } 129 } 130 } 131