com.android.ide.common.resources
Class ResourceRepository

java.lang.Object
  extended by com.android.ide.common.resources.ResourceRepository
Direct Known Subclasses:
FrameworkResources

public abstract class ResourceRepository
extends java.lang.Object

Base class for resource repository. A repository is both a file representation of a resource folder and a representation of the generated resources, organized by type. getResourceFolder(IAbstractFolder) and getSourceFiles(ResourceType, String, FolderConfiguration) give access to the folders and files of the resource folder. getResourceItemsOfType(ResourceType) gives access to the resources directly.


Field Summary
protected  java.util.Map<com.android.resources.ResourceFolderType,java.util.List<ResourceFolder>> mFolderMap
           
protected  java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,ResourceItem>> mResourceMap
           
 
Constructor Summary
protected ResourceRepository(com.android.io.IAbstractFolder resFolder, boolean isFrameworkRepository)
          Makes a resource repository
 
Method Summary
 void clear()
           
protected abstract  ResourceItem createResourceItem(java.lang.String name)
          Creates a resource item with the given name.
protected  java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,com.android.ide.common.rendering.api.ResourceValue>> doGetConfiguredResources(FolderConfiguration referenceConfig)
          Returns the resources values matching a given FolderConfiguration for the current project.
 boolean ensureInitialized()
          Ensures that the repository has been initialized again after a call to clear()
protected  ResourceFile findResourceFile(java.io.File file)
          Looks up the ResourceFile for the given File, if possible
 java.util.List<com.android.resources.ResourceType> getAvailableResourceTypes()
           
 java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,com.android.ide.common.rendering.api.ResourceValue>> getConfiguredResources(FolderConfiguration referenceConfig)
          Returns the resources values matching a given FolderConfiguration.
 java.util.List<ResourceFolder> getFolders(com.android.resources.ResourceFolderType type)
          Returns a list of ResourceFolder for a specific ResourceFolderType.
 java.util.SortedSet<java.lang.String> getLanguages()
          Returns the sorted list of languages used in the resources.
 ResourceFile getMatchingFile(java.lang.String name, com.android.resources.ResourceFolderType type, FolderConfiguration config)
          Returns the ResourceFile matching the given name, ResourceFolderType and configuration.
 ResourceFile getMatchingFile(java.lang.String name, com.android.resources.ResourceType type, FolderConfiguration config)
          Returns the ResourceFile matching the given name, ResourceType and configuration.
 java.util.SortedSet<java.lang.String> getRegions(java.lang.String currentLanguage)
          Returns the sorted list of regions used in the resources with the given language.
 com.android.io.IAbstractFolder getResFolder()
           
 ResourceFolder getResourceFolder(com.android.io.IAbstractFolder folder)
          Returns the ResourceFolder associated with a IAbstractFolder.
 ResourceItem getResourceItem(com.android.resources.ResourceType type, java.lang.String name)
          Returns a ResourceItem matching the given ResourceType and name.
 java.util.Collection<ResourceItem> getResourceItemsOfType(com.android.resources.ResourceType type)
          Returns a list of ResourceItem matching a given ResourceType.
 java.util.List<ResourceFile> getSourceFiles(com.android.resources.ResourceType type, java.lang.String name, FolderConfiguration referenceConfig)
          Returns the list of source files for a given resource.
 boolean hasResourceItem(com.android.resources.ResourceType type, java.lang.String name)
          Returns true if this resource repository contains a resource of the given name.
 boolean hasResourceItem(java.lang.String url)
          Returns true if this resource repository contains a resource of the given name.
 boolean hasResourcesOfType(com.android.resources.ResourceType type)
          Returns whether the repository has resources of a given ResourceType.
 boolean isFrameworkRepository()
           
 void loadResources()
          Loads the resources.
 void postUpdateCleanUp()
          Cleans up the repository of resource items that have no source file anymore.
 ResourceFolder processFolder(com.android.io.IAbstractFolder folder)
          Processes a folder and adds it to the list of existing folders.
protected  void removeFile(java.util.Collection<com.android.resources.ResourceType> types, ResourceFile file)
           
