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