Java Code Examples for com.android.resources.ResourceFolderType#getFolderType()

The following examples show how to use com.android.resources.ResourceFolderType#getFolderType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GeneratedResourceClassifier.java    From intellij with Apache License 2.0 6 votes vote down vote up
private static boolean mayHaveNonStringTranslations(String dirName) {
  // String translations only sit in the values-xx-rYY directories, so we can rule out other
  // directories quickly.
  if (!dirName.contains(SdkConstants.RES_QUALIFIER_SEP)) {
    return true;
  }
  if (ResourceFolderType.getFolderType(dirName) != ResourceFolderType.VALUES) {
    return true;
  }
  FolderConfiguration config = FolderConfiguration.getConfigForFolder(dirName);
  // Conservatively say it's interesting if there is an unrecognized configuration.
  if (config == null) {
    return true;
  }
  // If this is a translation mixed with something else, consider it a translation directory.
  boolean hasTranslation = false;
  for (ResourceQualifier qualifier : config.getQualifiers()) {
    if (qualifier instanceof LocaleQualifier) {
      hasTranslation = true;
    }
  }
  return !hasTranslation;
}
 
Example 2
Source File: OverdrawDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void beforeCheckFile(@NonNull Context context) {
    if (endsWith(context.file.getName(), DOT_XML)) {
        // Drawable XML files should not be considered for overdraw, except for <bitmap>'s.
        // The bitmap elements are handled in the scanBitmap() method; it will clear
        // out anything added by this method.
        File parent = context.file.getParentFile();
        ResourceFolderType type = ResourceFolderType.getFolderType(parent.getName());
        if (type == ResourceFolderType.DRAWABLE) {
            if (mValidDrawables == null) {
                mValidDrawables = new ArrayList<String>();
            }
            String resource = getDrawableResource(context.file);
            mValidDrawables.add(resource);
        }
    }
}
 
Example 3
Source File: PrivateResourceDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: ResourceUsageAnalyzer.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void recordResources(Path resDir)
    throws IOException, SAXException, ParserConfigurationException {

  File[] resourceFolders = resDir.toFile().listFiles();
  if (resourceFolders != null) {
    for (File folder : resourceFolders) {
      ResourceFolderType folderType = ResourceFolderType.getFolderType(folder.getName());
      if (folderType != null) {
        recordResources(folderType, folder);
      }
    }
  }
}
 
Example 5
Source File: BlazeCreateResourceFileDialog.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private String getNameError(String fileName) {
  String typeName = myResourceTypeCombo.getSelectedName();
  if (typeName != null) {
    ResourceFolderType type = ResourceFolderType.getFolderType(typeName);
    if (type != null) {
      IdeResourceNameValidator validator =
          IdeResourceNameValidator.forFilename(type, SdkConstants.DOT_XML);
      return validator.getErrorText(fileName);
    }
  }

  return null;
}
 
Example 6
Source File: ResourceRepository.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up the {@link ResourceFile} for the given {@link File}, if possible
 *
 * @param file the file
 * @return the corresponding {@link ResourceFile}, or null if not a known {@link ResourceFile}
 */
@Nullable
protected ResourceFile findResourceFile(@NonNull File file) {
    // Look up the right resource file for this path
    String parentName = file.getParentFile().getName();
    IAbstractFolder folder = getResFolder().getFolder(parentName);
    if (folder != null) {
        ResourceFolder resourceFolder = getResourceFolder(folder);
        if (resourceFolder == null) {
            FolderConfiguration configForFolder = FolderConfiguration
                    .getConfigForFolder(parentName);
            if (configForFolder != null) {
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType != null) {
                    resourceFolder = add(folderType, configForFolder, folder);
                }
            }
        }
        if (resourceFolder != null) {
            ResourceFile resourceFile = resourceFolder.getFile(file.getName());
            if (resourceFile != null) {
                return resourceFile;
            }
        }
    }

    return null;
}
 
Example 7
Source File: ResourceRepository.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Looks up the {@link ResourceFile} for the given {@link File}, if possible
 *
 * @param file the file
 * @return the corresponding {@link ResourceFile}, or null if not a known {@link ResourceFile}
 */
@Nullable
protected ResourceFile findResourceFile(@NonNull File file) {
    // Look up the right resource file for this path
    String parentName = file.getParentFile().getName();
    IAbstractFolder folder = getResFolder().getFolder(parentName);
    if (folder != null) {
        ResourceFolder resourceFolder = getResourceFolder(folder);
        if (resourceFolder == null) {
            FolderConfiguration configForFolder = FolderConfiguration
                    .getConfigForFolder(parentName);
            if (configForFolder != null) {
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType != null) {
                    resourceFolder = add(folderType, configForFolder, folder);
                }
            }
        }
        if (resourceFolder != null) {
            ResourceFile resourceFile = resourceFolder.getFile(file.getName());
            if (resourceFile != null) {
                return resourceFile;
            }
        }
    }

    return null;
}
 