protected  void removeFile(com.android.resources.ResourceType type, ResourceFile file)
           
 ResourceFolder removeFolder(com.android.resources.ResourceFolderType type, com.android.io.IAbstractFolder removedFolder, ScanningContext context)
          Removes a ResourceFolder associated with the specified IAbstractFolder.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mFolderMap

protected java.util.Map<com.android.resources.ResourceFolderType,java.util.List<ResourceFolder>> mFolderMap

mResourceMap

protected java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,ResourceItem>> mResourceMap
Constructor Detail

ResourceRepository

protected ResourceRepository(@NonNull
                             com.android.io.IAbstractFolder resFolder,
                             boolean isFrameworkRepository)
Makes a resource repository

Parameters:
resFolder - the resource folder of the repository.
isFrameworkRepository - whether the repository is for framework resources.
Method Detail

getResFolder

public com.android.io.IAbstractFolder getResFolder()

isFrameworkRepository

public boolean isFrameworkRepository()

clear

public void clear()

ensureInitialized

public boolean ensureInitialized()
Ensures that the repository has been initialized again after a call to clear()

Returns:
true if the repository was just re-initialized.

removeFolder

@Nullable
public ResourceFolder removeFolder(@NonNull
                                            com.android.resources.ResourceFolderType type,
                                            @NonNull
                                            com.android.io.IAbstractFolder removedFolder,
                                            @Nullable
                                            ScanningContext context)
Removes a ResourceFolder associated with the specified IAbstractFolder.

Parameters:
type - The type of the folder
removedFolder - the IAbstractFolder object.
context - the scanning context
Returns:
the ResourceFolder that was removed, or null if no matches were found.

hasResourceItem

public boolean hasResourceItem(@NonNull
                               java.lang.String url)
Returns true if this resource repository contains a resource of the given name.

Parameters:
url - the resource URL
Returns:
true if the resource is known

hasResourceItem

public boolean hasResourceItem(@NonNull
                               com.android.resources.ResourceType type,
                               @NonNull
                               java.lang.String name)
Returns true if this resource repository contains a resource of the given name.

Parameters:
type - the type of resource to look up
name - the name of the resource
Returns:
true if the resource is known

getResourceItem

@NonNull
public ResourceItem getResourceItem(@NonNull
                                            com.android.resources.ResourceType type,
                                            @NonNull
                                            java.lang.String name)
Returns a ResourceItem matching the given ResourceType and name. If none exist, it creates one.

Parameters:
type - the resource type
name - the name of the resource.
Returns:
A resource item matching the type and name.

createResourceItem

@NonNull
protected abstract ResourceItem createResourceItem(@NonNull
                                                           java.lang.String name)
Creates a resource item with the given name.

Parameters:
name - the name of the resource
Returns:
a new ResourceItem (or child class) instance.

processFolder

@Nullable
public ResourceFolder processFolder(@NonNull
                                             com.android.io.IAbstractFolder folder)
Processes a folder and adds it to the list of existing folders.

Parameters:
folder - the folder to process
Returns:
the ResourceFolder created from this folder, or null if the process failed.

getFolders

@Nullable
public java.util.List<ResourceFolder> getFolders(@NonNull
                                                          com.android.resources.ResourceFolderType type)
Returns a list of ResourceFolder for a specific ResourceFolderType.

Parameters:
type - The ResourceFolderType

getAvailableResourceTypes

@NonNull
public java.util.List<com.android.resources.ResourceType> getAvailableResourceTypes()

getResourceItemsOfType

@NonNull
public java.util.Collection<ResourceItem> getResourceItemsOfType(@NonNull
                                                                         com.android.resources.ResourceType type)
Returns a list of ResourceItem matching a given ResourceType.

Parameters:
type - the type of the resource items to return
Returns:
a non null collection of resource items

hasResourcesOfType

public boolean hasResourcesOfType(@NonNull
                                  com.android.resources.ResourceType type)
Returns whether the repository has resources of a given ResourceType.

Parameters:
type - the type of resource to check.
Returns:
true if the repository contains resources of the given type, false otherwise.

