Home | History | Annotate | Download | only in configuration
      1 /*
      2  * Copyright (C) 2007 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.resources.Density;
     20 import com.android.resources.ResourceEnum;
     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 DensityQualifier extends EnumBasedResourceQualifier {
     29     private final static Pattern sDensityLegacyPattern = Pattern.compile("^(\\d+)dpi$");//$NON-NLS-1$
     30 
     31     public static final String NAME = "Density";
     32 
     33     private Density mValue = Density.MEDIUM;
     34 
     35     public DensityQualifier() {
     36         // pass
     37     }
     38 
     39     public DensityQualifier(Density value) {
     40         mValue = value;
     41     }
     42 
     43     public Density getValue() {
     44         return mValue;
     45     }
     46 
     47     @Override
     48     ResourceEnum getEnumValue() {
     49         return mValue;
     50     }
     51 
     52     @Override
     53     public String getName() {
     54         return NAME;
     55     }
     56 
     57     @Override
     58     public String getShortName() {
     59         return NAME;
     60     }
     61 
     62     @Override
     63     public boolean checkAndSet(String value, FolderConfiguration config) {
     64         Density density = Density.getEnum(value);
     65         if (density == null) {
     66 
     67             // attempt to read a legacy value.
     68             Matcher m = sDensityLegacyPattern.matcher(value);
     69             if (m.matches()) {
     70                 String v = m.group(1);
     71 
     72                 try {
     73                     density = Density.getEnum(Integer.parseInt(v));
     74                 } catch (NumberFormatException e) {
     75                     // looks like the string we extracted wasn't a valid number
     76                     // which really shouldn't happen since the regexp would have failed.
     77                 }
     78             }
     79         }
     80 
     81         if (density != null) {
     82             DensityQualifier qualifier = new DensityQualifier();
     83             qualifier.mValue = density;
     84             config.setDensityQualifier(qualifier);
     85             return true;
     86         }
     87 
     88         return false;
     89     }
     90 
     91     @Override
     92     public boolean isMatchFor(ResourceQualifier qualifier) {
     93         if (qualifier instanceof DensityQualifier) {
     94             // as long as there's a density qualifier, it's always a match.
     95             // The best match will be found later.
     96             return true;
     97         }
     98 
     99         return false;
    100     }
    101 
    102     @Override
    103     public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
    104         if (compareTo == null) {
    105             return true;
    106         }
    107 
    108         DensityQualifier compareQ = (DensityQualifier)compareTo;
    109         DensityQualifier referenceQ = (DensityQualifier)reference;
    110 
    111         if (compareQ.mValue == referenceQ.mValue) {
    112             // what we have is already the best possible match (exact match)
    113             return false;
    114         } else if (mValue == referenceQ.mValue) {
    115             // got new exact value, this is the best!
    116             return true;
    117         } else {
    118             // in all case we're going to prefer the higher dpi.
    119             // if reference is high, we want highest dpi.
    120             // if reference is medium, we'll prefer to scale down high dpi, than scale up low dpi
    121             // if reference if low, we'll prefer to scale down high than medium (2:1 over 4:3)
    122             return mValue.getDpiValue() > compareQ.mValue.getDpiValue();
    123         }
    124     }
    125 }
    126