use of com.android.resources.ResourceFolderType in project kotlin by JetBrains.
the class LintDriver method checkIndividualResources.
/** Checks individual resources */
private void checkIndividualResources(@NonNull Project project, @Nullable Project main, @NonNull List<ResourceXmlDetector> xmlDetectors, @Nullable List<Detector> dirChecks, @Nullable List<Detector> binaryChecks, @NonNull List<File> files) {
for (File file : files) {
if (file.isDirectory()) {
// Is it a resource folder?
ResourceFolderType type = ResourceFolderType.getFolderType(file.getName());
if (type != null && new File(file.getParentFile(), RES_FOLDER).exists()) {
// Yes.
checkResourceFolder(project, main, file, type, xmlDetectors, dirChecks, binaryChecks);
} else if (file.getName().equals(RES_FOLDER)) {
// Is it the res folder?
// Yes
checkResFolder(project, main, file, xmlDetectors, dirChecks, binaryChecks);
} else {
mClient.log(null, "Unexpected folder %1$s; should be project, " + "\"res\" folder or resource folder", file.getPath());
}
} else if (file.isFile() && LintUtils.isXmlFile(file)) {
// Yes, find out its resource type
String folderName = file.getParentFile().getName();
ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
if (type != null) {
ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
if (visitor != null) {
XmlContext context = new XmlContext(this, project, main, file, type, visitor.getParser());
fireEvent(EventType.SCANNING_FILE, context);
visitor.visitFile(context, file);
}
}
} else if (binaryChecks != null && file.isFile() && LintUtils.isBitmapFile(file)) {
// Yes, find out its resource type
String folderName = file.getParentFile().getName();
ResourceFolderType type = ResourceFolderType.getFolderType(folderName);
if (type != null) {
ResourceVisitor visitor = getVisitor(type, xmlDetectors, binaryChecks);
if (visitor != null) {
ResourceContext context = new ResourceContext(this, project, main, file, type);
fireEvent(EventType.SCANNING_FILE, context);
visitor.visitBinaryResource(context);
if (mCanceled) {
return;
}
}
}
}
}
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class PsiResourceItem method getSource.
@Nullable
@Override
public ResourceFile getSource() {
ResourceFile source = super.getSource();
// Temporary safety workaround
if (source == null && myFile != null && myFile.getParent() != null) {
PsiDirectory parent = myFile.getParent();
if (parent != null) {
String name = parent.getName();
ResourceFolderType folderType = ResourceFolderType.getFolderType(name);
FolderConfiguration configuration = FolderConfiguration.getConfigForFolder(name);
int index = name.indexOf('-');
String qualifiers = index == -1 ? "" : name.substring(index + 1);
source = new PsiResourceFile(myFile, Collections.<ResourceItem>singletonList(this), qualifiers, folderType, configuration);
setSource(source);
}
}
return source;
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class AndroidResourceReference method handleElementRename.
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
if (newElementName.startsWith(SdkConstants.NEW_ID_PREFIX)) {
newElementName = AndroidResourceUtil.getResourceNameByReferenceText(newElementName);
}
ResourceValue value = myValue.getValue();
assert value != null;
final ResourceType resType = value.getType();
if (resType != null && newElementName != null) {
// todo: do not allow new value resource name to contain dot, because it is impossible to check if it file or value otherwise
final String newResName;
// Does renamed resource point to a file?
ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resType);
if (folderType != null && newElementName.contains(".")) {
// If it does, we need to chop off its extension when inserting the new value.
newResName = AndroidCommonUtils.getResourceName(resType.getName(), newElementName);
} else {
newResName = newElementName;
}
// Note: We're using value.getResourceType(), not resType.getName() here, because we want the "+" in the new name
myValue.setValue(ResourceValue.referenceTo(value.getPrefix(), value.getNamespace(), value.getResourceType(), newResName));
}
return myValue.getXmlTag();
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class AndroidResourceUtil method findResourceFieldsForValueResource.
@NotNull
public static PsiField[] findResourceFieldsForValueResource(XmlTag tag, boolean onlyInOwnPackages) {
final AndroidFacet facet = AndroidFacet.getInstance(tag);
if (facet == null) {
return PsiField.EMPTY_ARRAY;
}
ResourceFolderType fileResType = ResourceHelper.getFolderType(tag.getContainingFile());
final String resourceType = fileResType == ResourceFolderType.VALUES ? getResourceTypeByValueResourceTag(tag) : null;
if (resourceType == null) {
return PsiField.EMPTY_ARRAY;
}
String name = tag.getAttributeValue(ATTR_NAME);
if (name == null) {
return PsiField.EMPTY_ARRAY;
}
return findResourceFields(facet, resourceType, name, onlyInOwnPackages);
}
use of com.android.resources.ResourceFolderType in project android by JetBrains.
the class ThemeEditorUtils method copyTheme.
/**
* Copies a theme to a values folder with api version apiLevel,
* potentially creating the necessary folder or file.
* @param apiLevel api level of the folder the theme is copied to
* @param toBeCopied theme to be copied
*/
public static void copyTheme(int apiLevel, @NotNull final XmlTag toBeCopied) {
ApplicationManager.getApplication().assertWriteAccessAllowed();
PsiFile file = toBeCopied.getContainingFile();
assert file instanceof XmlFile : file;
ResourceFolderType folderType = ResourceHelper.getFolderType(file);
assert folderType != null : file;
FolderConfiguration config = ResourceHelper.getFolderConfiguration(file);
assert config != null : file;
VersionQualifier qualifier = new VersionQualifier(apiLevel);
config.setVersionQualifier(qualifier);
String folder = config.getFolderName(folderType);
if (folderType != ResourceFolderType.VALUES) {
OverrideResourceAction.forkResourceFile((XmlFile) file, folder, false);
} else {
XmlTag tag = OverrideResourceAction.getValueTag(PsiTreeUtil.getParentOfType(toBeCopied, XmlTag.class, false));
if (tag != null) {
PsiDirectory dir = null;
PsiDirectory resFolder = file.getParent();
if (resFolder != null) {
resFolder = resFolder.getParent();
}
if (resFolder != null) {
dir = resFolder.findSubdirectory(folder);
if (dir == null) {
dir = resFolder.createSubdirectory(folder);
}
}
OverrideResourceAction.forkResourceValue(toBeCopied.getProject(), tag, file, dir, false);
}
}
}
Aggregations