blob: 69cf80477dde42dcf9f20a61c956acee649c4987 [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 Lesinskibebfcc42018-02-12 14:27:46 -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 Lesinskibebfcc42018-02-12 14:27:46 -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 Lesinskibebfcc42018-02-12 14:27:46 -080040 std::unique_ptr<AssetDir> mAssetDir;
Christopher Tate6cce32b2010-07-12 18:21:36 -070041 size_t mCurFileIndex;
42 String8 mCachedFileName;
43
Adam Lesinskibebfcc42018-02-12 14:27:46 -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 Lesinskibebfcc42018-02-12 14:27:46 -080051 std::unique_ptr<Asset> mAsset;
Christopher Tate6cce32b2010-07-12 18:21:36 -070052
Adam Lesinskibebfcc42018-02-12 14:27:46 -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/**
Christopher Tate6cce32b2010-07-12 18:21:36 -070059 * Asset Manager functionality
60 */
61AAssetManager* AAssetManager_fromJava(JNIEnv* env, jobject assetManager)
62{
Ashok Bhat896043d2014-01-17 16:02:38 +000063 return (AAssetManager*) env->GetLongField(assetManager, gAssetManagerOffsets.mObject);
Christopher Tate6cce32b2010-07-12 18:21:36 -070064}
65
66AAsset* AAssetManager_open(AAssetManager* amgr, const char* filename, int mode)
67{
68 Asset::AccessMode amMode;
69 switch (mode) {
70 case AASSET_MODE_UNKNOWN:
71 amMode = Asset::ACCESS_UNKNOWN;
72 break;
73 case AASSET_MODE_RANDOM:
74 amMode = Asset::ACCESS_RANDOM;
75 break;
76 case AASSET_MODE_STREAMING:
77 amMode = Asset::ACCESS_STREAMING;
78 break;
79 case AASSET_MODE_BUFFER:
80 amMode = Asset::ACCESS_BUFFER;
81 break;
82 default:
83 return NULL;
84 }
85
Adam Lesinskibebfcc42018-02-12 14:27:46 -080086 ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(amgr));
87 std::unique_ptr<Asset> asset = locked_mgr->Open(filename, amMode);
88 if (asset == nullptr) {
89 return nullptr;
Christopher Tate6cce32b2010-07-12 18:21:36 -070090 }
Adam Lesinskibebfcc42018-02-12 14:27:46 -080091 return new AAsset(std::move(asset));
Christopher Tate6cce32b2010-07-12 18:21:36 -070092}
93
94AAssetDir* AAssetManager_openDir(AAssetManager* amgr, const char* dirName)
95{
Adam Lesinskibebfcc42018-02-12 14:27:46 -080096 ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(amgr));
97 return new AAssetDir(locked_mgr->OpenDir(dirName));
Christopher Tate6cce32b2010-07-12 18:21:36 -070098}
99
100/**
101 * AssetDir functionality
102 */
103
104const char* AAssetDir_getNextFileName(AAssetDir* assetDir)
105{
106 const char* returnName = NULL;
107 size_t index = assetDir->mCurFileIndex;
108 const size_t max = assetDir->mAssetDir->getFileCount();
109
110 // Find the next regular file; explicitly don't report directories even if the
111 // underlying implementation changes to report them. At that point we can add
112 // a more general iterator to this native interface set if appropriate.
113 while ((index < max) && (assetDir->mAssetDir->getFileType(index) != kFileTypeRegular)) {
114 index++;
115 }
116
117 // still in bounds? then the one at 'index' is the next to be reported; generate
118 // the string to return and advance the iterator for next time.
119 if (index < max) {
120 assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
121 returnName = assetDir->mCachedFileName.string();
122 index++;
123 }
124
125 assetDir->mCurFileIndex = index;
126 return returnName;
127}
128
129void AAssetDir_rewind(AAssetDir* assetDir)
130{
131 assetDir->mCurFileIndex = 0;
132}
133
134const char* AAssetDir_getFileName(AAssetDir* assetDir, int index)
135{
136 assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index);
137 return assetDir->mCachedFileName.string();
138}
139
140void AAssetDir_close(AAssetDir* assetDir)
141{
142 delete assetDir;
143}
144
145/**
146 * Asset functionality
147 */
148
149int AAsset_read(AAsset* asset, void* buf, size_t count)
150{
151 return asset->mAsset->read(buf, (size_t)count);
152}
153
154off_t AAsset_seek(AAsset* asset, off_t offset, int whence)
155{
156 return asset->mAsset->seek(offset, whence);
157}
158
Kenny Root06ea4d62010-12-13 11:58:55 -0800159off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence)
160{
161 return asset->mAsset->seek(offset, whence);
162}
163
Christopher Tate6cce32b2010-07-12 18:21:36 -0700164void AAsset_close(AAsset* asset)
165{
166 asset->mAsset->close();
167 delete asset;
168}
169
170const void* AAsset_getBuffer(AAsset* asset)
171{
172 return asset->mAsset->getBuffer(false);
173}
174
175off_t AAsset_getLength(AAsset* asset)
176{
177 return asset->mAsset->getLength();
178}
179
Kenny Root06ea4d62010-12-13 11:58:55 -0800180off64_t AAsset_getLength64(AAsset* asset)
181{
182 return asset->mAsset->getLength();
183}
184
Christopher Tate6cce32b2010-07-12 18:21:36 -0700185off_t AAsset_getRemainingLength(AAsset* asset)
186{
187 return asset->mAsset->getRemainingLength();
188}
189
Kenny Root06ea4d62010-12-13 11:58:55 -0800190off64_t AAsset_getRemainingLength64(AAsset* asset)
191{
192 return asset->mAsset->getRemainingLength();
193}
194
Christopher Tate6cce32b2010-07-12 18:21:36 -0700195int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength)
196{
Kenny Root06ea4d62010-12-13 11:58:55 -0800197 off64_t outStart64, outLength64;
198
199 int ret = asset->mAsset->openFileDescriptor(&outStart64, &outLength64);
200
201 *outStart = off_t(outStart64);
202 *outLength = off_t(outLength64);
203 return ret;
204}
205
206int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength)
207{
208 return asset->mAsset->openFileDescriptor(outStart, outLength);
Christopher Tate6cce32b2010-07-12 18:21:36 -0700209}
210
211int AAsset_isAllocated(AAsset* asset)
212{
213 return asset->mAsset->isAllocated() ? 1 : 0;
214}