Example 8
Source File: ResourceSet.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean isValidSourceFile(@NonNull File sourceFolder, @NonNull File file) {
    if (!super.isValidSourceFile(sourceFolder, file)) {
        return false;
    }

    File resFolder = file.getParentFile();
    // valid files are right under a resource folder under the source folder
    return resFolder.getParentFile().equals(sourceFolder) &&
            !isIgnored(resFolder) &&
            ResourceFolderType.getFolderType(resFolder.getName()) != null;
}
 
Example 9
Source File: ResourceSet.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValidSourceFile(@NonNull File sourceFolder, @NonNull File file) {
    if (!super.isValidSourceFile(sourceFolder, file)) {
        return false;
    }

    File resFolder = file.getParentFile();
    // valid files are right under a resource folder under the source folder
    return resFolder.getParentFile().equals(sourceFolder) &&
            !isIgnored(resFolder) &&
            ResourceFolderType.getFolderType(resFolder.getName()) != null;
}
 
Example 10
Source File: ResourceUsageAnalyzer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void recordResources(File resDir)
        throws IOException, SAXException, ParserConfigurationException {
    File[] resourceFolders = resDir.listFiles();
    if (resourceFolders != null) {
        for (File folder : resourceFolders) {
            ResourceFolderType folderType = ResourceFolderType.getFolderType(folder.getName());
            if (folderType != null) {
                recordResources(folderType, folder);
            }
        }
    }
}
 
Example 11
Source File: ResourceUsageAnalyzer.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
private Resource getResourceByJarPath(String path) {
    // Jars use forward slash paths, not File.separator
    if (path.startsWith("res/")) {
        int folderStart = 4; // "res/".length
        int folderEnd = path.indexOf('/', folderStart);
        if (folderEnd != -1) {
            String folderName = path.substring(folderStart, folderEnd);
            ResourceFolderType folderType = ResourceFolderType.getFolderType(folderName);
            if (folderType != null) {
                int nameStart = folderEnd + 1;
                int nameEnd = path.indexOf('.', nameStart);
                if (nameEnd != -1) {
                    String name = path.substring(nameStart, nameEnd);
                    List<ResourceType> types =
                            FolderTypeRelationship.getRelatedResourceTypes(folderType);
                    for (ResourceType type : types) {
                        if (type != ResourceType.ID) {
                            Resource resource = getResource(type, name);
                            if (resource != null) {
                                return resource;
                            }
                        }
                    }
                }
            }
        }
    }

    return null;
}
 
Example 12
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void checkResFolder(
        @NonNull Project project,
        @Nullable Project main,
        @NonNull File res,
        @NonNull List<ResourceXmlDetector> xmlChecks,
        @Nullable List<Detector> dirChecks,
        @Nullable List<Detector> binaryChecks) {
    File[] resourceDirs = res.listFiles();
    if (resourceDirs == null) {
        return;
    }

    // Sort alphabetically such that we can process related folder types at the
    // same time, and to have a defined behavior such that detectors can rely on
    // predictable ordering, e.g. layouts are seen before menus are seen before
    // values, etc (l < m < v).

    Arrays.sort(resourceDirs);
    for (File dir : resourceDirs) {
        ResourceFolderType type = ResourceFolderType.getFolderType(dir.getName());
        if (type != null) {
            checkResourceFolder(project, main, dir, type, xmlChecks, dirChecks, binaryChecks);
        }

        if (mCanceled) {
            return;
        }
    }
}
 
Example 13
Source File: ResourceRepository.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Looks up the {@link ResourceFile} for the given {@link File}, if possible
 *
 * @param file the file
 * @return the corresponding {@link ResourceFile}, or null if not a known {@link ResourceFile}
 */
@Nullable
protected ResourceFile findResourceFile(@NonNull File file) {
    // Look up the right resource file for this path
    String parentName = file.getParentFile().getName();
    IAbstractFolder folder = getResFolder().getFolder(parentName);
    if (folder != null) {
        ResourceFolder resourceFolder = getResourceFolder(folder);
        if (resourceFolder == null) {
            FolderConfiguration configForFolder = FolderConfiguration
                    .getConfigForFolder(parentName);
            if (configForFolder != null) {
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType != null) {
                    resourceFolder = add(folderType, configForFolder, folder);
                }
            }
        }
        if (resourceFolder != null) {
            ResourceFile resourceFile = resourceFolder.getFile(file.getName());
            if (resourceFile != null) {
                return resourceFile;
            }
        }
    }

    return null;
}
 
