Home | History | Annotate | Download | only in configuration
      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 ScreenHeightQualifier 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("^h(\\d+)dp$");//$NON-NLS-1$
     30     private final static String sPrintPattern = "h%1$ddp";
     31 
     32     public static final String NAME = "Screen Height";
     33 
     34     private int mValue = DEFAULT_SIZE;
     35 
     36     public ScreenHeightQualifier() {
     37         // pass
     38     }
     39 
     40     public ScreenHeightQualifier(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 int since() {
     60         return 13;
     61     }
     62 
     63     @Override
     64     public boolean hasFakeValue() {
     65         return false;
     66     }
     67 
     68     @Override
     69     public boolean isValid() {
     70         return mValue != DEFAULT_SIZE;
     71     }
     72 
     73     @Override
     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             ScreenHeightQualifier qualifier = getQualifier(v);
     80             if (qualifier != null) {
     81                 config.setScreenHeightQualifier(qualifier);
     82                 return true;
     83             }
     84         }
     85 
     86         return false;
     87     }
     88 
     89     public static ScreenHeightQualifier getQualifier(String value) {
     90         try {
     91             int dp = Integer.parseInt(value);
     92 
     93             ScreenHeightQualifier qualifier = new ScreenHeightQualifier();
     94             qualifier.mValue = dp;
     95             return qualifier;
     96 
     97         } catch (NumberFormatException e) {
     98         }
     99 
    100         return null;
    101     }
    102 
    103     @Override
    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 ScreenHeightQualifier) {
    107             return mValue <= ((ScreenHeightQualifier) qualifier).mValue;
    108         }
    109 
    110         return false;
    111     }
    112 
    113     @Override
    114     public boolean isBetterMatchThan(ResourceQualifier compareTo, ResourceQualifier reference) {
    115         if (compareTo == null) {
    116             return true;
    117         }
    118 
    119         ScreenHeightQualifier compareQ = (ScreenHeightQualifier)compareTo;
    120         ScreenHeightQualifier referenceQ = (ScreenHeightQualifier)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
    136     public String getFolderSegment() {
    137         return String.format(sPrintPattern, mValue);
    138     }
    139 
    140     @Override
    141     public String getShortDisplayValue() {
    142         if (isValid()) {
    143             return getFolderSegment();
    144         }
    145 
    146         return ""; //$NON-NLS-1$
    147     }
    148 
    149     @Override
    150     public String getLongDisplayValue() {
    151         if (isValid()) {
    152             return getFolderSegment();
    153         }
    154 
    155         return ""; //$NON-NLS-1$
    156     }
    157 
    158 
    159     @Override
    160     public int hashCode() {
    161         return mValue;
    162     }
    163 
    164     @Override
    165     public boolean equals(Object obj) {
    166         if (this == obj) {
    167             return true;
    168         }
    169         if (obj == null) {
    170             return false;
    171         }
    172         if (getClass() != obj.getClass()) {
    173             return false;
    174         }
    175         ScreenHeightQualifier other = (ScreenHeightQualifier) obj;
    176         if (mValue != other.mValue) {
    177             return false;
    178         }
    179         return true;
    180     }
    181 }
    182