| 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 |
import java.util.EnumSet; |
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
/** |
| 24 |
* Density enum. |
| 25 |
* <p>This is used in the manifest in the uses-configuration node and in the resource folder names |
| 26 |
* as well as other places needing to know the density values. |
| 27 |
*/ |
| 28 |
public enum Density implements ResourceEnum { |
| 29 |
XXXHIGH("xxxhdpi", "XXX-High Density", 640, 18), //$NON-NLS-1$ |
| 30 |
DPI_560("560dpi", "560 DPI Density", 560, 1), //$NON-NLS-1$ |
| 31 |
XXHIGH( "xxhdpi", "XX-High Density", 480, 16), //$NON-NLS-1$ |
| 32 |
DPI_420("420dpi", "420 DPI Density", 420, 23), //$NON-NLS-1$ |
| 33 |
DPI_400("400dpi", "400 DPI Density", 400, 1), //$NON-NLS-1$ |
| 34 |
DPI_360("360dpi", "360 DPI Density", 360, 23), //$NON-NLS-1$ |
| 35 |
XHIGH( "xhdpi", "X-High Density", 320, 8), //$NON-NLS-1$ |
| 36 |
DPI_280("280dpi", "280 DPI Density", 280, 22), //$NON-NLS-1$ |
| 37 |
HIGH( "hdpi", "High Density", 240, 4), //$NON-NLS-1$ |
| 38 |
TV( "tvdpi", "TV Density", 213, 13), //$NON-NLS-1$ |
| 39 |
MEDIUM( "mdpi", "Medium Density", 160, 4), //$NON-NLS-1$ |
| 40 |
LOW( "ldpi", "Low Density", 120, 4), //$NON-NLS-1$ |
| 41 |
ANYDPI( "anydpi", "Any Density", 0, 21), //$NON-NLS-1$ |
| 42 |
NODPI( "nodpi", "No Density", 0, 4); //$NON-NLS-1$ |
| 43 |
|
| 44 |
public static final int DEFAULT_DENSITY = 160; |
| 45 |
|
| 46 |
private final String mValue; |
| 47 |
private final String mDisplayValue; |
| 48 |
private final int mDensity; |
| 49 |
private final int mSince; |
| 50 |
|
| 51 |
Density(String value, String displayValue, int density, int since) { |
| 52 |
mValue = value; |
| 53 |
mDisplayValue = displayValue; |
| 54 |
mDensity = density; |
| 55 |
mSince = since; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the enum matching the provided qualifier value. |
| 60 |
* @param value The qualifier value. |
| 61 |
* @return the enum for the qualifier value or null if no match was found. |
| 62 |
*/ |
| 63 |
public static Density getEnum(String value) { |
| 64 |
for (Density orient : values()) { |
| 65 |
if (orient.mValue.equals(value)) { |
| 66 |
return orient; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Returns the enum matching the given density value |
| 75 |
* @param value The density value. |
| 76 |
* @return the enum for the density value or null if no match was found. |
| 77 |
*/ |
| 78 |
public static Density getEnum(int value) { |
| 79 |
for (Density d : values()) { |
| 80 |
if (d.mDensity == value) { |
| 81 |
return d; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return null; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public String getResourceValue() { |
| 90 |
return mValue; |
| 91 |
} |
| 92 |
|
| 93 |
public int getDpiValue() { |
| 94 |
return mDensity; |
| 95 |
} |
| 96 |
|
| 97 |
public int since() { |
| 98 |
return mSince; |
| 99 |
} |
| 100 |
|
| 101 |
public String getLegacyValue() { |
| 102 |
if (this != NODPI && this != ANYDPI) { |
| 103 |
return String.format("%1$ddpi", getDpiValue()); |
| 104 |
} |
| 105 |
|
| 106 |
return ""; |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public String getShortDisplayValue() { |
| 111 |
return mDisplayValue; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String getLongDisplayValue() { |
| 116 |
return mDisplayValue; |
| 117 |
} |
| 118 |
|
| 119 |
public static int getIndex(Density value) { |
| 120 |
return value == null ? -1 : value.ordinal(); |
| 121 |
} |
| 122 |
|
| 123 |
public static Density getByIndex(int index) { |
| 124 |
Density[] values = values(); |
| 125 |
if (index >=0 && index < values.length) { |
| 126 |
return values[index]; |
| 127 |
} |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns all densities which are recommended and valid for a device. |
| 133 |
* |
| 134 |
* @see #isRecommended() |
| 135 |
* @see #isValidValueForDevice() |
| 136 |
*/ |
| 137 |
public static Set<Density> getRecommendedValuesForDevice() { |
| 138 |
EnumSet<Density> result = EnumSet.allOf(Density.class); |
| 139 |
for (Density value : values()) { |
| 140 |
if (!value.isRecommended() || !value.isValidValueForDevice()) { |
| 141 |
result.remove(value); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
return result; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns true if this density is relevant for app developers (e.g. |
| 150 |
* a density you should consider providing resources for) |
| 151 |
*/ |
| 152 |
public boolean isRecommended() { |
| 153 |
switch (this) { |
| 154 |
case TV: |
| 155 |
case DPI_280: |
| 156 |
case DPI_360: |
| 157 |
case DPI_400: |
| 158 |
case DPI_420: |
| 159 |
case DPI_560: |
| 160 |
return false; |
| 161 |
default: |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public boolean isFakeValue() { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public boolean isValidValueForDevice() { |
| 173 |
return this != NODPI && this != ANYDPI; // nodpi/anydpi is not a valid config for devices. |
| 174 |
} |
| 175 |
} |