use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class AndroidResourceReferenceBase method resolveInner.
@NotNull
private ResolveResult[] resolveInner() {
final List<PsiElement> elements = new ArrayList<>();
final boolean attrReference = myResourceValue.getPrefix() == '?';
collectTargets(myFacet, myResourceValue, elements, attrReference);
final List<ResolveResult> result = new ArrayList<>();
if (elements.isEmpty() && myResourceValue.getResourceName() != null && !AndroidUtils.SYSTEM_RESOURCE_PACKAGE.equals(myResourceValue.getNamespace())) {
// Dynamic items do not appear in the XML scanning file index; look for
// these in the resource repositories.
LocalResourceRepository resources = AppResourceRepository.getAppResources(myFacet.getModule(), true);
ResourceType resourceType = myResourceValue.getType();
if (resourceType != null && (resourceType != ResourceType.ATTR || attrReference)) {
// If not, it could be some broken source, such as @android/test
assert resources != null;
List<ResourceItem> items = resources.getResourceItem(resourceType, myResourceValue.getResourceName());
if (items != null && FolderTypeRelationship.getRelatedFolders(resourceType).contains(ResourceFolderType.VALUES)) {
for (ResourceItem item : items) {
XmlTag tag = LocalResourceRepository.getItemTag(myFacet.getModule().getProject(), item);
if (tag != null) {
elements.add(tag);
} else if (item instanceof DynamicResourceValueItem) {
result.add(((DynamicResourceValueItem) item).createResolveResult());
}
}
}
}
}
if (elements.size() > 1) {
Collections.sort(elements, AndroidResourceUtil.RESOURCE_ELEMENT_COMPARATOR);
}
for (PsiElement target : elements) {
result.add(new PsiElementResolveResult(target));
}
return result.toArray(new ResolveResult[result.size()]);
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ConfigurationMatcher method getBetterMatch.
/**
* Returns a different file which is a better match for the given device, orientation, target version, etc
* than the current one. You can supply {@code null} for all parameters; in that case, the current value
* in the configuration is used.
*/
@Nullable
public static VirtualFile getBetterMatch(@NotNull Configuration configuration, @Nullable Device device, @Nullable String stateName, @Nullable Locale locale, @Nullable IAndroidTarget target) {
VirtualFile file = configuration.getFile();
Module module = configuration.getModule();
if (file != null && module != null) {
if (device == null) {
device = configuration.getDevice();
}
if (stateName == null) {
State deviceState = configuration.getDeviceState();
stateName = deviceState != null ? deviceState.getName() : null;
}
State selectedState = ConfigurationFileState.getState(device, stateName);
if (selectedState == null) {
// Invalid state name passed in for the current device
return null;
}
if (locale == null) {
locale = configuration.getLocale();
}
if (target == null) {
target = configuration.getTarget();
}
FolderConfiguration currentConfig = Configuration.getFolderConfig(module, selectedState, locale, target);
if (currentConfig != null) {
LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
if (resources != null) {
ResourceFolderType folderType = ResourceHelper.getFolderType(file);
if (folderType != null) {
List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
if (!types.isEmpty()) {
ResourceType type = types.get(0);
List<VirtualFile> matches = resources.getMatchingFiles(file, type, currentConfig);
if (!matches.contains(file) && !matches.isEmpty()) {
return matches.get(0);
}
}
}
}
}
}
return null;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class Configuration method computeBestDevice.
@Nullable
private Device computeBestDevice() {
for (Device device : myManager.getRecentDevices()) {
String stateName = myStateName;
if (stateName == null) {
stateName = device.getDefaultState().getName();
}
State selectedState = ConfigurationFileState.getState(device, stateName);
Module module = myManager.getModule();
FolderConfiguration currentConfig = getFolderConfig(module, selectedState, getLocale(), getTarget());
if (currentConfig != null) {
if (myEditedConfig.isMatchFor(currentConfig)) {
LocalResourceRepository resources = AppResourceRepository.getAppResources(module, true);
if (resources != null && myFile != null) {
ResourceFolderType folderType = ResourceHelper.getFolderType(myFile);
if (folderType != null) {
if (ResourceFolderType.VALUES.equals(folderType)) {
// If it's a file in the values folder, ResourceRepository.getMatchingFiles won't work.
// We get instead all the available folders and check that there is one compatible.
LocalResourceManager resourceManager = LocalResourceManager.getInstance(module);
if (resourceManager != null) {
for (PsiFile resourceFile : resourceManager.findResourceFiles("values")) {
if (myFile.equals(resourceFile.getVirtualFile()) && resourceFile.getParent() != null) {
FolderConfiguration folderConfiguration = FolderConfiguration.getConfigForFolder(resourceFile.getParent().getName());
if (currentConfig.isMatchFor(folderConfiguration)) {
return device;
}
}
}
}
} else {
List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
if (!types.isEmpty()) {
ResourceType type = types.get(0);
List<VirtualFile> matches = resources.getMatchingFiles(myFile, type, currentConfig);
if (matches.contains(myFile)) {
return device;
}
}
}
} else if ("Kotlin".equals(myFile.getFileType().getName())) {
return device;
} else if (myFile.equals(myManager.getProject().getProjectFile())) {
// takes care of correct device selection for Theme Editor
return device;
}
}
}
}
}
return myManager.getDefaultDevice();
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class ConfigurationManager method create.
/**
* Creates a new {@link Configuration} associated with this manager
* @return a new {@link Configuration}
*/
@NotNull
private Configuration create(@NotNull VirtualFile file) {
ConfigurationStateManager stateManager = getStateManager();
ConfigurationFileState fileState = stateManager.getConfigurationState(file);
assert file.getParent() != null : file;
FolderConfiguration config = FolderConfiguration.getConfigForFolder(file.getParent().getName());
if (config == null) {
config = new FolderConfiguration();
}
Configuration configuration = Configuration.create(this, file, fileState, config);
LocalResourceRepository resources = AppResourceRepository.getAppResources(myModule, true);
ConfigurationMatcher matcher = new ConfigurationMatcher(configuration, resources, file);
if (fileState != null) {
matcher.adaptConfigSelection(true);
} else {
matcher.findAndSetCompatibleConfig(false);
}
return configuration;
}
use of com.android.tools.idea.res.LocalResourceRepository in project android by JetBrains.
the class AndroidColorAnnotator method findResourceValue.
/** Looks up the resource item of the given type and name for the given configuration, if any */
@Nullable
private static ResourceValue findResourceValue(ResourceType type, String name, boolean isFramework, Module module, Configuration configuration) {
if (isFramework) {
ResourceRepository frameworkResources = configuration.getFrameworkResources();
if (frameworkResources == null) {
return null;
}
if (!frameworkResources.hasResourceItem(type, name)) {
return null;
}
ResourceItem item = frameworkResources.getResourceItem(type, name);
return item.getResourceValue(type, configuration.getFullConfig(), false);
} else {
LocalResourceRepository appResources = AppResourceRepository.getAppResources(module, true);
if (appResources == null) {
return null;
}
if (!appResources.hasResourceItem(type, name)) {
return null;
}
return appResources.getConfiguredValue(type, name, configuration.getFullConfig());
}
}
Aggregations