blob: e94c0e8feeee479b1a302a9728cabc776c18b583 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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//
18// Asset management class. AssetManager objects are thread-safe.
19//
20#ifndef __LIBS_ASSETMANAGER_H
21#define __LIBS_ASSETMANAGER_H
22
23#include <utils/Asset.h>
24#include <utils/AssetDir.h>
25#include <utils/KeyedVector.h>
26#include <utils/String8.h>
27#include <utils/Vector.h>
28#include <utils/String16.h>
29#include <utils/ZipFileRO.h>
30#include <utils/threads.h>
31
32namespace android {
33
34class Asset; // fwd decl for things that include Asset.h first
35class ResTable;
36struct ResTable_config;
37
38/*
39 * Every application that uses assets needs one instance of this. A
40 * single instance may be shared across multiple threads, and a single
41 * thread may have more than one instance (the latter is discouraged).
42 *
43 * The purpose of the AssetManager is to create Asset objects. To do
44 * this efficiently it may cache information about the locations of
45 * files it has seen. This can be controlled with the "cacheMode"
46 * argument.
47 *
48 * The asset hierarchy may be examined like a filesystem, using
49 * AssetDir objects to peruse a single directory.
50 */
51class AssetManager {
52public:
53 typedef enum CacheMode {
54 CACHE_UNKNOWN = 0,
55 CACHE_OFF, // don't try to cache file locations
56 CACHE_DEFER, // construct cache as pieces are needed
57 //CACHE_SCAN, // scan full(!) asset hierarchy at init() time
58 } CacheMode;
59
60 AssetManager(CacheMode cacheMode = CACHE_OFF);
61 virtual ~AssetManager(void);
62
63 static int32_t getGlobalCount();
64
65 /*
66 * Add a new source for assets. This can be called multiple times to
67 * look in multiple places for assets. It can be either a directory (for
68 * finding assets as raw files on the disk) or a ZIP file. This newly
69 * added asset path will be examined first when searching for assets,
70 * before any that were previously added.
71 *
72 * Returns "true" on success, "false" on failure. If 'cookie' is non-NULL,
73 * then on success, *cookie is set to the value corresponding to the
74 * newly-added asset source.
75 */
76 bool addAssetPath(const String8& path, void** cookie);
77
78 /*
79 * Convenience for adding the standard system assets. Uses the
80 * ANDROID_ROOT environment variable to find them.
81 */
82 bool addDefaultAssets();
83
84 /*
85 * Iterate over the asset paths in this manager. (Previously
86 * added via addAssetPath() and addDefaultAssets().) On first call,
87 * 'cookie' must be NULL, resulting in the first cookie being returned.
88 * Each next cookie will be returned there-after, until NULL indicating
89 * the end has been reached.
90 */
91 void* nextAssetPath(void* cookie) const;
92
93 /*
94 * Return an asset path in the manager. 'which' must be between 0 and
95 * countAssetPaths().
96 */
97 String8 getAssetPath(void* cookie) const;
98
99 /*
100 * Set the current locale and vendor. The locale can change during
101 * the lifetime of an AssetManager if the user updates the device's
102 * language setting. The vendor is less likely to change.
103 *
104 * Pass in NULL to indicate no preference.
105 */
106 void setLocale(const char* locale);
107 void setVendor(const char* vendor);
108
109 /*
110 * Choose screen orientation for resources values returned.
111 */
112 void setConfiguration(const ResTable_config& config, const char* locale = NULL);
113
114 typedef Asset::AccessMode AccessMode; // typing shortcut
115
116 /*
117 * Open an asset.
118 *
119 * This will search through locale-specific and vendor-specific
120 * directories and packages to find the file.
121 *
122 * The object returned does not depend on the AssetManager. It should
123 * be freed by calling Asset::close().
124 */
125 Asset* open(const char* fileName, AccessMode mode);
126
127 /*
128 * Open a non-asset file as an asset.
129 *
130 * This is for opening files that are included in an asset package
131 * but aren't assets. These sit outside the usual "locale/vendor"
132 * path hierarchy, and will not be seen by "AssetDir" or included
133 * in our filename cache.
134 */
135 Asset* openNonAsset(const char* fileName, AccessMode mode);
136
137 /*
138 * Explicit non-asset file. The file explicitly named by the cookie (the
139 * resource set to look in) and fileName will be opened and returned.
140 */
141 Asset* openNonAsset(void* cookie, const char* fileName, AccessMode mode);
142
143 /*
144 * Open a directory within the asset hierarchy.
145 *
146 * The contents of the directory are an amalgam of vendor-specific,
147 * locale-specific, and generic assets stored loosely or in asset
148 * packages. Depending on the cache setting and previous accesses,
149 * this call may incur significant disk overhead.
150 *
151 * To open the top-level directory, pass in "".
152 */
153 AssetDir* openDir(const char* dirName);
154
155 /*
156 * Get the type of a file in the asset hierarchy. They will either
157 * be "regular" or "directory". [Currently only works for "regular".]
158 *
159 * Can also be used as a quick test for existence of a file.
160 */
161 FileType getFileType(const char* fileName);
162
163 /*
164 * Return the complete resource table to find things in the package.
165 */
166 const ResTable& getResources(bool required = true) const;
167
168 /*
169 * Discard cached filename information. This only needs to be called
170 * if somebody has updated the set of "loose" files, and we want to
171 * discard our cached notion of what's where.
172 */
173 void purge(void) { purgeFileNameCacheLocked(); }
174
175 /*
176 * Return true if the files this AssetManager references are all
177 * up-to-date (have not been changed since it was created). If false
178 * is returned, you will need to create a new AssetManager to get
179 * the current data.
180 */
181 bool isUpToDate();
182
183 /**
184 * Get the known locales for this asset manager object.
185 */
186 void getLocales(Vector<String8>* locales) const;
187
188private:
189 struct asset_path
190 {
191 String8 path;
192 FileType type;
193 };
194
195 Asset* openInPathLocked(const char* fileName, AccessMode mode,
196 const asset_path& path);
197 Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode,
198 const asset_path& path);
199 Asset* openInLocaleVendorLocked(const char* fileName, AccessMode mode,
200 const asset_path& path, const char* locale, const char* vendor);
201 String8 createPathNameLocked(const asset_path& path, const char* locale,
202 const char* vendor);
203 String8 createPathNameLocked(const asset_path& path, const char* rootDir);
204 String8 createZipSourceNameLocked(const String8& zipFileName,
205 const String8& dirName, const String8& fileName);
206
207 ZipFileRO* getZipFileLocked(const asset_path& path);
208 Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode);
209 Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile,
210 const ZipEntryRO entry, AccessMode mode, const String8& entryName);
211
212 bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
213 const asset_path& path, const char* rootDir, const char* dirName);
214 SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path);
215 bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
216 const asset_path& path, const char* rootDir, const char* dirName);
217 void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
218 const SortedVector<AssetDir::FileInfo>* pContents);
219
220 void loadFileNameCacheLocked(void);
221 void fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
222 const char* dirName);
223 bool fncScanAndMergeDirLocked(
224 SortedVector<AssetDir::FileInfo>* pMergedInfo,
225 const asset_path& path, const char* locale, const char* vendor,
226 const char* dirName);
227 void purgeFileNameCacheLocked(void);
228
229 const ResTable* getResTable(bool required = true) const;
230 void setLocaleLocked(const char* locale);
231 void updateResourceParamsLocked() const;
232
233 class SharedZip : public RefBase {
234 public:
235 static sp<SharedZip> get(const String8& path);
236
237 ZipFileRO* getZip();
238
239 Asset* getResourceTableAsset();
240 Asset* setResourceTableAsset(Asset* asset);
241
242 bool isUpToDate();
243
244 protected:
245 ~SharedZip();
246
247 private:
248 SharedZip(const String8& path, time_t modWhen);
249 SharedZip(); // <-- not implemented
250
251 String8 mPath;
252 ZipFileRO* mZipFile;
253 time_t mModWhen;
254
255 Asset* mResourceTableAsset;
256
257 static Mutex gLock;
258 static DefaultKeyedVector<String8, wp<SharedZip> > gOpen;
259 };
260
261 /*
262 * Manage a set of Zip files. For each file we need a pointer to the
263 * ZipFile and a time_t with the file's modification date.
264 *
265 * We currently only have two zip files (current app, "common" app).
266 * (This was originally written for 8, based on app/locale/vendor.)
267 */
268 class ZipSet {
269 public:
270 ZipSet(void);
271 ~ZipSet(void);
272
273 /*
274 * Return a ZipFileRO structure for a ZipFileRO with the specified
275 * parameters.
276 */
277 ZipFileRO* getZip(const String8& path);
278
279 Asset* getZipResourceTable(const String8& path);
280 Asset* setZipResourceTable(const String8& path, Asset* asset);
281
282 // generate path, e.g. "common/en-US-noogle.zip"
283 static String8 getPathName(const char* path);
284
285 bool isUpToDate();
286
287 private:
288 void closeZip(int idx);
289
290 int getIndex(const String8& zip) const;
291 mutable Vector<String8> mZipPath;
292 mutable Vector<sp<SharedZip> > mZipFile;
293 };
294
295 // Protect all internal state.
296 mutable Mutex mLock;
297
298 ZipSet mZipSet;
299
300 Vector<asset_path> mAssetPaths;
301 char* mLocale;
302 char* mVendor;
303
304 mutable ResTable* mResources;
305 ResTable_config* mConfig;
306
307 /*
308 * Cached data for "loose" files. This lets us avoid poking at the
309 * filesystem when searching for loose assets. Each entry is the
310 * "extended partial" path, e.g. "default/default/foo/bar.txt". The
311 * full set of files is present, including ".EXCLUDE" entries.
312 *
313 * We do not cache directory names. We don't retain the ".gz",
314 * because to our clients "foo" and "foo.gz" both look like "foo".
315 */
316 CacheMode mCacheMode; // is the cache enabled?
317 bool mCacheValid; // clear when locale or vendor changes
318 SortedVector<AssetDir::FileInfo> mCache;
319};
320
321}; // namespace android
322
323#endif // __LIBS_ASSETMANAGER_H