| 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 com.android.annotations.NonNull; |
| 20 |
import com.android.annotations.Nullable; |
| 21 |
import com.android.resources.Density; |
| 22 |
import com.android.resources.ResourceEnum; |
| 23 |
|
| 24 |
import java.util.regex.Matcher; |
| 25 |
import java.util.regex.Pattern; |
| 26 |
|
| 27 |
/** |
| 28 |
* Resource Qualifier for Screen Pixel Density. |
| 29 |
*/ |
| 30 |
public final class DensityQualifier extends EnumBasedResourceQualifier { |
| 31 |
|
| 32 |
public static final String NAME = "Density"; |
| 33 |
|
| 34 |
/** |
| 35 |
* The qualifier to be used for configurables when there is no qualifier present. This should |
| 36 |
* not be used for the reference configuration. |
| 37 |
*/ |
| 38 |
private static final DensityQualifier NULL_QUALIFIER = new DensityQualifier(true); |
| 39 |
|
| 40 |
/** |
| 41 |
* null iff <code>this == {@link #NULL_QUALIFIER}</code> |
| 42 |
*/ |
| 43 |
@Nullable |
| 44 |
private final Density mValue; |
| 45 |
|
| 46 |
private static final Pattern sDensityLegacyPattern = Pattern.compile("^(\\d+)dpi$"); |
| 47 |
|
| 48 |
public DensityQualifier() { |
| 49 |
this(Density.MEDIUM); |
| 50 |
} |
| 51 |
|
| 52 |
public DensityQualifier(@NonNull Density value) { |
| 53 |
// value is marked as NonNull so that no usages from outside this method use a null value. |
| 54 |
mValue = value; |
| 55 |
} |
| 56 |
|
| 57 |
private DensityQualifier(@SuppressWarnings("UnusedParameters") boolean ignored) { |
| 58 |
mValue = null; |
| 59 |
} |
| 60 |
|
| 61 |
// Not marking as NonNull or Nullable because it can technically return null (for |
| 62 |
// NULL_QUALIFIER) but usually won't. So, no need to keep checking for null. |
| 63 |
@SuppressWarnings("NullableProblems") |
| 64 |
public Density getValue() { |
| 65 |
return mValue; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
ResourceEnum getEnumValue() { |
| 70 |
return mValue; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public String getName() { |
| 75 |
return NAME; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public String getShortName() { |
| 80 |
return NAME; |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public int since() { |
| 85 |
return 4; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public boolean checkAndSet(String value, FolderConfiguration config) { |
| 90 |
Density density = Density.getEnum(value); |
| 91 |
if (density == null) { |
| 92 |
|
| 93 |
// attempt to read a legacy value. |
| 94 |
Matcher m = sDensityLegacyPattern.matcher(value); |
| 95 |
if (m.matches()) { |
| 96 |
String v = m.group(1); |
| 97 |
|
| 98 |
try { |
| 99 |
density = Density.getEnum(Integer.parseInt(v)); |
| 100 |
} catch (NumberFormatException e) { |
| 101 |
// looks like the string we extracted wasn't a valid number |
| 102 |
// which really shouldn't happen since the regexp would have failed. |
| 103 |
throw new AssertionError(e); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (density != null) { |
| 109 |
config.setDensityQualifier(new DensityQualifier(density)); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public boolean isValid() { |
| 118 |
return this != NULL_QUALIFIER; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
public DensityQualifier getNullQualifier() { |
| 123 |
return NULL_QUALIFIER; |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public boolean isMatchFor(ResourceQualifier qualifier) { |
| 128 |
// as long as there's a density qualifier, it's always a match. |
| 129 |
// The best match will be found later. |
| 130 |
return qualifier instanceof DensityQualifier; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public boolean isBetterMatchThan(@Nullable ResourceQualifier compareTo, |
| 135 |
@NonNull ResourceQualifier reference) { |
| 136 |
if (compareTo == null) { |
| 137 |
return true; |
| 138 |
} |
| 139 |
|
| 140 |
Density other = ((DensityQualifier) compareTo).mValue; |
| 141 |
Density required = ((DensityQualifier) reference).mValue; |
| 142 |
assert required != null |
| 143 |
: "NULL_QUALIFIER Density Qualifier shouldn't be part of the reference"; |
| 144 |
Density value = mValue; |
| 145 |
if (value == other) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
value = value == null ? Density.MEDIUM : value; |
| 150 |
other = other == null ? Density.MEDIUM : other; |
| 151 |
|
| 152 |
// Always prefer ANYDPI |
| 153 |
if (value == Density.ANYDPI) { |
| 154 |
return true; |
| 155 |
} |
| 156 |
if (other == Density.ANYDPI) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
if (required == Density.ANYDPI || required == Density.NODPI) { |
| 160 |
// Not sure when this would happen, but that's what is there in |
| 161 |
// ResourceTypes.cpp in the method ResTable_config::isBetterThan |
| 162 |
required = Density.MEDIUM; |
| 163 |
} |
| 164 |
int requiredDensity = required.getDpiValue(); |
| 165 |
|
| 166 |
// DENSITY_ANY is now dealt with. We should look to pick a density bucket and potentially |
| 167 |
// scale it. Any density is potentially useful because the system will scale it. Scaling |
| 168 |
// down is generally better than scaling up. |
| 169 |
int high = value.getDpiValue(); |
| 170 |
int low = other.getDpiValue(); |
| 171 |
boolean bImBigger = true; |
| 172 |
if (low > high) { |
| 173 |
int temp = high; |
| 174 |
high = low; |
| 175 |
low = temp; |
| 176 |
bImBigger = false; |
| 177 |
} else if (low == high && low == Density.MEDIUM.getDpiValue()) { |
| 178 |
// This if branch is not present in the platform's code. However, it's necessary to |
| 179 |
// remove uncertainty in which configuration is chosen in case of no qualifier vs mdpi |
| 180 |
|
| 181 |
// mdpi is preferred to no qualifier on devices with resolution >= Medium. For ldpi, |
| 182 |
// no qualifier is preferred to mdpi. |
| 183 |
return requiredDensity >= Density.MEDIUM.getDpiValue() ^ this == NULL_QUALIFIER; |
| 184 |
} |
| 185 |
if (requiredDensity > high) { |
| 186 |
// reference higher than both, return the higher. |
| 187 |
return bImBigger; |
| 188 |
} |
| 189 |
if (low >= requiredDensity) { |
| 190 |
// reference lower than both, return lower; |
| 191 |
return !bImBigger; |
| 192 |
} |
| 193 |
// saying that scaling down is 2x better than up |
| 194 |
if (((2 * low) - requiredDensity) * high > requiredDensity * requiredDensity) { |
| 195 |
return !bImBigger; |
| 196 |
} else { |
| 197 |
return bImBigger; |
| 198 |
} |
| 199 |
} |
| 200 |
} |