blob: e70d5ea0d566f506ac0ca18e6a3074f1214ed9ed [file] [log] [blame]
Christopher Tate6cce32b2010-07-12 18:21:36 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "NAsset"
18#include <utils/Log.h>
19
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070020#include <android/asset_manager_jni.h>
Adam Lesinski11875902017-01-23 12:58:11 -080021#include <android_runtime/android_util_AssetManager.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080022#include <androidfw/Asset.h>
23#include <androidfw/AssetDir.h>
24#include <androidfw/AssetManager.h>
Adam Lesinski11875902017-01-23 12:58:11 -080025#include <androidfw/AssetManager2.h>
Christopher Tate6cce32b2010-07-12 18:21:36 -070026#include <utils/threads.h>
27
28#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070029#include <nativehelper/JNIHelp.h>
Christopher Tate6cce32b2010-07-12 18:21:36 -070030
31using namespace android;
32
33// -------------------- Backing implementation of the public API --------------------
34
35// AAssetManager is actually a secret typedef for an empty base class of AssetManager,
36// but AAssetDir and AAsset are actual wrappers for isolation.
37
38// -----
39struct AAssetDir {
Adam Lesinski11875902017-01-23 12:58:11 -080040 std::unique_ptr<AssetDir> mAssetDir;
Christopher Tate6cce32b2010-07-12 18:21:36 -070041 size_t mCurFileIndex;
42 String8 mCachedFileName;
43
Adam Lesinski11875902017-01-23 12:58:11 -080044 explicit AAssetDir(std::unique_ptr<AssetDir> dir) :
45 mAssetDir(std::move(dir)), mCurFileIndex(0) { }
Christopher Tate6cce32b2010-07-12 18:21:36 -070046};
47
48
49// -----
50struct AAsset {
Adam Lesinski11875902017-01-23 12:58:11 -080051 std::unique_ptr<Asset> mAsset;
Christopher Tate6cce32b2010-07-12 18:21:36 -070052
Adam Lesinski11875902017-01-23 12:58:11 -080053 explicit AAsset(std::unique_ptr<Asset> asset) : mAsset(std::move(asset)) { }
Christopher Tate6cce32b2010-07-12 18:21:36 -070054};
55
56// -------------------- Public native C API --------------------
57
58/**
59 * Supporting information
60 */
61
62static struct assetmanager_offsets_t
63{
64 jfieldID mObject;
65} gAssetManagerOffsets;
66
67static volatile bool gJNIConfigured = false;
68static Mutex gMutex;
69
70/**
71 * Asset Manager functionality
72 */
73AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager)
74{
75 {
76 Mutex::Autolock _l(gMutex);
77
78 if (gJNIConfigured == false) {
79 jclass amClass = env->FindClass("android/content/res/AssetManager");
Ashok Bhat896043d2014-01-17 16:02:38 +000080 gAssetManagerOffsets.mObject = env->GetFieldID(amClass, "mObject", "J");
Christopher Tate6cce32b2010-07-12 18:21:36 -070081 gJNIConfigured = true;
82 }
83 }
84
Ashok Bhat896043d2014-01-17 16:02:38 +000085 return (AAssetManager*) env->GetLongField(assetManager, gAssetManagerOffsets.mObject);
Christopher Tate6cce32b2010-07-12 18:21:36 -070086}
87
88AAsset* AAssetManager_open(AAssetManager* amgr, const char* filename, int mode)
89{
90 Asset::AccessMode amMode;
91 switch (mode) {
92 case AASSET_MODE_UNKNOWN:
93 amMode = Asset::ACCESS_UNKNOWN;
94 break;
95 case AASSET_MODE_RANDOM:
96 amMode = Asset::ACCESS_RANDOM;
97 break;
98 case AASSET_MODE_STREAMING:
99 amMode = Asset::ACCESS_STREAMING;
100 break;
101 case AASSET_MODE_BUFFER:
102 amMode = Asset::ACCESS_BUFFER;
103 break;
104 default:
105 return NULL;
106 }
107
Adam Lesinski11875902017-01-23 12:58:11 -0800108 ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(amgr));
109 std::unique_ptr<Asset> asset = locked_mgr->Open(filename, amMode);
110 if (asset == nullptr) {
111 return nullptr;
Christopher Tate6cce32b2010-07-12 18:21:36 -0700112 }
Adam Lesinski11875902017-01-23 12:58:11 -0800113 return new AAsset(std::move(asset));
Christopher Tate6cce32b2010-07-12 18:21:36 -0700114}
115
116AAssetDir* AAssetManager_openDir(AAssetManager* amgr, const char* dirName)
117{
Adam Lesinski11875902017-01-23 12:58:11 -0800118 ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(amgr));
119 return new AAssetDir(locked_mgr->OpenDir(dirName));
Christopher Tate6cce32b2010-07-12 18:21:36 -0700120}
121
122/**
123 * AssetDir functionality
124 */
125
126const char* AAssetDir_getNextFileName(AAssetDir* assetDir)
127{
128 const char* returnName = NULL;
129 size_t index = assetDir->mCurFileIndex;
130 const size_t max = assetDir->mAssetDir->getFileCount();
131
132 // Find the next regular file; explicitly don't report directories even if the
133 // underlying implementation changes to report them. At that point we can add
134 // a more general iterator to this native interface set if appropriate.
135 while ((index < max) && (assetDir->mAssetDir->getFileType(index) != kFileTypeRegular)) {
136 index++;
137 }
138
139 // still in bounds? then the one at 'index' is the next to be reported; generate
140 // the string to return and advance the iterator for next time.
141 if (index < max) {
142 assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
143 returnName = assetDir->mCachedFileName.string();
144 index++;
145 }
146
147 assetDir->mCurFileIndex = index;
148 return returnName;
149}
150
151void AAssetDir_rewind(AAssetDir* assetDir)
152{
153 assetDir->mCurFileIndex = 0;
154}
155
156const char* AAssetDir_getFileName(AAssetDir* assetDir, int index)
157{
158 assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
159 return assetDir->mCachedFileName.string();
160}
161
162void AAssetDir_close(AAssetDir* assetDir)
163{
164 delete assetDir;
165}
166
167/**
168 * Asset functionality
169 */
170
171int AAsset_read(AAsset* asset, void* buf, size_t count)
172{
173 return asset->mAsset->read(buf, (size_t)count);
174}
175
176off_t AAsset_seek(AAsset* asset, off_t offset, int whence)
177{
178 return asset->mAsset->seek(offset, whence);
179}
180
Kenny Root06ea4d62010-12-13 11:58:55 -0800181off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence)
182{
183 return asset->mAsset->seek(offset, whence);
184}
185
Christopher Tate6cce32b2010-07-12 18:21:36 -0700186void AAsset_close(AAsset* asset)
187{
188 asset->mAsset->close();
189 delete asset;
190}
191
192const void* AAsset_getBuffer(AAsset* asset)
193{
194 return asset->mAsset->getBuffer(false);
195}
196
197off_t AAsset_getLength(AAsset* asset)
198{
199 return asset->mAsset->getLength();
200}
201
Kenny Root06ea4d62010-12-13 11:58:55 -0800202off64_t AAsset_getLength64(AAsset* asset)
203{
204 return asset->mAsset->getLength();
205}
206
Christopher Tate6cce32b2010-07-12 18:21:36 -0700207off_t AAsset_getRemainingLength(AAsset* asset)
208{
209 return asset->mAsset->getRemainingLength();
210}
211
Kenny Root06ea4d62010-12-13 11:58:55 -0800212off64_t AAsset_getRemainingLength64(AAsset* asset)
213{
214 return asset->mAsset->getRemainingLength();
215}
216
Christopher Tate6cce32b2010-07-12 18:21:36 -0700217int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength)
218{
Kenny Root06ea4d62010-12-13 11:58:55 -0800219 off64_t outStart64, outLength64;
220
221 int ret = asset->mAsset->openFileDescriptor(&outStart64, &outLength64);
222
223 *outStart = off_t(outStart64);
224 *outLength = off_t(outLength64);
225 return ret;
226}
227
228int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength)
229{
230 return asset->mAsset->openFileDescriptor(outStart, outLength);
Christopher Tate6cce32b2010-07-12 18:21:36 -0700231}
232
233int AAsset_isAllocated(AAsset* asset)
234{
235 return asset->mAsset->isAllocated() ? 1 : 0;
236}