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