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 java.util.regex.Matcher;
     20 import java.util.regex.Pattern;
     21 
     22 /**
     23  * Resource Qualifier for Screen Dimension.
     24  */
     25 public final class ScreenDimensionQualifier 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 sDimensionPattern = Pattern.compile(
     30             "^(\\d+)x(\\d+)$"); //$NON-NLS-1$
     31 
     32     public static final String NAME = "Screen Dimension";
     33 
     34     /** Screen size 1 value. This is not size X or Y because the folder name always
     35      * contains the biggest size first. So if the qualifier is 400x200, size 1 will always be
     36      * 400 but that'll be X in landscape and Y in portrait.
     37      * Default value is <code>DEFAULT_SIZE</code> */
     38     private int mValue1 = DEFAULT_SIZE;
     39 
     40     /** Screen size 2 value. This is not size X or Y because the folder name always
     41      * contains the biggest size first. So if the qualifier is 400x200, size 2 will always be
     42      * 200 but that'll be Y in landscape and X in portrait.
     43      * Default value is <code>DEFAULT_SIZE</code> */
     44     private int mValue2 = DEFAULT_SIZE;
     45 
     46     public ScreenDimensionQualifier() {
     47         // pass
     48     }
     49 
     50     public ScreenDimensionQualifier(int value1, int value2) {
     51         mValue1 = value1;
     52         mValue2 = value2;
     53     }
     54 
     55     public int getValue1() {
     56         return mValue1;
     57     }
     58 
     59     public int getValue2() {
     60         return mValue2;
     61     }
     62 
     63     @Override
     64     public String getName() {
     65         return NAME;
     66     }
     67 
     68     @Override
     69     public String getShortName() {
     70         return "Dimension";
     71     }
     72 
     73     @Override
     74     public boolean isValid() {
     75         return mValue1 != DEFAULT_SIZE && mValue2 != DEFAULT_SIZE;
     76     }
     77 
     78     @Override
     79     public boolean hasFakeValue() {
     80         return false;
     81     }
     82 
     83     @Override
     84     public boolean checkAndSet(String value, FolderConfiguration config) {
     85         Matcher m = sDimensionPattern.matcher(value);
     86         if (m.matches()) {
     87             String d1 = m.group(1);
     88             String d2 = m.group(2);
     89 
     90             ScreenDimensionQualifier qualifier = getQualifier(d1, d2);
     91             if (qualifier != null) {
     92                 config.setScreenDimensionQualifier(qualifier);
     93                 return true;
     94             }
     95         }
     96         return false;
     97     }
     98 
     99     @Override
    100     public boolean equals(Object qualifier) {
    101         if (qualifier instanceof ScreenDimensionQualifier) {
    102             ScreenDimensionQualifier q = (ScreenDimensionQualifier)qualifier;
    103             return (mValue1 == q.mValue1 && mValue2 == q.mValue2);
    104         }
    105 
    106         return false;
    107     }
    108 
    109     @Override
    110     public int hashCode() {
    111         return toString().hashCode();
    112     }
    113 
    114     public static ScreenDimensionQualifier getQualifier(String size1, String size2) {
    115         try {
    116             int s1 = Integer.parseInt(size1);
    117             int s2 = Integer.parseInt(size2);
    118 
    119             ScreenDimensionQualifier qualifier = new ScreenDimensionQualifier();
    120 
    121             if (s1 > s2) {
    122                 qualifier.mValue1 = s1;
    123                 qualifier.mValue2 = s2;
    124             } else {
    125                 qualifier.mValue1 = s2;
    126                 qualifier.mValue2 = s1;
    127             }
    128 
    129             return qualifier;
    130         } catch (NumberFormatException e) {
    131             // looks like the string we extracted wasn't a valid number.
    132         }
    133 
    134         return null;
    135     }
    136 
    137     /**
    138      * Returns the string used to represent this qualifier in the folder name.
    139      */
    140     @Override
    141     public String getFolderSegment() {
    142         return String.format("%1$dx%2$d", mValue1, mValue2); //$NON-NLS-1$
    143     }
    144 
    145     @Override
    146     public String getShortDisplayValue() {
    147         if (isValid()) {
    148             return String.format("%1$dx%2$d", mValue1, mValue2);
    149         }
    150 
    151         return ""; //$NON-NLS-1$
    152     }
    153 
    154     @Override
    155     public String getLongDisplayValue() {
    156         if (isValid()) {
    157             return String.format("Screen resolution %1$dx%2$d", mValue1, mValue2);
    158         }
    159 
    160         return ""; //$NON-NLS-1$
    161     }
    162 }
    163