| 1 |
/* |
| 2 |
* Copyright (C) 2013 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 |
package com.android.ide.common.resources; |
| 17 |
|
| 18 |
import static com.android.SdkConstants.PREFIX_RESOURCE_REF; |
| 19 |
import static com.android.SdkConstants.PREFIX_THEME_REF; |
| 20 |
import static com.android.ide.common.resources.ResourceResolver.MAX_RESOURCE_INDIRECTION; |
| 21 |
|
| 22 |
import com.android.annotations.NonNull; |
| 23 |
import com.android.annotations.Nullable; |
| 24 |
import com.android.ide.common.rendering.api.*; |
| 25 |
import com.android.ide.common.res2.AbstractResourceRepository; |
| 26 |
import com.android.ide.common.resources.configuration.FolderConfiguration; |
| 27 |
import com.android.resources.ResourceType; |
| 28 |
|
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
/** |
| 32 |
* Like {@link ResourceResolver} but for a single item, so it does not need the full resource maps |
| 33 |
* to be resolved up front. Typically used for cases where we may not have fully configured |
| 34 |
* resource maps and we need to look up a specific value, such as in Android Studio where |
| 35 |
* a color reference is found in an XML style file, and we want to resolve it in order to |
| 36 |
* display the final resolved color in the editor margin. |
| 37 |
*/ |
| 38 |
public class ResourceItemResolver extends RenderResources { |
| 39 |
private final FolderConfiguration mConfiguration; |
| 40 |
private final LayoutLog mLogger; |
| 41 |
private final ResourceProvider mResourceProvider; |
| 42 |
private ResourceRepository mFrameworkResources; |
| 43 |
private ResourceResolver mResolver; |
| 44 |
private AbstractResourceRepository myAppResources; |
| 45 |
@Nullable private List<ResourceValue> mLookupChain; |
| 46 |
|
| 47 |
public ResourceItemResolver( |
| 48 |
@NonNull FolderConfiguration configuration, |
| 49 |
@NonNull ResourceProvider resourceProvider, |
| 50 |
@Nullable LayoutLog logger) { |
| 51 |
mConfiguration = configuration; |
| 52 |
mResourceProvider = resourceProvider; |
| 53 |
mLogger = logger; |
| 54 |
mResolver = resourceProvider.getResolver(false); |
| 55 |
} |
| 56 |
|
| 57 |
public ResourceItemResolver( |
| 58 |
@NonNull FolderConfiguration configuration, |
| 59 |
@NonNull ResourceRepository frameworkResources, |
| 60 |
@NonNull AbstractResourceRepository appResources, |
| 61 |
@Nullable LayoutLog logger) { |
| 62 |
mConfiguration = configuration; |
| 63 |
mResourceProvider = null; |
| 64 |
mLogger = logger; |
| 65 |
mFrameworkResources = frameworkResources; |
| 66 |
myAppResources = appResources; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
@Nullable |
| 71 |
public ResourceValue resolveResValue(@Nullable ResourceValue resValue) { |
| 72 |
if (mResolver != null) { |
| 73 |
return mResolver.resolveResValue(resValue); |
| 74 |
} |
| 75 |
if (mLookupChain != null) { |
| 76 |
mLookupChain.add(resValue); |
| 77 |
} |
| 78 |
return resolveResValue(resValue, 0); |
| 79 |
} |
| 80 |
|
| 81 |
@Nullable |
| 82 |
private ResourceValue resolveResValue(@Nullable ResourceValue resValue, int depth) { |
| 83 |
if (resValue == null) { |
| 84 |
return null; |
| 85 |
} |
| 86 |
|
| 87 |
String value = resValue.getValue(); |
| 88 |
if (value == null || resValue instanceof ArrayResourceValue) { |
| 89 |
// If there's no value or this an array resource (eg. <string-array>), return. |
| 90 |
return resValue; |
| 91 |
} |
| 92 |
|
| 93 |
// else attempt to find another ResourceValue referenced by this one. |
| 94 |
ResourceValue resolvedResValue = findResValue(value, resValue.isFramework()); |
| 95 |
|
| 96 |
// if the value did not reference anything, then we simply return the input value |
| 97 |
if (resolvedResValue == null) { |
| 98 |
return resValue; |
| 99 |
} |
| 100 |
|
| 101 |
// detect potential loop due to mishandled namespace in attributes |
| 102 |
if (resValue == resolvedResValue || depth >= MAX_RESOURCE_INDIRECTION) { |
| 103 |
if (mLogger != null) { |
| 104 |
mLogger.error(LayoutLog.TAG_BROKEN, |
| 105 |
String.format( |
| 106 |
"Potential stack overflow trying to resolve '%s': cyclic resource definitions? Render may not be accurate.", |
| 107 |
value), |
| 108 |
null); |
| 109 |
} |
| 110 |
return resValue; |
| 111 |
} |
| 112 |
|
| 113 |
// otherwise, we attempt to resolve this new value as well |
| 114 |
return resolveResValue(resolvedResValue, depth + 1); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
@Nullable |
| 119 |
public ResourceValue findResValue(@Nullable String reference, boolean inFramework) { |
| 120 |
if (mResolver != null) { |
| 121 |
return mResolver.findResValue(reference, inFramework); |
| 122 |
} |
| 123 |
|
| 124 |
if (reference == null) { |
| 125 |
return null; |
| 126 |
} |
| 127 |
|
| 128 |
if (mLookupChain != null && !mLookupChain.isEmpty() && |
| 129 |
reference.startsWith(PREFIX_RESOURCE_REF)) { |
| 130 |
ResourceValue prev = mLookupChain.get(mLookupChain.size() - 1); |
| 131 |
if (!reference.equals(prev.getValue())) { |
| 132 |
ResourceValue next = new ResourceValue(prev.getResourceType(), prev.getName(), |
| 133 |
prev.isFramework(), prev.getLibraryName()); |
| 134 |
next.setValue(reference); |
| 135 |
mLookupChain.add(next); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
ResourceUrl resource = ResourceUrl.parse(reference); |
| 140 |
if (resource != null && resource.hasValidName()) { |
| 141 |
if (resource.theme) { |
| 142 |
// Do theme lookup? We can't do that here; requires full global analysis, so just use |
| 143 |
// a real resource resolver |
| 144 |
ResourceResolver resolver = getFullResolver(); |
| 145 |
if (resolver != null) { |
| 146 |
return resolver.findResValue(reference, inFramework); |
| 147 |
} else { |
| 148 |
return null; |
| 149 |
} |
| 150 |
} else if (reference.startsWith(PREFIX_RESOURCE_REF)) { |
| 151 |
return findResValue(resource.type, resource.name, inFramework || resource.framework); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Looks like the value didn't reference anything. Return null. |
| 156 |
return null; |
| 157 |
} |
| 158 |
|
| 159 |
private ResourceValue findResValue(ResourceType resType, String resName, boolean framework) { |
| 160 |
// map of ResourceValue for the given type |
| 161 |
// if allowed, search in the project resources first. |
| 162 |
if (!framework) { |
| 163 |
if (myAppResources == null) { |
| 164 |
assert mResourceProvider != null; |
| 165 |
myAppResources = mResourceProvider.getAppResources(); |
| 166 |
if (myAppResources == null) { |
| 167 |
return null; |
| 168 |
} |
| 169 |
} |
| 170 |
ResourceValue item = null; |
| 171 |
item = myAppResources.getConfiguredValue(resType, resName, mConfiguration); |
| 172 |
if (item != null) { |
| 173 |
if (mLookupChain != null) { |
| 174 |
mLookupChain.add(item); |
| 175 |
} |
| 176 |
return item; |
| 177 |
} |
| 178 |
} else { |
| 179 |
if (mFrameworkResources == null) { |
| 180 |
assert mResourceProvider != null; |
| 181 |
mFrameworkResources = mResourceProvider.getFrameworkResources(); |
| 182 |
if (mFrameworkResources == null) { |
| 183 |
return null; |
| 184 |
} |
| 185 |
} |
| 186 |
// now search in the framework resources. |
| 187 |
if (mFrameworkResources.hasResourceItem(resType, resName)) { |
| 188 |
ResourceItem item = mFrameworkResources.getResourceItem(resType, resName); |
| 189 |
ResourceValue value = item.getResourceValue(resType, mConfiguration, true); |
| 190 |
if (value != null && mLookupChain != null) { |
| 191 |
mLookupChain.add(value); |
| 192 |
} |
| 193 |
return value; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
// didn't find the resource anywhere. |
| 198 |
if (mLogger != null) { |
| 199 |
mLogger.warning(LayoutLog.TAG_RESOURCES_RESOLVE, |
| 200 |
"Couldn't resolve resource @" + |
| 201 |
(framework ? "android:" : "") + resType + "/" + resName, |
| 202 |
new ResourceValue(resType, resName, framework)); |
| 203 |
} |
| 204 |
return null; |
| 205 |
} |
| 206 |
|
| 207 |
@Override |
| 208 |
public StyleResourceValue getCurrentTheme() { |
| 209 |
ResourceResolver resolver = getFullResolver(); |
| 210 |
if (resolver != null) { |
| 211 |
return resolver.getCurrentTheme(); |
| 212 |
} |
| 213 |
|
| 214 |
return null; |
| 215 |
} |
| 216 |
|
| 217 |
@Override |
| 218 |
public ResourceValue resolveValue(ResourceType type, String name, String value, |
| 219 |
boolean isFrameworkValue) { |
| 220 |
if (value == null) { |
| 221 |
return null; |
| 222 |
} |
| 223 |
|
| 224 |
// get the ResourceValue referenced by this value |
| 225 |
ResourceValue resValue = findResValue(value, isFrameworkValue); |
| 226 |
|
| 227 |
// if resValue is null, but value is not null, this means it was not a reference. |
| 228 |
// we return the name/value wrapper in a ResourceValue. the isFramework flag doesn't |
| 229 |
// matter. |
| 230 |
if (resValue == null) { |
| 231 |
return new ResourceValue(type, name, value, isFrameworkValue); |
| 232 |
} |
| 233 |
|
| 234 |
// we resolved a first reference, but we need to make sure this isn't a reference also. |
| 235 |
return resolveResValue(resValue); |
| 236 |
} |
| 237 |
|
| 238 |
// For theme lookup, we need to delegate to a full resource resolver |
| 239 |
|
| 240 |
@Override |
| 241 |
public StyleResourceValue getTheme(String name, boolean frameworkTheme) { |
| 242 |
assert false; // This method shouldn't be called on this resolver |
| 243 |
return super.getTheme(name, frameworkTheme); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public boolean themeIsParentOf(StyleResourceValue parentTheme, StyleResourceValue childTheme) { |
| 248 |
assert false; // This method shouldn't be called on this resolver |
| 249 |
return super.themeIsParentOf(parentTheme, childTheme); |
| 250 |
} |
| 251 |
|
| 252 |
@SuppressWarnings("deprecation") |
| 253 |
@Override |
| 254 |
public ResourceValue findItemInTheme(String itemName) { |
| 255 |
ResourceResolver resolver = getFullResolver(); |
| 256 |
return resolver != null ? resolver.findItemInTheme(itemName) : null; |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
public ResourceValue findItemInTheme(String attrName, boolean isFrameworkAttr) { |
| 261 |
ResourceResolver resolver = getFullResolver(); |
| 262 |
return resolver != null ? resolver.findItemInTheme(attrName, isFrameworkAttr) : null; |
| 263 |
} |
| 264 |
|
| 265 |
@SuppressWarnings("deprecation") |
| 266 |
@Override |
| 267 |
public ItemResourceValue findItemInStyle(StyleResourceValue style, String attrName) { |
| 268 |
ResourceResolver resolver = getFullResolver(); |
| 269 |
return resolver != null ? resolver.findItemInStyle(style, attrName) : null; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public ItemResourceValue findItemInStyle(StyleResourceValue style, String attrName, |
| 274 |
boolean isFrameworkAttr) { |
| 275 |
ResourceResolver resolver = getFullResolver(); |
| 276 |
return resolver != null ? resolver.findItemInStyle(style, attrName, isFrameworkAttr) : null; |
| 277 |
} |
| 278 |
|
| 279 |
@Override |
| 280 |
public StyleResourceValue getParent(StyleResourceValue style) { |
| 281 |
ResourceResolver resolver = getFullResolver(); |
| 282 |
return resolver != null ? resolver.getParent(style) : null; |
| 283 |
} |
| 284 |
|
| 285 |
private ResourceResolver getFullResolver() { |
| 286 |
if (mResolver == null) { |
| 287 |
if (mResourceProvider == null) { |
| 288 |
return null; |
| 289 |
} |
| 290 |
mResolver = mResourceProvider.getResolver(true); |
| 291 |
if (mResolver != null) { |
| 292 |
if (mLookupChain != null) { |
| 293 |
mResolver = mResolver.createRecorder(mLookupChain); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
return mResolver; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Optional method to set a list the resolver should record all value resolutions |
| 303 |
* into. Useful if you want to find out the resolution chain for a resource, |
| 304 |
* e.g. {@code @color/buttonForeground ⇒ @color/foreground ⇒ @android:color/black }. |
| 305 |
* <p> |
| 306 |
* There is no getter. Clients setting this list should look it up themselves. |
| 307 |
* Note also that if this resolver has to delegate to a full resource resolver, |
| 308 |
* e.g. to follow theme attributes, those resolutions will not be recorded. |
| 309 |
* |
| 310 |
* @param lookupChain the list to set, or null |
| 311 |
*/ |
| 312 |
public void setLookupChainList(@Nullable List<ResourceValue> lookupChain) { |
| 313 |
mLookupChain = lookupChain; |
| 314 |
} |
| 315 |
|
| 316 |
/** Returns the lookup chain being used by this resolver */ |
| 317 |
@Nullable |
| 318 |
public List<ResourceValue> getLookupChain() { |
| 319 |
return mLookupChain; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Returns a display string for a resource lookup |
| 324 |
* |
| 325 |
* @param type the resource type |
| 326 |
* @param name the resource name |
| 327 |
* @param isFramework whether the item is in the framework |
| 328 |
* @param lookupChain the list of resolved items to display |
| 329 |
* @return the display string |
| 330 |
*/ |
| 331 |
public static String getDisplayString( |
| 332 |
@NonNull ResourceType type, |
| 333 |
@NonNull String name, |
| 334 |
boolean isFramework, |
| 335 |
@NonNull List<ResourceValue> lookupChain) { |
| 336 |
String url = ResourceUrl.create(type, name, isFramework, false).toString(); |
| 337 |
return getDisplayString(url, lookupChain); |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Returns a display string for a resource lookup |
| 342 |
* @param url the resource url, such as {@code @string/foo} |
| 343 |
* @param lookupChain the list of resolved items to display |
| 344 |
* @return the display string |
| 345 |
*/ |
| 346 |
@NonNull |
| 347 |
public static String getDisplayString( |
| 348 |
@NonNull String url, |
| 349 |
@NonNull List<ResourceValue> lookupChain) { |
| 350 |
StringBuilder sb = new StringBuilder(); |
| 351 |
sb.append(url); |
| 352 |
String prev = url; |
| 353 |
for (ResourceValue element : lookupChain) { |
| 354 |
if (element == null) { |
| 355 |
continue; |
| 356 |
} |
| 357 |
String value = element.getValue(); |
| 358 |
if (value == null) { |
| 359 |
continue; |
| 360 |
} |
| 361 |
String text = value; |
| 362 |
if (text.equals(prev)) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
sb.append(" => "); |
| 367 |
|
| 368 |
// Strip paths |
| 369 |
if (!(text.startsWith(PREFIX_THEME_REF) || text.startsWith(PREFIX_RESOURCE_REF))) { |
| 370 |
int end = Math.max(text.lastIndexOf('/'), text.lastIndexOf('\\')); |
| 371 |
if (end != -1) { |
| 372 |
text = text.substring(end + 1); |
| 373 |
} |
| 374 |
} |
| 375 |
sb.append(text); |
| 376 |
|
| 377 |
prev = value; |
| 378 |
} |
| 379 |
|
| 380 |
return sb.toString(); |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Interface implemented by clients of the {@link ResourceItemResolver} which allows |
| 385 |
* it to lazily look up the project resources, the framework resources and optionally |
| 386 |
* to provide a fully configured resource resolver, if any |
| 387 |
*/ |
| 388 |
public interface ResourceProvider { |
| 389 |
@Nullable ResourceResolver getResolver(boolean createIfNecessary); |
| 390 |
@Nullable ResourceRepository getFrameworkResources(); |
| 391 |
@Nullable AbstractResourceRepository getAppResources(); |
| 392 |
} |
| 393 |
} |