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