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