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