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