use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceTypeClassBase method buildResourceFields.
@NotNull
static PsiField[] buildResourceFields(@NotNull ResourceManager manager, boolean nonFinal, @NotNull String resClassName, @NotNull final PsiClass context) {
ResourceType resourceType = ResourceType.getEnum(resClassName);
if (resourceType == null) {
return PsiField.EMPTY_ARRAY;
}
final Map<String, PsiType> fieldNames = new HashMap<String, PsiType>();
final boolean styleable = ResourceType.STYLEABLE == resourceType;
final PsiType basicType = styleable ? PsiType.INT.createArrayType() : PsiType.INT;
for (String resName : manager.getResourceNames(resourceType)) {
fieldNames.put(resName, basicType);
}
if (styleable) {
for (ResourceEntry entry : manager.getValueResourceEntries(ResourceType.ATTR)) {
final String resName = entry.getName();
final String resContext = entry.getContext();
if (resContext.length() > 0) {
fieldNames.put(resContext + '_' + resName, PsiType.INT);
}
}
}
final PsiField[] result = new PsiField[fieldNames.size()];
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(context.getProject());
int idIterator = resourceType.ordinal() * 100000;
int i = 0;
for (Map.Entry<String, PsiType> entry : fieldNames.entrySet()) {
final String fieldName = AndroidResourceUtil.getFieldNameByResourceName(entry.getKey());
final PsiType type = entry.getValue();
final int id = -(idIterator++);
final AndroidLightField field = new AndroidLightField(fieldName, context, type, !nonFinal, nonFinal ? null : id);
field.setInitializer(factory.createExpressionFromText(Integer.toString(id), field));
result[i++] = field;
}
return result;
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class ResourceManager method findValueResourceInfos.
@NotNull
public List<ValueResourceInfoImpl> findValueResourceInfos(@NotNull String resourceType, @NotNull final String resourceName, final boolean distinguishDelimetersInName, boolean searchAttrs) {
final ResourceType type = resourceType.startsWith("+") ? ResourceType.ID : ResourceType.getEnum(resourceType);
if (type == null || !AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(type) && (type != ResourceType.ATTR || !searchAttrs)) {
return Collections.emptyList();
}
final GlobalSearchScope scope = GlobalSearchScope.allScope(myProject);
final List<ValueResourceInfoImpl> result = new ArrayList<ValueResourceInfoImpl>();
final Set<VirtualFile> valueResourceFiles = getAllValueResourceFiles();
FileBasedIndex.getInstance().processValues(AndroidValueResourcesIndex.INDEX_ID, AndroidValueResourcesIndex.createTypeNameMarkerKey(resourceType, resourceName), null, new FileBasedIndex.ValueProcessor<ImmutableSet<AndroidValueResourcesIndex.MyResourceInfo>>() {
@Override
public boolean process(@NotNull VirtualFile file, ImmutableSet<AndroidValueResourcesIndex.MyResourceInfo> infos) {
for (AndroidValueResourcesIndex.MyResourceInfo info : infos) {
final String name = info.getResourceEntry().getName();
if (AndroidUtils.equal(resourceName, name, distinguishDelimetersInName)) {
if (valueResourceFiles.contains(file)) {
result.add(new ValueResourceInfoImpl(info.getResourceEntry().getName(), type, file, myProject, info.getOffset()));
}
}
}
return true;
}
}, scope);
return result;
}
use of com.android.resources.ResourceType in project kotlin by JetBrains.
the class PrivateResourceDetector method visitElement.
@Override
public void visitElement(@NonNull XmlContext context, @NonNull Element element) {
if (TAG_RESOURCES.equals(element.getTagName())) {
for (Element item : LintUtils.getChildren(element)) {
Attr nameAttribute = item.getAttributeNode(ATTR_NAME);
if (nameAttribute != null) {
String name = getResourceFieldName(nameAttribute.getValue());
String type = item.getTagName();
if (type.equals(TAG_ITEM)) {
type = item.getAttribute(ATTR_TYPE);
if (type == null || type.isEmpty()) {
type = RESOURCE_CLZ_ID;
}
} else if (type.equals("declare-styleable")) {
//$NON-NLS-1$
type = RESOURCE_CLR_STYLEABLE;
} else if (type.contains("array")) {
//$NON-NLS-1$
// <string-array> etc
type = RESOURCE_CLZ_ARRAY;
}
ResourceType t = ResourceType.getEnum(type);
if (t != null && isPrivate(context, t, name) && !VALUE_TRUE.equals(item.getAttributeNS(TOOLS_URI, ATTR_OVERRIDE))) {
String message = createOverrideErrorMessage(context, t, name);
Location location = context.getValueLocation(nameAttribute);
context.report(ISSUE, nameAttribute, location, message);
}
}
}
} else {
assert TAG_STYLE.equals(element.getTagName()) || TAG_ARRAY.equals(element.getTagName()) || TAG_PLURALS.equals(element.getTagName()) || TAG_INTEGER_ARRAY.equals(element.getTagName()) || TAG_STRING_ARRAY.equals(element.getTagName());
for (Element item : LintUtils.getChildren(element)) {
checkChildRefs(context, item);
}
}
}
use of com.android.resources.ResourceType in project kotlin by JetBrains.
the class PrivateResourceDetector method beforeCheckFile.
@Override
public void beforeCheckFile(@NonNull Context context) {
File file = context.file;
boolean isXmlFile = LintUtils.isXmlFile(file);
if (!isXmlFile && !LintUtils.isBitmapFile(file)) {
return;
}
String parentName = file.getParentFile().getName();
int dash = parentName.indexOf('-');
if (dash != -1 || FD_RES_VALUES.equals(parentName)) {
return;
}
ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
if (folderType == null) {
return;
}
List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folderType);
if (types.isEmpty()) {
return;
}
ResourceType type = types.get(0);
String resourceName = getResourceFieldName(getBaseName(file.getName()));
if (isPrivate(context, type, resourceName)) {
String message = createOverrideErrorMessage(context, type, resourceName);
Location location = Location.create(file);
context.report(ISSUE, location, message);
}
}
use of com.android.resources.ResourceType in project android by JetBrains.
the class ChooseResourceDialog method createNewResourcePopupMenu.
private ActionPopupMenu createNewResourcePopupMenu() {
ActionManager actionManager = ActionManager.getInstance();
DefaultActionGroup actionGroup = new DefaultActionGroup();
ResourcePanel panel = getSelectedPanel();
ResourceType resourceType = panel.getType();
ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(resourceType);
if (folderType != null) {
AnAction newFileAction = createNewResourceFileAction();
newFileAction.getTemplatePresentation().setText("New " + folderType.getName() + " File...");
newFileAction.getTemplatePresentation().putClientProperty(FOLDER_TYPE_KEY, folderType);
actionGroup.add(newFileAction);
}
if (AndroidResourceUtil.VALUE_RESOURCE_TYPES.contains(resourceType)) {
String title = "New " + resourceType + " Value...";
if (resourceType == ResourceType.LAYOUT) {
title = "New Layout Alias";
}
AnAction newValueAction = createNewResourceValueAction();
newValueAction.getTemplatePresentation().setText(title);
newValueAction.getTemplatePresentation().putClientProperty(TYPE_KEY, resourceType);
actionGroup.add(newValueAction);
}
if (myTag != null && ResourceType.STYLE.equals(resourceType)) {
final boolean enabled = AndroidBaseLayoutRefactoringAction.getLayoutViewElement(myTag) != null && AndroidExtractStyleAction.doIsEnabled(myTag);
AnAction extractStyleAction = createExtractStyleAction();
extractStyleAction.getTemplatePresentation().setEnabled(enabled);
actionGroup.add(extractStyleAction);
}
if (GraphicalResourceRendererEditor.COLORS_AND_DRAWABLES.contains(resourceType)) {
AnAction newReferenceAction = createNewResourceReferenceAction();
newReferenceAction.getTemplatePresentation().setText("New " + resourceType + " Reference...");
actionGroup.add(newReferenceAction);
}
return actionManager.createActionPopupMenu(ActionPlaces.UNKNOWN, actionGroup);
}
Aggregations