Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 20 | #include <android/asset_manager_jni.h> |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 21 | #include <android_runtime/android_util_AssetManager.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 22 | #include <androidfw/Asset.h> |
| 23 | #include <androidfw/AssetDir.h> |
| 24 | #include <androidfw/AssetManager.h> |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 25 | #include <androidfw/AssetManager2.h> |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 26 | #include <utils/threads.h> |
| 27 | |
| 28 | #include "jni.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 29 | #include <nativehelper/JNIHelp.h> |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 30 | |
| 31 | using 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 | // ----- |
| 39 | struct AAssetDir { |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 40 | std::unique_ptr<AssetDir> mAssetDir; |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 41 | size_t mCurFileIndex; |
| 42 | String8 mCachedFileName; |
| 43 | |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 44 | explicit AAssetDir(std::unique_ptr<AssetDir> dir) : |
| 45 | mAssetDir(std::move(dir)), mCurFileIndex(0) { } |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | |
| 49 | // ----- |
| 50 | struct AAsset { |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 51 | std::unique_ptr<Asset> mAsset; |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 52 | |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 53 | explicit AAsset(std::unique_ptr<Asset> asset) : mAsset(std::move(asset)) { } |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | // -------------------- Public native C API -------------------- |
| 57 | |
| 58 | /** |
| 59 | * Supporting information |
| 60 | */ |
| 61 | |
| 62 | static struct assetmanager_offsets_t |
| 63 | { |
| 64 | jfieldID mObject; |
| 65 | } gAssetManagerOffsets; |
| 66 | |
| 67 | static volatile bool gJNIConfigured = false; |
| 68 | static Mutex gMutex; |
| 69 | |
| 70 | /** |
| 71 | * Asset Manager functionality |
| 72 | */ |
| 73 | AAssetManager* 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 Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 80 | gAssetManagerOffsets.mObject = env->GetFieldID(amClass, "mObject", "J"); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 81 | gJNIConfigured = true; |
| 82 | } |
| 83 | } |
| 84 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 85 | return (AAssetManager*) env->GetLongField(assetManager, gAssetManagerOffsets.mObject); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | AAsset* 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 Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 108 | 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 Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 112 | } |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 113 | return new AAsset(std::move(asset)); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | AAssetDir* AAssetManager_openDir(AAssetManager* amgr, const char* dirName) |
| 117 | { |
Adam Lesinski | 1187590 | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 118 | ScopedLock<AssetManager2> locked_mgr(*AssetManagerForNdkAssetManager(amgr)); |
| 119 | return new AAssetDir(locked_mgr->OpenDir(dirName)); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * AssetDir functionality |
| 124 | */ |
| 125 | |
| 126 | const 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 | |
| 151 | void AAssetDir_rewind(AAssetDir* assetDir) |
| 152 | { |
| 153 | assetDir->mCurFileIndex = 0; |
| 154 | } |
| 155 | |
| 156 | const char* AAssetDir_getFileName(AAssetDir* assetDir, int index) |
| 157 | { |
| 158 | assetDir->mCachedFileName = assetDir->mAssetDir->getFileName(index); |
| 159 | return assetDir->mCachedFileName.string(); |
| 160 | } |
| 161 | |
| 162 | void AAssetDir_close(AAssetDir* assetDir) |
| 163 | { |
| 164 | delete assetDir; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Asset functionality |
| 169 | */ |
| 170 | |
| 171 | int AAsset_read(AAsset* asset, void* buf, size_t count) |
| 172 | { |
| 173 | return asset->mAsset->read(buf, (size_t)count); |
| 174 | } |
| 175 | |
| 176 | off_t AAsset_seek(AAsset* asset, off_t offset, int whence) |
| 177 | { |
| 178 | return asset->mAsset->seek(offset, whence); |
| 179 | } |
| 180 | |
Kenny Root | 06ea4d6 | 2010-12-13 11:58:55 -0800 | [diff] [blame] | 181 | off64_t AAsset_seek64(AAsset* asset, off64_t offset, int whence) |
| 182 | { |
| 183 | return asset->mAsset->seek(offset, whence); |
| 184 | } |
| 185 | |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 186 | void AAsset_close(AAsset* asset) |
| 187 | { |
| 188 | asset->mAsset->close(); |
| 189 | delete asset; |
| 190 | } |
| 191 | |
| 192 | const void* AAsset_getBuffer(AAsset* asset) |
| 193 | { |
| 194 | return asset->mAsset->getBuffer(false); |
| 195 | } |
| 196 | |
| 197 | off_t AAsset_getLength(AAsset* asset) |
| 198 | { |
| 199 | return asset->mAsset->getLength(); |
| 200 | } |
| 201 | |
Kenny Root | 06ea4d6 | 2010-12-13 11:58:55 -0800 | [diff] [blame] | 202 | off64_t AAsset_getLength64(AAsset* asset) |
| 203 | { |
| 204 | return asset->mAsset->getLength(); |
| 205 | } |
| 206 | |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 207 | off_t AAsset_getRemainingLength(AAsset* asset) |
| 208 | { |
| 209 | return asset->mAsset->getRemainingLength(); |
| 210 | } |
| 211 | |
Kenny Root | 06ea4d6 | 2010-12-13 11:58:55 -0800 | [diff] [blame] | 212 | off64_t AAsset_getRemainingLength64(AAsset* asset) |
| 213 | { |
| 214 | return asset->mAsset->getRemainingLength(); |
| 215 | } |
| 216 | |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 217 | int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength) |
| 218 | { |
Kenny Root | 06ea4d6 | 2010-12-13 11:58:55 -0800 | [diff] [blame] | 219 | 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 | |
| 228 | int AAsset_openFileDescriptor64(AAsset* asset, off64_t* outStart, off64_t* outLength) |
| 229 | { |
| 230 | return asset->mAsset->openFileDescriptor(outStart, outLength); |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | int AAsset_isAllocated(AAsset* asset) |
| 234 | { |
| 235 | return asset->mAsset->isAllocated() ? 1 : 0; |
| 236 | } |