The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_util_AssetManager.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "asset" |
| 19 | |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 20 | #define DEBUG_STYLES(x) //x |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 21 | #define THROW_ON_BAD_ID 0 |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <android_runtime/android_util_AssetManager.h> |
| 24 | |
| 25 | #include "jni.h" |
| 26 | #include "JNIHelp.h" |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 27 | #include "ScopedStringChars.h" |
| 28 | #include "ScopedUtfChars.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include "android_util_Binder.h" |
| 30 | #include <utils/misc.h> |
| 31 | #include <android_runtime/AndroidRuntime.h> |
| 32 | #include <utils/Log.h> |
| 33 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 34 | #include <androidfw/Asset.h> |
| 35 | #include <androidfw/AssetManager.h> |
| 36 | #include <androidfw/ResourceTypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 38 | #include <private/android_filesystem_config.h> // for AID_SYSTEM |
| 39 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | #include <stdio.h> |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
| 43 | |
| 44 | #include <linux/capability.h> |
| 45 | extern "C" int capget(cap_user_header_t hdrp, cap_user_data_t datap); |
| 46 | extern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap); |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | namespace android { |
| 50 | |
| 51 | // ---------------------------------------------------------------------------- |
| 52 | |
| 53 | static struct typedvalue_offsets_t |
| 54 | { |
| 55 | jfieldID mType; |
| 56 | jfieldID mData; |
| 57 | jfieldID mString; |
| 58 | jfieldID mAssetCookie; |
| 59 | jfieldID mResourceId; |
| 60 | jfieldID mChangingConfigurations; |
| 61 | jfieldID mDensity; |
| 62 | } gTypedValueOffsets; |
| 63 | |
| 64 | static struct assetfiledescriptor_offsets_t |
| 65 | { |
| 66 | jfieldID mFd; |
| 67 | jfieldID mStartOffset; |
| 68 | jfieldID mLength; |
| 69 | } gAssetFileDescriptorOffsets; |
| 70 | |
| 71 | static struct assetmanager_offsets_t |
| 72 | { |
| 73 | jfieldID mObject; |
| 74 | } gAssetManagerOffsets; |
| 75 | |
| 76 | jclass g_stringClass = NULL; |
| 77 | |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | enum { |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 81 | STYLE_NUM_ENTRIES = 6, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | STYLE_TYPE = 0, |
| 83 | STYLE_DATA = 1, |
| 84 | STYLE_ASSET_COOKIE = 2, |
| 85 | STYLE_RESOURCE_ID = 3, |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 86 | STYLE_CHANGING_CONFIGURATIONS = 4, |
| 87 | STYLE_DENSITY = 5 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static jint copyValue(JNIEnv* env, jobject outValue, const ResTable* table, |
| 91 | const Res_value& value, uint32_t ref, ssize_t block, |
| 92 | uint32_t typeSpecFlags, ResTable_config* config = NULL); |
| 93 | |
| 94 | jint copyValue(JNIEnv* env, jobject outValue, const ResTable* table, |
| 95 | const Res_value& value, uint32_t ref, ssize_t block, |
| 96 | uint32_t typeSpecFlags, ResTable_config* config) |
| 97 | { |
| 98 | env->SetIntField(outValue, gTypedValueOffsets.mType, value.dataType); |
| 99 | env->SetIntField(outValue, gTypedValueOffsets.mAssetCookie, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 100 | static_cast<jint>(table->getTableCookie(block))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | env->SetIntField(outValue, gTypedValueOffsets.mData, value.data); |
| 102 | env->SetObjectField(outValue, gTypedValueOffsets.mString, NULL); |
| 103 | env->SetIntField(outValue, gTypedValueOffsets.mResourceId, ref); |
| 104 | env->SetIntField(outValue, gTypedValueOffsets.mChangingConfigurations, |
| 105 | typeSpecFlags); |
| 106 | if (config != NULL) { |
| 107 | env->SetIntField(outValue, gTypedValueOffsets.mDensity, config->density); |
| 108 | } |
| 109 | return block; |
| 110 | } |
| 111 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 112 | // This is called by zygote (running as user root) as part of preloadResources. |
| 113 | static void verifySystemIdmaps() |
| 114 | { |
| 115 | pid_t pid; |
| 116 | char system_id[10]; |
| 117 | |
| 118 | snprintf(system_id, sizeof(system_id), "%d", AID_SYSTEM); |
| 119 | |
| 120 | switch (pid = fork()) { |
| 121 | case -1: |
| 122 | ALOGE("failed to fork for idmap: %s", strerror(errno)); |
| 123 | break; |
| 124 | case 0: // child |
| 125 | { |
| 126 | struct __user_cap_header_struct capheader; |
| 127 | struct __user_cap_data_struct capdata; |
| 128 | |
| 129 | memset(&capheader, 0, sizeof(capheader)); |
| 130 | memset(&capdata, 0, sizeof(capdata)); |
| 131 | |
| 132 | capheader.version = _LINUX_CAPABILITY_VERSION; |
| 133 | capheader.pid = 0; |
| 134 | |
| 135 | if (capget(&capheader, &capdata) != 0) { |
| 136 | ALOGE("capget: %s\n", strerror(errno)); |
| 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | capdata.effective = capdata.permitted; |
| 141 | if (capset(&capheader, &capdata) != 0) { |
| 142 | ALOGE("capset: %s\n", strerror(errno)); |
| 143 | exit(1); |
| 144 | } |
| 145 | |
| 146 | if (setgid(AID_SYSTEM) != 0) { |
| 147 | ALOGE("setgid: %s\n", strerror(errno)); |
| 148 | exit(1); |
| 149 | } |
| 150 | |
| 151 | if (setuid(AID_SYSTEM) != 0) { |
| 152 | ALOGE("setuid: %s\n", strerror(errno)); |
| 153 | exit(1); |
| 154 | } |
| 155 | |
| 156 | execl(AssetManager::IDMAP_BIN, AssetManager::IDMAP_BIN, "--scan", |
| 157 | AssetManager::OVERLAY_DIR, AssetManager::TARGET_PACKAGE_NAME, |
| 158 | AssetManager::TARGET_APK_PATH, AssetManager::IDMAP_DIR, (char*)NULL); |
| 159 | ALOGE("failed to execl for idmap: %s", strerror(errno)); |
| 160 | exit(1); // should never get here |
| 161 | } |
| 162 | break; |
| 163 | default: // parent |
| 164 | waitpid(pid, NULL, 0); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | // ---------------------------------------------------------------------------- |
| 170 | |
| 171 | // this guy is exported to other jni routines |
| 172 | AssetManager* assetManagerForJavaObject(JNIEnv* env, jobject obj) |
| 173 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 174 | jlong amHandle = env->GetLongField(obj, gAssetManagerOffsets.mObject); |
| 175 | AssetManager* am = reinterpret_cast<AssetManager*>(amHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | if (am != NULL) { |
| 177 | return am; |
| 178 | } |
| 179 | jniThrowException(env, "java/lang/IllegalStateException", "AssetManager has been finalized!"); |
| 180 | return NULL; |
| 181 | } |
| 182 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 183 | static jlong android_content_AssetManager_openAsset(JNIEnv* env, jobject clazz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | jstring fileName, jint mode) |
| 185 | { |
| 186 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 187 | if (am == NULL) { |
| 188 | return 0; |
| 189 | } |
| 190 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 191 | ALOGV("openAsset in %p (Java object %p)\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 193 | ScopedUtfChars fileName8(env, fileName); |
| 194 | if (fileName8.c_str() == NULL) { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 195 | jniThrowException(env, "java/lang/IllegalArgumentException", "Empty file name"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | if (mode != Asset::ACCESS_UNKNOWN && mode != Asset::ACCESS_RANDOM |
| 200 | && mode != Asset::ACCESS_STREAMING && mode != Asset::ACCESS_BUFFER) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 201 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad access mode"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | return -1; |
| 203 | } |
| 204 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 205 | Asset* a = am->open(fileName8.c_str(), (Asset::AccessMode)mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | |
| 207 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 208 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | return -1; |
| 210 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | |
| 212 | //printf("Created Asset Stream: %p\n", a); |
| 213 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 214 | return reinterpret_cast<jlong>(a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static jobject returnParcelFileDescriptor(JNIEnv* env, Asset* a, jlongArray outOffsets) |
| 218 | { |
Kenny Root | ddb76c4 | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 219 | off64_t startOffset, length; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | int fd = a->openFileDescriptor(&startOffset, &length); |
| 221 | delete a; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | if (fd < 0) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 224 | jniThrowException(env, "java/io/FileNotFoundException", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | "This file can not be opened as a file descriptor; it is probably compressed"); |
| 226 | return NULL; |
| 227 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 228 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | jlong* offsets = (jlong*)env->GetPrimitiveArrayCritical(outOffsets, 0); |
| 230 | if (offsets == NULL) { |
| 231 | close(fd); |
| 232 | return NULL; |
| 233 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 234 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | offsets[0] = startOffset; |
| 236 | offsets[1] = length; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 237 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | env->ReleasePrimitiveArrayCritical(outOffsets, offsets, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 239 | |
Elliott Hughes | a3804cf | 2011-04-11 16:50:19 -0700 | [diff] [blame] | 240 | jobject fileDesc = jniCreateFileDescriptor(env, fd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | if (fileDesc == NULL) { |
| 242 | close(fd); |
| 243 | return NULL; |
| 244 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 245 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | return newParcelFileDescriptor(env, fileDesc); |
| 247 | } |
| 248 | |
| 249 | static jobject android_content_AssetManager_openAssetFd(JNIEnv* env, jobject clazz, |
| 250 | jstring fileName, jlongArray outOffsets) |
| 251 | { |
| 252 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 253 | if (am == NULL) { |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 257 | ALOGV("openAssetFd in %p (Java object %p)\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 259 | ScopedUtfChars fileName8(env, fileName); |
| 260 | if (fileName8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | return NULL; |
| 262 | } |
| 263 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 264 | Asset* a = am->open(fileName8.c_str(), Asset::ACCESS_RANDOM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | |
| 266 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 267 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 270 | |
| 271 | //printf("Created Asset Stream: %p\n", a); |
| 272 | |
| 273 | return returnParcelFileDescriptor(env, a, outOffsets); |
| 274 | } |
| 275 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 276 | static jlong android_content_AssetManager_openNonAssetNative(JNIEnv* env, jobject clazz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 277 | jint cookie, |
| 278 | jstring fileName, |
| 279 | jint mode) |
| 280 | { |
| 281 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 282 | if (am == NULL) { |
| 283 | return 0; |
| 284 | } |
| 285 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 286 | ALOGV("openNonAssetNative in %p (Java object %p)\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 288 | ScopedUtfChars fileName8(env, fileName); |
| 289 | if (fileName8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | if (mode != Asset::ACCESS_UNKNOWN && mode != Asset::ACCESS_RANDOM |
| 294 | && mode != Asset::ACCESS_STREAMING && mode != Asset::ACCESS_BUFFER) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 295 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad access mode"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | return -1; |
| 297 | } |
| 298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | Asset* a = cookie |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 300 | ? am->openNonAsset(static_cast<int32_t>(cookie), fileName8.c_str(), |
| 301 | (Asset::AccessMode)mode) |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 302 | : am->openNonAsset(fileName8.c_str(), (Asset::AccessMode)mode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | |
| 304 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 305 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | return -1; |
| 307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 308 | |
| 309 | //printf("Created Asset Stream: %p\n", a); |
| 310 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 311 | return reinterpret_cast<jlong>(a); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static jobject android_content_AssetManager_openNonAssetFdNative(JNIEnv* env, jobject clazz, |
| 315 | jint cookie, |
| 316 | jstring fileName, |
| 317 | jlongArray outOffsets) |
| 318 | { |
| 319 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 320 | if (am == NULL) { |
| 321 | return NULL; |
| 322 | } |
| 323 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 324 | ALOGV("openNonAssetFd in %p (Java object %p)\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 326 | ScopedUtfChars fileName8(env, fileName); |
| 327 | if (fileName8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
| 330 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | Asset* a = cookie |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 332 | ? am->openNonAsset(static_cast<int32_t>(cookie), fileName8.c_str(), Asset::ACCESS_RANDOM) |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 333 | : am->openNonAsset(fileName8.c_str(), Asset::ACCESS_RANDOM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | |
| 335 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 336 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | return NULL; |
| 338 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | |
| 340 | //printf("Created Asset Stream: %p\n", a); |
| 341 | |
| 342 | return returnParcelFileDescriptor(env, a, outOffsets); |
| 343 | } |
| 344 | |
| 345 | static jobjectArray android_content_AssetManager_list(JNIEnv* env, jobject clazz, |
| 346 | jstring fileName) |
| 347 | { |
| 348 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 349 | if (am == NULL) { |
| 350 | return NULL; |
| 351 | } |
| 352 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 353 | ScopedUtfChars fileName8(env, fileName); |
| 354 | if (fileName8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
| 357 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 358 | AssetDir* dir = am->openDir(fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | |
| 360 | if (dir == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 361 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | size_t N = dir->getFileCount(); |
| 366 | |
| 367 | jobjectArray array = env->NewObjectArray(dir->getFileCount(), |
Vladimir Marko | aa5fe3d | 2013-06-17 12:46:22 +0100 | [diff] [blame] | 368 | g_stringClass, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | if (array == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | delete dir; |
| 371 | return NULL; |
| 372 | } |
| 373 | |
| 374 | for (size_t i=0; i<N; i++) { |
| 375 | const String8& name = dir->getFileName(i); |
| 376 | jstring str = env->NewStringUTF(name.string()); |
| 377 | if (str == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | delete dir; |
| 379 | return NULL; |
| 380 | } |
| 381 | env->SetObjectArrayElement(array, i, str); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 382 | env->DeleteLocalRef(str); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | delete dir; |
| 386 | |
| 387 | return array; |
| 388 | } |
| 389 | |
| 390 | static void android_content_AssetManager_destroyAsset(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 391 | jlong assetHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 393 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 394 | |
| 395 | //printf("Destroying Asset Stream: %p\n", a); |
| 396 | |
| 397 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 398 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | return; |
| 400 | } |
| 401 | |
| 402 | delete a; |
| 403 | } |
| 404 | |
| 405 | static jint android_content_AssetManager_readAssetChar(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 406 | jlong assetHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 407 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 408 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | |
| 410 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 411 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | return -1; |
| 413 | } |
| 414 | |
| 415 | uint8_t b; |
| 416 | ssize_t res = a->read(&b, 1); |
| 417 | return res == 1 ? b : -1; |
| 418 | } |
| 419 | |
| 420 | static jint android_content_AssetManager_readAsset(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 421 | jlong assetHandle, jbyteArray bArray, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | jint off, jint len) |
| 423 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 424 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | |
| 426 | if (a == NULL || bArray == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 427 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | return -1; |
| 429 | } |
| 430 | |
| 431 | if (len == 0) { |
| 432 | return 0; |
| 433 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | jsize bLen = env->GetArrayLength(bArray); |
| 436 | if (off < 0 || off >= bLen || len < 0 || len > bLen || (off+len) > bLen) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 437 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | jbyte* b = env->GetByteArrayElements(bArray, NULL); |
| 442 | ssize_t res = a->read(b+off, len); |
| 443 | env->ReleaseByteArrayElements(bArray, b, 0); |
| 444 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 445 | if (res > 0) return static_cast<jint>(res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | |
| 447 | if (res < 0) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 448 | jniThrowException(env, "java/io/IOException", ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | } |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | static jlong android_content_AssetManager_seekAsset(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 454 | jlong assetHandle, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | jlong offset, jint whence) |
| 456 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 457 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | |
| 459 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 460 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | return a->seek( |
| 465 | offset, (whence > 0) ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR)); |
| 466 | } |
| 467 | |
| 468 | static jlong android_content_AssetManager_getAssetLength(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 469 | jlong assetHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 471 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 472 | |
| 473 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 474 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | return -1; |
| 476 | } |
| 477 | |
| 478 | return a->getLength(); |
| 479 | } |
| 480 | |
| 481 | static jlong android_content_AssetManager_getAssetRemainingLength(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 482 | jlong assetHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 484 | Asset* a = reinterpret_cast<Asset*>(assetHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | |
| 486 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 487 | jniThrowNullPointerException(env, "asset"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | return a->getRemainingLength(); |
| 492 | } |
| 493 | |
| 494 | static jint android_content_AssetManager_addAssetPath(JNIEnv* env, jobject clazz, |
| 495 | jstring path) |
| 496 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 497 | ScopedUtfChars path8(env, path); |
| 498 | if (path8.c_str() == NULL) { |
Glenn Kasten | 129e19c | 2012-01-10 17:57:36 -0800 | [diff] [blame] | 499 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 503 | if (am == NULL) { |
Glenn Kasten | 129e19c | 2012-01-10 17:57:36 -0800 | [diff] [blame] | 504 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 507 | int32_t cookie; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 508 | bool res = am->addAssetPath(String8(path8.c_str()), &cookie); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 510 | return (res) ? static_cast<jint>(cookie) : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 513 | static jint android_content_AssetManager_addOverlayPath(JNIEnv* env, jobject clazz, |
| 514 | jstring idmapPath) |
| 515 | { |
| 516 | ScopedUtfChars idmapPath8(env, idmapPath); |
| 517 | if (idmapPath8.c_str() == NULL) { |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 522 | if (am == NULL) { |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | int32_t cookie; |
| 527 | bool res = am->addOverlayPath(String8(idmapPath8.c_str()), &cookie); |
| 528 | |
| 529 | return (res) ? (jint)cookie : 0; |
| 530 | } |
| 531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | static jboolean android_content_AssetManager_isUpToDate(JNIEnv* env, jobject clazz) |
| 533 | { |
| 534 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 535 | if (am == NULL) { |
| 536 | return JNI_TRUE; |
| 537 | } |
| 538 | return am->isUpToDate() ? JNI_TRUE : JNI_FALSE; |
| 539 | } |
| 540 | |
| 541 | static void android_content_AssetManager_setLocale(JNIEnv* env, jobject clazz, |
| 542 | jstring locale) |
| 543 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 544 | ScopedUtfChars locale8(env, locale); |
| 545 | if (locale8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | return; |
| 547 | } |
| 548 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 550 | if (am == NULL) { |
| 551 | return; |
| 552 | } |
| 553 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 554 | am->setLocale(locale8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static jobjectArray android_content_AssetManager_getLocales(JNIEnv* env, jobject clazz) |
| 558 | { |
| 559 | Vector<String8> locales; |
| 560 | |
| 561 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 562 | if (am == NULL) { |
| 563 | return NULL; |
| 564 | } |
| 565 | |
| 566 | am->getLocales(&locales); |
| 567 | |
| 568 | const int N = locales.size(); |
| 569 | |
| 570 | jobjectArray result = env->NewObjectArray(N, g_stringClass, NULL); |
| 571 | if (result == NULL) { |
| 572 | return NULL; |
| 573 | } |
| 574 | |
| 575 | for (int i=0; i<N; i++) { |
Gilles Debunne | 0db187a | 2010-08-27 11:51:34 -0700 | [diff] [blame] | 576 | jstring str = env->NewStringUTF(locales[i].string()); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 577 | if (str == NULL) { |
| 578 | return NULL; |
| 579 | } |
| 580 | env->SetObjectArrayElement(result, i, str); |
| 581 | env->DeleteLocalRef(str); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | return result; |
| 585 | } |
| 586 | |
| 587 | static void android_content_AssetManager_setConfiguration(JNIEnv* env, jobject clazz, |
| 588 | jint mcc, jint mnc, |
| 589 | jstring locale, jint orientation, |
| 590 | jint touchscreen, jint density, |
| 591 | jint keyboard, jint keyboardHidden, |
| 592 | jint navigation, |
| 593 | jint screenWidth, jint screenHeight, |
Dianne Hackborn | 69cb875 | 2011-05-19 18:13:32 -0700 | [diff] [blame] | 594 | jint smallestScreenWidthDp, |
Dianne Hackborn | 3fc982f | 2011-03-30 16:20:26 -0700 | [diff] [blame] | 595 | jint screenWidthDp, jint screenHeightDp, |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 596 | jint screenLayout, jint uiMode, |
| 597 | jint sdkVersion) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | { |
| 599 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 600 | if (am == NULL) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | ResTable_config config; |
| 605 | memset(&config, 0, sizeof(config)); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | const char* locale8 = locale != NULL ? env->GetStringUTFChars(locale, NULL) : NULL; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 608 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | config.mcc = (uint16_t)mcc; |
| 610 | config.mnc = (uint16_t)mnc; |
| 611 | config.orientation = (uint8_t)orientation; |
| 612 | config.touchscreen = (uint8_t)touchscreen; |
| 613 | config.density = (uint16_t)density; |
| 614 | config.keyboard = (uint8_t)keyboard; |
Dianne Hackborn | c4db95c | 2009-07-21 17:46:02 -0700 | [diff] [blame] | 615 | config.inputFlags = (uint8_t)keyboardHidden; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | config.navigation = (uint8_t)navigation; |
| 617 | config.screenWidth = (uint16_t)screenWidth; |
| 618 | config.screenHeight = (uint16_t)screenHeight; |
Dianne Hackborn | 69cb875 | 2011-05-19 18:13:32 -0700 | [diff] [blame] | 619 | config.smallestScreenWidthDp = (uint16_t)smallestScreenWidthDp; |
Dianne Hackborn | 3fc982f | 2011-03-30 16:20:26 -0700 | [diff] [blame] | 620 | config.screenWidthDp = (uint16_t)screenWidthDp; |
| 621 | config.screenHeightDp = (uint16_t)screenHeightDp; |
Dianne Hackborn | 723738c | 2009-06-25 19:48:04 -0700 | [diff] [blame] | 622 | config.screenLayout = (uint8_t)screenLayout; |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 623 | config.uiMode = (uint8_t)uiMode; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | config.sdkVersion = (uint16_t)sdkVersion; |
| 625 | config.minorVersion = 0; |
| 626 | am->setConfiguration(config, locale8); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 627 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | if (locale != NULL) env->ReleaseStringUTFChars(locale, locale8); |
| 629 | } |
| 630 | |
| 631 | static jint android_content_AssetManager_getResourceIdentifier(JNIEnv* env, jobject clazz, |
| 632 | jstring name, |
| 633 | jstring defType, |
| 634 | jstring defPackage) |
| 635 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 636 | ScopedStringChars name16(env, name); |
| 637 | if (name16.get() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 642 | if (am == NULL) { |
| 643 | return 0; |
| 644 | } |
| 645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | const char16_t* defType16 = defType |
| 647 | ? env->GetStringChars(defType, NULL) : NULL; |
| 648 | jsize defTypeLen = defType |
| 649 | ? env->GetStringLength(defType) : 0; |
| 650 | const char16_t* defPackage16 = defPackage |
| 651 | ? env->GetStringChars(defPackage, NULL) : NULL; |
| 652 | jsize defPackageLen = defPackage |
| 653 | ? env->GetStringLength(defPackage) : 0; |
| 654 | |
| 655 | jint ident = am->getResources().identifierForName( |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 656 | name16.get(), name16.size(), defType16, defTypeLen, defPackage16, defPackageLen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | |
| 658 | if (defPackage16) { |
| 659 | env->ReleaseStringChars(defPackage, defPackage16); |
| 660 | } |
| 661 | if (defType16) { |
| 662 | env->ReleaseStringChars(defType, defType16); |
| 663 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | |
| 665 | return ident; |
| 666 | } |
| 667 | |
| 668 | static jstring android_content_AssetManager_getResourceName(JNIEnv* env, jobject clazz, |
| 669 | jint resid) |
| 670 | { |
| 671 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 672 | if (am == NULL) { |
| 673 | return NULL; |
| 674 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 675 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | ResTable::resource_name name; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 677 | if (!am->getResources().getResourceName(resid, true, &name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 680 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | String16 str; |
| 682 | if (name.package != NULL) { |
| 683 | str.setTo(name.package, name.packageLen); |
| 684 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 685 | if (name.type8 != NULL || name.type != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | if (str.size() > 0) { |
| 687 | char16_t div = ':'; |
| 688 | str.append(&div, 1); |
| 689 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 690 | if (name.type8 != NULL) { |
| 691 | str.append(String16(name.type8, name.typeLen)); |
| 692 | } else { |
| 693 | str.append(name.type, name.typeLen); |
| 694 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 695 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 696 | if (name.name8 != NULL || name.name != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | if (str.size() > 0) { |
| 698 | char16_t div = '/'; |
| 699 | str.append(&div, 1); |
| 700 | } |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 701 | if (name.name8 != NULL) { |
| 702 | str.append(String16(name.name8, name.nameLen)); |
| 703 | } else { |
| 704 | str.append(name.name, name.nameLen); |
| 705 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | return env->NewString((const jchar*)str.string(), str.size()); |
| 709 | } |
| 710 | |
| 711 | static jstring android_content_AssetManager_getResourcePackageName(JNIEnv* env, jobject clazz, |
| 712 | jint resid) |
| 713 | { |
| 714 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 715 | if (am == NULL) { |
| 716 | return NULL; |
| 717 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 718 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | ResTable::resource_name name; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 720 | if (!am->getResources().getResourceName(resid, true, &name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | return NULL; |
| 722 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 723 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | if (name.package != NULL) { |
| 725 | return env->NewString((const jchar*)name.package, name.packageLen); |
| 726 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | return NULL; |
| 729 | } |
| 730 | |
| 731 | static jstring android_content_AssetManager_getResourceTypeName(JNIEnv* env, jobject clazz, |
| 732 | jint resid) |
| 733 | { |
| 734 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 735 | if (am == NULL) { |
| 736 | return NULL; |
| 737 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 738 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | ResTable::resource_name name; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 740 | if (!am->getResources().getResourceName(resid, true, &name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | return NULL; |
| 742 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 743 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 744 | if (name.type8 != NULL) { |
| 745 | return env->NewStringUTF(name.type8); |
| 746 | } |
| 747 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 748 | if (name.type != NULL) { |
| 749 | return env->NewString((const jchar*)name.type, name.typeLen); |
| 750 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | static jstring android_content_AssetManager_getResourceEntryName(JNIEnv* env, jobject clazz, |
| 756 | jint resid) |
| 757 | { |
| 758 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 759 | if (am == NULL) { |
| 760 | return NULL; |
| 761 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 762 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | ResTable::resource_name name; |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 764 | if (!am->getResources().getResourceName(resid, true, &name)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | return NULL; |
| 766 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 767 | |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 768 | if (name.name8 != NULL) { |
| 769 | return env->NewStringUTF(name.name8); |
| 770 | } |
| 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | if (name.name != NULL) { |
| 773 | return env->NewString((const jchar*)name.name, name.nameLen); |
| 774 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | return NULL; |
| 777 | } |
| 778 | |
| 779 | static jint android_content_AssetManager_loadResourceValue(JNIEnv* env, jobject clazz, |
| 780 | jint ident, |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 781 | jshort density, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | jobject outValue, |
| 783 | jboolean resolve) |
| 784 | { |
Dianne Hackborn | 1f7d307 | 2013-02-11 17:03:32 -0800 | [diff] [blame] | 785 | if (outValue == NULL) { |
Dianne Hackborn | e5b50a6 | 2013-02-11 16:18:42 -0800 | [diff] [blame] | 786 | jniThrowNullPointerException(env, "outValue"); |
Dianne Hackborn | d45c68d | 2013-07-31 12:14:24 -0700 | [diff] [blame] | 787 | return 0; |
Dianne Hackborn | e5b50a6 | 2013-02-11 16:18:42 -0800 | [diff] [blame] | 788 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 790 | if (am == NULL) { |
| 791 | return 0; |
| 792 | } |
| 793 | const ResTable& res(am->getResources()); |
| 794 | |
| 795 | Res_value value; |
| 796 | ResTable_config config; |
| 797 | uint32_t typeSpecFlags; |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 798 | ssize_t block = res.getResource(ident, &value, false, density, &typeSpecFlags, &config); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 799 | #if THROW_ON_BAD_ID |
| 800 | if (block == BAD_INDEX) { |
| 801 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 802 | return 0; |
| 803 | } |
| 804 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | uint32_t ref = ident; |
| 806 | if (resolve) { |
Dianne Hackborn | fb5c3db | 2012-05-18 15:24:24 -0700 | [diff] [blame] | 807 | block = res.resolveReference(&value, block, &ref, &typeSpecFlags, &config); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 808 | #if THROW_ON_BAD_ID |
| 809 | if (block == BAD_INDEX) { |
| 810 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 811 | return 0; |
| 812 | } |
| 813 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 814 | } |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 815 | if (block >= 0) { |
| 816 | return copyValue(env, outValue, &res, value, ref, block, typeSpecFlags, &config); |
| 817 | } |
| 818 | |
| 819 | return static_cast<jint>(block); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | static jint android_content_AssetManager_loadResourceBagValue(JNIEnv* env, jobject clazz, |
| 823 | jint ident, jint bagEntryId, |
| 824 | jobject outValue, jboolean resolve) |
| 825 | { |
| 826 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 827 | if (am == NULL) { |
| 828 | return 0; |
| 829 | } |
| 830 | const ResTable& res(am->getResources()); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 831 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | // Now lock down the resource object and start pulling stuff from it. |
| 833 | res.lock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 834 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | ssize_t block = -1; |
| 836 | Res_value value; |
| 837 | |
| 838 | const ResTable::bag_entry* entry = NULL; |
| 839 | uint32_t typeSpecFlags; |
| 840 | ssize_t entryCount = res.getBagLocked(ident, &entry, &typeSpecFlags); |
| 841 | |
| 842 | for (ssize_t i=0; i<entryCount; i++) { |
| 843 | if (((uint32_t)bagEntryId) == entry->map.name.ident) { |
| 844 | block = entry->stringBlock; |
| 845 | value = entry->map.value; |
| 846 | } |
| 847 | entry++; |
| 848 | } |
| 849 | |
| 850 | res.unlock(); |
| 851 | |
| 852 | if (block < 0) { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 853 | return static_cast<jint>(block); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 855 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | uint32_t ref = ident; |
| 857 | if (resolve) { |
| 858 | block = res.resolveReference(&value, block, &ref, &typeSpecFlags); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 859 | #if THROW_ON_BAD_ID |
| 860 | if (block == BAD_INDEX) { |
| 861 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 862 | return 0; |
| 863 | } |
| 864 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 865 | } |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 866 | if (block >= 0) { |
| 867 | return copyValue(env, outValue, &res, value, ref, block, typeSpecFlags); |
| 868 | } |
| 869 | |
| 870 | return static_cast<jint>(block); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static jint android_content_AssetManager_getStringBlockCount(JNIEnv* env, jobject clazz) |
| 874 | { |
| 875 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 876 | if (am == NULL) { |
| 877 | return 0; |
| 878 | } |
| 879 | return am->getResources().getTableCount(); |
| 880 | } |
| 881 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 882 | static jlong android_content_AssetManager_getNativeStringBlock(JNIEnv* env, jobject clazz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 883 | jint block) |
| 884 | { |
| 885 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 886 | if (am == NULL) { |
| 887 | return 0; |
| 888 | } |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 889 | return reinterpret_cast<jlong>(am->getResources().getTableStringBlock(block)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static jstring android_content_AssetManager_getCookieName(JNIEnv* env, jobject clazz, |
| 893 | jint cookie) |
| 894 | { |
| 895 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 896 | if (am == NULL) { |
| 897 | return NULL; |
| 898 | } |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 899 | String8 name(am->getAssetPath(static_cast<int32_t>(cookie))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | if (name.length() == 0) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 901 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", "Empty cookie name"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | return NULL; |
| 903 | } |
| 904 | jstring str = env->NewStringUTF(name.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | return str; |
| 906 | } |
| 907 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 908 | static jlong android_content_AssetManager_newTheme(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 909 | { |
| 910 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 911 | if (am == NULL) { |
| 912 | return 0; |
| 913 | } |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 914 | return reinterpret_cast<jlong>(new ResTable::Theme(am->getResources())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static void android_content_AssetManager_deleteTheme(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 918 | jlong themeHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 920 | ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | delete theme; |
| 922 | } |
| 923 | |
| 924 | static void android_content_AssetManager_applyThemeStyle(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 925 | jlong themeHandle, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | jint styleRes, |
| 927 | jboolean force) |
| 928 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 929 | ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | theme->applyStyle(styleRes, force ? true : false); |
| 931 | } |
| 932 | |
| 933 | static void android_content_AssetManager_copyTheme(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 934 | jlong destHandle, jlong srcHandle) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 935 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 936 | ResTable::Theme* dest = reinterpret_cast<ResTable::Theme*>(destHandle); |
| 937 | ResTable::Theme* src = reinterpret_cast<ResTable::Theme*>(srcHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | dest->setTo(*src); |
| 939 | } |
| 940 | |
| 941 | static jint android_content_AssetManager_loadThemeAttributeValue( |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 942 | JNIEnv* env, jobject clazz, jlong themeHandle, jint ident, jobject outValue, jboolean resolve) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 944 | ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | const ResTable& res(theme->getResTable()); |
| 946 | |
| 947 | Res_value value; |
| 948 | // XXX value could be different in different configs! |
| 949 | uint32_t typeSpecFlags = 0; |
| 950 | ssize_t block = theme->getAttribute(ident, &value, &typeSpecFlags); |
| 951 | uint32_t ref = 0; |
| 952 | if (resolve) { |
| 953 | block = res.resolveReference(&value, block, &ref, &typeSpecFlags); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 954 | #if THROW_ON_BAD_ID |
| 955 | if (block == BAD_INDEX) { |
| 956 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 957 | return 0; |
| 958 | } |
| 959 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
| 961 | return block >= 0 ? copyValue(env, outValue, &res, value, ref, block, typeSpecFlags) : block; |
| 962 | } |
| 963 | |
| 964 | static void android_content_AssetManager_dumpTheme(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 965 | jlong themeHandle, jint pri, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 966 | jstring tag, jstring prefix) |
| 967 | { |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 968 | ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeHandle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | const ResTable& res(theme->getResTable()); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 970 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | // XXX Need to use params. |
| 972 | theme->dumpToLog(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | static jboolean android_content_AssetManager_applyStyle(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 976 | jlong themeToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | jint defStyleAttr, |
| 978 | jint defStyleRes, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 979 | jlong xmlParserToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | jintArray attrs, |
| 981 | jintArray outValues, |
| 982 | jintArray outIndices) |
| 983 | { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 984 | if (themeToken == 0) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 985 | jniThrowNullPointerException(env, "theme token"); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 986 | return JNI_FALSE; |
| 987 | } |
| 988 | if (attrs == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 989 | jniThrowNullPointerException(env, "attrs"); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 990 | return JNI_FALSE; |
| 991 | } |
| 992 | if (outValues == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 993 | jniThrowNullPointerException(env, "out values"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | return JNI_FALSE; |
| 995 | } |
| 996 | |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 997 | DEBUG_STYLES(LOGI("APPLY STYLE: theme=0x%x defStyleAttr=0x%x defStyleRes=0x%x xml=0x%x", |
| 998 | themeToken, defStyleAttr, defStyleRes, xmlParserToken)); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 999 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1000 | ResTable::Theme* theme = reinterpret_cast<ResTable::Theme*>(themeToken); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | const ResTable& res = theme->getResTable(); |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1002 | ResXMLParser* xmlParser = reinterpret_cast<ResXMLParser*>(xmlParserToken); |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1003 | ResTable_config config; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | Res_value value; |
| 1005 | |
| 1006 | const jsize NI = env->GetArrayLength(attrs); |
| 1007 | const jsize NV = env->GetArrayLength(outValues); |
| 1008 | if (NV < (NI*STYLE_NUM_ENTRIES)) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1009 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", "out values too small"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1010 | return JNI_FALSE; |
| 1011 | } |
| 1012 | |
| 1013 | jint* src = (jint*)env->GetPrimitiveArrayCritical(attrs, 0); |
| 1014 | if (src == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | return JNI_FALSE; |
| 1016 | } |
| 1017 | |
| 1018 | jint* baseDest = (jint*)env->GetPrimitiveArrayCritical(outValues, 0); |
| 1019 | jint* dest = baseDest; |
| 1020 | if (dest == NULL) { |
| 1021 | env->ReleasePrimitiveArrayCritical(attrs, src, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1022 | return JNI_FALSE; |
| 1023 | } |
| 1024 | |
| 1025 | jint* indices = NULL; |
| 1026 | int indicesIdx = 0; |
| 1027 | if (outIndices != NULL) { |
| 1028 | if (env->GetArrayLength(outIndices) > NI) { |
| 1029 | indices = (jint*)env->GetPrimitiveArrayCritical(outIndices, 0); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | // Load default style from attribute, if specified... |
| 1034 | uint32_t defStyleBagTypeSetFlags = 0; |
| 1035 | if (defStyleAttr != 0) { |
| 1036 | Res_value value; |
| 1037 | if (theme->getAttribute(defStyleAttr, &value, &defStyleBagTypeSetFlags) >= 0) { |
| 1038 | if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 1039 | defStyleRes = value.data; |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | // Retrieve the style class associated with the current XML tag. |
| 1045 | int style = 0; |
| 1046 | uint32_t styleBagTypeSetFlags = 0; |
| 1047 | if (xmlParser != NULL) { |
| 1048 | ssize_t idx = xmlParser->indexOfStyle(); |
| 1049 | if (idx >= 0 && xmlParser->getAttributeValue(idx, &value) >= 0) { |
| 1050 | if (value.dataType == value.TYPE_ATTRIBUTE) { |
| 1051 | if (theme->getAttribute(value.data, &value, &styleBagTypeSetFlags) < 0) { |
| 1052 | value.dataType = Res_value::TYPE_NULL; |
| 1053 | } |
| 1054 | } |
| 1055 | if (value.dataType == value.TYPE_REFERENCE) { |
| 1056 | style = value.data; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | // Now lock down the resource object and start pulling stuff from it. |
| 1062 | res.lock(); |
| 1063 | |
| 1064 | // Retrieve the default style bag, if requested. |
| 1065 | const ResTable::bag_entry* defStyleEnt = NULL; |
| 1066 | uint32_t defStyleTypeSetFlags = 0; |
| 1067 | ssize_t bagOff = defStyleRes != 0 |
| 1068 | ? res.getBagLocked(defStyleRes, &defStyleEnt, &defStyleTypeSetFlags) : -1; |
| 1069 | defStyleTypeSetFlags |= defStyleBagTypeSetFlags; |
| 1070 | const ResTable::bag_entry* endDefStyleEnt = defStyleEnt + |
| 1071 | (bagOff >= 0 ? bagOff : 0); |
| 1072 | |
| 1073 | // Retrieve the style class bag, if requested. |
| 1074 | const ResTable::bag_entry* styleEnt = NULL; |
| 1075 | uint32_t styleTypeSetFlags = 0; |
| 1076 | bagOff = style != 0 ? res.getBagLocked(style, &styleEnt, &styleTypeSetFlags) : -1; |
| 1077 | styleTypeSetFlags |= styleBagTypeSetFlags; |
| 1078 | const ResTable::bag_entry* endStyleEnt = styleEnt + |
| 1079 | (bagOff >= 0 ? bagOff : 0); |
| 1080 | |
| 1081 | // Retrieve the XML attributes, if requested. |
| 1082 | const jsize NX = xmlParser ? xmlParser->getAttributeCount() : 0; |
| 1083 | jsize ix=0; |
| 1084 | uint32_t curXmlAttr = xmlParser ? xmlParser->getAttributeNameResID(ix) : 0; |
| 1085 | |
| 1086 | static const ssize_t kXmlBlock = 0x10000000; |
| 1087 | |
| 1088 | // Now iterate through all of the attributes that the client has requested, |
| 1089 | // filling in each with whatever data we can find. |
| 1090 | ssize_t block = 0; |
| 1091 | uint32_t typeSetFlags; |
| 1092 | for (jsize ii=0; ii<NI; ii++) { |
| 1093 | const uint32_t curIdent = (uint32_t)src[ii]; |
| 1094 | |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1095 | DEBUG_STYLES(LOGI("RETRIEVING ATTR 0x%08x...", curIdent)); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1096 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | // Try to find a value for this attribute... we prioritize values |
| 1098 | // coming from, first XML attributes, then XML style, then default |
| 1099 | // style, and finally the theme. |
| 1100 | value.dataType = Res_value::TYPE_NULL; |
| 1101 | value.data = 0; |
| 1102 | typeSetFlags = 0; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1103 | config.density = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | |
| 1105 | // Skip through XML attributes until the end or the next possible match. |
| 1106 | while (ix < NX && curIdent > curXmlAttr) { |
| 1107 | ix++; |
| 1108 | curXmlAttr = xmlParser->getAttributeNameResID(ix); |
| 1109 | } |
| 1110 | // Retrieve the current XML attribute if it matches, and step to next. |
| 1111 | if (ix < NX && curIdent == curXmlAttr) { |
| 1112 | block = kXmlBlock; |
| 1113 | xmlParser->getAttributeValue(ix, &value); |
| 1114 | ix++; |
| 1115 | curXmlAttr = xmlParser->getAttributeNameResID(ix); |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1116 | DEBUG_STYLES(LOGI("-> From XML: type=0x%x, data=0x%08x", |
| 1117 | value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | // Skip through the style values until the end or the next possible match. |
| 1121 | while (styleEnt < endStyleEnt && curIdent > styleEnt->map.name.ident) { |
| 1122 | styleEnt++; |
| 1123 | } |
| 1124 | // Retrieve the current style attribute if it matches, and step to next. |
| 1125 | if (styleEnt < endStyleEnt && curIdent == styleEnt->map.name.ident) { |
| 1126 | if (value.dataType == Res_value::TYPE_NULL) { |
| 1127 | block = styleEnt->stringBlock; |
| 1128 | typeSetFlags = styleTypeSetFlags; |
| 1129 | value = styleEnt->map.value; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1130 | DEBUG_STYLES(LOGI("-> From style: type=0x%x, data=0x%08x", |
| 1131 | value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1132 | } |
| 1133 | styleEnt++; |
| 1134 | } |
| 1135 | |
| 1136 | // Skip through the default style values until the end or the next possible match. |
| 1137 | while (defStyleEnt < endDefStyleEnt && curIdent > defStyleEnt->map.name.ident) { |
| 1138 | defStyleEnt++; |
| 1139 | } |
| 1140 | // Retrieve the current default style attribute if it matches, and step to next. |
| 1141 | if (defStyleEnt < endDefStyleEnt && curIdent == defStyleEnt->map.name.ident) { |
| 1142 | if (value.dataType == Res_value::TYPE_NULL) { |
| 1143 | block = defStyleEnt->stringBlock; |
| 1144 | typeSetFlags = defStyleTypeSetFlags; |
| 1145 | value = defStyleEnt->map.value; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1146 | DEBUG_STYLES(LOGI("-> From def style: type=0x%x, data=0x%08x", |
| 1147 | value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } |
| 1149 | defStyleEnt++; |
| 1150 | } |
| 1151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1152 | uint32_t resid = 0; |
| 1153 | if (value.dataType != Res_value::TYPE_NULL) { |
| 1154 | // Take care of resolving the found resource to its final value. |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1155 | ssize_t newBlock = theme->resolveAttributeReference(&value, block, |
| 1156 | &resid, &typeSetFlags, &config); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | if (newBlock >= 0) block = newBlock; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1158 | DEBUG_STYLES(LOGI("-> Resolved attr: type=0x%x, data=0x%08x", |
| 1159 | value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | } else { |
| 1161 | // If we still don't have a value for this attribute, try to find |
| 1162 | // it in the theme! |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1163 | ssize_t newBlock = theme->getAttribute(curIdent, &value, &typeSetFlags); |
| 1164 | if (newBlock >= 0) { |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1165 | DEBUG_STYLES(LOGI("-> From theme: type=0x%x, data=0x%08x", |
| 1166 | value.dataType, value.data)); |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1167 | newBlock = res.resolveReference(&value, block, &resid, |
| 1168 | &typeSetFlags, &config); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1169 | #if THROW_ON_BAD_ID |
| 1170 | if (newBlock == BAD_INDEX) { |
| 1171 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1172 | return JNI_FALSE; |
| 1173 | } |
| 1174 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | if (newBlock >= 0) block = newBlock; |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1176 | DEBUG_STYLES(LOGI("-> Resolved theme: type=0x%x, data=0x%08x", |
| 1177 | value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 1182 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1183 | DEBUG_STYLES(LOGI("-> Setting to @null!")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1184 | value.dataType = Res_value::TYPE_NULL; |
Kenny Root | 7fbe4d2 | 2011-01-20 13:15:44 -0800 | [diff] [blame] | 1185 | block = kXmlBlock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Dianne Hackborn | b8d8167 | 2009-11-20 14:26:42 -0800 | [diff] [blame] | 1188 | DEBUG_STYLES(LOGI("Attribute 0x%08x: type=0x%x, data=0x%08x", |
| 1189 | curIdent, value.dataType, value.data)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | |
| 1191 | // Write the final value back to Java. |
| 1192 | dest[STYLE_TYPE] = value.dataType; |
| 1193 | dest[STYLE_DATA] = value.data; |
| 1194 | dest[STYLE_ASSET_COOKIE] = |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1195 | block != kXmlBlock ? reinterpret_cast<jint>(res.getTableCookie(block)) : (jint)-1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | dest[STYLE_RESOURCE_ID] = resid; |
| 1197 | dest[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1198 | dest[STYLE_DENSITY] = config.density; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | if (indices != NULL && value.dataType != Res_value::TYPE_NULL) { |
| 1201 | indicesIdx++; |
| 1202 | indices[indicesIdx] = ii; |
| 1203 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | dest += STYLE_NUM_ENTRIES; |
| 1206 | } |
| 1207 | |
| 1208 | res.unlock(); |
| 1209 | |
| 1210 | if (indices != NULL) { |
| 1211 | indices[0] = indicesIdx; |
| 1212 | env->ReleasePrimitiveArrayCritical(outIndices, indices, 0); |
| 1213 | } |
| 1214 | env->ReleasePrimitiveArrayCritical(outValues, baseDest, 0); |
| 1215 | env->ReleasePrimitiveArrayCritical(attrs, src, 0); |
| 1216 | |
| 1217 | return JNI_TRUE; |
| 1218 | } |
| 1219 | |
| 1220 | static jboolean android_content_AssetManager_retrieveAttributes(JNIEnv* env, jobject clazz, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1221 | jlong xmlParserToken, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | jintArray attrs, |
| 1223 | jintArray outValues, |
| 1224 | jintArray outIndices) |
| 1225 | { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1226 | if (xmlParserToken == 0) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1227 | jniThrowNullPointerException(env, "xmlParserToken"); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1228 | return JNI_FALSE; |
| 1229 | } |
| 1230 | if (attrs == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1231 | jniThrowNullPointerException(env, "attrs"); |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1232 | return JNI_FALSE; |
| 1233 | } |
| 1234 | if (outValues == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1235 | jniThrowNullPointerException(env, "out values"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | return JNI_FALSE; |
| 1237 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1238 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1239 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1240 | if (am == NULL) { |
| 1241 | return JNI_FALSE; |
| 1242 | } |
| 1243 | const ResTable& res(am->getResources()); |
| 1244 | ResXMLParser* xmlParser = (ResXMLParser*)xmlParserToken; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1245 | ResTable_config config; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1246 | Res_value value; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1248 | const jsize NI = env->GetArrayLength(attrs); |
| 1249 | const jsize NV = env->GetArrayLength(outValues); |
| 1250 | if (NV < (NI*STYLE_NUM_ENTRIES)) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1251 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", "out values too small"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | return JNI_FALSE; |
| 1253 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | jint* src = (jint*)env->GetPrimitiveArrayCritical(attrs, 0); |
| 1256 | if (src == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | return JNI_FALSE; |
| 1258 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1259 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | jint* baseDest = (jint*)env->GetPrimitiveArrayCritical(outValues, 0); |
| 1261 | jint* dest = baseDest; |
| 1262 | if (dest == NULL) { |
| 1263 | env->ReleasePrimitiveArrayCritical(attrs, src, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | return JNI_FALSE; |
| 1265 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1266 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | jint* indices = NULL; |
| 1268 | int indicesIdx = 0; |
| 1269 | if (outIndices != NULL) { |
| 1270 | if (env->GetArrayLength(outIndices) > NI) { |
| 1271 | indices = (jint*)env->GetPrimitiveArrayCritical(outIndices, 0); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | // Now lock down the resource object and start pulling stuff from it. |
| 1276 | res.lock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1278 | // Retrieve the XML attributes, if requested. |
| 1279 | const jsize NX = xmlParser->getAttributeCount(); |
| 1280 | jsize ix=0; |
| 1281 | uint32_t curXmlAttr = xmlParser->getAttributeNameResID(ix); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | static const ssize_t kXmlBlock = 0x10000000; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | // Now iterate through all of the attributes that the client has requested, |
| 1286 | // filling in each with whatever data we can find. |
| 1287 | ssize_t block = 0; |
| 1288 | uint32_t typeSetFlags; |
| 1289 | for (jsize ii=0; ii<NI; ii++) { |
| 1290 | const uint32_t curIdent = (uint32_t)src[ii]; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | // Try to find a value for this attribute... |
| 1293 | value.dataType = Res_value::TYPE_NULL; |
| 1294 | value.data = 0; |
| 1295 | typeSetFlags = 0; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1296 | config.density = 0; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1298 | // Skip through XML attributes until the end or the next possible match. |
| 1299 | while (ix < NX && curIdent > curXmlAttr) { |
| 1300 | ix++; |
| 1301 | curXmlAttr = xmlParser->getAttributeNameResID(ix); |
| 1302 | } |
| 1303 | // Retrieve the current XML attribute if it matches, and step to next. |
| 1304 | if (ix < NX && curIdent == curXmlAttr) { |
| 1305 | block = kXmlBlock; |
| 1306 | xmlParser->getAttributeValue(ix, &value); |
| 1307 | ix++; |
| 1308 | curXmlAttr = xmlParser->getAttributeNameResID(ix); |
| 1309 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | //printf("Attribute 0x%08x: type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data); |
| 1312 | uint32_t resid = 0; |
| 1313 | if (value.dataType != Res_value::TYPE_NULL) { |
| 1314 | // Take care of resolving the found resource to its final value. |
| 1315 | //printf("Resolving attribute reference\n"); |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1316 | ssize_t newBlock = res.resolveReference(&value, block, &resid, |
| 1317 | &typeSetFlags, &config); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1318 | #if THROW_ON_BAD_ID |
| 1319 | if (newBlock == BAD_INDEX) { |
| 1320 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1321 | return JNI_FALSE; |
| 1322 | } |
| 1323 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1324 | if (newBlock >= 0) block = newBlock; |
| 1325 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 1328 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 1329 | value.dataType = Res_value::TYPE_NULL; |
| 1330 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | //printf("Attribute 0x%08x: final type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | // Write the final value back to Java. |
| 1335 | dest[STYLE_TYPE] = value.dataType; |
| 1336 | dest[STYLE_DATA] = value.data; |
| 1337 | dest[STYLE_ASSET_COOKIE] = |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1338 | block != kXmlBlock ? reinterpret_cast<jint>(res.getTableCookie(block)) : (jint)-1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | dest[STYLE_RESOURCE_ID] = resid; |
| 1340 | dest[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1341 | dest[STYLE_DENSITY] = config.density; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1342 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | if (indices != NULL && value.dataType != Res_value::TYPE_NULL) { |
| 1344 | indicesIdx++; |
| 1345 | indices[indicesIdx] = ii; |
| 1346 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | dest += STYLE_NUM_ENTRIES; |
| 1349 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | res.unlock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1352 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | if (indices != NULL) { |
| 1354 | indices[0] = indicesIdx; |
| 1355 | env->ReleasePrimitiveArrayCritical(outIndices, indices, 0); |
| 1356 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1357 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | env->ReleasePrimitiveArrayCritical(outValues, baseDest, 0); |
| 1359 | env->ReleasePrimitiveArrayCritical(attrs, src, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1361 | return JNI_TRUE; |
| 1362 | } |
| 1363 | |
| 1364 | static jint android_content_AssetManager_getArraySize(JNIEnv* env, jobject clazz, |
| 1365 | jint id) |
| 1366 | { |
| 1367 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1368 | if (am == NULL) { |
Olivier Bailly | d7c8672 | 2010-11-18 14:43:36 -0800 | [diff] [blame] | 1369 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | } |
| 1371 | const ResTable& res(am->getResources()); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1372 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | res.lock(); |
| 1374 | const ResTable::bag_entry* defStyleEnt = NULL; |
| 1375 | ssize_t bagOff = res.getBagLocked(id, &defStyleEnt); |
| 1376 | res.unlock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1377 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1378 | return static_cast<jint>(bagOff); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | static jint android_content_AssetManager_retrieveArray(JNIEnv* env, jobject clazz, |
| 1382 | jint id, |
| 1383 | jintArray outValues) |
| 1384 | { |
| 1385 | if (outValues == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1386 | jniThrowNullPointerException(env, "out values"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | return JNI_FALSE; |
| 1388 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1391 | if (am == NULL) { |
| 1392 | return JNI_FALSE; |
| 1393 | } |
| 1394 | const ResTable& res(am->getResources()); |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1395 | ResTable_config config; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | Res_value value; |
| 1397 | ssize_t block; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1399 | const jsize NV = env->GetArrayLength(outValues); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1401 | jint* baseDest = (jint*)env->GetPrimitiveArrayCritical(outValues, 0); |
| 1402 | jint* dest = baseDest; |
| 1403 | if (dest == NULL) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1404 | jniThrowException(env, "java/lang/OutOfMemoryError", ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | return JNI_FALSE; |
| 1406 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | // Now lock down the resource object and start pulling stuff from it. |
| 1409 | res.lock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1410 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1411 | const ResTable::bag_entry* arrayEnt = NULL; |
| 1412 | uint32_t arrayTypeSetFlags = 0; |
| 1413 | ssize_t bagOff = res.getBagLocked(id, &arrayEnt, &arrayTypeSetFlags); |
| 1414 | const ResTable::bag_entry* endArrayEnt = arrayEnt + |
| 1415 | (bagOff >= 0 ? bagOff : 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | int i = 0; |
| 1418 | uint32_t typeSetFlags; |
| 1419 | while (i < NV && arrayEnt < endArrayEnt) { |
| 1420 | block = arrayEnt->stringBlock; |
| 1421 | typeSetFlags = arrayTypeSetFlags; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1422 | config.density = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | value = arrayEnt->map.value; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1424 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1425 | uint32_t resid = 0; |
| 1426 | if (value.dataType != Res_value::TYPE_NULL) { |
| 1427 | // Take care of resolving the found resource to its final value. |
| 1428 | //printf("Resolving attribute reference\n"); |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1429 | ssize_t newBlock = res.resolveReference(&value, block, &resid, |
| 1430 | &typeSetFlags, &config); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1431 | #if THROW_ON_BAD_ID |
| 1432 | if (newBlock == BAD_INDEX) { |
| 1433 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1434 | return JNI_FALSE; |
| 1435 | } |
| 1436 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1437 | if (newBlock >= 0) block = newBlock; |
| 1438 | } |
| 1439 | |
| 1440 | // Deal with the special @null value -- it turns back to TYPE_NULL. |
| 1441 | if (value.dataType == Res_value::TYPE_REFERENCE && value.data == 0) { |
| 1442 | value.dataType = Res_value::TYPE_NULL; |
| 1443 | } |
| 1444 | |
| 1445 | //printf("Attribute 0x%08x: final type=0x%x, data=0x%08x\n", curIdent, value.dataType, value.data); |
| 1446 | |
| 1447 | // Write the final value back to Java. |
| 1448 | dest[STYLE_TYPE] = value.dataType; |
| 1449 | dest[STYLE_DATA] = value.data; |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1450 | dest[STYLE_ASSET_COOKIE] = reinterpret_cast<jint>(res.getTableCookie(block)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | dest[STYLE_RESOURCE_ID] = resid; |
| 1452 | dest[STYLE_CHANGING_CONFIGURATIONS] = typeSetFlags; |
Dianne Hackborn | 0d22101 | 2009-07-29 15:41:19 -0700 | [diff] [blame] | 1453 | dest[STYLE_DENSITY] = config.density; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | dest += STYLE_NUM_ENTRIES; |
| 1455 | i+= STYLE_NUM_ENTRIES; |
| 1456 | arrayEnt++; |
| 1457 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1458 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | i /= STYLE_NUM_ENTRIES; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1460 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | res.unlock(); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | env->ReleasePrimitiveArrayCritical(outValues, baseDest, 0); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1464 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | return i; |
| 1466 | } |
| 1467 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1468 | static jlong android_content_AssetManager_openXmlAssetNative(JNIEnv* env, jobject clazz, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1469 | jint cookie, |
| 1470 | jstring fileName) |
| 1471 | { |
| 1472 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1473 | if (am == NULL) { |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1477 | ALOGV("openXmlAsset in %p (Java object %p)\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1478 | |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1479 | ScopedUtfChars fileName8(env, fileName); |
| 1480 | if (fileName8.c_str() == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | return 0; |
| 1482 | } |
| 1483 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | Asset* a = cookie |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 1485 | ? am->openNonAsset(static_cast<int32_t>(cookie), fileName8.c_str(), Asset::ACCESS_BUFFER) |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1486 | : am->openNonAsset(fileName8.c_str(), Asset::ACCESS_BUFFER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | |
| 1488 | if (a == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1489 | jniThrowException(env, "java/io/FileNotFoundException", fileName8.c_str()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | return 0; |
| 1491 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | |
| 1493 | ResXMLTree* block = new ResXMLTree(); |
| 1494 | status_t err = block->setTo(a->getBuffer(true), a->getLength(), true); |
| 1495 | a->close(); |
| 1496 | delete a; |
| 1497 | |
| 1498 | if (err != NO_ERROR) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1499 | jniThrowException(env, "java/io/FileNotFoundException", "Corrupt XML binary file"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1500 | return 0; |
| 1501 | } |
| 1502 | |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1503 | return reinterpret_cast<jlong>(block); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static jintArray android_content_AssetManager_getArrayStringInfo(JNIEnv* env, jobject clazz, |
| 1507 | jint arrayResId) |
| 1508 | { |
| 1509 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1510 | if (am == NULL) { |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | const ResTable& res(am->getResources()); |
| 1514 | |
| 1515 | const ResTable::bag_entry* startOfBag; |
| 1516 | const ssize_t N = res.lockBag(arrayResId, &startOfBag); |
| 1517 | if (N < 0) { |
| 1518 | return NULL; |
| 1519 | } |
| 1520 | |
| 1521 | jintArray array = env->NewIntArray(N * 2); |
| 1522 | if (array == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1523 | res.unlockBag(startOfBag); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
| 1527 | Res_value value; |
| 1528 | const ResTable::bag_entry* bag = startOfBag; |
| 1529 | for (size_t i = 0, j = 0; ((ssize_t)i)<N; i++, bag++) { |
| 1530 | jint stringIndex = -1; |
| 1531 | jint stringBlock = 0; |
| 1532 | value = bag->map.value; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | // Take care of resolving the found resource to its final value. |
| 1535 | stringBlock = res.resolveReference(&value, bag->stringBlock, NULL); |
| 1536 | if (value.dataType == Res_value::TYPE_STRING) { |
| 1537 | stringIndex = value.data; |
| 1538 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1539 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1540 | #if THROW_ON_BAD_ID |
| 1541 | if (stringBlock == BAD_INDEX) { |
| 1542 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1543 | return array; |
| 1544 | } |
| 1545 | #endif |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1546 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1547 | //todo: It might be faster to allocate a C array to contain |
| 1548 | // the blocknums and indices, put them in there and then |
| 1549 | // do just one SetIntArrayRegion() |
| 1550 | env->SetIntArrayRegion(array, j, 1, &stringBlock); |
| 1551 | env->SetIntArrayRegion(array, j + 1, 1, &stringIndex); |
| 1552 | j = j + 2; |
| 1553 | } |
| 1554 | res.unlockBag(startOfBag); |
| 1555 | return array; |
| 1556 | } |
| 1557 | |
| 1558 | static jobjectArray android_content_AssetManager_getArrayStringResource(JNIEnv* env, jobject clazz, |
| 1559 | jint arrayResId) |
| 1560 | { |
| 1561 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1562 | if (am == NULL) { |
| 1563 | return NULL; |
| 1564 | } |
| 1565 | const ResTable& res(am->getResources()); |
| 1566 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1567 | const ResTable::bag_entry* startOfBag; |
| 1568 | const ssize_t N = res.lockBag(arrayResId, &startOfBag); |
| 1569 | if (N < 0) { |
| 1570 | return NULL; |
| 1571 | } |
| 1572 | |
Vladimir Marko | aa5fe3d | 2013-06-17 12:46:22 +0100 | [diff] [blame] | 1573 | jobjectArray array = env->NewObjectArray(N, g_stringClass, NULL); |
Kenny Root | 485dd21 | 2010-05-06 16:06:48 -0700 | [diff] [blame] | 1574 | if (env->ExceptionCheck()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1575 | res.unlockBag(startOfBag); |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | |
| 1579 | Res_value value; |
| 1580 | const ResTable::bag_entry* bag = startOfBag; |
| 1581 | size_t strLen = 0; |
| 1582 | for (size_t i=0; ((ssize_t)i)<N; i++, bag++) { |
| 1583 | value = bag->map.value; |
| 1584 | jstring str = NULL; |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 1585 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1586 | // Take care of resolving the found resource to its final value. |
| 1587 | ssize_t block = res.resolveReference(&value, bag->stringBlock, NULL); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1588 | #if THROW_ON_BAD_ID |
| 1589 | if (block == BAD_INDEX) { |
| 1590 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1591 | return array; |
| 1592 | } |
| 1593 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | if (value.dataType == Res_value::TYPE_STRING) { |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 1595 | const ResStringPool* pool = res.getTableStringBlock(block); |
| 1596 | const char* str8 = pool->string8At(value.data, &strLen); |
| 1597 | if (str8 != NULL) { |
| 1598 | str = env->NewStringUTF(str8); |
| 1599 | } else { |
| 1600 | const char16_t* str16 = pool->stringAt(value.data, &strLen); |
| 1601 | str = env->NewString(str16, strLen); |
Kenny Root | 485dd21 | 2010-05-06 16:06:48 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | // If one of our NewString{UTF} calls failed due to memory, an |
| 1605 | // exception will be pending. |
| 1606 | if (env->ExceptionCheck()) { |
| 1607 | res.unlockBag(startOfBag); |
| 1608 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
Kenny Root | 780d2a1 | 2010-02-22 22:36:26 -0800 | [diff] [blame] | 1610 | |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1611 | env->SetObjectArrayElement(array, i, str); |
Kenny Root | 485dd21 | 2010-05-06 16:06:48 -0700 | [diff] [blame] | 1612 | |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1613 | // str is not NULL at that point, otherwise ExceptionCheck would have been true. |
| 1614 | // If we have a large amount of strings in our array, we might |
| 1615 | // overflow the local reference table of the VM. |
Kenny Root | 485dd21 | 2010-05-06 16:06:48 -0700 | [diff] [blame] | 1616 | env->DeleteLocalRef(str); |
| 1617 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | } |
| 1619 | res.unlockBag(startOfBag); |
| 1620 | return array; |
| 1621 | } |
| 1622 | |
| 1623 | static jintArray android_content_AssetManager_getArrayIntResource(JNIEnv* env, jobject clazz, |
| 1624 | jint arrayResId) |
| 1625 | { |
| 1626 | AssetManager* am = assetManagerForJavaObject(env, clazz); |
| 1627 | if (am == NULL) { |
| 1628 | return NULL; |
| 1629 | } |
| 1630 | const ResTable& res(am->getResources()); |
| 1631 | |
| 1632 | const ResTable::bag_entry* startOfBag; |
| 1633 | const ssize_t N = res.lockBag(arrayResId, &startOfBag); |
| 1634 | if (N < 0) { |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | |
| 1638 | jintArray array = env->NewIntArray(N); |
| 1639 | if (array == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1640 | res.unlockBag(startOfBag); |
| 1641 | return NULL; |
| 1642 | } |
| 1643 | |
| 1644 | Res_value value; |
| 1645 | const ResTable::bag_entry* bag = startOfBag; |
| 1646 | for (size_t i=0; ((ssize_t)i)<N; i++, bag++) { |
| 1647 | value = bag->map.value; |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1648 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | // Take care of resolving the found resource to its final value. |
| 1650 | ssize_t block = res.resolveReference(&value, bag->stringBlock, NULL); |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 1651 | #if THROW_ON_BAD_ID |
| 1652 | if (block == BAD_INDEX) { |
| 1653 | jniThrowException(env, "java/lang/IllegalStateException", "Bad resource!"); |
| 1654 | return array; |
| 1655 | } |
| 1656 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | if (value.dataType >= Res_value::TYPE_FIRST_INT |
| 1658 | && value.dataType <= Res_value::TYPE_LAST_INT) { |
| 1659 | int intVal = value.data; |
| 1660 | env->SetIntArrayRegion(array, i, 1, &intVal); |
| 1661 | } |
| 1662 | } |
| 1663 | res.unlockBag(startOfBag); |
| 1664 | return array; |
| 1665 | } |
| 1666 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 1667 | static void android_content_AssetManager_init(JNIEnv* env, jobject clazz, jboolean isSystem) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | { |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 1669 | if (isSystem) { |
| 1670 | verifySystemIdmaps(); |
| 1671 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1672 | AssetManager* am = new AssetManager(); |
| 1673 | if (am == NULL) { |
Gilles Debunne | 4d4040b | 2010-08-26 15:59:54 -0700 | [diff] [blame] | 1674 | jniThrowException(env, "java/lang/OutOfMemoryError", ""); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | am->addDefaultAssets(); |
| 1679 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1680 | ALOGV("Created AssetManager %p for Java object %p\n", am, clazz); |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1681 | env->SetLongField(clazz, gAssetManagerOffsets.mObject, reinterpret_cast<jlong>(am)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | static void android_content_AssetManager_destroy(JNIEnv* env, jobject clazz) |
| 1685 | { |
| 1686 | AssetManager* am = (AssetManager*) |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1687 | (env->GetLongField(clazz, gAssetManagerOffsets.mObject)); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1688 | ALOGV("Destroying AssetManager %p for Java object %p\n", am, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | if (am != NULL) { |
| 1690 | delete am; |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1691 | env->SetLongField(clazz, gAssetManagerOffsets.mObject, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | static jint android_content_AssetManager_getGlobalAssetCount(JNIEnv* env, jobject clazz) |
| 1696 | { |
| 1697 | return Asset::getGlobalCount(); |
| 1698 | } |
| 1699 | |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 1700 | static jobject android_content_AssetManager_getAssetAllocations(JNIEnv* env, jobject clazz) |
| 1701 | { |
| 1702 | String8 alloc = Asset::getAssetAllocations(); |
| 1703 | if (alloc.length() <= 0) { |
| 1704 | return NULL; |
| 1705 | } |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1706 | |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 1707 | jstring str = env->NewStringUTF(alloc.string()); |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 1708 | return str; |
| 1709 | } |
| 1710 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | static jint android_content_AssetManager_getGlobalAssetManagerCount(JNIEnv* env, jobject clazz) |
| 1712 | { |
| 1713 | return AssetManager::getGlobalCount(); |
| 1714 | } |
| 1715 | |
| 1716 | // ---------------------------------------------------------------------------- |
| 1717 | |
| 1718 | /* |
| 1719 | * JNI registration. |
| 1720 | */ |
| 1721 | static JNINativeMethod gAssetManagerMethods[] = { |
| 1722 | /* name, signature, funcPtr */ |
| 1723 | |
| 1724 | // Basic asset stuff. |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1725 | { "openAsset", "(Ljava/lang/String;I)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | (void*) android_content_AssetManager_openAsset }, |
| 1727 | { "openAssetFd", "(Ljava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", |
| 1728 | (void*) android_content_AssetManager_openAssetFd }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1729 | { "openNonAssetNative", "(ILjava/lang/String;I)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1730 | (void*) android_content_AssetManager_openNonAssetNative }, |
| 1731 | { "openNonAssetFdNative", "(ILjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", |
| 1732 | (void*) android_content_AssetManager_openNonAssetFdNative }, |
| 1733 | { "list", "(Ljava/lang/String;)[Ljava/lang/String;", |
| 1734 | (void*) android_content_AssetManager_list }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1735 | { "destroyAsset", "(J)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1736 | (void*) android_content_AssetManager_destroyAsset }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1737 | { "readAssetChar", "(J)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1738 | (void*) android_content_AssetManager_readAssetChar }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1739 | { "readAsset", "(J[BII)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1740 | (void*) android_content_AssetManager_readAsset }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1741 | { "seekAsset", "(JJI)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1742 | (void*) android_content_AssetManager_seekAsset }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1743 | { "getAssetLength", "(J)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1744 | (void*) android_content_AssetManager_getAssetLength }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1745 | { "getAssetRemainingLength", "(J)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1746 | (void*) android_content_AssetManager_getAssetRemainingLength }, |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 1747 | { "addAssetPathNative", "(Ljava/lang/String;)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1748 | (void*) android_content_AssetManager_addAssetPath }, |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 1749 | { "addOverlayPath", "(Ljava/lang/String;)I", |
| 1750 | (void*) android_content_AssetManager_addOverlayPath }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1751 | { "isUpToDate", "()Z", |
| 1752 | (void*) android_content_AssetManager_isUpToDate }, |
| 1753 | |
| 1754 | // Resources. |
| 1755 | { "setLocale", "(Ljava/lang/String;)V", |
| 1756 | (void*) android_content_AssetManager_setLocale }, |
| 1757 | { "getLocales", "()[Ljava/lang/String;", |
| 1758 | (void*) android_content_AssetManager_getLocales }, |
Dianne Hackborn | 69cb875 | 2011-05-19 18:13:32 -0700 | [diff] [blame] | 1759 | { "setConfiguration", "(IILjava/lang/String;IIIIIIIIIIIIII)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1760 | (void*) android_content_AssetManager_setConfiguration }, |
| 1761 | { "getResourceIdentifier","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", |
| 1762 | (void*) android_content_AssetManager_getResourceIdentifier }, |
| 1763 | { "getResourceName","(I)Ljava/lang/String;", |
| 1764 | (void*) android_content_AssetManager_getResourceName }, |
| 1765 | { "getResourcePackageName","(I)Ljava/lang/String;", |
| 1766 | (void*) android_content_AssetManager_getResourcePackageName }, |
| 1767 | { "getResourceTypeName","(I)Ljava/lang/String;", |
| 1768 | (void*) android_content_AssetManager_getResourceTypeName }, |
| 1769 | { "getResourceEntryName","(I)Ljava/lang/String;", |
| 1770 | (void*) android_content_AssetManager_getResourceEntryName }, |
Kenny Root | 55fc850 | 2010-10-28 14:47:01 -0700 | [diff] [blame] | 1771 | { "loadResourceValue","(ISLandroid/util/TypedValue;Z)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | (void*) android_content_AssetManager_loadResourceValue }, |
| 1773 | { "loadResourceBagValue","(IILandroid/util/TypedValue;Z)I", |
| 1774 | (void*) android_content_AssetManager_loadResourceBagValue }, |
| 1775 | { "getStringBlockCount","()I", |
| 1776 | (void*) android_content_AssetManager_getStringBlockCount }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1777 | { "getNativeStringBlock","(I)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | (void*) android_content_AssetManager_getNativeStringBlock }, |
| 1779 | { "getCookieName","(I)Ljava/lang/String;", |
| 1780 | (void*) android_content_AssetManager_getCookieName }, |
| 1781 | |
| 1782 | // Themes. |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1783 | { "newTheme", "()J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | (void*) android_content_AssetManager_newTheme }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1785 | { "deleteTheme", "(J)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | (void*) android_content_AssetManager_deleteTheme }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1787 | { "applyThemeStyle", "(JIZ)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | (void*) android_content_AssetManager_applyThemeStyle }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1789 | { "copyTheme", "(JJ)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1790 | (void*) android_content_AssetManager_copyTheme }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1791 | { "loadThemeAttributeValue", "(JILandroid/util/TypedValue;Z)I", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | (void*) android_content_AssetManager_loadThemeAttributeValue }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1793 | { "dumpTheme", "(JILjava/lang/String;Ljava/lang/String;)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1794 | (void*) android_content_AssetManager_dumpTheme }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1795 | { "applyStyle","(JIIJ[I[I[I)Z", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1796 | (void*) android_content_AssetManager_applyStyle }, |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1797 | { "retrieveAttributes","(J[I[I[I)Z", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1798 | (void*) android_content_AssetManager_retrieveAttributes }, |
| 1799 | { "getArraySize","(I)I", |
| 1800 | (void*) android_content_AssetManager_getArraySize }, |
| 1801 | { "retrieveArray","(I[I)I", |
| 1802 | (void*) android_content_AssetManager_retrieveArray }, |
| 1803 | |
| 1804 | // XML files. |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1805 | { "openXmlAssetNative", "(ILjava/lang/String;)J", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1806 | (void*) android_content_AssetManager_openXmlAssetNative }, |
| 1807 | |
| 1808 | // Arrays. |
| 1809 | { "getArrayStringResource","(I)[Ljava/lang/String;", |
| 1810 | (void*) android_content_AssetManager_getArrayStringResource }, |
| 1811 | { "getArrayStringInfo","(I)[I", |
| 1812 | (void*) android_content_AssetManager_getArrayStringInfo }, |
| 1813 | { "getArrayIntResource","(I)[I", |
| 1814 | (void*) android_content_AssetManager_getArrayIntResource }, |
| 1815 | |
| 1816 | // Bookkeeping. |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame^] | 1817 | { "init", "(Z)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1818 | (void*) android_content_AssetManager_init }, |
| 1819 | { "destroy", "()V", |
| 1820 | (void*) android_content_AssetManager_destroy }, |
| 1821 | { "getGlobalAssetCount", "()I", |
| 1822 | (void*) android_content_AssetManager_getGlobalAssetCount }, |
Dianne Hackborn | 82e1ee9 | 2009-08-11 18:56:41 -0700 | [diff] [blame] | 1823 | { "getAssetAllocations", "()Ljava/lang/String;", |
| 1824 | (void*) android_content_AssetManager_getAssetAllocations }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1825 | { "getGlobalAssetManagerCount", "()I", |
| 1826 | (void*) android_content_AssetManager_getGlobalAssetCount }, |
| 1827 | }; |
| 1828 | |
| 1829 | int register_android_content_AssetManager(JNIEnv* env) |
| 1830 | { |
| 1831 | jclass typedValue = env->FindClass("android/util/TypedValue"); |
| 1832 | LOG_FATAL_IF(typedValue == NULL, "Unable to find class android/util/TypedValue"); |
| 1833 | gTypedValueOffsets.mType |
| 1834 | = env->GetFieldID(typedValue, "type", "I"); |
| 1835 | LOG_FATAL_IF(gTypedValueOffsets.mType == NULL, "Unable to find TypedValue.type"); |
| 1836 | gTypedValueOffsets.mData |
| 1837 | = env->GetFieldID(typedValue, "data", "I"); |
| 1838 | LOG_FATAL_IF(gTypedValueOffsets.mData == NULL, "Unable to find TypedValue.data"); |
| 1839 | gTypedValueOffsets.mString |
| 1840 | = env->GetFieldID(typedValue, "string", "Ljava/lang/CharSequence;"); |
| 1841 | LOG_FATAL_IF(gTypedValueOffsets.mString == NULL, "Unable to find TypedValue.string"); |
| 1842 | gTypedValueOffsets.mAssetCookie |
| 1843 | = env->GetFieldID(typedValue, "assetCookie", "I"); |
| 1844 | LOG_FATAL_IF(gTypedValueOffsets.mAssetCookie == NULL, "Unable to find TypedValue.assetCookie"); |
| 1845 | gTypedValueOffsets.mResourceId |
| 1846 | = env->GetFieldID(typedValue, "resourceId", "I"); |
| 1847 | LOG_FATAL_IF(gTypedValueOffsets.mResourceId == NULL, "Unable to find TypedValue.resourceId"); |
| 1848 | gTypedValueOffsets.mChangingConfigurations |
| 1849 | = env->GetFieldID(typedValue, "changingConfigurations", "I"); |
| 1850 | LOG_FATAL_IF(gTypedValueOffsets.mChangingConfigurations == NULL, "Unable to find TypedValue.changingConfigurations"); |
| 1851 | gTypedValueOffsets.mDensity = env->GetFieldID(typedValue, "density", "I"); |
| 1852 | LOG_FATAL_IF(gTypedValueOffsets.mDensity == NULL, "Unable to find TypedValue.density"); |
| 1853 | |
| 1854 | jclass assetFd = env->FindClass("android/content/res/AssetFileDescriptor"); |
| 1855 | LOG_FATAL_IF(assetFd == NULL, "Unable to find class android/content/res/AssetFileDescriptor"); |
| 1856 | gAssetFileDescriptorOffsets.mFd |
| 1857 | = env->GetFieldID(assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;"); |
| 1858 | LOG_FATAL_IF(gAssetFileDescriptorOffsets.mFd == NULL, "Unable to find AssetFileDescriptor.mFd"); |
| 1859 | gAssetFileDescriptorOffsets.mStartOffset |
| 1860 | = env->GetFieldID(assetFd, "mStartOffset", "J"); |
| 1861 | LOG_FATAL_IF(gAssetFileDescriptorOffsets.mStartOffset == NULL, "Unable to find AssetFileDescriptor.mStartOffset"); |
| 1862 | gAssetFileDescriptorOffsets.mLength |
| 1863 | = env->GetFieldID(assetFd, "mLength", "J"); |
| 1864 | LOG_FATAL_IF(gAssetFileDescriptorOffsets.mLength == NULL, "Unable to find AssetFileDescriptor.mLength"); |
| 1865 | |
| 1866 | jclass assetManager = env->FindClass("android/content/res/AssetManager"); |
| 1867 | LOG_FATAL_IF(assetManager == NULL, "Unable to find class android/content/res/AssetManager"); |
| 1868 | gAssetManagerOffsets.mObject |
Ashok Bhat | 896043d | 2014-01-17 16:02:38 +0000 | [diff] [blame] | 1869 | = env->GetFieldID(assetManager, "mObject", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1870 | LOG_FATAL_IF(gAssetManagerOffsets.mObject == NULL, "Unable to find AssetManager.mObject"); |
| 1871 | |
Carl Shapiro | c1318ba | 2011-03-03 14:22:28 -0800 | [diff] [blame] | 1872 | jclass stringClass = env->FindClass("java/lang/String"); |
| 1873 | LOG_FATAL_IF(stringClass == NULL, "Unable to find class java/lang/String"); |
| 1874 | g_stringClass = (jclass)env->NewGlobalRef(stringClass); |
Vladimir Marko | aa5fe3d | 2013-06-17 12:46:22 +0100 | [diff] [blame] | 1875 | LOG_FATAL_IF(g_stringClass == NULL, "Unable to create global reference for class java/lang/String"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | |
| 1877 | return AndroidRuntime::registerNativeMethods(env, |
| 1878 | "android/content/res/AssetManager", gAssetManagerMethods, NELEM(gAssetManagerMethods)); |
| 1879 | } |
| 1880 | |
| 1881 | }; // namespace android |