blob: 0b22802392603987bf96830419ece2ba091002da [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;
Mårten Kongstad48d22322014-01-31 14:43:27 +010073 static const char* IDMAP_BIN;
74 static const char* OVERLAY_DIR;
Jakub Adamek1c15c632016-09-23 09:07:11 +010075 /*
76 * If OVERLAY_SUBDIR_PROPERTY is set, search for runtime resource overlay
77 * APKs in OVERLAY_SUBDIR/<value of OVERLAY_SUBDIR_PROPERTY>/ rather than in
78 * OVERLAY_DIR.
79 */
80 static const char* OVERLAY_SUBDIR;
81 static const char* OVERLAY_SUBDIR_PROPERTY;
Mårten Kongstad48d22322014-01-31 14:43:27 +010082 static const char* TARGET_PACKAGE_NAME;
83 static const char* TARGET_APK_PATH;
84 static const char* IDMAP_DIR;
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 typedef enum CacheMode {
87 CACHE_UNKNOWN = 0,
88 CACHE_OFF, // don't try to cache file locations
89 CACHE_DEFER, // construct cache as pieces are needed
90 //CACHE_SCAN, // scan full(!) asset hierarchy at init() time
91 } CacheMode;
92
93 AssetManager(CacheMode cacheMode = CACHE_OFF);
94 virtual ~AssetManager(void);
95
96 static int32_t getGlobalCount();
97
98 /*
99 * Add a new source for assets. This can be called multiple times to
100 * look in multiple places for assets. It can be either a directory (for
101 * finding assets as raw files on the disk) or a ZIP file. This newly
102 * added asset path will be examined first when searching for assets,
Tao Baia6d7e3f2015-09-01 18:49:54 -0700103 * before any that were previously added, the assets are added as shared
104 * library if appAsLib is true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 *
106 * Returns "true" on success, "false" on failure. If 'cookie' is non-NULL,
107 * then on success, *cookie is set to the value corresponding to the
108 * newly-added asset source.
109 */
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800110 bool addAssetPath(const String8& path, int32_t* cookie,
111 bool appAsLib=false, bool isSystemAsset=false);
Mårten Kongstad48d22322014-01-31 14:43:27 +0100112 bool addOverlayPath(const String8& path, int32_t* cookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800114 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 * Convenience for adding the standard system assets. Uses the
116 * ANDROID_ROOT environment variable to find them.
117 */
118 bool addDefaultAssets();
119
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800120 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 * Iterate over the asset paths in this manager. (Previously
122 * added via addAssetPath() and addDefaultAssets().) On first call,
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000123 * 'cookie' must be 0, resulting in the first cookie being returned.
124 * Each next cookie will be returned there-after, until -1 indicating
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 * the end has been reached.
126 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000127 int32_t nextAssetPath(const int32_t cookie) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800129 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * Return an asset path in the manager. 'which' must be between 0 and
131 * countAssetPaths().
132 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000133 String8 getAssetPath(const int32_t cookie) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 /*
136 * Set the current locale and vendor. The locale can change during
137 * the lifetime of an AssetManager if the user updates the device's
138 * language setting. The vendor is less likely to change.
139 *
140 * Pass in NULL to indicate no preference.
141 */
142 void setLocale(const char* locale);
143 void setVendor(const char* vendor);
144
145 /*
146 * Choose screen orientation for resources values returned.
147 */
148 void setConfiguration(const ResTable_config& config, const char* locale = NULL);
149
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700150 void getConfiguration(ResTable_config* outConfig) const;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 typedef Asset::AccessMode AccessMode; // typing shortcut
153
154 /*
155 * Open an asset.
156 *
157 * This will search through locale-specific and vendor-specific
158 * directories and packages to find the file.
159 *
160 * The object returned does not depend on the AssetManager. It should
161 * be freed by calling Asset::close().
162 */
163 Asset* open(const char* fileName, AccessMode mode);
164
165 /*
166 * Open a non-asset file as an asset.
167 *
168 * This is for opening files that are included in an asset package
169 * but aren't assets. These sit outside the usual "locale/vendor"
170 * path hierarchy, and will not be seen by "AssetDir" or included
171 * in our filename cache.
172 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800173 Asset* openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174
175 /*
176 * Explicit non-asset file. The file explicitly named by the cookie (the
177 * resource set to look in) and fileName will be opened and returned.
178 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000179 Asset* openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180
181 /*
182 * Open a directory within the asset hierarchy.
183 *
184 * The contents of the directory are an amalgam of vendor-specific,
185 * locale-specific, and generic assets stored loosely or in asset
186 * packages. Depending on the cache setting and previous accesses,
187 * this call may incur significant disk overhead.
188 *
189 * To open the top-level directory, pass in "".
190 */
191 AssetDir* openDir(const char* dirName);
192
193 /*
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700194 * Open a directory within a particular path of the asset manager.
195 *
196 * The contents of the directory are an amalgam of vendor-specific,
197 * locale-specific, and generic assets stored loosely or in asset
198 * packages. Depending on the cache setting and previous accesses,
199 * this call may incur significant disk overhead.
200 *
201 * To open the top-level directory, pass in "".
202 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000203 AssetDir* openNonAssetDir(const int32_t cookie, const char* dirName);
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700204
205 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 * Get the type of a file in the asset hierarchy. They will either
207 * be "regular" or "directory". [Currently only works for "regular".]
208 *
209 * Can also be used as a quick test for existence of a file.
210 */
211 FileType getFileType(const char* fileName);
212
213 /*
214 * Return the complete resource table to find things in the package.
215 */
216 const ResTable& getResources(bool required = true) const;
217
218 /*
219 * Discard cached filename information. This only needs to be called
220 * if somebody has updated the set of "loose" files, and we want to
221 * discard our cached notion of what's where.
222 */
223 void purge(void) { purgeFileNameCacheLocked(); }
224
225 /*
226 * Return true if the files this AssetManager references are all
227 * up-to-date (have not been changed since it was created). If false
228 * is returned, you will need to create a new AssetManager to get
229 * the current data.
230 */
231 bool isUpToDate();
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 /**
234 * Get the known locales for this asset manager object.
235 */
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800236 void getLocales(Vector<String8>* locales, bool includeSystemLocales=true) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100238 /**
239 * Generate idmap data to translate resources IDs between a package and a
240 * corresponding overlay package.
241 */
242 bool createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackbornd9e385b2014-02-11 13:56:21 -0800243 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize);
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245private:
246 struct asset_path
247 {
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800248 asset_path() : path(""), type(kFileTypeRegular), idmap(""),
249 isSystemOverlay(false), isSystemAsset(false) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 String8 path;
251 FileType type;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100252 String8 idmap;
Mårten Kongstadcb7b63d2014-11-07 10:57:15 +0100253 bool isSystemOverlay;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800254 bool isSystemAsset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 };
256
257 Asset* openInPathLocked(const char* fileName, AccessMode mode,
258 const asset_path& path);
259 Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode,
260 const asset_path& path);
261 Asset* openInLocaleVendorLocked(const char* fileName, AccessMode mode,
262 const asset_path& path, const char* locale, const char* vendor);
263 String8 createPathNameLocked(const asset_path& path, const char* locale,
264 const char* vendor);
265 String8 createPathNameLocked(const asset_path& path, const char* rootDir);
266 String8 createZipSourceNameLocked(const String8& zipFileName,
267 const String8& dirName, const String8& fileName);
268
269 ZipFileRO* getZipFileLocked(const asset_path& path);
270 Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode);
271 Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile,
272 const ZipEntryRO entry, AccessMode mode, const String8& entryName);
273
274 bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
275 const asset_path& path, const char* rootDir, const char* dirName);
276 SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path);
277 bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
278 const asset_path& path, const char* rootDir, const char* dirName);
279 void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
280 const SortedVector<AssetDir::FileInfo>* pContents);
281
282 void loadFileNameCacheLocked(void);
283 void fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
284 const char* dirName);
285 bool fncScanAndMergeDirLocked(
286 SortedVector<AssetDir::FileInfo>* pMergedInfo,
287 const asset_path& path, const char* locale, const char* vendor,
288 const char* dirName);
289 void purgeFileNameCacheLocked(void);
290
291 const ResTable* getResTable(bool required = true) const;
292 void setLocaleLocked(const char* locale);
293 void updateResourceParamsLocked() const;
Tao Baia6d7e3f2015-09-01 18:49:54 -0700294 bool appendPathToResTable(const asset_path& ap, bool appAsLib=false) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100296 Asset* openIdmapLocked(const struct asset_path& ap) const;
297
Mårten Kongstad48d22322014-01-31 14:43:27 +0100298 void addSystemOverlays(const char* pathOverlaysList, const String8& targetPackagePath,
299 ResTable* sharedRes, size_t offset) const;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 class SharedZip : public RefBase {
302 public:
Mårten Kongstad48d22322014-01-31 14:43:27 +0100303 static sp<SharedZip> get(const String8& path, bool createIfNotPresent = true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
305 ZipFileRO* getZip();
306
307 Asset* getResourceTableAsset();
308 Asset* setResourceTableAsset(Asset* asset);
309
Dianne Hackborn78c40512009-07-06 11:07:40 -0700310 ResTable* getResourceTable();
311 ResTable* setResourceTable(ResTable* res);
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 bool isUpToDate();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100314
315 void addOverlay(const asset_path& ap);
316 bool getOverlay(size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317
318 protected:
319 ~SharedZip();
320
321 private:
322 SharedZip(const String8& path, time_t modWhen);
323 SharedZip(); // <-- not implemented
324
325 String8 mPath;
326 ZipFileRO* mZipFile;
327 time_t mModWhen;
328
329 Asset* mResourceTableAsset;
Dianne Hackborn78c40512009-07-06 11:07:40 -0700330 ResTable* mResourceTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
Mårten Kongstad48d22322014-01-31 14:43:27 +0100332 Vector<asset_path> mOverlays;
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 static Mutex gLock;
335 static DefaultKeyedVector<String8, wp<SharedZip> > gOpen;
336 };
337
338 /*
339 * Manage a set of Zip files. For each file we need a pointer to the
340 * ZipFile and a time_t with the file's modification date.
341 *
342 * We currently only have two zip files (current app, "common" app).
343 * (This was originally written for 8, based on app/locale/vendor.)
344 */
345 class ZipSet {
346 public:
347 ZipSet(void);
348 ~ZipSet(void);
349
350 /*
351 * Return a ZipFileRO structure for a ZipFileRO with the specified
352 * parameters.
353 */
354 ZipFileRO* getZip(const String8& path);
355
Dianne Hackborn78c40512009-07-06 11:07:40 -0700356 Asset* getZipResourceTableAsset(const String8& path);
357 Asset* setZipResourceTableAsset(const String8& path, Asset* asset);
358
359 ResTable* getZipResourceTable(const String8& path);
360 ResTable* setZipResourceTable(const String8& path, ResTable* res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
362 // generate path, e.g. "common/en-US-noogle.zip"
363 static String8 getPathName(const char* path);
364
365 bool isUpToDate();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100366
367 void addOverlay(const String8& path, const asset_path& overlay);
368 bool getOverlay(const String8& path, size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369
370 private:
371 void closeZip(int idx);
372
373 int getIndex(const String8& zip) const;
374 mutable Vector<String8> mZipPath;
375 mutable Vector<sp<SharedZip> > mZipFile;
376 };
377
378 // Protect all internal state.
379 mutable Mutex mLock;
380
381 ZipSet mZipSet;
382
383 Vector<asset_path> mAssetPaths;
384 char* mLocale;
385 char* mVendor;
386
387 mutable ResTable* mResources;
388 ResTable_config* mConfig;
389
390 /*
391 * Cached data for "loose" files. This lets us avoid poking at the
392 * filesystem when searching for loose assets. Each entry is the
393 * "extended partial" path, e.g. "default/default/foo/bar.txt". The
394 * full set of files is present, including ".EXCLUDE" entries.
395 *
396 * We do not cache directory names. We don't retain the ".gz",
397 * because to our clients "foo" and "foo.gz" both look like "foo".
398 */
399 CacheMode mCacheMode; // is the cache enabled?
400 bool mCacheValid; // clear when locale or vendor changes
401 SortedVector<AssetDir::FileInfo> mCache;
402};
403
404}; // namespace android
405
406#endif // __LIBS_ASSETMANAGER_H