blob: ce8d0f8e57ff213cbef68a245988cb1f35f7c7c0 [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 *
Adam Lesinskia77685f2016-10-03 16:26:28 -070062 * The purpose of the AssetManager is to create Asset objects.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 *
64 * The asset hierarchy may be examined like a filesystem, using
65 * AssetDir objects to peruse a single directory.
66 */
Christopher Tate6cce32b2010-07-12 18:21:36 -070067class AssetManager : public AAssetManager {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068public:
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010069 static const char* RESOURCES_FILENAME;
Mårten Kongstad48d22322014-01-31 14:43:27 +010070 static const char* IDMAP_BIN;
71 static const char* OVERLAY_DIR;
Jakub Adamek1c15c632016-09-23 09:07:11 +010072 /*
Jakub Adamekc03d9482016-09-30 09:19:09 +010073 * If OVERLAY_SKU_DIR_PROPERTY is set, search for runtime resource overlay
74 * APKs in OVERLAY_DIR/<value of OVERLAY_SKU_DIR_PROPERTY> rather than in
Jakub Adamek1c15c632016-09-23 09:07:11 +010075 * OVERLAY_DIR.
76 */
Jakub Adamekc03d9482016-09-30 09:19:09 +010077 static const char* OVERLAY_SKU_DIR_PROPERTY;
Mårten Kongstad48d22322014-01-31 14:43:27 +010078 static const char* TARGET_PACKAGE_NAME;
79 static const char* TARGET_APK_PATH;
80 static const char* IDMAP_DIR;
81
Adam Lesinskia77685f2016-10-03 16:26:28 -070082 AssetManager();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 virtual ~AssetManager(void);
84
85 static int32_t getGlobalCount();
86
87 /*
88 * Add a new source for assets. This can be called multiple times to
89 * look in multiple places for assets. It can be either a directory (for
90 * finding assets as raw files on the disk) or a ZIP file. This newly
91 * added asset path will be examined first when searching for assets,
Tao Baia6d7e3f2015-09-01 18:49:54 -070092 * before any that were previously added, the assets are added as shared
93 * library if appAsLib is true.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 *
95 * Returns "true" on success, "false" on failure. If 'cookie' is non-NULL,
96 * then on success, *cookie is set to the value corresponding to the
97 * newly-added asset source.
98 */
Roozbeh Pournader1c686f22015-12-18 14:22:14 -080099 bool addAssetPath(const String8& path, int32_t* cookie,
100 bool appAsLib=false, bool isSystemAsset=false);
Mårten Kongstad48d22322014-01-31 14:43:27 +0100101 bool addOverlayPath(const String8& path, int32_t* cookie);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800103 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 * Convenience for adding the standard system assets. Uses the
105 * ANDROID_ROOT environment variable to find them.
106 */
107 bool addDefaultAssets();
108
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800109 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * Iterate over the asset paths in this manager. (Previously
111 * added via addAssetPath() and addDefaultAssets().) On first call,
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000112 * 'cookie' must be 0, resulting in the first cookie being returned.
113 * Each next cookie will be returned there-after, until -1 indicating
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 * the end has been reached.
115 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000116 int32_t nextAssetPath(const int32_t cookie) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800118 /*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700119 * Return an asset path in the manager. 'cookie' must be a non-negative value
120 * previously returned from addAssetPath() or nextAssetPath().
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000122 String8 getAssetPath(const int32_t cookie) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123
124 /*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700125 * Sets various device configuration parameters, like screen orientation, layout,
126 * size, locale, etc.
127 * The optional 'locale' string takes precedence over the locale within 'config'
128 * and must be in bcp47 format.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
130 void setConfiguration(const ResTable_config& config, const char* locale = NULL);
131
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700132 void getConfiguration(ResTable_config* outConfig) const;
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 typedef Asset::AccessMode AccessMode; // typing shortcut
135
136 /*
137 * Open an asset.
138 *
139 * This will search through locale-specific and vendor-specific
140 * directories and packages to find the file.
141 *
142 * The object returned does not depend on the AssetManager. It should
143 * be freed by calling Asset::close().
144 */
145 Asset* open(const char* fileName, AccessMode mode);
146
147 /*
148 * Open a non-asset file as an asset.
149 *
150 * This is for opening files that are included in an asset package
151 * but aren't assets. These sit outside the usual "locale/vendor"
152 * path hierarchy, and will not be seen by "AssetDir" or included
153 * in our filename cache.
154 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800155 Asset* openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
157 /*
158 * Explicit non-asset file. The file explicitly named by the cookie (the
159 * resource set to look in) and fileName will be opened and returned.
160 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000161 Asset* openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163 /*
164 * Open a directory within the asset hierarchy.
165 *
166 * The contents of the directory are an amalgam of vendor-specific,
167 * locale-specific, and generic assets stored loosely or in asset
168 * packages. Depending on the cache setting and previous accesses,
169 * this call may incur significant disk overhead.
170 *
171 * To open the top-level directory, pass in "".
172 */
173 AssetDir* openDir(const char* dirName);
174
175 /*
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700176 * Open a directory within a particular path of the asset manager.
177 *
178 * The contents of the directory are an amalgam of vendor-specific,
179 * locale-specific, and generic assets stored loosely or in asset
180 * packages. Depending on the cache setting and previous accesses,
181 * this call may incur significant disk overhead.
182 *
183 * To open the top-level directory, pass in "".
184 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000185 AssetDir* openNonAssetDir(const int32_t cookie, const char* dirName);
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700186
187 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * Get the type of a file in the asset hierarchy. They will either
189 * be "regular" or "directory". [Currently only works for "regular".]
190 *
191 * Can also be used as a quick test for existence of a file.
192 */
193 FileType getFileType(const char* fileName);
194
195 /*
196 * Return the complete resource table to find things in the package.
197 */
198 const ResTable& getResources(bool required = true) const;
199
200 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 * Return true if the files this AssetManager references are all
202 * up-to-date (have not been changed since it was created). If false
203 * is returned, you will need to create a new AssetManager to get
204 * the current data.
205 */
206 bool isUpToDate();
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 /**
209 * Get the known locales for this asset manager object.
210 */
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800211 void getLocales(Vector<String8>* locales, bool includeSystemLocales=true) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100213 /**
214 * Generate idmap data to translate resources IDs between a package and a
215 * corresponding overlay package.
216 */
217 bool createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackbornd9e385b2014-02-11 13:56:21 -0800218 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize);
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220private:
221 struct asset_path
222 {
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800223 asset_path() : path(""), type(kFileTypeRegular), idmap(""),
224 isSystemOverlay(false), isSystemAsset(false) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 String8 path;
226 FileType type;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100227 String8 idmap;
Mårten Kongstadcb7b63d2014-11-07 10:57:15 +0100228 bool isSystemOverlay;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800229 bool isSystemAsset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 };
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode,
233 const asset_path& path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 String8 createPathNameLocked(const asset_path& path, const char* locale,
235 const char* vendor);
236 String8 createPathNameLocked(const asset_path& path, const char* rootDir);
237 String8 createZipSourceNameLocked(const String8& zipFileName,
238 const String8& dirName, const String8& fileName);
239
240 ZipFileRO* getZipFileLocked(const asset_path& path);
241 Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode);
242 Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile,
243 const ZipEntryRO entry, AccessMode mode, const String8& entryName);
244
245 bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
246 const asset_path& path, const char* rootDir, const char* dirName);
247 SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path);
248 bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
249 const asset_path& path, const char* rootDir, const char* dirName);
250 void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
251 const SortedVector<AssetDir::FileInfo>* pContents);
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 const ResTable* getResTable(bool required = true) const;
254 void setLocaleLocked(const char* locale);
255 void updateResourceParamsLocked() const;
Tao Baia6d7e3f2015-09-01 18:49:54 -0700256 bool appendPathToResTable(const asset_path& ap, bool appAsLib=false) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100258 Asset* openIdmapLocked(const struct asset_path& ap) const;
259
Mårten Kongstad48d22322014-01-31 14:43:27 +0100260 void addSystemOverlays(const char* pathOverlaysList, const String8& targetPackagePath,
261 ResTable* sharedRes, size_t offset) const;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 class SharedZip : public RefBase {
264 public:
Mårten Kongstad48d22322014-01-31 14:43:27 +0100265 static sp<SharedZip> get(const String8& path, bool createIfNotPresent = true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
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();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100276
277 void addOverlay(const asset_path& ap);
278 bool getOverlay(size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279
280 protected:
281 ~SharedZip();
282
283 private:
284 SharedZip(const String8& path, time_t modWhen);
285 SharedZip(); // <-- not implemented
286
287 String8 mPath;
288 ZipFileRO* mZipFile;
289 time_t mModWhen;
290
291 Asset* mResourceTableAsset;
Dianne Hackborn78c40512009-07-06 11:07:40 -0700292 ResTable* mResourceTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Mårten Kongstad48d22322014-01-31 14:43:27 +0100294 Vector<asset_path> mOverlays;
295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 static Mutex gLock;
297 static DefaultKeyedVector<String8, wp<SharedZip> > gOpen;
298 };
299
300 /*
301 * Manage a set of Zip files. For each file we need a pointer to the
302 * ZipFile and a time_t with the file's modification date.
303 *
304 * We currently only have two zip files (current app, "common" app).
305 * (This was originally written for 8, based on app/locale/vendor.)
306 */
307 class ZipSet {
308 public:
309 ZipSet(void);
310 ~ZipSet(void);
311
312 /*
313 * Return a ZipFileRO structure for a ZipFileRO with the specified
314 * parameters.
315 */
316 ZipFileRO* getZip(const String8& path);
317
Dianne Hackborn78c40512009-07-06 11:07:40 -0700318 Asset* getZipResourceTableAsset(const String8& path);
319 Asset* setZipResourceTableAsset(const String8& path, Asset* asset);
320
321 ResTable* getZipResourceTable(const String8& path);
322 ResTable* setZipResourceTable(const String8& path, ResTable* res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323
324 // generate path, e.g. "common/en-US-noogle.zip"
325 static String8 getPathName(const char* path);
326
327 bool isUpToDate();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100328
329 void addOverlay(const String8& path, const asset_path& overlay);
330 bool getOverlay(const String8& path, size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
332 private:
333 void closeZip(int idx);
334
335 int getIndex(const String8& zip) const;
336 mutable Vector<String8> mZipPath;
337 mutable Vector<sp<SharedZip> > mZipFile;
338 };
339
340 // Protect all internal state.
341 mutable Mutex mLock;
342
343 ZipSet mZipSet;
344
345 Vector<asset_path> mAssetPaths;
346 char* mLocale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
348 mutable ResTable* mResources;
349 ResTable_config* mConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350};
351
352}; // namespace android
353
354#endif // __LIBS_ASSETMANAGER_H