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