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