• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 8), //$NON-NLS-1$
27     HIGH("hdpi", "High Density", 240, 4), //$NON-NLS-1$
28     TV("tvdpi", "TV Density", 213, 13), //$NON-NLS-1$
29     MEDIUM("mdpi", "Medium Density", 160, 4), //$NON-NLS-1$
30     LOW("ldpi", "Low Density", 120, 4), //$NON-NLS-1$
31     NODPI("nodpi", "No Density", 0, 4); //$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     private final int mSince;
39 
Density(String value, String displayValue, int density, int since)40     private Density(String value, String displayValue, int density, int since) {
41         mValue = value;
42         mDisplayValue = displayValue;
43         mDensity = density;
44         mSince = since;
45     }
46 
47     /**
48      * Returns the enum matching the provided qualifier value.
49      * @param value The qualifier value.
50      * @return the enum for the qualifier value or null if no match was found.
51      */
getEnum(String value)52     public static Density getEnum(String value) {
53         for (Density orient : values()) {
54             if (orient.mValue.equals(value)) {
55                 return orient;
56             }
57         }
58 
59         return null;
60     }
61 
62     /**
63      * Returns the enum matching the given density value
64      * @param value The density value.
65      * @return the enum for the density value or null if no match was found.
66      */
getEnum(int value)67     public static Density getEnum(int value) {
68         for (Density d : values()) {
69             if (d.mDensity == value) {
70                 return d;
71             }
72         }
73 
74         return null;
75     }
76 
77     @Override
getResourceValue()78     public String getResourceValue() {
79         return mValue;
80     }
81 
getDpiValue()82     public int getDpiValue() {
83         return mDensity;
84     }
85 
since()86     public int since() {
87         return mSince;
88     }
89 
getLegacyValue()90     public String getLegacyValue() {
91         if (this != NODPI) {
92             return String.format("%1$ddpi", getDpiValue());
93         }
94 
95         return "";
96     }
97 
98     @Override
getShortDisplayValue()99     public String getShortDisplayValue() {
100         return mDisplayValue;
101     }
102 
103     @Override
getLongDisplayValue()104     public String getLongDisplayValue() {
105         return mDisplayValue;
106     }
107 
getIndex(Density value)108     public static int getIndex(Density value) {
109         int i = 0;
110         for (Density input : values()) {
111             if (value == input) {
112                 return i;
113             }
114 
115             i++;
116         }
117 
118         return -1;
119     }
120 
getByIndex(int index)121     public static Density getByIndex(int index) {
122         int i = 0;
123         for (Density value : values()) {
124             if (i == index) {
125                 return value;
126             }
127             i++;
128         }
129         return null;
130     }
131 
132     @Override
isFakeValue()133     public boolean isFakeValue() {
134         return false;
135     }
136 
137     @Override
isValidValueForDevice()138     public boolean isValidValueForDevice() {
139         return this != NODPI; // nodpi is not a valid config for devices.
140     }
141 }
142