Home | History | Annotate | Download | only in resources
      1 /*
      2  * Copyright (C) 2010 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.resources;
     18 
     19 
     20 /**
     21  * Density enum.
     22  * <p/>This is used in the manifest in the uses-configuration node and in the resource folder names
     23  * as well as other places needing to know the density values.
     24  */
     25 public enum Density implements ResourceEnum {
     26     XHIGH("xhdpi", "X-High Density", 320), //$NON-NLS-1$
     27     HIGH("hdpi", "High Density", 240), //$NON-NLS-1$
     28     TV("tvdpi", "TV Density", 213), //$NON-NLS-1$
     29     MEDIUM("mdpi", "Medium Density", 160), //$NON-NLS-1$
     30     LOW("ldpi", "Low Density", 120), //$NON-NLS-1$
     31     NODPI("nodpi", "No Density", 0); //$NON-NLS-1$
     32 
     33     public final static int DEFAULT_DENSITY = 160;
     34 
     35     private final String mValue;
     36     private final String mDisplayValue;
     37     private final int mDensity;
     38 
     39     private Density(String value, String displayValue, int density) {
     40         mValue = value;
     41         mDisplayValue = displayValue;
     42         mDensity = density;
     43     }
     44 
     45     /**
     46      * Returns the enum matching the provided qualifier value.
     47      * @param value The qualifier value.
     48      * @return the enum for the qualifier value or null if no match was found.
     49      */
     50     public static Density getEnum(String value) {
     51         for (Density orient : values()) {
     52             if (orient.mValue.equals(value)) {
     53                 return orient;
     54             }
     55         }
     56 
     57         return null;
     58     }
     59 
     60     /**
     61      * Returns the enum matching the given density value
     62      * @param value The density value.
     63      * @return the enum for the density value or null if no match was found.
     64      */
     65     public static Density getEnum(int value) {
     66         for (Density d : values()) {
     67             if (d.mDensity == value) {
     68                 return d;
     69             }
     70         }
     71 
     72         return null;
     73     }
     74 
     75     public String getResourceValue() {
     76         return mValue;
     77     }
     78 
     79     public int getDpiValue() {
     80         return mDensity;
     81     }
     82 
     83     public String getLegacyValue() {
     84         if (this != NODPI) {
     85             return String.format("%1$ddpi", getDpiValue());
     86         }
     87 
     88         return "";
     89     }
     90 
     91     public String getShortDisplayValue() {
     92         return mDisplayValue;
     93     }
     94 
     95     public String getLongDisplayValue() {
     96         return mDisplayValue;
     97     }
     98 
     99     public static int getIndex(Density value) {
    100         int i = 0;
    101         for (Density input : values()) {
    102             if (value == input) {
    103                 return i;
    104             }
    105 
    106             i++;
    107         }
    108 
    109         return -1;
    110     }
    111 
    112     public static Density getByIndex(int index) {
    113         int i = 0;
    114         for (Density value : values()) {
    115             if (i == index) {
    116                 return value;
    117             }
    118             i++;
    119         }
    120         return null;
    121     }
    122 
    123     public boolean isFakeValue() {
    124         return false;
    125     }
    126 
    127     public boolean isValidValueForDevice() {
    128         return this != NODPI; // nodpi is not a valid config for devices.
    129     }
    130 }
    131