| 1 |
/* |
| 2 |
* Copyright (C) 2011 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.ide.common.resources; |
| 18 |
|
| 19 |
import com.android.ide.common.rendering.api.DensityBasedResourceValue; |
| 20 |
import com.android.ide.common.rendering.api.ResourceValue; |
| 21 |
import com.android.ide.common.resources.ValueResourceParser.IValueResourceRepository; |
| 22 |
import com.android.ide.common.resources.configuration.DensityQualifier; |
| 23 |
import com.android.ide.common.resources.configuration.ResourceQualifier; |
| 24 |
import com.android.io.IAbstractFile; |
| 25 |
import com.android.io.StreamException; |
| 26 |
import com.android.resources.ResourceType; |
| 27 |
|
| 28 |
import java.io.IOException; |
| 29 |
import java.util.Collection; |
| 30 |
import java.util.EnumSet; |
| 31 |
import java.util.HashMap; |
| 32 |
import java.util.HashSet; |
| 33 |
import java.util.Map; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
/** |
| 37 |
* Represents a resource file that also generates ID resources. |
| 38 |
* <p> |
| 39 |
* This is typically an XML file in res/layout or res/menu |
| 40 |
*/ |
| 41 |
public final class IdGeneratingResourceFile extends ResourceFile |
| 42 |
implements IValueResourceRepository { |
| 43 |
|
| 44 |
private final Map<String, ResourceValue> mIdResources = |
| 45 |
new HashMap<String, ResourceValue>(); |
| 46 |
|
| 47 |
private final Collection<ResourceType> mResourceTypeList; |
| 48 |
|
| 49 |
private final String mFileName; |
| 50 |
|
| 51 |
private final ResourceType mFileType; |
| 52 |
|
| 53 |
private final ResourceValue mFileValue; |
| 54 |
|
| 55 |
public IdGeneratingResourceFile(IAbstractFile file, ResourceFolder folder, ResourceType type) { |
| 56 |
super(file, folder); |
| 57 |
|
| 58 |
mFileType = type; |
| 59 |
|
| 60 |
// Set up our resource types |
| 61 |
mResourceTypeList = EnumSet.of(mFileType, ResourceType.ID); |
| 62 |
|
| 63 |
// compute the resource name |
| 64 |
mFileName = getFileName(type); |
| 65 |
|
| 66 |
// Get the resource value of this file as a whole layout |
| 67 |
mFileValue = getFileValue(file, folder); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
protected void load(ScanningContext context) { |
| 72 |
// Parse the file and look for @+id/ entries |
| 73 |
parseFileForIds(context); |
| 74 |
|
| 75 |
// create the resource items in the repository |
| 76 |
updateResourceItems(context); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
protected void update(ScanningContext context) { |
| 81 |
// Copy the previous list of ID names |
| 82 |
Set<String> oldIdNames = new HashSet<String>(mIdResources.keySet()); |
| 83 |
|
| 84 |
// reset current content. |
| 85 |
mIdResources.clear(); |
| 86 |
|
| 87 |
// need to parse the file and find the IDs. |
| 88 |
if (!parseFileForIds(context)) { |
| 89 |
context.requestFullAapt(); |
| 90 |
// Continue through to updating the resource item here since it |
| 91 |
// will make for example layout rendering more accurate until |
| 92 |
// aapt is re-run |
| 93 |
} |
| 94 |
|
| 95 |
// We only need to update the repository if our IDs have changed |
| 96 |
Set<String> keySet = mIdResources.keySet(); |
| 97 |
assert keySet != oldIdNames; |
| 98 |
if (oldIdNames.equals(keySet) == false) { |
| 99 |
updateResourceItems(context); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
protected void dispose(ScanningContext context) { |
| 105 |
ResourceRepository repository = getRepository(); |
| 106 |
|
| 107 |
// Remove declarations from this file from the repository |
| 108 |
repository.removeFile(mResourceTypeList, this); |
| 109 |
|
| 110 |
// Ask for an ID refresh since we'll be taking away ID generating items |
| 111 |
context.requestFullAapt(); |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public Collection<ResourceType> getResourceTypes() { |
| 116 |
return mResourceTypeList; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean hasResources(ResourceType type) { |
| 121 |
return (type == mFileType) || (type == ResourceType.ID && !mIdResources.isEmpty()); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public ResourceValue getValue(ResourceType type, String name) { |
| 126 |
// Check to see if they're asking for one of the right types: |
| 127 |
if (type != mFileType && type != ResourceType.ID) { |
| 128 |
return null; |
| 129 |
} |
| 130 |
|
| 131 |
// If they're looking for a resource of this type with this name give them the whole file |
| 132 |
if (type == mFileType && name.equals(mFileName)) { |
| 133 |
return mFileValue; |
| 134 |
} else { |
| 135 |
// Otherwise try to return them an ID |
| 136 |
// the map will return null if it's not found |
| 137 |
return mIdResources.get(name); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Looks through the file represented for Ids and adds them to |
| 143 |
* our id repository |
| 144 |
* |
| 145 |
* @return true if parsing succeeds and false if it fails |
| 146 |
*/ |
| 147 |
private boolean parseFileForIds(ScanningContext context) { |
| 148 |
IdResourceParser parser = new IdResourceParser(this, context, isFramework()); |
| 149 |
try { |
| 150 |
IAbstractFile file = getFile(); |
| 151 |
return parser.parse(mFileType, file.getOsLocation(), file.getContents()); |
| 152 |
} catch (IOException e) { |
| 153 |
// Pass |
| 154 |
} catch (StreamException e) { |
| 155 |
// Pass |
| 156 |
} |
| 157 |
|
| 158 |
return false; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Add the resources represented by this file to the repository |
| 163 |
*/ |
| 164 |
private void updateResourceItems(ScanningContext context) { |
| 165 |
ResourceRepository repository = getRepository(); |
| 166 |
|
| 167 |
// remove this file from all existing ResourceItem. |
| 168 |
repository.removeFile(mResourceTypeList, this); |
| 169 |
|
| 170 |
// First add this as a layout file |
| 171 |
ResourceItem item = repository.getResourceItem(mFileType, mFileName); |
| 172 |
item.add(this); |
| 173 |
|
| 174 |
// Now iterate through our IDs and add |
| 175 |
for (String idName : mIdResources.keySet()) { |
| 176 |
item = repository.getResourceItem(ResourceType.ID, idName); |
| 177 |
// add this file to the list of files generating ID resources. |
| 178 |
item.add(this); |
| 179 |
} |
| 180 |
|
| 181 |
// Ask the repository for an ID refresh |
| 182 |
context.requestFullAapt(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns the resource value associated with this whole file as a layout resource |
| 187 |
* @param file the file handler that represents this file |
| 188 |
* @param folder the folder this file is under |
| 189 |
* @return a resource value associated with this layout |
| 190 |
*/ |
| 191 |
private ResourceValue getFileValue(IAbstractFile file, ResourceFolder folder) { |
| 192 |
// test if there's a density qualifier associated with the resource |
| 193 |
DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier(); |
| 194 |
|
| 195 |
ResourceValue value; |
| 196 |
if (!ResourceQualifier.isValid(qualifier)) { |
| 197 |
value = new ResourceValue(mFileType, mFileName, |
| 198 |
file.getOsLocation(), isFramework()); |
| 199 |
} else { |
| 200 |
value = new DensityBasedResourceValue( |
| 201 |
mFileType, mFileName, |
| 202 |
file.getOsLocation(), |
| 203 |
qualifier.getValue(), |
| 204 |
isFramework()); |
| 205 |
} |
| 206 |
return value; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Returns the name of this resource. |
| 212 |
*/ |
| 213 |
private String getFileName(ResourceType type) { |
| 214 |
// get the name from the filename. |
| 215 |
String name = getFile().getName(); |
| 216 |
|
| 217 |
int pos = name.indexOf('.'); |
| 218 |
if (pos != -1) { |
| 219 |
name = name.substring(0, pos); |
| 220 |
} |
| 221 |
|
| 222 |
return name; |
| 223 |
} |
| 224 |
|
| 225 |
@Override |
| 226 |
public void addResourceValue(ResourceValue value) { |
| 227 |
// Just overwrite collisions. We're only interested in the unique |
| 228 |
// IDs declared |
| 229 |
mIdResources.put(value.getName(), value); |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
public boolean hasResourceValue(ResourceType type, String name) { |
| 234 |
if (type == ResourceType.ID) { |
| 235 |
return mIdResources.containsKey(name); |
| 236 |
} |
| 237 |
|
| 238 |
return false; |
| 239 |
} |
| 240 |
} |