diff options
Diffstat (limited to 'ide_common')
4 files changed, 94 insertions, 10 deletions
diff --git a/ide_common/src/com/android/ide/common/resources/IdGeneratingResourceFile.java b/ide_common/src/com/android/ide/common/resources/IdGeneratingResourceFile.java index fa8d0e7..6706715 100644 --- a/ide_common/src/com/android/ide/common/resources/IdGeneratingResourceFile.java +++ b/ide_common/src/com/android/ide/common/resources/IdGeneratingResourceFile.java @@ -31,6 +31,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Set; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; @@ -88,8 +89,8 @@ public final class IdGeneratingResourceFile extends ResourceFile @Override protected void update() { - // remove this file from all existing ResourceItem. - getFolder().getRepository().removeFile(mResourceTypeList, this); + // Copy the previous list of ID names + Set<String> oldIdNames = mIdResources.keySet(); // reset current content. mIdResources.clear(); @@ -97,14 +98,21 @@ public final class IdGeneratingResourceFile extends ResourceFile // need to parse the file and find the IDs. parseFileForIds(); - // Notify the repository about any changes - updateResourceItems(); + // We only need to update the repository if our IDs have changed + if (oldIdNames.equals(mIdResources.keySet()) == false) { + updateResourceItems(); + } } @Override protected void dispose() { + ResourceRepository repository = getRepository(); + // Remove declarations from this file from the repository - getFolder().getRepository().removeFile(mResourceTypeList, this); + repository.removeFile(mResourceTypeList, this); + + // Ask for an ID refresh since we'll be taking away ID generating items + repository.markForIdRefresh(); } @Override @@ -155,6 +163,9 @@ public final class IdGeneratingResourceFile extends ResourceFile private void updateResourceItems() { ResourceRepository repository = getRepository(); + // remove this file from all existing ResourceItem. + repository.removeFile(mResourceTypeList, this); + // First add this as a layout file ResourceItem item = repository.getResourceItem(mFileType, mFileName); item.add(this); @@ -165,6 +176,9 @@ public final class IdGeneratingResourceFile extends ResourceFile // add this file to the list of files generating ID resources. item.add(this); } + + // Ask the repository for an ID refresh + repository.markForIdRefresh(); } /** diff --git a/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java b/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java index 6d8ca0a..b3e35d9 100644 --- a/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java +++ b/ide_common/src/com/android/ide/common/resources/MultiResourceFile.java @@ -54,6 +54,10 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou super(file, folder); } + // Boolean flag to track whether a named element has been added or removed, thus requiring + // a new ID table to be generated + private boolean mNeedIdRefresh; + @Override protected void load() { // need to parse the file and find the content. @@ -62,14 +66,21 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou // create new ResourceItems for the new content. mResourceTypeList = Collections.unmodifiableCollection(mResourceItems.keySet()); + // We need an ID generation step + mNeedIdRefresh = true; + // create/update the resource items. updateResourceItems(); } @Override protected void update() { - // remove this file from all existing ResourceItem. - getFolder().getRepository().removeFile(mResourceTypeList, this); + // Reset the ID generation flag + mNeedIdRefresh = false; + + // Copy the previous version of our list of ResourceItems and types + Map<ResourceType, Map<String, ResourceValue>> oldResourceItems + = new EnumMap<ResourceType, Map<String, ResourceValue>>(mResourceItems); // reset current content. mResourceItems.clear(); @@ -80,14 +91,34 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou // create new ResourceItems for the new content. mResourceTypeList = Collections.unmodifiableCollection(mResourceItems.keySet()); + // Check to see if any names have changed. If so, mark the flag so updateResourceItems + // can notify the ResourceRepository that an ID refresh is needed + if (oldResourceItems.keySet().equals(mResourceItems.keySet())) { + for (ResourceType type : mResourceTypeList) { + // We just need to check the names of the items. + // If there are new or removed names then we'll have to regenerate IDs + if (mResourceItems.get(type).keySet() + .equals(oldResourceItems.get(type).keySet()) == false) { + mNeedIdRefresh = true; + } + } + } else { + // If our type list is different, obviously the names will be different + mNeedIdRefresh = true; + } // create/update the resource items. updateResourceItems(); } @Override protected void dispose() { + ResourceRepository repository = getRepository(); + // only remove this file from all existing ResourceItem. - getFolder().getRepository().removeFile(mResourceTypeList, this); + repository.removeFile(mResourceTypeList, this); + + // We'll need an ID refresh because we deleted items + repository.markForIdRefresh(); // don't need to touch the content, it'll get reclaimed as this objects disappear. // In the mean time other objects may need to access it. @@ -106,6 +137,10 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou private void updateResourceItems() { ResourceRepository repository = getRepository(); + + // remove this file from all existing ResourceItem. + repository.removeFile(mResourceTypeList, this); + for (ResourceType type : mResourceTypeList) { Map<String, ResourceValue> list = mResourceItems.get(type); @@ -119,6 +154,11 @@ public final class MultiResourceFile extends ResourceFile implements IValueResou } } } + + // If we need an ID refresh, ask the repository for that now + if (mNeedIdRefresh) { + repository.markForIdRefresh(); + } } /** diff --git a/ide_common/src/com/android/ide/common/resources/ResourceRepository.java b/ide_common/src/com/android/ide/common/resources/ResourceRepository.java index fa533cb..4af4a1a 100644 --- a/ide_common/src/com/android/ide/common/resources/ResourceRepository.java +++ b/ide_common/src/com/android/ide/common/resources/ResourceRepository.java @@ -68,6 +68,7 @@ public abstract class ResourceRepository { protected final IntArrayWrapper mWrapper = new IntArrayWrapper(null); + private boolean mNeedsIdRefresh; /** * Makes a resource repository @@ -195,6 +196,29 @@ public abstract class ResourceRepository { protected abstract ResourceItem createResourceItem(String name); /** + * Sets a flag which determines whether aapt needs to be run to regenerate resource IDs + */ + protected void markForIdRefresh() { + mNeedsIdRefresh = true; + } + + /** + * Returns whether this repository has been marked as "dirty"; if one or more of the constituent + * files have declared that the resource item names that they provide have changed. + */ + public boolean needsIdRefresh() { + return mNeedsIdRefresh; + } + + /** + * Indicates that the resources IDs have been regenerated, so the repository is now in a clean + * state + */ + public void setIdsRefreshed() { + mNeedsIdRefresh = false; + } + + /** * Processes a folder and adds it to the list of existing folders. * @param folder the folder to process * @return the ResourceFolder created from this folder, or null if the process failed. diff --git a/ide_common/src/com/android/ide/common/resources/SingleResourceFile.java b/ide_common/src/com/android/ide/common/resources/SingleResourceFile.java index 9c8977e..b589b35 100644 --- a/ide_common/src/com/android/ide/common/resources/SingleResourceFile.java +++ b/ide_common/src/com/android/ide/common/resources/SingleResourceFile.java @@ -41,8 +41,8 @@ public class SingleResourceFile extends ResourceFile { sParserFactory.setNamespaceAware(true); } - private String mResourceName; - private ResourceType mType; + private final String mResourceName; + private final ResourceType mType; private ResourceValue mValue; public SingleResourceFile(IAbstractFile file, ResourceFolder folder) { @@ -79,6 +79,9 @@ public class SingleResourceFile extends ResourceFile { // add this file to the list of files generating this resource item. item.add(this); + + // Ask for an ID refresh since we're adding an item that will generate an ID + getRepository().markForIdRefresh(); } @Override @@ -92,6 +95,9 @@ public class SingleResourceFile extends ResourceFile { // only remove this file from the existing ResourceItem. getFolder().getRepository().removeFile(mType, this); + // Ask for an ID refresh since we're removing an item that previously generated an ID + getRepository().markForIdRefresh(); + // don't need to touch the content, it'll get reclaimed as this objects disappear. // In the mean time other objects may need to access it. } |