getResourceFolder

@Nullable
public ResourceFolder getResourceFolder(@NonNull
                                                 com.android.io.IAbstractFolder folder)
Returns the ResourceFolder associated with a IAbstractFolder.

Parameters:
folder - The IAbstractFolder object.
Returns:
the ResourceFolder or null if it was not found.

getMatchingFile

@Nullable
public ResourceFile getMatchingFile(@NonNull
                                             java.lang.String name,
                                             @NonNull
                                             com.android.resources.ResourceFolderType type,
                                             @NonNull
                                             FolderConfiguration config)
Returns the ResourceFile matching the given name, ResourceFolderType and configuration.

This only works with files generating one resource named after the file (for instance, layouts, bitmap based drawable, xml, anims).

Parameters:
name - the resource name or file name
type - the folder type search for
config - the folder configuration to match for
Returns:
the matching file or null if no match was found.

getMatchingFile

@Nullable
public ResourceFile getMatchingFile(@NonNull
                                             java.lang.String name,
                                             @NonNull
                                             com.android.resources.ResourceType type,
                                             @NonNull
                                             FolderConfiguration config)
Returns the ResourceFile matching the given name, ResourceType and configuration.

This only works with files generating one resource named after the file (for instance, layouts, bitmap based drawable, xml, anims).

Parameters:
name - the resource name or file name
type - the folder type search for
config - the folder configuration to match for
Returns:
the matching file or null if no match was found.

findResourceFile

@Nullable
protected ResourceFile findResourceFile(@NonNull
                                                 java.io.File file)
Looks up the ResourceFile for the given File, if possible

Parameters:
file - the file
Returns:
the corresponding ResourceFile, or null if not a known ResourceFile

getSourceFiles

@Nullable
public java.util.List<ResourceFile> getSourceFiles(@NonNull
                                                            com.android.resources.ResourceType type,
                                                            @NonNull
                                                            java.lang.String name,
                                                            @Nullable
                                                            FolderConfiguration referenceConfig)
Returns the list of source files for a given resource. Optionally, if a FolderConfiguration is given, then only the best match for this config is returned.

Parameters:
type - the type of the resource.
name - the name of the resource.
referenceConfig - an optional config for which only the best match will be returned.
Returns:
a list of files generating this resource or null if it was not found.

getConfiguredResources

@NonNull
public java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,com.android.ide.common.rendering.api.ResourceValue>> getConfiguredResources(@NonNull
                                                                                                                                                                           FolderConfiguration referenceConfig)
Returns the resources values matching a given FolderConfiguration.

Parameters:
referenceConfig - the configuration that each value must match.
Returns:
a map with guaranteed to contain an entry for each ResourceType

doGetConfiguredResources

@NonNull
protected final java.util.Map<com.android.resources.ResourceType,java.util.Map<java.lang.String,com.android.ide.common.rendering.api.ResourceValue>> doGetConfiguredResources(@NonNull
                                                                                                                                                                                      FolderConfiguration referenceConfig)
Returns the resources values matching a given FolderConfiguration for the current project.

Parameters:
referenceConfig - the configuration that each value must match.
Returns:
a map with guaranteed to contain an entry for each ResourceType

getLanguages

@NonNull
public java.util.SortedSet<java.lang.String> getLanguages()
Returns the sorted list of languages used in the resources.


getRegions

@NonNull
public java.util.SortedSet<java.lang.String> getRegions(@NonNull
                                                                java.lang.String currentLanguage)
Returns the sorted list of regions used in the resources with the given language.

Parameters:
currentLanguage - the current language the region must be associated with.

loadResources

public void loadResources()
Loads the resources.


removeFile

protected void removeFile(@NonNull
                          java.util.Collection<com.android.resources.ResourceType> types,
                          @NonNull
                          ResourceFile file)

removeFile

protected void removeFile(@NonNull
                          com.android.resources.ResourceType type,
                          @NonNull
                          ResourceFile file)

postUpdateCleanUp

public void postUpdateCleanUp()
Cleans up the repository of resource items that have no source file anymore.