Example 14
Source File: MissingClassDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void afterCheckProject(@NonNull Context context) {
    if (!context.getProject().isLibrary() && mHaveClasses
            && mReferencedClasses != null && !mReferencedClasses.isEmpty()
            && context.getDriver().getScope().contains(Scope.CLASS_FILE)) {
        List<String> classes = new ArrayList<String>(mReferencedClasses.keySet());
        Collections.sort(classes);
        for (String owner : classes) {
            Location.Handle handle = mReferencedClasses.get(owner);
            String fqcn = ClassContext.getFqcn(owner);

            String signature = ClassContext.getInternalName(fqcn);
            if (!signature.equals(owner)) {
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            } else if (signature.indexOf('$') != -1) {
                signature = signature.replace('$', '/');
                if (!mReferencedClasses.containsKey(signature)) {
                    continue;
                }
            }
            mReferencedClasses.remove(owner);

            // Ignore usages of platform libraries
            if (owner.startsWith("android/")) { //$NON-NLS-1$
                continue;
            }

            String message = String.format(
                    "Class referenced in the manifest, `%1$s`, was not found in the " +
                            "project or the libraries", fqcn);
            Location location = handle.resolve();
            File parentFile = location.getFile().getParentFile();
            if (parentFile != null) {
                String parent = parentFile.getName();
                ResourceFolderType type = ResourceFolderType.getFolderType(parent);
                if (type == LAYOUT) {
                    message = String.format(
                        "Class referenced in the layout file, `%1$s`, was not found in "
                            + "the project or the libraries", fqcn);
                } else if (type == XML) {
                    message = String.format(
                            "Class referenced in the preference header file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);

                } else if (type == VALUES) {
                    message = String.format(
                            "Class referenced in the analytics file, `%1$s`, was not "
                                    + "found in the project or the libraries", fqcn);
                }
            }

            context.report(MISSING, location, message);
        }
    }
}
 
Example 15
Source File: XmlPrettyPrinter.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file,
        boolean stdout) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                formatFile(prefs, child, stdout);
            }
        }
    } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) {
        XmlFormatStyle style = null;
        if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) {
            style = XmlFormatStyle.MANIFEST;
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                String parentName = parent.getName();
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType == ResourceFolderType.LAYOUT) {
                    style = XmlFormatStyle.LAYOUT;
                } else if (folderType == ResourceFolderType.VALUES) {
                    style = XmlFormatStyle.RESOURCE;
                }
            }
        }

        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document == null) {
                System.err.println("Could not parse " + file);
                System.exit(1);
                return;
            }

            if (style == null) {
                style = XmlFormatStyle.get(document);
            }
            boolean endWithNewline = xml.endsWith("\n");
            int firstNewLine = xml.indexOf('\n');
            String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ?
                    "\r\n" : "\n";
            String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style,
                    lineSeparator, endWithNewline);
            if (stdout) {
                System.out.println(formatted);
            } else {
                Files.write(formatted, file, Charsets.UTF_8);
            }
        } catch (IOException e) {
            System.err.println("Could not read " + file);
            System.exit(1);
        }
    }
}
 
Example 16
Source File: VectorDrawableRenderer.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private boolean isInDrawable(@NonNull File inputXmlFile) {
    ResourceFolderType folderType =
            ResourceFolderType.getFolderType(inputXmlFile.getParentFile().getName());

    return folderType == ResourceFolderType.DRAWABLE;
}
 
Example 17
Source File: XmlPrettyPrinter.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private static void formatFile(@NonNull XmlFormatPreferences prefs, File file,
        boolean stdout) {
    if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                formatFile(prefs, child, stdout);
            }
        }
    } else if (file.isFile() && SdkUtils.endsWithIgnoreCase(file.getName(), DOT_XML)) {
        XmlFormatStyle style = null;
        if (file.getName().equals(SdkConstants.ANDROID_MANIFEST_XML)) {
            style = XmlFormatStyle.MANIFEST;
        } else {
            File parent = file.getParentFile();
            if (parent != null) {
                String parentName = parent.getName();
                ResourceFolderType folderType = ResourceFolderType.getFolderType(parentName);
                if (folderType == ResourceFolderType.LAYOUT) {
                    style = XmlFormatStyle.LAYOUT;
                } else if (folderType == ResourceFolderType.VALUES) {
                    style = XmlFormatStyle.RESOURCE;
                }
            }
        }

        try {
            String xml = Files.toString(file, Charsets.UTF_8);
            Document document = XmlUtils.parseDocumentSilently(xml, true);
            if (document == null) {
                System.err.println("Could not parse " + file);
                System.exit(1);
                return;
            }

            if (style == null) {
                style = XmlFormatStyle.get(document);
            }
            boolean endWithNewline = xml.endsWith("\n");
            int firstNewLine = xml.indexOf('\n');
            String lineSeparator = firstNewLine > 0 && xml.charAt(firstNewLine - 1) == '\r' ?
                    "\r\n" : "\n";
            String formatted = XmlPrettyPrinter.prettyPrint(document, prefs, style,
                    lineSeparator, endWithNewline);
            if (stdout) {
                System.out.println(formatted);
            } else {
                Files.write(formatted, file, Charsets.UTF_8);
            }
        } catch (IOException e) {
            System.err.println("Could not read " + file);
            System.exit(1);
        }
    }
}