use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ResourceResolverCache method getResourceResolver.
@NotNull
public ResourceResolver getResourceResolver(@Nullable IAndroidTarget target, @NotNull String themeStyle, @NotNull final FolderConfiguration fullConfiguration) {
// Are caches up to date?
final LocalResourceRepository resources = AppResourceRepository.getAppResources(myManager.getModule(), true);
assert resources != null;
if (myCachedGeneration != resources.getModificationCount()) {
myResolverMap.clear();
myAppResourceMap.clear();
}
// Store the modification count as soon as possible. This ensures that if there is any modification of resources while the
// resolver is being created, it will be cleared subsequently.
myCachedGeneration = resources.getModificationCount();
// When looking up the configured project and framework resources, the theme doesn't matter, so we look up only
// by the configuration qualifiers; for example, here's a sample key:
// -ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
// Note that the target version is already baked in via the -v qualifier.
//
// However, the resource resolver also depends on the theme, so we use a more specific key for the resolver map than
// for the configured resource maps, by prepending the theme name:
// @style/MyTheme-ldltr-sw384dp-w384dp-h640dp-normal-notlong-port-notnight-xhdpi-finger-keyssoft-nokeys-navhidden-nonav-1280x768-v17
String configurationKey = fullConfiguration.getUniqueKey();
String resolverKey = themeStyle + configurationKey;
ResourceResolver resolver = myResolverMap.get(resolverKey);
if (resolver == null) {
Map<ResourceType, ResourceValueMap> configuredAppRes;
Map<ResourceType, ResourceValueMap> frameworkResources;
// Framework resources
if (target == null) {
target = myManager.getTarget();
}
if (target == null) {
frameworkResources = Collections.emptyMap();
} else {
ResourceRepository frameworkRes = getFrameworkResources(fullConfiguration, target);
if (frameworkRes == null) {
frameworkResources = Collections.emptyMap();
} else {
// get the framework resource values based on the current config
frameworkResources = myFrameworkResourceMap.get(configurationKey);
if (frameworkResources == null) {
frameworkResources = frameworkRes.getConfiguredResources(fullConfiguration);
// assets replaced the look for the same theme; that doesn't happen to the same extend for Holo)
if (target instanceof CompatibilityRenderTarget && target.getVersion().getApiLevel() == 8) {
IAndroidTarget realTarget = ((CompatibilityRenderTarget) target).getRealTarget();
if (realTarget != null) {
replaceDrawableBitmaps(frameworkResources, target, realTarget);
}
}
myFrameworkResourceMap.put(configurationKey, frameworkResources);
}
}
}
// App resources
configuredAppRes = myAppResourceMap.get(configurationKey);
if (configuredAppRes == null) {
// get the project resource values based on the current config
Application application = ApplicationManager.getApplication();
configuredAppRes = application.runReadAction(new Computable<Map<ResourceType, ResourceValueMap>>() {
@Override
public Map<ResourceType, ResourceValueMap> compute() {
return resources.getConfiguredResources(fullConfiguration);
}
});
myAppResourceMap.put(configurationKey, configuredAppRes);
}
// Resource Resolver
assert themeStyle.startsWith(PREFIX_RESOURCE_REF) : themeStyle;
boolean isProjectTheme = ResourceHelper.isProjectStyle(themeStyle);
String themeName = ResourceHelper.styleToTheme(themeStyle);
resolver = ResourceResolver.create(configuredAppRes, frameworkResources, themeName, isProjectTheme);
if (target instanceof CompatibilityRenderTarget) {
int apiLevel = target.getVersion().getFeatureLevel();
if (apiLevel >= 21) {
resolver.setDeviceDefaults("Material");
} else if (apiLevel >= 14) {
resolver.setDeviceDefaults("Holo");
} else {
resolver.setDeviceDefaults(ResourceResolver.LEGACY_THEME);
}
}
myResolverMap.put(resolverKey, resolver);
}
return resolver;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class DataBindingUtil method invalidateAllSources.
public static boolean invalidateAllSources(DataBindingProjectComponent component) {
boolean invalidated = false;
for (AndroidFacet facet : component.getDataBindingEnabledFacets()) {
LocalResourceRepository moduleResources = facet.getModuleResources(true);
Map<String, DataBindingInfo> dataBindingResourceFiles = moduleResources.getDataBindingResourceFiles();
if (dataBindingResourceFiles == null) {
continue;
}
for (DataBindingInfo info : dataBindingResourceFiles.values()) {
PsiClass psiClass = info.getPsiClass();
if (psiClass != null) {
PsiFile containingFile = psiClass.getContainingFile();
if (containingFile != null) {
containingFile.subtreeChanged();
invalidated = true;
}
}
}
}
return invalidated;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class DataBindingClassFinder method findClass.
@Nullable
@Override
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
if (!isEnabled()) {
return null;
}
for (AndroidFacet facet : myComponent.getDataBindingEnabledFacets()) {
LocalResourceRepository moduleResources = facet.getModuleResources(true);
Map<String, DataBindingInfo> dataBindingResourceFiles = moduleResources.getDataBindingResourceFiles();
if (dataBindingResourceFiles == null) {
continue;
}
DataBindingInfo dataBindingInfo = dataBindingResourceFiles.get(qualifiedName);
if (dataBindingInfo == null) {
continue;
}
VirtualFile file = dataBindingInfo.getPsiFile().getVirtualFile();
if (file != null && scope.accept(file)) {
return DataBindingUtil.getOrCreatePsiClass(dataBindingInfo);
}
}
return null;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class LocaleMenuAction method getRelevantLocales.
/**
* Like {@link ConfigurationManager#getLocales} but filters out locales not compatible
* with language and region qualifiers in the current configuration's folder config
*
* @return the list of relevant locales in the project
*/
@NotNull
private List<Locale> getRelevantLocales() {
List<Locale> locales = new ArrayList<Locale>();
Configuration configuration = myRenderContext.getConfiguration();
if (configuration == null) {
return Collections.emptyList();
}
Module module = configuration.getConfigurationManager().getModule();
LocaleQualifier specificLocale = configuration.getEditedConfig().getLocaleQualifier();
// locale.
if (specificLocale != null) {
List<VirtualFile> variations = ResourceHelper.getResourceVariations(configuration.getFile(), false);
for (VirtualFile variation : variations) {
FolderConfiguration config = FolderConfiguration.getConfigForFolder(variation.getParent().getName());
if (config != null && config.getLocaleQualifier() == null) {
specificLocale = null;
break;
}
}
}
LocalResourceRepository projectResources = ProjectResourceRepository.getProjectResources(module, true);
Set<LocaleQualifier> languages = projectResources != null ? projectResources.getLocales() : Collections.<LocaleQualifier>emptySet();
for (LocaleQualifier l : languages) {
if (specificLocale != null && !specificLocale.isMatchFor(l)) {
continue;
}
locales.add(Locale.create(l));
}
return locales;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ThemeEditorStyle method getStyleResourceItems.
/**
* Returns all the {@link ResourceItem} where this style is defined. This includes all the definitions in the
* different resource folders.
*/
@NotNull
protected Collection<ResourceItem> getStyleResourceItems() {
assert !isFramework();
Collection<ResourceItem> resultItems;
final Module module = myManager.getModule();
if (isProjectStyle()) {
AndroidFacet facet = AndroidFacet.getInstance(module);
assert facet != null : module.getName() + " module doesn't have AndroidFacet";
// We need to keep a Set of ResourceItems to override them. The key is the folder configuration + the name
final HashMap<String, ResourceItem> resourceItems = Maps.newHashMap();
ThemeEditorUtils.acceptResourceResolverVisitor(facet, new ThemeEditorUtils.ResourceFolderVisitor() {
@Override
public void visitResourceFolder(@NotNull LocalResourceRepository resources, String moduleName, @NotNull String variantName, boolean isSourceSelected) {
if (!isSourceSelected) {
// TODO: Process all source sets
return;
}
List<ResourceItem> items = resources.getResourceItem(ResourceType.STYLE, myQualifiedName);
if (items == null) {
return;
}
for (ResourceItem item : items) {
String key = item.getConfiguration().toShortDisplayString() + "/" + item.getName();
resourceItems.put(key, item);
}
}
});
resultItems = ImmutableList.copyOf(resourceItems.values());
} else {
LocalResourceRepository resourceRepository = AppResourceRepository.getAppResources(module, true);
assert resourceRepository != null;
List<ResourceItem> items = resourceRepository.getResourceItem(ResourceType.STYLE, myQualifiedName);
if (items != null) {
resultItems = items;
} else {
resultItems = Collections.emptyList();
}
}
return resultItems;
}
Aggregations