/**
   * Returns the {@link ResourceFile} matching the given name, {@link ResourceType} and
   * configuration.
   *
   * <p>This only works with files generating one resource named after the file (for instance,
   * layouts, bitmap based drawable, xml, anims).
   *
   * @param name the resource name or file name
   * @param type the folder type search for
   * @param config the folder configuration to match for
   * @return the matching file or <code>null</code> if no match was found.
   */
  @Nullable
  public ResourceFile getMatchingFile(
      @NonNull String name, @NonNull ResourceType type, @NonNull FolderConfiguration config) {
    ensureInitialized();

    String resourceName = name;
    int dot = resourceName.indexOf('.');
    if (dot != -1) {
      resourceName = resourceName.substring(0, dot);
    }

    Map<String, ResourceItem> items = mResourceMap.get(type);
    if (items != null) {
      ResourceItem item = items.get(resourceName);
      if (item != null) {
        List<ResourceFile> files = item.getSourceFileList();
        if (files != null) {
          if (files.size() > 1) {
            ResourceValue value = item.getResourceValue(type, config, isFrameworkRepository());
            if (value != null) {
              String v = value.getValue();
              if (v != null) {
                ResourceUrl url = ResourceUrl.parse(v);
                if (url != null) {
                  return getMatchingFile(url.name, url.type, config);
                } else {
                  // Looks like the resource value is pointing to a file
                  // It's most likely one of the source files for this
                  // resource item, so check those first
                  for (ResourceFile f : files) {
                    if (v.equals(f.getFile().getOsLocation())) {
                      // Found the file
                      return f;
                    }
                  }

                  // No; look up the resource file from the full path
                  File file = new File(v);
                  if (file.exists()) {
                    ResourceFile f = findResourceFile(file);
                    if (f != null) {
                      return f;
                    }
                  }
                }
              }
            }
          } else if (files.size() == 1) {
            // Single file: see if it matches
            ResourceFile matchingFile = files.get(0);
            if (matchingFile.getFolder().getConfiguration().isMatchFor(config)) {
              return matchingFile;
            }
          }
        }
      }
    }

    return null;
  }
Example #2
0
  @Override
  public ResourceValue findResValue(String reference, boolean forceFrameworkOnly) {
    if (reference == null) {
      return null;
    }

    ResourceUrl resource = ResourceUrl.parse(reference);
    if (resource != null && resource.hasValidName()) {
      if (resource.theme) {
        // no theme? no need to go further!
        if (mDefaultTheme == null) {
          return null;
        }

        if (resource.type != ResourceType.ATTR) {
          // At this time, no support for ?type/name where type is not "attr"
          return null;
        }

        // Now look for the item in the theme, starting with the current one.
        ResourceValue item =
            findItemInTheme(resource.name, forceFrameworkOnly || resource.framework);
        if (item == null && mLogger != null) {
          mLogger.warning(
              LayoutLog.TAG_RESOURCES_RESOLVE_THEME_ATTR,
              String.format("Couldn't find theme resource %1$s for the current theme", reference),
              new ResourceValue(ResourceType.ATTR, reference, resource.framework));
        }

        return item;
      } else {
        return findResValue(resource.type, resource.name, forceFrameworkOnly || resource.framework);
      }
    }

    // Looks like the value didn't reference anything. Return null.
    return null;
  }