blob: 3b5cdcefd631b77bb0f77f3539e51fd51ad5f207 [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 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * The object returned does not depend on the AssetManager. It should
140 * be freed by calling Asset::close().
141 */
142 Asset* open(const char* fileName, AccessMode mode);
143
144 /*
145 * Open a non-asset file as an asset.
146 *
147 * This is for opening files that are included in an asset package
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700148 * but aren't assets. These sit outside the usual "assets/"
149 * path hierarchy, and will not be seen by "AssetDir".
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800151 Asset* openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie = NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
153 /*
154 * Explicit non-asset file. The file explicitly named by the cookie (the
155 * resource set to look in) and fileName will be opened and returned.
156 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000157 Asset* openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158
159 /*
160 * Open a directory within the asset hierarchy.
161 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 * To open the top-level directory, pass in "".
163 */
164 AssetDir* openDir(const char* dirName);
165
166 /*
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700167 * Open a directory within a particular path of the asset manager.
168 *
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700169 * To open the top-level directory, pass in "".
170 */
Narayan Kamath745d4ef2014-01-27 11:17:22 +0000171 AssetDir* openNonAssetDir(const int32_t cookie, const char* dirName);
Dianne Hackbornbb9ea302009-05-18 15:22:00 -0700172
173 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * Get the type of a file in the asset hierarchy. They will either
175 * be "regular" or "directory". [Currently only works for "regular".]
176 *
177 * Can also be used as a quick test for existence of a file.
178 */
179 FileType getFileType(const char* fileName);
180
181 /*
182 * Return the complete resource table to find things in the package.
183 */
184 const ResTable& getResources(bool required = true) const;
185
186 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * Return true if the files this AssetManager references are all
188 * up-to-date (have not been changed since it was created). If false
189 * is returned, you will need to create a new AssetManager to get
190 * the current data.
191 */
192 bool isUpToDate();
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 /**
195 * Get the known locales for this asset manager object.
196 */
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800197 void getLocales(Vector<String8>* locales, bool includeSystemLocales=true) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100199 /**
200 * Generate idmap data to translate resources IDs between a package and a
201 * corresponding overlay package.
202 */
203 bool createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackbornd9e385b2014-02-11 13:56:21 -0800204 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize);
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206private:
207 struct asset_path
208 {
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800209 asset_path() : path(""), type(kFileTypeRegular), idmap(""),
210 isSystemOverlay(false), isSystemAsset(false) {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 String8 path;
212 FileType type;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100213 String8 idmap;
Mårten Kongstadcb7b63d2014-11-07 10:57:15 +0100214 bool isSystemOverlay;
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800215 bool isSystemAsset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 };
217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 Asset* openNonAssetInPathLocked(const char* fileName, AccessMode mode,
219 const asset_path& path);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 String8 createPathNameLocked(const asset_path& path, const char* rootDir);
221 String8 createZipSourceNameLocked(const String8& zipFileName,
222 const String8& dirName, const String8& fileName);
223
224 ZipFileRO* getZipFileLocked(const asset_path& path);
225 Asset* openAssetFromFileLocked(const String8& fileName, AccessMode mode);
226 Asset* openAssetFromZipLocked(const ZipFileRO* pZipFile,
227 const ZipEntryRO entry, AccessMode mode, const String8& entryName);
228
229 bool scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
230 const asset_path& path, const char* rootDir, const char* dirName);
231 SortedVector<AssetDir::FileInfo>* scanDirLocked(const String8& path);
232 bool scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
233 const asset_path& path, const char* rootDir, const char* dirName);
234 void mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
235 const SortedVector<AssetDir::FileInfo>* pContents);
236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 const ResTable* getResTable(bool required = true) const;
238 void setLocaleLocked(const char* locale);
239 void updateResourceParamsLocked() const;
Tao Baia6d7e3f2015-09-01 18:49:54 -0700240 bool appendPathToResTable(const asset_path& ap, bool appAsLib=false) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100242 Asset* openIdmapLocked(const struct asset_path& ap) const;
243
Mårten Kongstad48d22322014-01-31 14:43:27 +0100244 void addSystemOverlays(const char* pathOverlaysList, const String8& targetPackagePath,
245 ResTable* sharedRes, size_t offset) const;
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 class SharedZip : public RefBase {
248 public:
Mårten Kongstad48d22322014-01-31 14:43:27 +0100249 static sp<SharedZip> get(const String8& path, bool createIfNotPresent = true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
251 ZipFileRO* getZip();
252
253 Asset* getResourceTableAsset();
254 Asset* setResourceTableAsset(Asset* asset);
255
Dianne Hackborn78c40512009-07-06 11:07:40 -0700256 ResTable* getResourceTable();
257 ResTable* setResourceTable(ResTable* res);
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 bool isUpToDate();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100260
261 void addOverlay(const asset_path& ap);
262 bool getOverlay(size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263
264 protected:
265 ~SharedZip();
266
267 private:
268 SharedZip(const String8& path, time_t modWhen);
269 SharedZip(); // <-- not implemented
270
271 String8 mPath;
272 ZipFileRO* mZipFile;
273 time_t mModWhen;
274
275 Asset* mResourceTableAsset;
Dianne Hackborn78c40512009-07-06 11:07:40 -0700276 ResTable* mResourceTable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277
Mårten Kongstad48d22322014-01-31 14:43:27 +0100278 Vector<asset_path> mOverlays;
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 static Mutex gLock;
281 static DefaultKeyedVector<String8, wp<SharedZip> > gOpen;
282 };
283
284 /*
285 * Manage a set of Zip files. For each file we need a pointer to the
286 * ZipFile and a time_t with the file's modification date.
287 *
288 * We currently only have two zip files (current app, "common" app).
289 * (This was originally written for 8, based on app/locale/vendor.)
290 */
291 class ZipSet {
292 public:
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700293 ZipSet() = default;
294 ~ZipSet();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
296 /*
297 * Return a ZipFileRO structure for a ZipFileRO with the specified
298 * parameters.
299 */
300 ZipFileRO* getZip(const String8& path);
301
Dianne Hackborn78c40512009-07-06 11:07:40 -0700302 Asset* getZipResourceTableAsset(const String8& path);
303 Asset* setZipResourceTableAsset(const String8& path, Asset* asset);
304
305 ResTable* getZipResourceTable(const String8& path);
306 ResTable* setZipResourceTable(const String8& path, ResTable* res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307
308 // generate path, e.g. "common/en-US-noogle.zip"
309 static String8 getPathName(const char* path);
310
311 bool isUpToDate();
Mårten Kongstad48d22322014-01-31 14:43:27 +0100312
313 void addOverlay(const String8& path, const asset_path& overlay);
314 bool getOverlay(const String8& path, size_t idx, asset_path* out) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 private:
317 void closeZip(int idx);
318
319 int getIndex(const String8& zip) const;
320 mutable Vector<String8> mZipPath;
321 mutable Vector<sp<SharedZip> > mZipFile;
322 };
323
324 // Protect all internal state.
325 mutable Mutex mLock;
326
327 ZipSet mZipSet;
328
329 Vector<asset_path> mAssetPaths;
330 char* mLocale;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
332 mutable ResTable* mResources;
333 ResTable_config* mConfig;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334};
335
336}; // namespace android
337
338#endif // __LIBS_ASSETMANAGER_H