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 int since() {
     64         return 4;
     65     }
     66 
     67     @Override
     68     public boolean checkAndSet(String value, FolderConfiguration config) {
     69         Density density = Density.getEnum(value);
     70         if (density == null) {
     71 
     72             // attempt to read a legacy value.
     73             Matcher m = sDensityLegacyPattern.matcher(value);
     74             if (m.matches()) {
     75                 String v = m.group(1);
     76 
     77                 try {
     78                     density = Density.getEnum(Integer.parseInt(v));
     79                 } catch (NumberFormatException e) {
     80                     // looks like the string we extracted wasn't a valid number
     81                     // which really shouldn't happen since the regexp would have failed.
     82                 }
     83             }
     84         }
     85 
     86         if (density != null) {
     87             DensityQualifier qualifier = new DensityQualifier();
     88             qualifier.mValue = density;
     89             config.setDensityQualifier(qualifier);
     90             return true;
     91         }
     92 
     93         return false;
     94     }
     95 
     96     @Override
     97     public boolean isMatchFor(ResourceQualifier qualifier) {
     98         if (qualifier instanceof DensityQualifier) {
     99             // as long as there's a density qualifier, it's always a match.
    100             // The best match will be found later.
    101             return true;
    102         }
    103 
    104         return false;
    105     }
    106 
    107     @Override
    108     public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
    109         if (compareTo == null) {
    110             return true;
    111         }
    112 
    113         DensityQualifier compareQ = (DensityQualifier)compareTo;
    114         DensityQualifier referenceQ = (DensityQualifier)reference;
    115 
    116         if (compareQ.mValue == referenceQ.mValue) {
    117             // what we have is already the best possible match (exact match)
    118             return false;
    119         } else if (mValue == referenceQ.mValue) {
    120             // got new 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