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