• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.eclipse.adt.internal.resources.manager;
18 
19 import com.android.ide.common.rendering.api.ResourceValue;
20 import com.android.ide.common.resources.IntArrayWrapper;
21 import com.android.ide.common.resources.ResourceFolder;
22 import com.android.ide.common.resources.ResourceItem;
23 import com.android.ide.common.resources.ResourceRepository;
24 import com.android.ide.common.resources.configuration.FolderConfiguration;
25 import com.android.ide.eclipse.adt.internal.sdk.ProjectState;
26 import com.android.ide.eclipse.adt.internal.sdk.Sdk;
27 import com.android.ide.eclipse.adt.io.IFolderWrapper;
28 import com.android.resources.ResourceType;
29 import com.android.util.Pair;
30 
31 import org.eclipse.core.resources.IFolder;
32 import org.eclipse.core.resources.IProject;
33 
34 import java.util.EnumMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Map.Entry;
38 
39 /**
40  * Represents the resources of a project.
41  * On top of the regular {@link ResourceRepository} features it provides:
42  *<ul>
43  *<li>configured resources contain the resources coming from the libraries.</li>
44  *<li>resolution to and from resource integer (compiled value in R.java).</li>
45  *<li>handles resource integer for non existing values of type ID. This is used when rendering.</li>
46  *<li>layouts that have no been saved yet. This is handled by generating dynamic IDs
47  *       on the fly.</li>
48  *</ul>
49  */
50 public class ProjectResources extends ResourceRepository {
51     // project resources are defined as 0x7FXX#### where XX is the resource type (layout, drawable,
52     // etc...). Using FF as the type allows for 255 resource types before we get a collision
53     // which should be fine.
54     private final static int DYNAMIC_ID_SEED_START = 0x7fff0000;
55 
56     /** Map of (name, id) for resources of type {@link ResourceType#ID} coming from R.java */
57     private Map<ResourceType, Map<String, Integer>> mResourceValueMap;
58     /** Map of (id, [name, resType]) for all resources coming from R.java */
59     private Map<Integer, Pair<ResourceType, String>> mResIdValueToNameMap;
60     /** Map of (int[], name) for styleable resources coming from R.java */
61     private Map<IntArrayWrapper, String> mStyleableValueToNameMap;
62 
63     private final DynamicIdMap mDynamicIdMap = new DynamicIdMap(DYNAMIC_ID_SEED_START);
64 
65     private final IProject mProject;
66 
67     /**
68      * Makes a ProjectResources for a given <var>project</var>.
69      * @param project the project.
70      */
ProjectResources(IProject project)71     public ProjectResources(IProject project) {
72         super(false /*isFrameworkRepository*/);
73         mProject = project;
74     }
75 
76     /**
77      * Returns the resources values matching a given {@link FolderConfiguration}, this will
78      * include library dependency.
79      *
80      * @param referenceConfig the configuration that each value must match.
81      * @return a map with guaranteed to contain an entry for each {@link ResourceType}
82      */
83     @Override
getConfiguredResources( FolderConfiguration referenceConfig)84     public Map<ResourceType, Map<String, ResourceValue>> getConfiguredResources(
85             FolderConfiguration referenceConfig) {
86 
87         Map<ResourceType, Map<String, ResourceValue>> resultMap =
88             new EnumMap<ResourceType, Map<String, ResourceValue>>(ResourceType.class);
89 
90         // if the project contains libraries, we need to add the libraries resources here
91         // so that they are accessible to the layout rendering.
92         if (mProject != null) {
93             ProjectState state = Sdk.getProjectState(mProject);
94             if (state != null) {
95                 List<IProject> libraries = state.getFullLibraryProjects();
96 
97                 ResourceManager resMgr = ResourceManager.getInstance();
98 
99                 // because aapt put all the library in their order in this array, the first
100                 // one will have priority over the 2nd one. So it's better to loop in the inverse
101                 // order and fill the map with resources that will be overwritten by higher
102                 // priority resources
103                 for (int i = libraries.size() - 1 ; i >= 0 ; i--) {
104                     IProject library = libraries.get(i);
105 
106                     ProjectResources libRes = resMgr.getProjectResources(library);
107                     if (libRes != null) {
108                         // get the library resources, and only the library, not the dependencies
109                         // so call doGetConfiguredResources() directly.
110                         Map<ResourceType, Map<String, ResourceValue>> libMap =
111                                 libRes.doGetConfiguredResources(referenceConfig);
112 
113                         // we don't want to simply replace the whole map, but instead merge the
114                         // content of any sub-map
115                         for (Entry<ResourceType, Map<String, ResourceValue>> libEntry :
116                                 libMap.entrySet()) {
117 
118                             // get the map currently in the result map for this resource type
119                             Map<String, ResourceValue> tempMap = resultMap.get(libEntry.getKey());
120                             if (tempMap == null) {
121                                 // since there's no current map for this type, just add the map
122                                 // directly coming from the library resources
123                                 resultMap.put(libEntry.getKey(), libEntry.getValue());
124                             } else {
125                                 // already a map for this type. add the resources from the
126                                 // library, this will override existing value, which is why
127                                 // we loop in a specific library order.
128                                 tempMap.putAll(libEntry.getValue());
129                             }
130                         }
131                     }
132                 }
133             }
134         }
135 
136         // now the project resources themselves.
137         Map<ResourceType, Map<String, ResourceValue>> thisProjectMap =
138                 doGetConfiguredResources(referenceConfig);
139 
140         // now merge the maps.
141         for (Entry<ResourceType, Map<String, ResourceValue>> entry : thisProjectMap.entrySet()) {
142             ResourceType type = entry.getKey();
143             Map<String, ResourceValue> typeMap = resultMap.get(type);
144             if (typeMap == null) {
145                 resultMap.put(type, entry.getValue());
146             } else {
147                 typeMap.putAll(entry.getValue());
148             }
149         }
150 
151         return resultMap;
152     }
153 
154     /**
155      * Returns the {@link ResourceFolder} associated with a {@link IFolder}.
156      * @param folder The {@link IFolder} object.
157      * @return the {@link ResourceFolder} or null if it was not found.
158      *
159      * @see ResourceRepository#getResourceFolder(com.android.io.IAbstractFolder)
160      */
getResourceFolder(IFolder folder)161     public ResourceFolder getResourceFolder(IFolder folder) {
162         return getResourceFolder(new IFolderWrapper(folder));
163     }
164 
165     /**
166      * Resolves a compiled resource id into the resource name and type
167      * @param id the resource integer id.
168      * @return a {@link Pair} of 2 strings { name, type } or null if the id could not be resolved
169      */
resolveResourceId(int id)170     public Pair<ResourceType, String> resolveResourceId(int id) {
171         Pair<ResourceType, String> result = null;
172         if (mResIdValueToNameMap != null) {
173             result = mResIdValueToNameMap.get(id);
174         }
175 
176         if (result == null) {
177             synchronized (mDynamicIdMap) {
178                 result = mDynamicIdMap.resolveId(id);
179             }
180         }
181 
182         return result;
183     }
184 
185     /**
186      * Resolves a compiled styleable id of type int[] into the styleable name.
187      */
resolveStyleable(int[] id)188     public String resolveStyleable(int[] id) {
189         if (mStyleableValueToNameMap != null) {
190             mWrapper.set(id);
191             return mStyleableValueToNameMap.get(mWrapper);
192         }
193 
194         return null;
195     }
196 
197     /**
198      * Returns the integer id of a resource given its type and name.
199      * <p/>If the resource is of type {@link ResourceType#ID} and does not exist in the
200      * internal map, then new id values are dynamically generated (and stored so that queries
201      * with the same names will return the same value).
202      */
getResourceId(ResourceType type, String name)203     public Integer getResourceId(ResourceType type, String name) {
204         Integer result = null;
205         if (mResourceValueMap != null) {
206             Map<String, Integer> map = mResourceValueMap.get(type);
207             if (map != null) {
208                 result = map.get(name);
209             }
210         }
211 
212         if (result == null) {
213             synchronized (mDynamicIdMap) {
214                 result = mDynamicIdMap.getId(type, name);
215             }
216         }
217 
218         return result;
219     }
220 
221     /**
222      * Resets the list of dynamic Ids. This list is used by
223      * {@link #getResourceId(String, String)} when the resource query is an ID that doesn't
224      * exist (for example for ID automatically generated in layout files that are not saved yet.)
225      * <p/>This method resets those dynamic ID and must be called whenever the actual list of IDs
226      * change.
227      */
resetDynamicIds()228     public void resetDynamicIds() {
229         synchronized (mDynamicIdMap) {
230             mDynamicIdMap.reset(DYNAMIC_ID_SEED_START);
231         }
232     }
233 
234     @Override
createResourceItem(String name)235     protected ResourceItem createResourceItem(String name) {
236         return new ResourceItem(name);
237     }
238 
239     /**
240      * Sets compiled resource information.
241      *
242      * @param resIdValueToNameMap a map of compiled resource id to resource name.
243      *    The map is acquired by the {@link ProjectResources} object.
244      * @param styleableValueMap a map of (int[], name) for the styleable information. The map is
245      *    acquired by the {@link ProjectResources} object.
246      * @param resourceValueMap a map of (name, id) for resources of type {@link ResourceType#ID}.
247      *    The list is acquired by the {@link ProjectResources} object.
248      */
setCompiledResources(Map<Integer, Pair<ResourceType, String>> resIdValueToNameMap, Map<IntArrayWrapper, String> styleableValueMap, Map<ResourceType, Map<String, Integer>> resourceValueMap)249     void setCompiledResources(Map<Integer, Pair<ResourceType, String>> resIdValueToNameMap,
250             Map<IntArrayWrapper, String> styleableValueMap,
251             Map<ResourceType, Map<String, Integer>> resourceValueMap) {
252         mResourceValueMap = resourceValueMap;
253         mResIdValueToNameMap = resIdValueToNameMap;
254         mStyleableValueToNameMap = styleableValueMap;
255 
256         resetDynamicIds();
257     }
258 }
259