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