| 1 |
/* |
| 2 |
* Copyright (C) 2011 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; |
| 18 |
|
| 19 |
import static com.android.SdkConstants.ANDROID_STYLE_RESOURCE_PREFIX; |
| 20 |
import static com.android.SdkConstants.PREFIX_ANDROID; |
| 21 |
import static com.android.SdkConstants.PREFIX_RESOURCE_REF; |
| 22 |
import static com.android.SdkConstants.REFERENCE_STYLE; |
| 23 |
|
| 24 |
import com.android.annotations.NonNull; |
| 25 |
import com.android.annotations.Nullable; |
| 26 |
import com.android.ide.common.rendering.api.*; |
| 27 |
import com.android.resources.ResourceType; |
| 28 |
import com.google.common.base.Joiner; |
| 29 |
import com.google.common.base.Strings; |
| 30 |
import com.google.common.collect.Maps; |
| 31 |
|
| 32 |
import java.util.Collection; |
| 33 |
import java.util.HashMap; |
| 34 |
import java.util.HashSet; |
| 35 |
import java.util.LinkedList; |
| 36 |
import java.util.List; |
| 37 |
import java.util.Map; |
| 38 |
import java.util.Set; |
| 39 |
import java.util.regex.Matcher; |
| 40 |
import java.util.regex.Pattern; |
| 41 |
|
| 42 |
public class ResourceResolver extends RenderResources { |
| 43 |
public static final String THEME_NAME = "Theme"; |
| 44 |
public static final String THEME_NAME_DOT = "Theme."; |
| 45 |
public static final String XLIFF_NAMESPACE_PREFIX = "urn:oasis:names:tc:xliff:document:"; |
| 46 |
public static final String XLIFF_G_TAG = "g"; |
| 47 |
public static final String ATTR_EXAMPLE = "example"; |
| 48 |
|
| 49 |
/** |
| 50 |
* Constant passed to {@link #setDeviceDefaults(String)} to indicate the DeviceDefault styles |
| 51 |
* should point to the default styles |
| 52 |
*/ |
| 53 |
public static final String LEGACY_THEME = ""; |
| 54 |
|
| 55 |
public static final Pattern DEVICE_DEFAULT_PATTERN = Pattern |
| 56 |
.compile("(\\p{Alpha}+)?\\.?DeviceDefault\\.?(.+)?"); |
| 57 |
|
| 58 |
/** |
| 59 |
* Number of indirections we'll follow for resource resolution before assuming there |
| 60 |
* is a cyclic dependency error in the input |
| 61 |
*/ |
| 62 |
public static final int MAX_RESOURCE_INDIRECTION = 50; |
| 63 |
|
| 64 |
private final Map<ResourceType, Map<String, ResourceValue>> mProjectResources; |
| 65 |
private final Map<ResourceType, Map<String, ResourceValue>> mFrameworkResources; |
| 66 |
private final Map<StyleResourceValue, StyleResourceValue> mStyleInheritanceMap = |
| 67 |
new HashMap<StyleResourceValue, StyleResourceValue>(); |
| 68 |
private StyleResourceValue mDefaultTheme; |
| 69 |
// The resources should be searched in all the themes in the list in order. |
| 70 |
private final List<StyleResourceValue> mThemes; |
| 71 |
private FrameworkResourceIdProvider mFrameworkProvider; |
| 72 |
private LayoutLog mLogger; |
| 73 |
private String mThemeName; |
| 74 |
private boolean mIsProjectTheme; |
| 75 |
// AAPT flattens the names by converting '.', '-' and ':' to '_'. These maps undo the |
| 76 |
// flattening. We prefer lazy initialization of these maps since they are not used in many |
| 77 |
// applications. |
| 78 |
@Nullable |
| 79 |
private Map<String, String> mReverseFrameworkStyles; |
| 80 |
@Nullable |
| 81 |
private Map<String, String> mReverseProjectStyles; |
| 82 |
/** Contains the default parent for DeviceDefault styles (e.g. for API 18, "Holo") */ |
| 83 |
private String mDeviceDefaultParent = null; |
| 84 |
|
| 85 |
private ResourceResolver( |
| 86 |
Map<ResourceType, Map<String, ResourceValue>> projectResources, |
| 87 |
Map<ResourceType, Map<String, ResourceValue>> frameworkResources, |
| 88 |
String themeName, boolean isProjectTheme) { |
| 89 |
mProjectResources = projectResources; |
| 90 |
mFrameworkResources = frameworkResources; |
| 91 |
mThemeName = themeName; |
| 92 |
mIsProjectTheme = isProjectTheme; |
| 93 |
mThemes = new LinkedList<StyleResourceValue>(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Creates a new {@link ResourceResolver} object. |
| 98 |
* |
| 99 |
* @param projectResources the project resources. |
| 100 |
* @param frameworkResources the framework resources. |
| 101 |
* @param themeName the name of the current theme. |
| 102 |
* @param isProjectTheme Is this a project theme? |
| 103 |
* @return a new {@link ResourceResolver} |
| 104 |
*/ |
| 105 |
public static ResourceResolver create( |
| 106 |
Map<ResourceType, Map<String, ResourceValue>> projectResources, |
| 107 |
Map<ResourceType, Map<String, ResourceValue>> frameworkResources, |
| 108 |
String themeName, boolean isProjectTheme) { |
| 109 |
|
| 110 |
ResourceResolver resolver = new ResourceResolver(projectResources, frameworkResources, |
| 111 |
themeName, isProjectTheme); |
| 112 |
resolver.computeStyleMaps(); |
| 113 |
|
| 114 |
return resolver; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* This will override the DeviceDefault styles so they point to the given parent styles (e.g. If |
| 119 |
* "Material" is passed, Theme.DeviceDefault parent will become Theme.Material). This patches |
| 120 |
* all the styles (not only themes) and takes care of the light and dark variants. If {@link |
| 121 |
* #LEGACY_THEME} is passed, parents will be directed to the default themes (i.e. Theme). |
| 122 |
*/ |
| 123 |
public void setDeviceDefaults(@NonNull String deviceDefaultParent) { |
| 124 |
if (deviceDefaultParent.equals(mDeviceDefaultParent)) { |
| 125 |
// No need to patch again with the same parent |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
mDeviceDefaultParent = deviceDefaultParent; |
| 130 |
// The joiner will ignore nulls so if the caller specified an empty name, we replace it with |
| 131 |
// a null so it gets ignored |
| 132 |
String parentName = Strings.emptyToNull(deviceDefaultParent); |
| 133 |
|
| 134 |
// The regexp gets the prefix and suffix if they exist (without the dots) |
| 135 |
for (ResourceValue value : mFrameworkResources.get(ResourceType.STYLE).values()) { |
| 136 |
Matcher matcher = DEVICE_DEFAULT_PATTERN.matcher(value.getName()); |
| 137 |
if (!matcher.matches()) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
String newParentStyle = |
| 142 |
Joiner.on('.').skipNulls().join(matcher.group(1), parentName, |
| 143 |
((matcher.groupCount() > 1) ? matcher.group(2) : null)); |
| 144 |
patchFrameworkStyleParent(value.getName(), newParentStyle); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Updates the parent of a given framework style. This method is used to patch DeviceDefault |
| 150 |
* styles when using a CompatibilityTarget |
| 151 |
*/ |
| 152 |
private void patchFrameworkStyleParent(String childStyleName, String parentName) { |
| 153 |
Map<String, ResourceValue> map = mFrameworkResources.get(ResourceType.STYLE); |
| 154 |
if (map != null) { |
| 155 |
StyleResourceValue from = (StyleResourceValue)map.get(childStyleName); |
| 156 |
StyleResourceValue to = (StyleResourceValue)map.get(parentName); |
| 157 |
|
| 158 |
if (from != null && to != null) { |
| 159 |
StyleResourceValue newStyle = new StyleResourceValue(ResourceType.STYLE, |
| 160 |
from.getName(), parentName, from.isFramework(), from.getLibraryName()); |
| 161 |
newStyle.replaceWith(from); |
| 162 |
mStyleInheritanceMap.put(newStyle, to); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
// ---- Methods to help dealing with older LayoutLibs. |
| 168 |
|
| 169 |
public String getThemeName() { |
| 170 |
return mThemeName; |
| 171 |
} |
| 172 |
|
| 173 |
public boolean isProjectTheme() { |
| 174 |
return mIsProjectTheme; |
| 175 |
} |
| 176 |
|
| 177 |
public Map<ResourceType, Map<String, ResourceValue>> getProjectResources() { |
| 178 |
return mProjectResources; |
| 179 |
} |
| 180 |
|
| 181 |
public Map<ResourceType, Map<String, ResourceValue>> getFrameworkResources() { |
| 182 |
return mFrameworkResources; |
| 183 |
} |
| 184 |
|
| 185 |
// ---- RenderResources Methods |
| 186 |
|
| 187 |
@Override |
| 188 |
public void setFrameworkResourceIdProvider(FrameworkResourceIdProvider provider) { |
| 189 |
mFrameworkProvider = provider; |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void setLogger(LayoutLog logger) { |
| 194 |
mLogger = logger; |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public StyleResourceValue getDefaultTheme() { |
| 199 |
return mDefaultTheme; |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public void applyStyle(StyleResourceValue theme, boolean useAsPrimary) { |
| 204 |
if (theme == null) { |
| 205 |
return; |
| 206 |
} |
| 207 |
if (useAsPrimary) { |
| 208 |
mThemes.add(0, theme); |
| 209 |
} else { |
| 210 |
mThemes.add(theme); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void clearStyles() { |
| 216 |
mThemes.clear(); |
| 217 |
mThemes.add(mDefaultTheme); |
| 218 |
} |
| 219 |
|
| 220 |
@Override |
| 221 |
public List<StyleResourceValue> getAllThemes() { |
| 222 |
return mThemes; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public StyleResourceValue getTheme(String name, boolean frameworkTheme) { |
| 227 |
ResourceValue theme; |
| 228 |
|
| 229 |
if (frameworkTheme) { |
| 230 |
Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get( |
| 231 |
ResourceType.STYLE); |
| 232 |
theme = frameworkStyleMap.get(name); |
| 233 |
} else { |
| 234 |
Map<String, ResourceValue> projectStyleMap = mProjectResources.get(ResourceType.STYLE); |
| 235 |
theme = projectStyleMap.get(name); |
| 236 |
} |
| 237 |
|
| 238 |
if (theme instanceof StyleResourceValue) { |
| 239 |
return (StyleResourceValue) theme; |
| 240 |
} |
| 241 |
|
| 242 |
return null; |
| 243 |
} |
| 244 |
|
| 245 |
@Override |
| 246 |
public boolean themeIsParentOf(StyleResourceValue parentTheme, StyleResourceValue childTheme) { |
| 247 |
do { |
| 248 |
childTheme = mStyleInheritanceMap.get(childTheme); |
| 249 |
if (childTheme == null) { |
| 250 |
return false; |
| 251 |
} else if (childTheme == parentTheme) { |
| 252 |
return true; |
| 253 |
} |
| 254 |
} while (true); |
| 255 |
} |
| 256 |
|
| 257 |
@Override |
| 258 |
public ResourceValue getFrameworkResource(ResourceType resourceType, String resourceName) { |
| 259 |
return getResource(resourceType, resourceName, mFrameworkResources); |
| 260 |
} |
| 261 |
|
| 262 |
@Override |
| 263 |
public ResourceValue getProjectResource(ResourceType resourceType, String resourceName) { |
| 264 |
return getResource(resourceType, resourceName, mProjectResources); |
| 265 |
} |
| 266 |
|
| 267 |
@SuppressWarnings("deprecation") // Required to support older layoutlib clients |
| 268 |
@Override |
| 269 |
@Deprecated |
| 270 |
public ItemResourceValue findItemInStyle(StyleResourceValue style, String attrName) { |
| 271 |
// this method is deprecated because it doesn't know about the namespace of the |
| 272 |
// attribute so we search for the project namespace first and then in the |
| 273 |
// android namespace if needed. |
| 274 |
ItemResourceValue item = findItemInStyle(style, attrName, false); |
| 275 |
if (item == null) { |
| 276 |
item = findItemInStyle(style, attrName, true); |
| 277 |
} |
| 278 |
|
| 279 |
return item; |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
public ItemResourceValue findItemInStyle(StyleResourceValue style, String itemName, |
| 284 |
boolean isFrameworkAttr) { |
| 285 |
return findItemInStyle(style, itemName, isFrameworkAttr, 0); |
| 286 |
} |
| 287 |
|
| 288 |
private ItemResourceValue findItemInStyle(StyleResourceValue style, String itemName, |
| 289 |
boolean isFrameworkAttr, int depth) { |
| 290 |
ItemResourceValue item = style.getItem(itemName, isFrameworkAttr); |
| 291 |
|
| 292 |
// if we didn't find it, we look in the parent style (if applicable) |
| 293 |
//noinspection VariableNotUsedInsideIf |
| 294 |
if (item == null) { |
| 295 |
StyleResourceValue parentStyle = mStyleInheritanceMap.get(style); |
| 296 |
if (parentStyle != null) { |
| 297 |
if (depth >= MAX_RESOURCE_INDIRECTION) { |
| 298 |
if (mLogger != null) { |
| 299 |
mLogger.error(LayoutLog.TAG_BROKEN, |
| 300 |
String.format("Cyclic style parent definitions: %1$s", |
| 301 |
computeCyclicStyleChain(style)), |
| 302 |
null); |
| 303 |
} |
| 304 |
|
| 305 |
return null; |
| 306 |
} |
| 307 |
|
| 308 |
return findItemInStyle(parentStyle, itemName, isFrameworkAttr, depth + 1); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return item; |
| 313 |
} |
| 314 |
|
| 315 |
private String computeCyclicStyleChain(StyleResourceValue style) { |
| 316 |
StringBuilder sb = new StringBuilder(100); |
| 317 |
appendStyleParents(style, new HashSet<StyleResourceValue>(), 0, sb); |
| 318 |
return sb.toString(); |
| 319 |
} |
| 320 |
|
| 321 |
private void appendStyleParents(StyleResourceValue style, Set<StyleResourceValue> seen, |
| 322 |
int depth, StringBuilder sb) { |
| 323 |
if (depth >= MAX_RESOURCE_INDIRECTION) { |
| 324 |
sb.append("..."); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
boolean haveSeen = seen.contains(style); |
| 329 |
seen.add(style); |
| 330 |
|
| 331 |
sb.append('"'); |
| 332 |
if (style.isFramework()) { |
| 333 |
sb.append(PREFIX_ANDROID); |
| 334 |
} |
| 335 |
sb.append(style.getName()); |
| 336 |
sb.append('"'); |
| 337 |
|
| 338 |
if (haveSeen) { |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
StyleResourceValue parentStyle = mStyleInheritanceMap.get(style); |
| 343 |
if (parentStyle != null) { |
| 344 |
if (style.getParentStyle() != null) { |
| 345 |
sb.append(" specifies parent "); |
| 346 |
} else { |
| 347 |
sb.append(" implies parent "); |
| 348 |
} |
| 349 |
|
| 350 |
appendStyleParents(parentStyle, seen, depth + 1, sb); |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
@Override |
| 355 |
public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) { |
| 356 |
if (reference == null) { |
| 357 |
return null; |
| 358 |
} |
| 359 |
|
| 360 |
ResourceUrl resource = ResourceUrl.parse(reference); |
| 361 |
if (resource != null && resource.hasValidName()) { |
| 362 |
if (resource.theme) { |
| 363 |
// no theme? no need to go further! |
| 364 |
if (mDefaultTheme == null) { |
| 365 |
return null; |
| 366 |
} |
| 367 |
|
| 368 |
if (resource.type != ResourceType.ATTR) { |
| 369 |
// At this time, no support for ?type/name where type is not "attr" |
| 370 |
return null; |
| 371 |
} |
| 372 |
|
| 373 |
// Now look for the item in the theme, starting with the current one. |
| 374 |
return findItemInTheme(resource.name, forceFrameworkOnly || resource.framework); |
| 375 |
} else { |
| 376 |
return findResValue(resource, forceFrameworkOnly); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
// Looks like the value didn't reference anything. Return null. |
| 381 |
return null; |
| 382 |
} |
| 383 |
|
| 384 |
@Override |
| 385 |
public ResourceValue resolveValue(ResourceType type, String name, String value, |
| 386 |
boolean isFrameworkValue) { |
| 387 |
if (value == null) { |
| 388 |
return null; |
| 389 |
} |
| 390 |
|
| 391 |
// get the ResourceValue referenced by this value |
| 392 |
ResourceValue resValue = findResValue(value, isFrameworkValue); |
| 393 |
|
| 394 |
// if resValue is null, but value is not null, this means it was not a reference. |
| 395 |
// we return the name/value wrapper in a ResourceValue. the isFramework flag doesn't |
| 396 |
// matter. |
| 397 |
if (resValue == null) { |
| 398 |
return new ResourceValue(type, name, value, isFrameworkValue); |
| 399 |
} |
| 400 |
|
| 401 |
// we resolved a first reference, but we need to make sure this isn't a reference also. |
| 402 |
return resolveResValue(resValue); |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public ResourceValue resolveResValue(ResourceValue resValue) { |
| 407 |
return resolveResValue(resValue, 0); |
| 408 |
} |
| 409 |
|
| 410 |
private ResourceValue resolveResValue(ResourceValue resValue, int depth) { |
| 411 |
if (resValue == null) { |
| 412 |
return null; |
| 413 |
} |
| 414 |
|
| 415 |
String value = resValue.getValue(); |
| 416 |
if (value == null || resValue instanceof ArrayResourceValue) { |
| 417 |
// If there's no value or this an array resource (eg. <string-array>), return. |
| 418 |
return resValue; |
| 419 |
} |
| 420 |
|
| 421 |
// else attempt to find another ResourceValue referenced by this one. |
| 422 |
ResourceValue resolvedResValue = findResValue(value, resValue.isFramework()); |
| 423 |
|
| 424 |
// if the value did not reference anything, then we simply return the input value |
| 425 |
if (resolvedResValue == null) { |
| 426 |
return resValue; |
| 427 |
} |
| 428 |
|
| 429 |
// detect potential loop due to mishandled namespace in attributes |
| 430 |
if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) { |
| 431 |
if (mLogger != null) { |
| 432 |
mLogger.error(LayoutLog.TAG_BROKEN, |
| 433 |
String.format("Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.", value), |
| 434 |
null); |
| 435 |
} |
| 436 |
return resValue; |
| 437 |
} |
| 438 |
|
| 439 |
// otherwise, we attempt to resolve this new value as well |
| 440 |
return resolveResValue(resolvedResValue, depth + 1); |
| 441 |
} |
| 442 |
|
| 443 |
// ---- Private helper methods. |
| 444 |
|
| 445 |
/** |
| 446 |
* Searches for, and returns a {@link ResourceValue} by its parsed reference. |
| 447 |
* @param resource the parsed resource |
| 448 |
* @param forceFramework if <code>true</code>, the method does not search in the |
| 449 |
* project resources |
| 450 |
*/ |
| 451 |
private ResourceValue findResValue(ResourceUrl resource, boolean forceFramework) { |
| 452 |
// map of ResourceValue for the given type |
| 453 |
Map<String, ResourceValue> typeMap; |
| 454 |
ResourceType resType = resource.type; |
| 455 |
String resName = resource.name; |
| 456 |
boolean isFramework = forceFramework || resource.framework; |
| 457 |
|
| 458 |
if (!isFramework) { |
| 459 |
typeMap = mProjectResources.get(resType); |
| 460 |
ResourceValue item = typeMap.get(resName); |
| 461 |
if (item != null) { |
| 462 |
return item; |
| 463 |
} |
| 464 |
} else { |
| 465 |
typeMap = mFrameworkResources.get(resType); |
| 466 |
if (typeMap != null) { |
| 467 |
ResourceValue item = typeMap.get(resName); |
| 468 |
if (item != null) { |
| 469 |
return item; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
// if it was not found and the type is an id, it is possible that the ID was |
| 474 |
// generated dynamically when compiling the framework resources. |
| 475 |
// Look for it in the R map. |
| 476 |
if (mFrameworkProvider != null && resType == ResourceType.ID) { |
| 477 |
if (mFrameworkProvider.getId(resType, resName) != null) { |
| 478 |
return new ResourceValue(resType, resName, true); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
// didn't find the resource anywhere. |
| 484 |
if (!resource.create && mLogger != null) { |
| 485 |
mLogger.warning(LayoutLog.TAG_RESOURCES_RESOLVE, |
| 486 |
"Couldn't resolve resource @" + |
| 487 |
(isFramework ? "android:" : "") + resType + "/" + resName, |
| 488 |
new ResourceValue(resType, resName, isFramework)); |
| 489 |
} |
| 490 |
return null; |
| 491 |
} |
| 492 |
|
| 493 |
private ResourceValue getResource(ResourceType resourceType, String resourceName, |
| 494 |
Map<ResourceType, Map<String, ResourceValue>> resourceRepository) { |
| 495 |
Map<String, ResourceValue> typeMap = resourceRepository.get(resourceType); |
| 496 |
if (typeMap != null) { |
| 497 |
ResourceValue item = typeMap.get(resourceName); |
| 498 |
if (item != null) { |
| 499 |
item = resolveResValue(item); |
| 500 |
return item; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
// didn't find the resource anywhere. |
| 505 |
return null; |
| 506 |
} |
| 507 |
|
| 508 |
/** |
| 509 |
* Compute style information from the given list of style for the project and framework. |
| 510 |
*/ |
| 511 |
private void computeStyleMaps() { |
| 512 |
Map<String, ResourceValue> projectStyleMap = mProjectResources.get(ResourceType.STYLE); |
| 513 |
Map<String, ResourceValue> frameworkStyleMap = mFrameworkResources.get(ResourceType.STYLE); |
| 514 |
|
| 515 |
// first, get the theme |
| 516 |
ResourceValue theme = null; |
| 517 |
|
| 518 |
// project theme names have been prepended with a * |
| 519 |
if (mIsProjectTheme) { |
| 520 |
if (projectStyleMap != null) { |
| 521 |
theme = projectStyleMap.get(mThemeName); |
| 522 |
} |
| 523 |
} else { |
| 524 |
if (frameworkStyleMap != null) { |
| 525 |
theme = frameworkStyleMap.get(mThemeName); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
if (theme instanceof StyleResourceValue) { |
| 530 |
// compute the inheritance map for both the project and framework styles |
| 531 |
computeStyleInheritance(projectStyleMap.values(), projectStyleMap, |
| 532 |
frameworkStyleMap); |
| 533 |
|
| 534 |
// Compute the style inheritance for the framework styles/themes. |
| 535 |
// Since, for those, the style parent values do not contain 'android:' |
| 536 |
// we want to force looking in the framework style only to avoid using |
| 537 |
// similarly named styles from the project. |
| 538 |
// To do this, we pass null in lieu of the project style map. |
| 539 |
if (frameworkStyleMap != null) { |
| 540 |
computeStyleInheritance(frameworkStyleMap.values(), null /*inProjectStyleMap */, |
| 541 |
frameworkStyleMap); |
| 542 |
} |
| 543 |
|
| 544 |
mDefaultTheme = (StyleResourceValue) theme; |
| 545 |
mThemes.clear(); |
| 546 |
mThemes.add(mDefaultTheme); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Compute the parent style for all the styles in a given list. |
| 552 |
* @param styles the styles for which we compute the parent. |
| 553 |
* @param inProjectStyleMap the map of project styles. |
| 554 |
* @param inFrameworkStyleMap the map of framework styles. |
| 555 |
*/ |
| 556 |
private void computeStyleInheritance(Collection<ResourceValue> styles, |
| 557 |
Map<String, ResourceValue> inProjectStyleMap, |
| 558 |
Map<String, ResourceValue> inFrameworkStyleMap) { |
| 559 |
// This method will recalculate the inheritance map so any modifications done by |
| 560 |
// setDeviceDefault will be lost. Set mDeviceDefaultParent to null so when setDeviceDefault |
| 561 |
// is called again, it knows that it needs to modify the inheritance map again. |
| 562 |
mDeviceDefaultParent = null; |
| 563 |
for (ResourceValue value : styles) { |
| 564 |
if (value instanceof StyleResourceValue) { |
| 565 |
StyleResourceValue style = (StyleResourceValue)value; |
| 566 |
|
| 567 |
String parentName = getParentName(style); |
| 568 |
|
| 569 |
if (parentName != null) { |
| 570 |
StyleResourceValue parentStyle = getStyle(parentName, inProjectStyleMap, |
| 571 |
inFrameworkStyleMap); |
| 572 |
|
| 573 |
if (parentStyle != null) { |
| 574 |
mStyleInheritanceMap.put(style, parentStyle); |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* Computes the name of the parent style, or <code>null</code> if the style is a root style. |
| 583 |
* You probably want to use {@code ResolutionUtils,getParentQualifiedName(StyleResourceValue)} |
| 584 |
* instead |
| 585 |
*/ |
| 586 |
@Nullable |
| 587 |
public static String getParentName(StyleResourceValue style) { |
| 588 |
String parentName = style.getParentStyle(); |
| 589 |
if (parentName != null) { |
| 590 |
return parentName; |
| 591 |
} |
| 592 |
|
| 593 |
String styleName = style.getName(); |
| 594 |
int index = styleName.lastIndexOf('.'); |
| 595 |
if (index != -1) { |
| 596 |
return styleName.substring(0, index); |
| 597 |
} |
| 598 |
return null; |
| 599 |
} |
| 600 |
|
| 601 |
@Override |
| 602 |
@Nullable |
| 603 |
public StyleResourceValue getParent(@NonNull StyleResourceValue style) { |
| 604 |
return mStyleInheritanceMap.get(style); |
| 605 |
} |
| 606 |
|
| 607 |
@Override |
| 608 |
@Nullable |
| 609 |
public StyleResourceValue getStyle(@NonNull String styleName, boolean isFramework) { |
| 610 |
ResourceValue res; |
| 611 |
Map<String, ResourceValue> styleMap; |
| 612 |
|
| 613 |
// First check if we can find the style directly. |
| 614 |
if (isFramework) { |
| 615 |
styleMap = mFrameworkResources.get(ResourceType.STYLE); |
| 616 |
} else { |
| 617 |
styleMap = mProjectResources.get(ResourceType.STYLE); |
| 618 |
} |
| 619 |
res = getStyleFromMap(styleMap, styleName); |
| 620 |
if (res != null) { |
| 621 |
// If the obtained resource is not StyleResourceValue, return null. |
| 622 |
return res instanceof StyleResourceValue ? (StyleResourceValue) res : null; |
| 623 |
} |
| 624 |
|
| 625 |
// We cannot find the style directly. The style name may have been flattened by AAPT for use |
| 626 |
// in the R class. Try and obtain the original name. |
| 627 |
String xmlStyleName = getReverseStyleMap(isFramework) |
| 628 |
.get(getNormalizedStyleName(styleName)); |
| 629 |
if (!styleName.equals(xmlStyleName)) { |
| 630 |
res = getStyleFromMap(styleMap, xmlStyleName); |
| 631 |
} |
| 632 |
return res instanceof StyleResourceValue ? (StyleResourceValue) res : null; |
| 633 |
} |
| 634 |
|
| 635 |
@Override |
| 636 |
@Nullable |
| 637 |
public String getXmlName(@NonNull ResourceType type, @NonNull String name, |
| 638 |
boolean isFramework) { |
| 639 |
if (type != ResourceType.STYLE) { |
| 640 |
// The method is currently implemented for styles only. |
| 641 |
return null; |
| 642 |
} |
| 643 |
Map<String, String> reverseStyles; |
| 644 |
reverseStyles = getReverseStyleMap(isFramework); |
| 645 |
return reverseStyles.get(name); |
| 646 |
} |
| 647 |
|
| 648 |
/** |
| 649 |
* Returns the reverse style map using the appropriate resources. It also initializes the map if |
| 650 |
* it hasn't been initialized yet. |
| 651 |
*/ |
| 652 |
private Map<String, String> getReverseStyleMap(boolean isFramework) { |
| 653 |
if (isFramework) { |
| 654 |
// The reverse style map may need to be initialized. |
| 655 |
if (mReverseFrameworkStyles == null) { |
| 656 |
Map<String, ResourceValue> styleMap = mFrameworkResources.get(ResourceType.STYLE); |
| 657 |
mReverseFrameworkStyles = createReverseStyleMap(styleMap.keySet()); |
| 658 |
} |
| 659 |
return mReverseFrameworkStyles; |
| 660 |
} else { |
| 661 |
if (mReverseProjectStyles == null) { |
| 662 |
Map<String, ResourceValue> styleMap = mProjectResources.get(ResourceType.STYLE); |
| 663 |
mReverseProjectStyles = createReverseStyleMap(styleMap.keySet()); |
| 664 |
} |
| 665 |
return mReverseProjectStyles; |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
/** |
| 670 |
* Create a map from the normalized form of the style names in {@code styles} to the original |
| 671 |
* style name. |
| 672 |
* |
| 673 |
* @see #getNormalizedStyleName(String) |
| 674 |
*/ |
| 675 |
private static Map<String, String> createReverseStyleMap(@NonNull Set<String> styles) { |
| 676 |
Map<String, String> reverseStyles = Maps.newHashMapWithExpectedSize(styles.size()); |
| 677 |
for (String style : styles) { |
| 678 |
reverseStyles.put(getNormalizedStyleName(style), style); |
| 679 |
} |
| 680 |
return reverseStyles; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* Flatten the styleName like AAPT by replacing dots, dashes and colons with underscores. |
| 685 |
*/ |
| 686 |
@NonNull |
| 687 |
private static String getNormalizedStyleName(@NonNull String styleName) { |
| 688 |
return styleName.replace('.', '_').replace('-', '_').replace(':', '_'); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Search for the style in the given map and log an error if the obtained resource is not |
| 693 |
* {@link StyleResourceValue}. |
| 694 |
* |
| 695 |
* @return The {@link ResourceValue} found in the map. |
| 696 |
*/ |
| 697 |
@Nullable |
| 698 |
private ResourceValue getStyleFromMap(@NonNull Map<String, ResourceValue> styleMap, |
| 699 |
@NonNull String styleName) { |
| 700 |
ResourceValue res; |
| 701 |
res = styleMap.get(styleName); |
| 702 |
if (res != null) { |
| 703 |
if (!(res instanceof StyleResourceValue) && mLogger != null) { |
| 704 |
mLogger.error(null, String.format( |
| 705 |
"Style %1$s is not of type STYLE (instead %2$s)", |
| 706 |
styleName, res.getResourceType().toString()), |
| 707 |
null); |
| 708 |
} |
| 709 |
} |
| 710 |
return res; |
| 711 |
} |
| 712 |
|
| 713 |
/** |
| 714 |
* Searches for and returns the {@link StyleResourceValue} from a given name. |
| 715 |
* <p>The format of the name can be: |
| 716 |
* <ul> |
| 717 |
* <li>[android:]<name></li> |
| 718 |
* <li>[android:]style/<name></li> |
| 719 |
* <li>@[android:]style/<name></li> |
| 720 |
* </ul> |
| 721 |
* @param parentName the name of the style. |
| 722 |
* @param inProjectStyleMap the project style map. Can be <code>null</code> |
| 723 |
* @param inFrameworkStyleMap the framework style map. |
| 724 |
* @return The matching {@link StyleResourceValue} object or <code>null</code> if not found. |
| 725 |
*/ |
| 726 |
private StyleResourceValue getStyle(String parentName, |
| 727 |
Map<String, ResourceValue> inProjectStyleMap, |
| 728 |
Map<String, ResourceValue> inFrameworkStyleMap) { |
| 729 |
boolean frameworkOnly = false; |
| 730 |
|
| 731 |
String name = parentName; |
| 732 |
|
| 733 |
// remove the useless @ if it's there |
| 734 |
if (name.startsWith(PREFIX_RESOURCE_REF)) { |
| 735 |
name = name.substring(PREFIX_RESOURCE_REF.length()); |
| 736 |
} |
| 737 |
|
| 738 |
// check for framework identifier. |
| 739 |
if (name.startsWith(PREFIX_ANDROID)) { |
| 740 |
frameworkOnly = true; |
| 741 |
name = name.substring(PREFIX_ANDROID.length()); |
| 742 |
} |
| 743 |
|
| 744 |
// at this point we could have the format <type>/<name>. we want only the name as long as |
| 745 |
// the type is style. |
| 746 |
if (name.startsWith(REFERENCE_STYLE)) { |
| 747 |
name = name.substring(REFERENCE_STYLE.length()); |
| 748 |
} else if (name.indexOf('/') != -1) { |
| 749 |
return null; |
| 750 |
} |
| 751 |
|
| 752 |
ResourceValue parent = null; |
| 753 |
|
| 754 |
// if allowed, search in the project resources. |
| 755 |
if (!frameworkOnly && inProjectStyleMap != null) { |
| 756 |
parent = inProjectStyleMap.get(name); |
| 757 |
} |
| 758 |
|
| 759 |
// if not found, then look in the framework resources. |
| 760 |
if (parent == null) { |
| 761 |
if (inFrameworkStyleMap == null) { |
| 762 |
return null; |
| 763 |
} |
| 764 |
parent = inFrameworkStyleMap.get(name); |
| 765 |
} |
| 766 |
|
| 767 |
// make sure the result is the proper class type and return it. |
| 768 |
if (parent instanceof StyleResourceValue) { |
| 769 |
return (StyleResourceValue)parent; |
| 770 |
} |
| 771 |
|
| 772 |
if (mLogger != null) { |
| 773 |
mLogger.error(LayoutLog.TAG_RESOURCES_RESOLVE, |
| 774 |
String.format("Unable to resolve parent style name: %s", parentName), |
| 775 |
null /*data*/); |
| 776 |
} |
| 777 |
|
| 778 |
return null; |
| 779 |
} |
| 780 |
|
| 781 |
/** Returns true if the given {@link ResourceValue} represents a theme */ |
| 782 |
public boolean isTheme( |
| 783 |
@NonNull ResourceValue value, |
| 784 |
@Nullable Map<ResourceValue, Boolean> cache) { |
| 785 |
return isTheme(value, cache, 0); |
| 786 |
} |
| 787 |
|
| 788 |
private boolean isTheme( |
| 789 |
@NonNull ResourceValue value, |
| 790 |
@Nullable Map<ResourceValue, Boolean> cache, |
| 791 |
int depth) { |
| 792 |
if (cache != null) { |
| 793 |
Boolean known = cache.get(value); |
| 794 |
if (known != null) { |
| 795 |
return known; |
| 796 |
} |
| 797 |
} |
| 798 |
if (value instanceof StyleResourceValue) { |
| 799 |
StyleResourceValue srv = (StyleResourceValue) value; |
| 800 |
String name = srv.getName(); |
| 801 |
if (srv.isFramework() && (name.equals(THEME_NAME) || name.startsWith(THEME_NAME_DOT))) { |
| 802 |
if (cache != null) { |
| 803 |
cache.put(value, true); |
| 804 |
} |
| 805 |
return true; |
| 806 |
} |
| 807 |
|
| 808 |
StyleResourceValue parentStyle = mStyleInheritanceMap.get(srv); |
| 809 |
if (parentStyle != null) { |
| 810 |
if (depth >= MAX_RESOURCE_INDIRECTION) { |
| 811 |
if (mLogger != null) { |
| 812 |
mLogger.error(LayoutLog.TAG_BROKEN, |
| 813 |
String.format("Cyclic style parent definitions: %1$s", |
| 814 |
computeCyclicStyleChain(srv)), |
| 815 |
null); |
| 816 |
} |
| 817 |
|
| 818 |
return false; |
| 819 |
} |
| 820 |
|
| 821 |
boolean result = isTheme(parentStyle, cache, depth + 1); |
| 822 |
if (cache != null) { |
| 823 |
cache.put(value, result); |
| 824 |
} |
| 825 |
return result; |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
return false; |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* Returns true if the given {@code themeStyle} extends the theme given by |
| 834 |
* {@code parentStyle} |
| 835 |
*/ |
| 836 |
public boolean themeExtends(@NonNull String parentStyle, @NonNull String themeStyle) { |
| 837 |
ResourceValue parentValue = findResValue(parentStyle, |
| 838 |
parentStyle.startsWith(ANDROID_STYLE_RESOURCE_PREFIX)); |
| 839 |
if (parentValue instanceof StyleResourceValue) { |
| 840 |
ResourceValue themeValue = findResValue(themeStyle, |
| 841 |
themeStyle.startsWith(ANDROID_STYLE_RESOURCE_PREFIX)); |
| 842 |
if (themeValue == parentValue) { |
| 843 |
return true; |
| 844 |
} |
| 845 |
if (themeValue instanceof StyleResourceValue) { |
| 846 |
return themeIsParentOf((StyleResourceValue) parentValue, |
| 847 |
(StyleResourceValue) themeValue); |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
return false; |
| 852 |
} |
| 853 |
|
| 854 |
/** |
| 855 |
* Creates a new {@link ResourceResolver} which records all resource resolution |
| 856 |
* lookups into the given list. Note that it is the responsibility of the caller |
| 857 |
* to clear/reset the list between subsequent lookup operations. |
| 858 |
* |
| 859 |
* @param lookupChain the list to write resource lookups into |
| 860 |
* @return a new {@link ResourceResolver} |
| 861 |
*/ |
| 862 |
public ResourceResolver createRecorder(List<ResourceValue> lookupChain) { |
| 863 |
ResourceResolver resolver = new RecordingResourceResolver( |
| 864 |
lookupChain, mProjectResources, mFrameworkResources, mThemeName, mIsProjectTheme); |
| 865 |
resolver.mFrameworkProvider = mFrameworkProvider; |
| 866 |
resolver.mLogger = mLogger; |
| 867 |
resolver.mDefaultTheme = mDefaultTheme; |
| 868 |
resolver.mStyleInheritanceMap.putAll(mStyleInheritanceMap); |
| 869 |
resolver.mThemes.addAll(mThemes); |
| 870 |
return resolver; |
| 871 |
} |
| 872 |
|
| 873 |
private static class RecordingResourceResolver extends ResourceResolver { |
| 874 |
@NonNull private List<ResourceValue> mLookupChain; |
| 875 |
|
| 876 |
private RecordingResourceResolver( |
| 877 |
@NonNull List<ResourceValue> lookupChain, |
| 878 |
@NonNull Map<ResourceType, Map<String, ResourceValue>> projectResources, |
| 879 |
@NonNull Map<ResourceType, Map<String, ResourceValue>> frameworkResources, |
| 880 |
@NonNull String themeName, boolean isProjectTheme) { |
| 881 |
super(projectResources, frameworkResources, themeName, isProjectTheme); |
| 882 |
mLookupChain = lookupChain; |
| 883 |
} |
| 884 |
|
| 885 |
@Override |
| 886 |
public ResourceValue resolveResValue(ResourceValue resValue) { |
| 887 |
if (resValue != null) { |
| 888 |
mLookupChain.add(resValue); |
| 889 |
} |
| 890 |
|
| 891 |
return super.resolveResValue(resValue); |
| 892 |
} |
| 893 |
|
| 894 |
@Override |
| 895 |
public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) { |
| 896 |
if (!mLookupChain.isEmpty() && reference != null && reference.startsWith(PREFIX_RESOURCE_REF)) { |
| 897 |
ResourceValue prev = mLookupChain.get(mLookupChain.size() - 1); |
| 898 |
if (!reference.equals(prev.getValue())) { |
| 899 |
ResourceValue next = new ResourceValue(prev.getResourceType(), prev.getName(), |
| 900 |
prev.isFramework(), prev.getLibraryName()); |
| 901 |
next.setValue(reference); |
| 902 |
mLookupChain.add(next); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
ResourceValue resValue = super.findResValue(reference, forceFrameworkOnly); |
| 907 |
|
| 908 |
if (resValue != null) { |
| 909 |
mLookupChain.add(resValue); |
| 910 |
} |
| 911 |
|
| 912 |
return resValue; |
| 913 |
} |
| 914 |
|
| 915 |
@Override |
| 916 |
public ItemResourceValue findItemInStyle(StyleResourceValue style, String itemName, |
| 917 |
boolean isFrameworkAttr) { |
| 918 |
ItemResourceValue value = super.findItemInStyle(style, itemName, isFrameworkAttr); |
| 919 |
if (value != null) { |
| 920 |
mLookupChain.add(value); |
| 921 |
} |
| 922 |
return value; |
| 923 |
} |
| 924 |
|
| 925 |
@Override |
| 926 |
public ResourceValue findItemInTheme(String attrName, boolean isFrameworkAttr) { |
| 927 |
ResourceValue value = super.findItemInTheme(attrName, isFrameworkAttr); |
| 928 |
if (value != null) { |
| 929 |
mLookupChain.add(value); |
| 930 |
} |
| 931 |
return value; |
| 932 |
} |
| 933 |
|
| 934 |
@Override |
| 935 |
public ResourceValue resolveValue(ResourceType type, String name, String value, |
| 936 |
boolean isFrameworkValue) { |
| 937 |
ResourceValue resourceValue = super.resolveValue(type, name, value, isFrameworkValue); |
| 938 |
if (resourceValue != null) { |
| 939 |
mLookupChain.add(resourceValue); |
| 940 |
} |
| 941 |
return resourceValue; |
| 942 |
} |
| 943 |
} |
| 944 |
} |