From 4563c31b3cfa8a80a22d094f744c6485db0609e6 Mon Sep 17 00:00:00 2001 From: devashru Date: Thu, 1 Jul 2021 11:59:26 -0700 Subject: [PATCH] Running Google formatting on ShadowArscAssetManager files. This is in preparation for converting the version of directlyOn(...) that takes a method name as a String to better understand the true delta. PiperOrigin-RevId: 382573741 --- .../shadows/ShadowArscAssetManager10.java | 2887 +++++++++-------- .../shadows/ShadowArscAssetManager9.java | 448 ++- 2 files changed, 1763 insertions(+), 1572 deletions(-) diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager10.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager10.java index 6445b6fade4..9487234ac2f 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager10.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager10.java @@ -1,7 +1,8 @@ package org.robolectric.shadows; // TODO: update path to released version. -// transliterated from https://android.googlesource.com/platform/frameworks/base/+/android-10.0.0_rXX/core/jni/android_util_AssetManager.cpp +// transliterated from +// https://android.googlesource.com/platform/frameworks/base/+/android-10.0.0_rXX/core/jni/android_util_AssetManager.cpp import static android.os.Build.VERSION_CODES.P; import static android.os.Build.VERSION_CODES.Q; @@ -80,30 +81,32 @@ import org.robolectric.util.ReflectionHelpers; import org.robolectric.util.ReflectionHelpers.ClassParameter; -@Implements(value = AssetManager.class, minSdk = Build.VERSION_CODES.Q, - shadowPicker = ShadowAssetManager.Picker.class) +@Implements( + value = AssetManager.class, + minSdk = Build.VERSION_CODES.Q, + shadowPicker = ShadowAssetManager.Picker.class) @SuppressWarnings("NewApi") public class ShadowArscAssetManager10 extends ShadowAssetManager.ArscBase { - // Offsets into the outValues array populated by the methods below. outValues is a uint32_t - // array, but each logical element takes up 7 uint32_t-sized physical elements. - // Keep these in sync with android.content.res.TypedArray java class - private static final int STYLE_NUM_ENTRIES = 7; - private static final int STYLE_TYPE = 0; - private static final int STYLE_DATA = 1; - private static final int STYLE_ASSET_COOKIE = 2; - private static final int STYLE_RESOURCE_ID = 3; - private static final int STYLE_CHANGING_CONFIGURATIONS = 4; - private static final int STYLE_DENSITY = 5; - private static final int STYLE_SOURCE_STYLE_RESOURCE_ID = 6; - - private static CppAssetManager2 systemCppAssetManager2; - private static long systemCppAssetManager2Ref; - private static boolean inNonSystemConstructor; - private static ApkAssets[] cachedSystemApkAssets; - private static ArraySet cachedSystemApkAssetsSet; - - @RealObject AssetManager realAssetManager; + // Offsets into the outValues array populated by the methods below. outValues is a uint32_t + // array, but each logical element takes up 7 uint32_t-sized physical elements. + // Keep these in sync with android.content.res.TypedArray java class + private static final int STYLE_NUM_ENTRIES = 7; + private static final int STYLE_TYPE = 0; + private static final int STYLE_DATA = 1; + private static final int STYLE_ASSET_COOKIE = 2; + private static final int STYLE_RESOURCE_ID = 3; + private static final int STYLE_CHANGING_CONFIGURATIONS = 4; + private static final int STYLE_DENSITY = 5; + private static final int STYLE_SOURCE_STYLE_RESOURCE_ID = 6; + + private static CppAssetManager2 systemCppAssetManager2; + private static long systemCppAssetManager2Ref; + private static boolean inNonSystemConstructor; + private static ApkAssets[] cachedSystemApkAssets; + private static ArraySet cachedSystemApkAssetsSet; + + @RealObject AssetManager realAssetManager; // @RealObject // protected AssetManager realObject; @@ -207,11 +210,11 @@ public class ShadowArscAssetManager10 extends ShadowAssetManager.ArscBase { @Implementation(maxSdk = Q) protected static void createSystemAssetsInZygoteLocked() { - _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class); - AssetManager sSystem = _assetManagerStatic_.getSystem(); - if (sSystem != null) { - return; - } + _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class); + AssetManager sSystem = _assetManagerStatic_.getSystem(); + if (sSystem != null) { + return; + } if (systemCppAssetManager2 == null) { // first time! let the framework create a CppAssetManager2 and an AssetManager, which we'll @@ -224,8 +227,9 @@ protected static void createSystemAssetsInZygoteLocked() { _assetManagerStatic_.setSystemApkAssets(cachedSystemApkAssets); _assetManagerStatic_.setSystemApkAssetsSet(cachedSystemApkAssetsSet); - sSystem = ReflectionHelpers.callConstructor(AssetManager.class, - ClassParameter.from(boolean.class, true /*sentinel*/)); + sSystem = + ReflectionHelpers.callConstructor( + AssetManager.class, ClassParameter.from(boolean.class, true /*sentinel*/)); sSystem.setApkAssets(cachedSystemApkAssets, false /*invalidateCaches*/); ReflectionHelpers.setStaticField(AssetManager.class, "sSystem", sSystem); } @@ -233,7 +237,8 @@ protected static void createSystemAssetsInZygoteLocked() { @Resetter public static void reset() { - // todo: ShadowPicker doesn't discriminate properly between concrete shadow classes for resetters... + // todo: ShadowPicker doesn't discriminate properly between concrete shadow classes for + // resetters... if (!useLegacy() && RuntimeEnvironment.getApiLevel() >= P) { _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class); _assetManagerStatic_.setSystemApkAssetsSet(null); @@ -242,130 +247,135 @@ public static void reset() { } } - // Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0. - static int ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) { - return cookie.intValue() != kInvalidCookie ? (cookie.intValue() + 1) : -1; - } + // Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0. + static int ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) { + return cookie.intValue() != kInvalidCookie ? (cookie.intValue() + 1) : -1; + } - static ApkAssetsCookie JavaCookieToApkAssetsCookie(int cookie) { - return ApkAssetsCookie.forInt(cookie > 0 ? (cookie - 1) : kInvalidCookie); - } + static ApkAssetsCookie JavaCookieToApkAssetsCookie(int cookie) { + return ApkAssetsCookie.forInt(cookie > 0 ? (cookie - 1) : kInvalidCookie); + } // This is called by zygote (running as user root) as part of preloadResources. // static void NativeVerifySystemIdmaps(JNIEnv* /*env*/, jclass /*clazz*/) { @Implementation(minSdk = P, maxSdk = Q) protected static void nativeVerifySystemIdmaps() { - return; - - // todo: maybe implement? - // switch (pid_t pid = fork()) { - // case -1: - // PLOG(ERROR) << "failed to fork for idmap"; - // break; - // - // // child - // case 0: { - // struct __user_cap_header_struct capheader; - // struct __user_cap_data_struct capdata; - // - // memset(&capheader, 0, sizeof(capheader)); - // memset(&capdata, 0, sizeof(capdata)); - // - // capheader.version = _LINUX_CAPABILITY_VERSION; - // capheader.pid = 0; - // - // if (capget(&capheader, &capdata) != 0) { - // PLOG(ERROR) << "capget"; - // exit(1); - // } - // - // capdata.effective = capdata.permitted; - // if (capset(&capheader, &capdata) != 0) { - // PLOG(ERROR) << "capset"; - // exit(1); - // } - // - // if (setgid(AID_SYSTEM) != 0) { - // PLOG(ERROR) << "setgid"; - // exit(1); - // } - // - // if (setuid(AID_SYSTEM) != 0) { - // PLOG(ERROR) << "setuid"; - // exit(1); - // } - // - // // Generic idmap parameters - // char* argv[8]; - // int argc = 0; - // struct stat st; - // - // memset(argv, 0, sizeof(argv)); - // argv[argc++] = AssetManager.IDMAP_BIN; - // argv[argc++] = "--scan"; - // argv[argc++] = AssetManager.TARGET_PACKAGE_NAME; - // argv[argc++] = AssetManager.TARGET_APK_PATH; - // argv[argc++] = AssetManager.IDMAP_DIR; - // - // // Directories to scan for overlays: if OVERLAY_THEME_DIR_PROPERTY is defined, - // // use OVERLAY_DIR/ in addition to OVERLAY_DIR. - // String overlay_theme_path = base.GetProperty(AssetManager.OVERLAY_THEME_DIR_PROPERTY, - // ""); - // if (!overlay_theme_path.empty()) { - // overlay_theme_path = String(AssetManager.OVERLAY_DIR) + "/" + overlay_theme_path; - // if (stat(overlay_theme_path, &st) == 0) { - // argv[argc++] = overlay_theme_path; - // } - // } - // - // if (stat(AssetManager.OVERLAY_DIR, &st) == 0) { - // argv[argc++] = AssetManager.OVERLAY_DIR; - // } - // - // if (stat(AssetManager.PRODUCT_OVERLAY_DIR, &st) == 0) { - // argv[argc++] = AssetManager.PRODUCT_OVERLAY_DIR; - // } - // - // // Finally, invoke idmap (if any overlay directory exists) - // if (argc > 5) { - // execv(AssetManager.IDMAP_BIN, (char* const*)argv); - // PLOG(ERROR) << "failed to execv for idmap"; - // exit(1); // should never get here - // } else { - // exit(0); - // } - // } break; - // - // // parent - // default: - // waitpid(pid, null, 0); - // break; - // } - } + return; + + // todo: maybe implement? + // switch (pid_t pid = fork()) { + // case -1: + // PLOG(ERROR) << "failed to fork for idmap"; + // break; + // + // // child + // case 0: { + // struct __user_cap_header_struct capheader; + // struct __user_cap_data_struct capdata; + // + // memset(&capheader, 0, sizeof(capheader)); + // memset(&capdata, 0, sizeof(capdata)); + // + // capheader.version = _LINUX_CAPABILITY_VERSION; + // capheader.pid = 0; + // + // if (capget(&capheader, &capdata) != 0) { + // PLOG(ERROR) << "capget"; + // exit(1); + // } + // + // capdata.effective = capdata.permitted; + // if (capset(&capheader, &capdata) != 0) { + // PLOG(ERROR) << "capset"; + // exit(1); + // } + // + // if (setgid(AID_SYSTEM) != 0) { + // PLOG(ERROR) << "setgid"; + // exit(1); + // } + // + // if (setuid(AID_SYSTEM) != 0) { + // PLOG(ERROR) << "setuid"; + // exit(1); + // } + // + // // Generic idmap parameters + // char* argv[8]; + // int argc = 0; + // struct stat st; + // + // memset(argv, 0, sizeof(argv)); + // argv[argc++] = AssetManager.IDMAP_BIN; + // argv[argc++] = "--scan"; + // argv[argc++] = AssetManager.TARGET_PACKAGE_NAME; + // argv[argc++] = AssetManager.TARGET_APK_PATH; + // argv[argc++] = AssetManager.IDMAP_DIR; + // + // // Directories to scan for overlays: if OVERLAY_THEME_DIR_PROPERTY is defined, + // // use OVERLAY_DIR/ in addition to OVERLAY_DIR. + // String overlay_theme_path = base.GetProperty(AssetManager.OVERLAY_THEME_DIR_PROPERTY, + // ""); + // if (!overlay_theme_path.empty()) { + // overlay_theme_path = String(AssetManager.OVERLAY_DIR) + "/" + overlay_theme_path; + // if (stat(overlay_theme_path, &st) == 0) { + // argv[argc++] = overlay_theme_path; + // } + // } + // + // if (stat(AssetManager.OVERLAY_DIR, &st) == 0) { + // argv[argc++] = AssetManager.OVERLAY_DIR; + // } + // + // if (stat(AssetManager.PRODUCT_OVERLAY_DIR, &st) == 0) { + // argv[argc++] = AssetManager.PRODUCT_OVERLAY_DIR; + // } + // + // // Finally, invoke idmap (if any overlay directory exists) + // if (argc > 5) { + // execv(AssetManager.IDMAP_BIN, (char* const*)argv); + // PLOG(ERROR) << "failed to execv for idmap"; + // exit(1); // should never get here + // } else { + // exit(0); + // } + // } break; + // + // // parent + // default: + // waitpid(pid, null, 0); + // break; + // } + } @Implementation(minSdk = Build.VERSION_CODES.Q, maxSdk = Build.VERSION_CODES.R) protected static String[] nativeCreateIdmapsForStaticOverlaysTargetingAndroid() { - return new String[0]; - } - - static int CopyValue(/*JNIEnv* env,*/ ApkAssetsCookie cookie, Res_value value, int ref, - int type_spec_flags, ResTable_config config, TypedValue out_typed_value) { - out_typed_value.type = value.dataType; - out_typed_value.assetCookie = ApkAssetsCookieToJavaCookie(cookie); - out_typed_value.data = value.data; - out_typed_value.string = null; - out_typed_value.resourceId = ref; - out_typed_value.changingConfigurations = type_spec_flags; - if (config != null) { - out_typed_value.density = config.density; - } - return (int) (ApkAssetsCookieToJavaCookie(cookie)); + return new String[0]; + } + + static int CopyValue( + /*JNIEnv* env,*/ ApkAssetsCookie cookie, + Res_value value, + int ref, + int type_spec_flags, + ResTable_config config, + TypedValue out_typed_value) { + out_typed_value.type = value.dataType; + out_typed_value.assetCookie = ApkAssetsCookieToJavaCookie(cookie); + out_typed_value.data = value.data; + out_typed_value.string = null; + out_typed_value.resourceId = ref; + out_typed_value.changingConfigurations = type_spec_flags; + if (config != null) { + out_typed_value.density = config.density; } + return (int) (ApkAssetsCookieToJavaCookie(cookie)); + } -// @Override -// protected int addAssetPathNative(String path) { -// throw new UnsupportedOperationException(); // todo -// } + // @Override + // protected int addAssetPathNative(String path) { + // throw new UnsupportedOperationException(); // todo + // } @Override Collection getAllAssetDirs() { @@ -373,8 +383,10 @@ Collection getAllAssetDirs() { ArrayList assetDirs = new ArrayList<>(); for (ApkAssets apkAssets : apkAssetsArray) { - long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); - CppApkAssets cppApkAssets = Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr); + long apk_assets_native_ptr = + ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); + CppApkAssets cppApkAssets = + Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr); if (new File(cppApkAssets.GetPath()).isFile()) { assetDirs.add(Fs.forJar(Paths.get(cppApkAssets.GetPath())).getPath("assets")); @@ -385,59 +397,62 @@ Collection getAllAssetDirs() { return assetDirs; } - @Override - List getAssetPaths() { - return AssetManagerForJavaObject(realAssetManager).getAssetPaths(); - } + @Override + List getAssetPaths() { + return AssetManagerForJavaObject(realAssetManager).getAssetPaths(); + } -// ---------------------------------------------------------------------------- + // ---------------------------------------------------------------------------- - // interface AAssetManager {} - // - // // Let the opaque type AAssetManager refer to a guarded AssetManager2 instance. - // static class GuardedAssetManager implements AAssetManager { - // CppAssetManager2 guarded_assetmanager = new CppAssetManager2(); - // } + // interface AAssetManager {} + // + // // Let the opaque type AAssetManager refer to a guarded AssetManager2 instance. + // static class GuardedAssetManager implements AAssetManager { + // CppAssetManager2 guarded_assetmanager = new CppAssetManager2(); + // } - static CppAssetManager2 NdkAssetManagerForJavaObject(/* JNIEnv* env,*/ AssetManager jassetmanager) { - // long assetmanager_handle = env.GetLongField(jassetmanager, gAssetManagerOffsets.mObject); - long assetmanager_handle = ReflectionHelpers.getField(jassetmanager, "mObject"); - CppAssetManager2 am = Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(assetmanager_handle); - if (am == null) { - throw new IllegalStateException("AssetManager has been finalized!"); - } - return am; + static CppAssetManager2 NdkAssetManagerForJavaObject( + /* JNIEnv* env,*/ AssetManager jassetmanager) { + // long assetmanager_handle = env.GetLongField(jassetmanager, gAssetManagerOffsets.mObject); + long assetmanager_handle = ReflectionHelpers.getField(jassetmanager, "mObject"); + CppAssetManager2 am = + Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(assetmanager_handle); + if (am == null) { + throw new IllegalStateException("AssetManager has been finalized!"); } + return am; + } - static CppAssetManager2 AssetManagerForJavaObject(/* JNIEnv* env,*/ AssetManager jassetmanager) { - return NdkAssetManagerForJavaObject(jassetmanager); - } + static CppAssetManager2 AssetManagerForJavaObject(/* JNIEnv* env,*/ AssetManager jassetmanager) { + return NdkAssetManagerForJavaObject(jassetmanager); + } - static CppAssetManager2 AssetManagerFromLong(long ptr) { - // return *AssetManagerForNdkAssetManager(reinterpret_cast(ptr)); - return Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(ptr); - } + static CppAssetManager2 AssetManagerFromLong(long ptr) { + // return *AssetManagerForNdkAssetManager(reinterpret_cast(ptr)); + return Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(ptr); + } - static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/ Asset asset, - long[] out_offsets) throws FileNotFoundException { - final Ref start_offset = new Ref<>(0L); - final Ref length = new Ref<>(0L); - FileDescriptor fd = asset.openFileDescriptor(start_offset, length); - // asset.reset(); + static ParcelFileDescriptor ReturnParcelFileDescriptor( + /* JNIEnv* env,*/ Asset asset, long[] out_offsets) throws FileNotFoundException { + final Ref start_offset = new Ref<>(0L); + final Ref length = new Ref<>(0L); + FileDescriptor fd = asset.openFileDescriptor(start_offset, length); + // asset.reset(); - if (fd == null) { - throw new FileNotFoundException( - "This file can not be opened as a file descriptor; it is probably compressed"); - } + if (fd == null) { + throw new FileNotFoundException( + "This file can not be opened as a file descriptor; it is probably compressed"); + } - long[] offsets = out_offsets; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_offsets, 0)); - if (offsets == null) { - // close(fd); - return null; - } + long[] offsets = + out_offsets; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_offsets, 0)); + if (offsets == null) { + // close(fd); + return null; + } - offsets[0] = start_offset.get(); - offsets[1] = length.get(); + offsets[0] = start_offset.get(); + offsets[1] = length.get(); // env.ReleasePrimitiveArrayCritical(out_offsets, offsets, 0); @@ -450,192 +465,212 @@ static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/ Asset a // TODO: consider doing this // return new ParcelFileDescriptor(file_desc); return ParcelFileDescriptor.open(asset.getFile(), ParcelFileDescriptor.MODE_READ_ONLY); - } - - /** - * Used for the creation of system assets. - */ - @Implementation(minSdk = P) - protected void __constructor__(boolean sentinel) { - inNonSystemConstructor = true; - try { - // call real constructor so field initialization happens. - invokeConstructor( - AssetManager.class, realAssetManager, ClassParameter.from(boolean.class, sentinel)); - } finally { - inNonSystemConstructor = false; - } - } + } - // static jint NativeGetGlobalAssetCount(JNIEnv* /*env*/, jobject /*clazz*/) { - @Implementation(minSdk = P) - protected static int getGlobalAssetCount() { - return Asset.getGlobalCount(); + /** Used for the creation of system assets. */ + @Implementation(minSdk = P) + protected void __constructor__(boolean sentinel) { + inNonSystemConstructor = true; + try { + // call real constructor so field initialization happens. + invokeConstructor( + AssetManager.class, realAssetManager, ClassParameter.from(boolean.class, sentinel)); + } finally { + inNonSystemConstructor = false; } + } - // static jobject NativeGetAssetAllocations(JNIEnv* env, jobject /*clazz*/) { - @Implementation(minSdk = P) - protected static String getAssetAllocations() { - String alloc = Asset.getAssetAllocations(); - if (alloc.length() <= 0) { - return null; - } - return alloc; - } + // static jint NativeGetGlobalAssetCount(JNIEnv* /*env*/, jobject /*clazz*/) { + @Implementation(minSdk = P) + protected static int getGlobalAssetCount() { + return Asset.getGlobalCount(); + } - // static jint NativeGetGlobalAssetManagerCount(JNIEnv* /*env*/, jobject /*clazz*/) { - @Implementation(minSdk = P) - protected static int getGlobalAssetManagerCount() { - // TODO(adamlesinski): Switch to AssetManager2. - return CppAssetManager.getGlobalCount(); + // static jobject NativeGetAssetAllocations(JNIEnv* env, jobject /*clazz*/) { + @Implementation(minSdk = P) + protected static String getAssetAllocations() { + String alloc = Asset.getAssetAllocations(); + if (alloc.length() <= 0) { + return null; } + return alloc; + } - // static jlong NativeCreate(JNIEnv* /*env*/, jclass /*clazz*/) { - @Implementation(minSdk = P) - protected static long nativeCreate() { - // AssetManager2 needs to be protected by a lock. To avoid cache misses, we allocate the lock and - // AssetManager2 in a contiguous block (GuardedAssetManager). - // return reinterpret_cast(new GuardedAssetManager()); + // static jint NativeGetGlobalAssetManagerCount(JNIEnv* /*env*/, jobject /*clazz*/) { + @Implementation(minSdk = P) + protected static int getGlobalAssetManagerCount() { + // TODO(adamlesinski): Switch to AssetManager2. + return CppAssetManager.getGlobalCount(); + } - long cppAssetManagerRef; + // static jlong NativeCreate(JNIEnv* /*env*/, jclass /*clazz*/) { + @Implementation(minSdk = P) + protected static long nativeCreate() { + // AssetManager2 needs to be protected by a lock. To avoid cache misses, we allocate the lock + // and + // AssetManager2 in a contiguous block (GuardedAssetManager). + // return reinterpret_cast(new GuardedAssetManager()); - // we want to share a single instance of the system CppAssetManager2 - if (inNonSystemConstructor) { - CppAssetManager2 appAssetManager = new CppAssetManager2(); - cppAssetManagerRef = Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(appAssetManager); - } else { - if (systemCppAssetManager2 == null) { - systemCppAssetManager2 = new CppAssetManager2(); - systemCppAssetManager2Ref = - Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(systemCppAssetManager2); - } + long cppAssetManagerRef; - cppAssetManagerRef = systemCppAssetManager2Ref; - } + // we want to share a single instance of the system CppAssetManager2 + if (inNonSystemConstructor) { + CppAssetManager2 appAssetManager = new CppAssetManager2(); + cppAssetManagerRef = Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(appAssetManager); + } else { + if (systemCppAssetManager2 == null) { + systemCppAssetManager2 = new CppAssetManager2(); + systemCppAssetManager2Ref = + Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(systemCppAssetManager2); + } - return cppAssetManagerRef; + cppAssetManagerRef = systemCppAssetManager2Ref; } - // static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { - @Implementation(minSdk = P) - protected static void nativeDestroy(long ptr) { - if (ptr == systemCppAssetManager2Ref) { - // don't destroy the shared system CppAssetManager2! - return; - } + return cppAssetManagerRef; + } - // delete reinterpret_cast(ptr); - Registries.NATIVE_ASSET_MANAGER_REGISTRY.unregister(ptr); - } - - // static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jobjectArray apk_assets_array, jboolean invalidate_caches) { - @Implementation(minSdk = P) - protected static void nativeSetApkAssets(long ptr, @NonNull android.content.res.ApkAssets[] apk_assets_array, - boolean invalidate_caches) { - ATRACE_NAME("AssetManager::SetApkAssets"); - - int apk_assets_len = apk_assets_array.length; - List apk_assets = new ArrayList<>(); - // apk_assets.reserve(apk_assets_len); - for (int i = 0; i < apk_assets_len; i++) { - android.content.res.ApkAssets apkAssets = apk_assets_array[i]; // env.GetObjectArrayElement(apk_assets_array, i); - if (apkAssets == null) { - throw new NullPointerException(String.format("ApkAssets at index %d is null", i)); - } - - long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); - // if (env.ExceptionCheck()) { - // return; - // } - apk_assets.add(Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr)); - } + // static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { + @Implementation(minSdk = P) + protected static void nativeDestroy(long ptr) { + if (ptr == systemCppAssetManager2Ref) { + // don't destroy the shared system CppAssetManager2! + return; + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - assetmanager.SetApkAssets(apk_assets, invalidate_caches); - } - - // static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint mnc, -// jstring locale, jint orientation, jint touchscreen, jint density, -// jint keyboard, jint keyboard_hidden, jint navigation, -// jint screen_width, jint screen_height, -// jint smallest_screen_width_dp, jint screen_width_dp, -// jint screen_height_dp, jint screen_layout, jint ui_mode, -// jint color_mode, jint major_version) { - @Implementation(minSdk = P) - protected static void nativeSetConfiguration(long ptr, int mcc, int mnc, - @Nullable String locale, int orientation, int touchscreen, int density, int keyboard, - int keyboard_hidden, int navigation, int screen_width, int screen_height, - int smallest_screen_width_dp, int screen_width_dp, int screen_height_dp, int screen_layout, - int ui_mode, int color_mode, int major_version) { - ATRACE_NAME("AssetManager::SetConfiguration"); - - ResTable_config configuration = new ResTable_config(); - // memset(&configuration, 0, sizeof(configuration)); - configuration.mcc = (short) (mcc); - configuration.mnc = (short) (mnc); - configuration.orientation = (byte) (orientation); - configuration.touchscreen = (byte) (touchscreen); - configuration.density = (short) (density); - configuration.keyboard = (byte) (keyboard); - configuration.inputFlags = (byte) (keyboard_hidden); - configuration.navigation = (byte) (navigation); - configuration.screenWidth = (short) (screen_width); - configuration.screenHeight = (short) (screen_height); - configuration.smallestScreenWidthDp = (short) (smallest_screen_width_dp); - configuration.screenWidthDp = (short) (screen_width_dp); - configuration.screenHeightDp = (short) (screen_height_dp); - configuration.screenLayout = (byte) (screen_layout); - configuration.uiMode = (byte) (ui_mode); - configuration.colorMode = (byte) (color_mode); - configuration.sdkVersion = (short) (major_version); - - if (locale != null) { - String locale_utf8 = locale; - CHECK(locale_utf8 != null); - configuration.setBcp47Locale(locale_utf8); - } + // delete reinterpret_cast(ptr); + Registries.NATIVE_ASSET_MANAGER_REGISTRY.unregister(ptr); + } - // Constants duplicated from Java class android.content.res.Configuration. - int kScreenLayoutRoundMask = 0x300; - int kScreenLayoutRoundShift = 8; + // static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr, + // jobjectArray apk_assets_array, jboolean invalidate_caches) { + @Implementation(minSdk = P) + protected static void nativeSetApkAssets( + long ptr, + @NonNull android.content.res.ApkAssets[] apk_assets_array, + boolean invalidate_caches) { + ATRACE_NAME("AssetManager::SetApkAssets"); + + int apk_assets_len = apk_assets_array.length; + List apk_assets = new ArrayList<>(); + // apk_assets.reserve(apk_assets_len); + for (int i = 0; i < apk_assets_len; i++) { + android.content.res.ApkAssets apkAssets = + apk_assets_array[i]; // env.GetObjectArrayElement(apk_assets_array, i); + if (apkAssets == null) { + throw new NullPointerException(String.format("ApkAssets at index %d is null", i)); + } + + long apk_assets_native_ptr = + ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); + // if (env.ExceptionCheck()) { + // return; + // } + apk_assets.add(Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr)); + } - // In Java, we use a 32bit integer for screenLayout, while we only use an 8bit integer - // in C++. We must extract the round qualifier out of the Java screenLayout and put it - // into screenLayout2. - configuration.screenLayout2 = - (byte) ((screen_layout & kScreenLayoutRoundMask) >> kScreenLayoutRoundShift); + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + assetmanager.SetApkAssets(apk_assets, invalidate_caches); + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - assetmanager.SetConfiguration(configuration); + // static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint + // mnc, + // jstring locale, jint orientation, jint touchscreen, jint + // density, + // jint keyboard, jint keyboard_hidden, jint navigation, + // jint screen_width, jint screen_height, + // jint smallest_screen_width_dp, jint screen_width_dp, + // jint screen_height_dp, jint screen_layout, jint ui_mode, + // jint color_mode, jint major_version) { + @Implementation(minSdk = P) + protected static void nativeSetConfiguration( + long ptr, + int mcc, + int mnc, + @Nullable String locale, + int orientation, + int touchscreen, + int density, + int keyboard, + int keyboard_hidden, + int navigation, + int screen_width, + int screen_height, + int smallest_screen_width_dp, + int screen_width_dp, + int screen_height_dp, + int screen_layout, + int ui_mode, + int color_mode, + int major_version) { + ATRACE_NAME("AssetManager::SetConfiguration"); + + ResTable_config configuration = new ResTable_config(); + // memset(&configuration, 0, sizeof(configuration)); + configuration.mcc = (short) (mcc); + configuration.mnc = (short) (mnc); + configuration.orientation = (byte) (orientation); + configuration.touchscreen = (byte) (touchscreen); + configuration.density = (short) (density); + configuration.keyboard = (byte) (keyboard); + configuration.inputFlags = (byte) (keyboard_hidden); + configuration.navigation = (byte) (navigation); + configuration.screenWidth = (short) (screen_width); + configuration.screenHeight = (short) (screen_height); + configuration.smallestScreenWidthDp = (short) (smallest_screen_width_dp); + configuration.screenWidthDp = (short) (screen_width_dp); + configuration.screenHeightDp = (short) (screen_height_dp); + configuration.screenLayout = (byte) (screen_layout); + configuration.uiMode = (byte) (ui_mode); + configuration.colorMode = (byte) (color_mode); + configuration.sdkVersion = (short) (major_version); + + if (locale != null) { + String locale_utf8 = locale; + CHECK(locale_utf8 != null); + configuration.setBcp47Locale(locale_utf8); } + // Constants duplicated from Java class android.content.res.Configuration. + int kScreenLayoutRoundMask = 0x300; + int kScreenLayoutRoundShift = 8; + + // In Java, we use a 32bit integer for screenLayout, while we only use an 8bit integer + // in C++. We must extract the round qualifier out of the Java screenLayout and put it + // into screenLayout2. + configuration.screenLayout2 = + (byte) ((screen_layout & kScreenLayoutRoundMask) >> kScreenLayoutRoundShift); + + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + assetmanager.SetConfiguration(configuration); + } + // static jobject NativeGetAssignedPackageIdentifiers(JNIEnv* env, jclass /*clazz*/, jlong ptr) { @Implementation(minSdk = P, maxSdk = Q) protected static @NonNull SparseArray nativeGetAssignedPackageIdentifiers(long ptr) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - SparseArray sparse_array = - new SparseArray<>(); + SparseArray sparse_array = new SparseArray<>(); - if (sparse_array == null) { - // An exception is pending. - return null; - } + if (sparse_array == null) { + // An exception is pending. + return null; + } - assetmanager.ForEachPackage((String package_name, byte package_id) -> { - String jpackage_name = package_name; // env.NewStringUTF(package_name); - if (jpackage_name == null) { - // An exception is pending. - return; - } + assetmanager.ForEachPackage( + (String package_name, byte package_id) -> { + String jpackage_name = package_name; // env.NewStringUTF(package_name); + if (jpackage_name == null) { + // An exception is pending. + return; + } - // env.CallVoidMethod(sparse_array, gSparseArrayOffsets.put, (int) (package_id), - // jpackage_name); - sparse_array.put(package_id, jpackage_name); + // env.CallVoidMethod(sparse_array, gSparseArrayOffsets.put, (int) (package_id), + // jpackage_name); + sparse_array.put(package_id, jpackage_name); }); - return sparse_array; - } + return sparse_array; + } @Implementation(minSdk = R) protected static SparseArray nativeGetAssignedPackageIdentifiers( @@ -643,1172 +678,1232 @@ protected static SparseArray nativeGetAssignedPackageIdentifiers( return nativeGetAssignedPackageIdentifiers(ptr); } - // static jobjectArray NativeList(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring path) { - @Implementation(minSdk = P) - protected static @Nullable String[] nativeList(long ptr, @NonNull String path) - throws IOException { - String path_utf8 = path; - if (path_utf8 == null) { - // This will throw NPE. - return null; - } + // static jobjectArray NativeList(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring path) { + @Implementation(minSdk = P) + protected static @Nullable String[] nativeList(long ptr, @NonNull String path) + throws IOException { + String path_utf8 = path; + if (path_utf8 == null) { + // This will throw NPE. + return null; + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - AssetDir asset_dir = - assetmanager.OpenDir(path_utf8); - if (asset_dir == null) { - throw new FileNotFoundException(path_utf8); - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + AssetDir asset_dir = assetmanager.OpenDir(path_utf8); + if (asset_dir == null) { + throw new FileNotFoundException(path_utf8); + } - int file_count = asset_dir.getFileCount(); + int file_count = asset_dir.getFileCount(); - String[] array = new String[file_count]; // env.NewObjectArray(file_count, g_stringClass, null); - // if (array == null) { - // return null; - // } + String[] array = new String[file_count]; // env.NewObjectArray(file_count, g_stringClass, null); + // if (array == null) { + // return null; + // } - for (int i = 0; i < file_count; i++) { - String java_string = asset_dir.getFileName(i).string(); + for (int i = 0; i < file_count; i++) { + String java_string = asset_dir.getFileName(i).string(); - // Check for errors creating the strings (if malformed or no memory). - // if (env.ExceptionCheck()) { - // return null; - // } + // Check for errors creating the strings (if malformed or no memory). + // if (env.ExceptionCheck()) { + // return null; + // } - // env.SetObjectArrayElement(array, i, java_string); - array[i] = java_string; + // env.SetObjectArrayElement(array, i, java_string); + array[i] = java_string; - // If we have a large amount of string in our array, we might overflow the - // local reference table of the VM. - // env.DeleteLocalRef(java_string); - } - return array; - } - - // static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, -// jint access_mode) { - @Implementation(minSdk = P) - protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode) - throws FileNotFoundException { - String asset_path_utf8 = asset_path; - if (asset_path_utf8 == null) { - // This will throw NPE. - return 0; - } + // If we have a large amount of string in our array, we might overflow the + // local reference table of the VM. + // env.DeleteLocalRef(java_string); + } + return array; + } - ATRACE_NAME(String.format("AssetManager::OpenAsset(%s)", asset_path_utf8)); + // static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, + // jint access_mode) { + @Implementation(minSdk = P) + protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode) + throws FileNotFoundException { + String asset_path_utf8 = asset_path; + if (asset_path_utf8 == null) { + // This will throw NPE. + return 0; + } - if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() - && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() - && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() - && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) { - throw new IllegalArgumentException("Bad access mode"); - } + ATRACE_NAME(String.format("AssetManager::OpenAsset(%s)", asset_path_utf8)); - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset = - assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); - if (!isTruthy(asset)) { - throw new FileNotFoundException(asset_path_utf8); - } - return Registries.NATIVE_ASSET_REGISTRY.register(asset); - } - - // static jobject NativeOpenAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, -// jlongArray out_offsets) { - @Implementation(minSdk = P) - protected static ParcelFileDescriptor nativeOpenAssetFd(long ptr, - @NonNull String asset_path, long[] out_offsets) throws IOException { - String asset_path_utf8 = asset_path; - if (asset_path_utf8 == null) { - // This will throw NPE. - return null; - } + if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() + && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() + && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() + && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) { + throw new IllegalArgumentException("Bad access mode"); + } - ATRACE_NAME(String.format("AssetManager::OpenAssetFd(%s)", asset_path_utf8)); + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); + if (!isTruthy(asset)) { + throw new FileNotFoundException(asset_path_utf8); + } + return Registries.NATIVE_ASSET_REGISTRY.register(asset); + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM); - if (!isTruthy(asset)) { - throw new FileNotFoundException(asset_path_utf8); - } - return ReturnParcelFileDescriptor(asset, out_offsets); - } - - // static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path, jint access_mode) { - @Implementation(minSdk = P) - protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String asset_path, - int access_mode) throws FileNotFoundException { - ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); - String asset_path_utf8 = asset_path; - if (asset_path_utf8 == null) { - // This will throw NPE. - return 0; - } + // static jobject NativeOpenAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, + // jlongArray out_offsets) { + @Implementation(minSdk = P) + protected static ParcelFileDescriptor nativeOpenAssetFd( + long ptr, @NonNull String asset_path, long[] out_offsets) throws IOException { + String asset_path_utf8 = asset_path; + if (asset_path_utf8 == null) { + // This will throw NPE. + return null; + } - ATRACE_NAME(String.format("AssetManager::OpenNonAsset(%s)", asset_path_utf8)); + ATRACE_NAME(String.format("AssetManager::OpenAssetFd(%s)", asset_path_utf8)); - if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() - && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() - && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() - && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) { - throw new IllegalArgumentException("Bad access mode"); - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM); + if (!isTruthy(asset)) { + throw new FileNotFoundException(asset_path_utf8); + } + return ReturnParcelFileDescriptor(asset, out_offsets); + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset; - if (cookie.intValue() != kInvalidCookie) { - asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, - Asset.AccessMode.fromInt(access_mode)); - } else { - asset = assetmanager.OpenNonAsset(asset_path_utf8, - Asset.AccessMode.fromInt(access_mode)); - } + // static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, + // jstring asset_path, jint access_mode) { + @Implementation(minSdk = P) + protected static long nativeOpenNonAsset( + long ptr, int jcookie, @NonNull String asset_path, int access_mode) + throws FileNotFoundException { + ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); + String asset_path_utf8 = asset_path; + if (asset_path_utf8 == null) { + // This will throw NPE. + return 0; + } - if (!isTruthy(asset)) { - throw new FileNotFoundException(asset_path_utf8); - } - return Registries.NATIVE_ASSET_REGISTRY.register(asset); - } - - // static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path, jlongArray out_offsets) { - @Implementation(minSdk = P) - protected static @Nullable ParcelFileDescriptor nativeOpenNonAssetFd(long ptr, int jcookie, - @NonNull String asset_path, @NonNull long[] out_offsets) throws IOException { - ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); - String asset_path_utf8 = asset_path; - if (asset_path_utf8 == null) { - // This will throw NPE. - return null; - } + ATRACE_NAME(String.format("AssetManager::OpenNonAsset(%s)", asset_path_utf8)); - ATRACE_NAME(String.format("AssetManager::OpenNonAssetFd(%s)", asset_path_utf8)); + if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode() + && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode() + && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode() + && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) { + throw new IllegalArgumentException("Bad access mode"); + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset; - if (cookie.intValue() != kInvalidCookie) { - asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM); - } else { - asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM); - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Asset asset; + if (cookie.intValue() != kInvalidCookie) { + asset = + assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.fromInt(access_mode)); + } else { + asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); + } - if (!isTruthy(asset)) { - throw new FileNotFoundException(asset_path_utf8); - } - return ReturnParcelFileDescriptor(asset, out_offsets); - } - - // static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path) { - @Implementation(minSdk = P) - protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) - throws FileNotFoundException { - ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); - String asset_path_utf8 = asset_path; - if (asset_path_utf8 == null) { - // This will throw NPE. - return 0; - } + if (!isTruthy(asset)) { + throw new FileNotFoundException(asset_path_utf8); + } + return Registries.NATIVE_ASSET_REGISTRY.register(asset); + } - ATRACE_NAME(String.format("AssetManager::OpenXmlAsset(%s)", asset_path_utf8)); + // static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, + // jstring asset_path, jlongArray out_offsets) { + @Implementation(minSdk = P) + protected static @Nullable ParcelFileDescriptor nativeOpenNonAssetFd( + long ptr, int jcookie, @NonNull String asset_path, @NonNull long[] out_offsets) + throws IOException { + ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); + String asset_path_utf8 = asset_path; + if (asset_path_utf8 == null) { + // This will throw NPE. + return null; + } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset; - if (cookie.intValue() != kInvalidCookie) { - asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM); - } else { - Ref cookieRef = new Ref<>(cookie); - asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef); - cookie = cookieRef.get(); - } + ATRACE_NAME(String.format("AssetManager::OpenNonAssetFd(%s)", asset_path_utf8)); - if (!isTruthy(asset)) { - throw new FileNotFoundException(asset_path_utf8); - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Asset asset; + if (cookie.intValue() != kInvalidCookie) { + asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM); + } else { + asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM); + } - // May be nullptr. - DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie); + if (!isTruthy(asset)) { + throw new FileNotFoundException(asset_path_utf8); + } + return ReturnParcelFileDescriptor(asset, out_offsets); + } - ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table); - int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true); - // asset.reset(); + // static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie, + // jstring asset_path) { + @Implementation(minSdk = P) + protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) + throws FileNotFoundException { + ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); + String asset_path_utf8 = asset_path; + if (asset_path_utf8 == null) { + // This will throw NPE. + return 0; + } - if (err != NO_ERROR) { - throw new FileNotFoundException("Corrupt XML binary file"); - } - return NATIVE_RES_XML_TREES.register(xml_tree); - } - - // static jint NativeGetResourceValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jshort density, jobject typed_value, -// jboolean resolve_references) { - @Implementation(minSdk = P) - protected static int nativeGetResourceValue(long ptr, @AnyRes int resid, short density, - @NonNull TypedValue typed_value, boolean resolve_references) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - final Ref value = new Ref<>(null); - final Ref selected_config = new Ref<>(null); - final Ref flags = new Ref<>(0); - ApkAssetsCookie cookie = - assetmanager.GetResource(resid, false /*may_be_bag*/, - (short) (density), value, selected_config, flags); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } + ATRACE_NAME(String.format("AssetManager::OpenXmlAsset(%s)", asset_path_utf8)); - final Ref ref = new Ref<>(resid); - if (resolve_references) { - cookie = assetmanager.ResolveReference(cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } - } - return CopyValue(cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value); - } - - // static jint NativeGetResourceBagValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jint bag_entry_id, jobject typed_value) { - @Implementation(minSdk = P) - protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int bag_entry_id, - @NonNull TypedValue typed_value) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Asset asset; + if (cookie.intValue() != kInvalidCookie) { + asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM); + } else { + Ref cookieRef = new Ref<>(cookie); + asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef); + cookie = cookieRef.get(); + } - final Ref type_spec_flags = new Ref<>(bag.type_spec_flags); - ApkAssetsCookie cookie = K_INVALID_COOKIE; - Res_value bag_value = null; - for (ResolvedBag.Entry entry : bag.entries) { - if (entry.key == (int) (bag_entry_id)) { - cookie = entry.cookie; - bag_value = entry.value; + if (!isTruthy(asset)) { + throw new FileNotFoundException(asset_path_utf8); + } - // Keep searching (the old implementation did that). - } - } + // May be nullptr. + DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } + ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table); + int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true); + // asset.reset(); - final Ref value = new Ref<>(bag_value); - final Ref ref = new Ref<>(resid); - final Ref selected_config = new Ref<>(null); - cookie = assetmanager.ResolveReference(cookie, value, selected_config, type_spec_flags, ref); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } - return CopyValue(cookie, value.get(), ref.get(), type_spec_flags.get(), null, typed_value); + if (err != NO_ERROR) { + throw new FileNotFoundException("Corrupt XML binary file"); } + return NATIVE_RES_XML_TREES.register(xml_tree); + } - // static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable @AttrRes int[] nativeGetStyleAttributes(long ptr, - @StyleRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return null; - } + // static jint NativeGetResourceValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, + // jshort density, jobject typed_value, + // jboolean resolve_references) { + @Implementation(minSdk = P) + protected static int nativeGetResourceValue( + long ptr, + @AnyRes int resid, + short density, + @NonNull TypedValue typed_value, + boolean resolve_references) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + final Ref value = new Ref<>(null); + final Ref selected_config = new Ref<>(null); + final Ref flags = new Ref<>(0); + ApkAssetsCookie cookie = + assetmanager.GetResource( + resid, false /*may_be_bag*/, (short) (density), value, selected_config, flags); + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } - int[] array = new int[bag.entry_count]; - // if (env.ExceptionCheck()) { - // return null; - // } + final Ref ref = new Ref<>(resid); + if (resolve_references) { + cookie = assetmanager.ResolveReference(cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } + } + return CopyValue( + cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value); + } - for (int i = 0; i < bag.entry_count; i++) { - int attr_resid = bag.entries[i].key; - // env.SetIntArrayRegion(array, i, 1, &attr_resid); - array[i] = attr_resid; - } - return array; - } - - // static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jint resid) { - @Implementation(minSdk = P) - protected static @Nullable String[] nativeGetResourceStringArray(long ptr, - @ArrayRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return null; - } + // static jint NativeGetResourceBagValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, + // jint bag_entry_id, jobject typed_value) { + @Implementation(minSdk = P) + protected static int nativeGetResourceBagValue( + long ptr, @AnyRes int resid, int bag_entry_id, @NonNull TypedValue typed_value) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } - String[] array = new String[bag.entry_count]; - if (array == null) { - return null; - } + final Ref type_spec_flags = new Ref<>(bag.type_spec_flags); + ApkAssetsCookie cookie = K_INVALID_COOKIE; + Res_value bag_value = null; + for (ResolvedBag.Entry entry : bag.entries) { + if (entry.key == (int) (bag_entry_id)) { + cookie = entry.cookie; + bag_value = entry.value; + + // Keep searching (the old implementation did that). + } + } + + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } + + final Ref value = new Ref<>(bag_value); + final Ref ref = new Ref<>(resid); + final Ref selected_config = new Ref<>(null); + cookie = assetmanager.ResolveReference(cookie, value, selected_config, type_spec_flags, ref); + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } + return CopyValue(cookie, value.get(), ref.get(), type_spec_flags.get(), null, typed_value); + } - for (int i = 0; i < bag.entry_count; i++) { - ResolvedBag.Entry entry = bag.entries[i]; - - // Resolve any references to their final value. - final Ref value = new Ref<>(entry.value); - final Ref selected_config = new Ref<>(null); - final Ref flags = new Ref<>(0); - final Ref ref = new Ref<>(0); - ApkAssetsCookie cookie = - assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - return null; - } - - if (value.get().dataType == Res_value.TYPE_STRING) { - CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue()); - ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool(); - - String java_string = null; + // static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { + @Implementation(minSdk = P) + protected static @Nullable @AttrRes int[] nativeGetStyleAttributes( + long ptr, @StyleRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return null; + } + + int[] array = new int[bag.entry_count]; + // if (env.ExceptionCheck()) { + // return null; + // } + + for (int i = 0; i < bag.entry_count; i++) { + int attr_resid = bag.entries[i].key; + // env.SetIntArrayRegion(array, i, 1, &attr_resid); + array[i] = attr_resid; + } + return array; + } + + // static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, + // jint resid) { + @Implementation(minSdk = P) + protected static @Nullable String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return null; + } + + String[] array = new String[bag.entry_count]; + if (array == null) { + return null; + } + + for (int i = 0; i < bag.entry_count; i++) { + ResolvedBag.Entry entry = bag.entries[i]; + + // Resolve any references to their final value. + final Ref value = new Ref<>(entry.value); + final Ref selected_config = new Ref<>(null); + final Ref flags = new Ref<>(0); + final Ref ref = new Ref<>(0); + ApkAssetsCookie cookie = + assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + return null; + } + + if (value.get().dataType == Res_value.TYPE_STRING) { + CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue()); + ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool(); + + String java_string = null; String str_utf8 = pool.stringAt(value.get().data); - if (str_utf8 != null) { - java_string = str_utf8; - } else { - String str_utf16 = pool.stringAt(value.get().data); - java_string = str_utf16; - } - - // // Check for errors creating the strings (if malformed or no memory). - // if (env.ExceptionCheck()) { - // return null; - // } - - // env.SetObjectArrayElement(array, i, java_string); - array[i] = java_string; - - // If we have a large amount of string in our array, we might overflow the - // local reference table of the VM. - // env.DeleteLocalRef(java_string); - } - } - return array; - } - - // static jintArray NativeGetResourceStringArrayInfo(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jint resid) { - @Implementation(minSdk = P) - protected static @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, - @ArrayRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return null; + if (str_utf8 != null) { + java_string = str_utf8; + } else { + String str_utf16 = pool.stringAt(value.get().data); + java_string = str_utf16; } - int[] array = new int[bag.entry_count * 2]; - // if (array == null) { + // // Check for errors creating the strings (if malformed or no memory). + // if (env.ExceptionCheck()) { // return null; // } - int[] buffer = array; //reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); - // if (buffer == null) { - // return null; - // } + // env.SetObjectArrayElement(array, i, java_string); + array[i] = java_string; - for (int i = 0; i < bag.entry_count; i++) { - ResolvedBag.Entry entry = bag.entries[i]; - final Ref value = new Ref<>(entry.value); - final Ref selected_config = new Ref<>(null); - final Ref flags = new Ref<>(0); - final Ref ref = new Ref<>(0); - ApkAssetsCookie cookie = - assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT); - return null; - } - - int string_index = -1; - if (value.get().dataType == Res_value.TYPE_STRING) { - string_index = (int) (value.get().data); - } - - buffer[i * 2] = ApkAssetsCookieToJavaCookie(cookie); - buffer[(i * 2) + 1] = string_index; - } - // env.ReleasePrimitiveArrayCritical(array, buffer, 0); - return array; + // If we have a large amount of string in our array, we might overflow the + // local reference table of the VM. + // env.DeleteLocalRef(java_string); + } } + return array; + } - // static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return null; - } + // static jintArray NativeGetResourceStringArrayInfo(JNIEnv* env, jclass /*clazz*/, jlong ptr, + // jint resid) { + @Implementation(minSdk = P) + protected static @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, @ArrayRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return null; + } - int[] array = new int[bag.entry_count]; - // if (array == null) { - // return null; - // } + int[] array = new int[bag.entry_count * 2]; + // if (array == null) { + // return null; + // } - int[] buffer = array; // reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); - // if (buffer == null) { - // return null; - // } + int[] buffer = array; // reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); + // if (buffer == null) { + // return null; + // } - for (int i = 0; i < bag.entry_count; i++) { - ResolvedBag.Entry entry = bag.entries[i]; - final Ref value = new Ref<>(entry.value); - final Ref selected_config = new Ref<>(null); - final Ref flags = new Ref<>(0); - final Ref ref = new Ref<>(0); - ApkAssetsCookie cookie = - assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT); - return null; - } - - if (value.get().dataType >= Res_value.TYPE_FIRST_INT && value.get().dataType <= Res_value.TYPE_LAST_INT) { - buffer[i] = (int) (value.get().data); - } - } - // env.ReleasePrimitiveArrayCritical(array, buffer, 0); - return array; + for (int i = 0; i < bag.entry_count; i++) { + ResolvedBag.Entry entry = bag.entries[i]; + final Ref value = new Ref<>(entry.value); + final Ref selected_config = new Ref<>(null); + final Ref flags = new Ref<>(0); + final Ref ref = new Ref<>(0); + ApkAssetsCookie cookie = + assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT); + return null; + } + + int string_index = -1; + if (value.get().dataType == Res_value.TYPE_STRING) { + string_index = (int) (value.get().data); + } + + buffer[i * 2] = ApkAssetsCookieToJavaCookie(cookie); + buffer[(i * 2) + 1] = string_index; } + // env.ReleasePrimitiveArrayCritical(array, buffer, 0); + return array; + } - // static jint NativeGetResourceArraySize(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static int nativeGetResourceArraySize(long ptr, @ArrayRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return -1; - } - return (int) (bag.entry_count); - } - - // static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jintArray out_data) { - @Implementation(minSdk = P) - protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, - @NonNull int[] out_data) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResolvedBag bag = assetmanager.GetBag(resid); - if (bag == null) { - return -1; - } + // static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint + // resid) { + @Implementation(minSdk = P) + protected static @Nullable int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return null; + } - int out_data_length = out_data.length; - // if (env.ExceptionCheck()) { - // return -1; - // } + int[] array = new int[bag.entry_count]; + // if (array == null) { + // return null; + // } - if ((int) (bag.entry_count) > out_data_length * STYLE_NUM_ENTRIES) { - throw new IllegalArgumentException("Input array is not large enough"); - } + int[] buffer = array; // reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); + // if (buffer == null) { + // return null; + // } - int[] buffer = out_data; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_data, null)); - if (buffer == null) { - return -1; - } + for (int i = 0; i < bag.entry_count; i++) { + ResolvedBag.Entry entry = bag.entries[i]; + final Ref value = new Ref<>(entry.value); + final Ref selected_config = new Ref<>(null); + final Ref flags = new Ref<>(0); + final Ref ref = new Ref<>(0); + ApkAssetsCookie cookie = + assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT); + return null; + } - int[] cursor = buffer; - for (int i = 0; i < bag.entry_count; i++) { - ResolvedBag.Entry entry = bag.entries[i]; - final Ref value = new Ref<>(entry.value); - final Ref selected_config = new Ref<>(new ResTable_config()); - selected_config.get().density = 0; - final Ref flags = new Ref<>(bag.type_spec_flags); - final Ref ref = new Ref<>(0); - ApkAssetsCookie cookie = - assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - // env.ReleasePrimitiveArrayCritical(out_data, buffer, JNI_ABORT); - return -1; - } - - // Deal with the special @null value -- it turns back to TYPE_NULL. - if (value.get().dataType == Res_value.TYPE_REFERENCE && value.get().data == 0) { - value.set(Res_value.NULL_VALUE); - } - - int offset = i * STYLE_NUM_ENTRIES; - cursor[offset + STYLE_TYPE] = (int) (value.get().dataType); - cursor[offset + STYLE_DATA] = (int) (value.get().data); - cursor[offset + STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(cookie); - cursor[offset + STYLE_RESOURCE_ID] = (int) (ref.get()); - cursor[offset + STYLE_CHANGING_CONFIGURATIONS] = (int) (flags.get()); - cursor[offset + STYLE_DENSITY] = (int) (selected_config.get().density); - // cursor += STYLE_NUM_ENTRIES; - } - // env.ReleasePrimitiveArrayCritical(out_data, buffer, 0); - return (int) (bag.entry_count); - } - - // static jint NativeGetResourceIdentifier(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring name, -// jstring def_type, jstring def_package) { - @Implementation(minSdk = P) - protected static @AnyRes int nativeGetResourceIdentifier(long ptr, @NonNull String name, - @Nullable String def_type, @Nullable String def_package) { - String name_utf8 = name; - if (name_utf8 == null) { - // This will throw NPE. - return 0; - } + if (value.get().dataType >= Res_value.TYPE_FIRST_INT + && value.get().dataType <= Res_value.TYPE_LAST_INT) { + buffer[i] = (int) (value.get().data); + } + } + // env.ReleasePrimitiveArrayCritical(array, buffer, 0); + return array; + } - String type = null; - if (def_type != null) { - String type_utf8 = def_type; - CHECK(type_utf8 != null); - type = type_utf8; - } + // static jint NativeGetResourceArraySize(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jint + // resid) { + @Implementation(minSdk = P) + protected static int nativeGetResourceArraySize(long ptr, @ArrayRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return -1; + } + return (int) (bag.entry_count); + } - String package_ = null; - if (def_package != null) { - String package_utf8 = def_package; - CHECK(package_utf8 != null); - package_ = package_utf8; - } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - return (int) (assetmanager.GetResourceId(name_utf8, type, package_)); + // static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, + // jintArray out_data) { + @Implementation(minSdk = P) + protected static int nativeGetResourceArray( + long ptr, @ArrayRes int resid, @NonNull int[] out_data) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResolvedBag bag = assetmanager.GetBag(resid); + if (bag == null) { + return -1; } - // static jstring NativeGetResourceName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable String nativeGetResourceName(long ptr, @AnyRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - CppAssetManager2.ResourceName name = new ResourceName(); - if (!assetmanager.GetResourceName(resid, name)) { - return null; - } + int out_data_length = out_data.length; + // if (env.ExceptionCheck()) { + // return -1; + // } - StringBuilder result = new StringBuilder(); - if (name.package_ != null) { - result.append(name.package_/*, name.package_len*/); - } + if ((int) (bag.entry_count) > out_data_length * STYLE_NUM_ENTRIES) { + throw new IllegalArgumentException("Input array is not large enough"); + } - if (name.type != null /*|| name.type16 != null*/) { - if (!(result.length() == 0)) { - result.append(":"); - } + int[] buffer = + out_data; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_data, null)); + if (buffer == null) { + return -1; + } - // if (name.type != null) { - result.append(name.type/*, name.type_len*/); - // } else { - // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.type16 /*, name.type_len))*/); - // } - } + int[] cursor = buffer; + for (int i = 0; i < bag.entry_count; i++) { + ResolvedBag.Entry entry = bag.entries[i]; + final Ref value = new Ref<>(entry.value); + final Ref selected_config = new Ref<>(new ResTable_config()); + selected_config.get().density = 0; + final Ref flags = new Ref<>(bag.type_spec_flags); + final Ref ref = new Ref<>(0); + ApkAssetsCookie cookie = + assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + // env.ReleasePrimitiveArrayCritical(out_data, buffer, JNI_ABORT); + return -1; + } - if (name.entry != null /*|| name.entry16 != null*/) { - if (!(result.length() == 0)) { - result.append("/"); - } + // Deal with the special @null value -- it turns back to TYPE_NULL. + if (value.get().dataType == Res_value.TYPE_REFERENCE && value.get().data == 0) { + value.set(Res_value.NULL_VALUE); + } - // if (name.entry != null) { - result.append(name.entry/*, name.entry_len*/); - // } else { - // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.entry16 /*, name.entry_len)*/); - // } - } - return result.toString(); + int offset = i * STYLE_NUM_ENTRIES; + cursor[offset + STYLE_TYPE] = (int) (value.get().dataType); + cursor[offset + STYLE_DATA] = (int) (value.get().data); + cursor[offset + STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(cookie); + cursor[offset + STYLE_RESOURCE_ID] = (int) (ref.get()); + cursor[offset + STYLE_CHANGING_CONFIGURATIONS] = (int) (flags.get()); + cursor[offset + STYLE_DENSITY] = (int) (selected_config.get().density); + // cursor += STYLE_NUM_ENTRIES; } + // env.ReleasePrimitiveArrayCritical(out_data, buffer, 0); + return (int) (bag.entry_count); + } - // static jstring NativeGetResourcePackageName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable String nativeGetResourcePackageName(long ptr, - @AnyRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - final ResourceName name = new ResourceName(); - if (!assetmanager.GetResourceName(resid, name)) { - return null; - } + // static jint NativeGetResourceIdentifier(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring name, + // jstring def_type, jstring def_package) { + @Implementation(minSdk = P) + protected static @AnyRes int nativeGetResourceIdentifier( + long ptr, @NonNull String name, @Nullable String def_type, @Nullable String def_package) { + String name_utf8 = name; + if (name_utf8 == null) { + // This will throw NPE. + return 0; + } - if (name.package_ != null) { - return name.package_; - } - return null; + String type = null; + if (def_type != null) { + String type_utf8 = def_type; + CHECK(type_utf8 != null); + type = type_utf8; } - // static jstring NativeGetResourceTypeName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable String nativeGetResourceTypeName(long ptr, @AnyRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - final ResourceName name = new ResourceName(); - if (!assetmanager.GetResourceName(resid, name)) { - return null; - } + String package_ = null; + if (def_package != null) { + String package_utf8 = def_package; + CHECK(package_utf8 != null); + package_ = package_utf8; + } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + return (int) (assetmanager.GetResourceId(name_utf8, type, package_)); + } - if (name.type != null) { - return name.type; - // } else if (name.get().type16 != null) { - // return name.get().type16; // env.NewString(reinterpret_cast(name.type16), name.type_len); - } - return null; + // static jstring NativeGetResourceName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + @Implementation(minSdk = P) + protected static @Nullable String nativeGetResourceName(long ptr, @AnyRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + CppAssetManager2.ResourceName name = new ResourceName(); + if (!assetmanager.GetResourceName(resid, name)) { + return null; } - // static jstring NativeGetResourceEntryName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { - @Implementation(minSdk = P) - protected static @Nullable String nativeGetResourceEntryName(long ptr, @AnyRes int resid) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - final ResourceName name = new ResourceName(); - if (!assetmanager.GetResourceName(resid, name)) { - return null; - } + StringBuilder result = new StringBuilder(); + if (name.package_ != null) { + result.append(name.package_ /*, name.package_len*/); + } - if (name.entry != null) { - return name.entry; - // } else if (name.entry16 != null) { - // return name.entry16; // env.NewString(reinterpret_cast(name.entry16), name.entry_len); - } - return null; + if (name.type != null /*|| name.type16 != null*/) { + if (!(result.length() == 0)) { + result.append(":"); + } + + // if (name.type != null) { + result.append(name.type /*, name.type_len*/); + // } else { + // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.type16 /*, name.type_len))*/); + // } } - // static jobjectArray NativeGetLocales(JNIEnv* env, jclass /*class*/, jlong ptr, -// jboolean exclude_system) { - @Implementation(minSdk = P) - protected static @Nullable String[] nativeGetLocales(long ptr, boolean exclude_system) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Set locales = - assetmanager.GetResourceLocales(exclude_system, true /*merge_equivalent_languages*/); + if (name.entry != null /*|| name.entry16 != null*/) { + if (!(result.length() == 0)) { + result.append("/"); + } - String[] array = new String[locales.size()]; // env.NewObjectArray(locales.size(), g_stringClass, null); - // if (array == null) { - // return null; - // } + // if (name.entry != null) { + result.append(name.entry /*, name.entry_len*/); + // } else { + // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.entry16 /*, name.entry_len)*/); + // } + } + return result.toString(); + } - int idx = 0; - for (String locale : locales) { - String java_string = locale; - if (java_string == null) { - return null; - } - // env.SetObjectArrayElement(array, idx++, java_string); - array[idx++] = java_string; - // env.DeleteLocalRef(java_string); - } - return array; + // static jstring NativeGetResourcePackageName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint + // resid) { + @Implementation(minSdk = P) + protected static @Nullable String nativeGetResourcePackageName(long ptr, @AnyRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + final ResourceName name = new ResourceName(); + if (!assetmanager.GetResourceName(resid, name)) { + return null; } - static Configuration ConstructConfigurationObject(/* JNIEnv* env,*/ ResTable_config config) { - // jobject result = - // env.NewObject(gConfigurationOffsets.classObject, gConfigurationOffsets.constructor); - Configuration result = new Configuration(); - // if (result == null) { - // return null; - // } + if (name.package_ != null) { + return name.package_; + } + return null; + } - result.smallestScreenWidthDp = config.smallestScreenWidthDp; - result.screenWidthDp = config.screenWidthDp; - result.screenHeightDp = config.screenHeightDp; - return result; + // static jstring NativeGetResourceTypeName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { + @Implementation(minSdk = P) + protected static @Nullable String nativeGetResourceTypeName(long ptr, @AnyRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + final ResourceName name = new ResourceName(); + if (!assetmanager.GetResourceName(resid, name)) { + return null; } - // static jobjectArray NativeGetSizeConfigurations(JNIEnv* env, jclass /*clazz*/, jlong ptr) { - @Implementation(minSdk = P) - protected static @Nullable Configuration[] nativeGetSizeConfigurations(long ptr) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Set configurations = - assetmanager.GetResourceConfigurations(true /*exclude_system*/, false /*exclude_mipmap*/); + if (name.type != null) { + return name.type; + // } else if (name.get().type16 != null) { + // return name.get().type16; // env.NewString(reinterpret_cast(name.type16), + // name.type_len); + } + return null; + } - Configuration[] array = new Configuration[configurations.size()]; - // env.NewObjectArray(configurations.size(), gConfigurationOffsets.classObject, null); - // if (array == null) { - // return null; - // } + // static jstring NativeGetResourceEntryName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { + @Implementation(minSdk = P) + protected static @Nullable String nativeGetResourceEntryName(long ptr, @AnyRes int resid) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + final ResourceName name = new ResourceName(); + if (!assetmanager.GetResourceName(resid, name)) { + return null; + } - int idx = 0; - for (ResTable_config configuration : configurations) { - Configuration java_configuration = ConstructConfigurationObject(configuration); - // if (java_configuration == null) { - // return null; - // } + if (name.entry != null) { + return name.entry; + // } else if (name.entry16 != null) { + // return name.entry16; // env.NewString(reinterpret_cast(name.entry16), + // name.entry_len); + } + return null; + } - // env.SetObjectArrayElement(array, idx++, java_configuration); - array[idx++] = java_configuration; - // env.DeleteLocalRef(java_configuration); - } - return array; - } - - // static void NativeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint def_style_attr, jint def_style_resid, jlong xml_parser_ptr, -// jintArray java_attrs, jlong out_values_ptr, jlong out_indices_ptr) { - @Implementation(minSdk = P) - protected static void nativeApplyStyle(long ptr, long theme_ptr, @AttrRes int def_style_attr, - @StyleRes int def_style_resid, long xml_parser_ptr, @NonNull int[] java_attrs, - long out_values_ptr, long out_indices_ptr) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - CHECK(theme.GetAssetManager() == assetmanager); - // (void) assetmanager; - - ResXMLParser xml_parser = - xml_parser_ptr == 0 ? null : NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr); - // int[] out_values = reinterpret_cast(out_values_ptr); - // int[] out_indices = reinterpret_cast(out_indices_ptr); - ShadowVMRuntime shadowVMRuntime = Shadow.extract(VMRuntime.getRuntime()); - int[] out_values = (int[])shadowVMRuntime.getObjectForAddress(out_values_ptr); - int[] out_indices = (int[])shadowVMRuntime.getObjectForAddress(out_indices_ptr); - - int attrs_len = java_attrs.length; - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); - // if (attrs == null) { - // return; - // } + // static jobjectArray NativeGetLocales(JNIEnv* env, jclass /*class*/, jlong ptr, + // jboolean exclude_system) { + @Implementation(minSdk = P) + protected static @Nullable String[] nativeGetLocales(long ptr, boolean exclude_system) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Set locales = + assetmanager.GetResourceLocales(exclude_system, true /*merge_equivalent_languages*/); + + String[] array = + new String[locales.size()]; // env.NewObjectArray(locales.size(), g_stringClass, null); + // if (array == null) { + // return null; + // } - ApplyStyle(theme, xml_parser, (int) (def_style_attr), - (int) (def_style_resid), attrs, attrs_len, - out_values, out_indices); - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + int idx = 0; + for (String locale : locales) { + String java_string = locale; + if (java_string == null) { + return null; + } + // env.SetObjectArrayElement(array, idx++, java_string); + array[idx++] = java_string; + // env.DeleteLocalRef(java_string); } + return array; + } - // static jboolean NativeResolveAttrs(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint def_style_attr, jint def_style_resid, jintArray java_values, -// jintArray java_attrs, jintArray out_java_values, -// jintArray out_java_indices) { - @Implementation(minSdk = P) - protected static boolean nativeResolveAttrs(long ptr, long theme_ptr, - @AttrRes int def_style_attr, @StyleRes int def_style_resid, @Nullable int[] java_values, - @NonNull int[] java_attrs, @NonNull int[] out_java_values, @NonNull int[] out_java_indices) { - int attrs_len = java_attrs.length; - int out_values_len = out_java_values.length; - if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { - throw new IndexOutOfBoundsException("outValues too small"); - } + static Configuration ConstructConfigurationObject(/* JNIEnv* env,*/ ResTable_config config) { + // jobject result = + // env.NewObject(gConfigurationOffsets.classObject, gConfigurationOffsets.constructor); + Configuration result = new Configuration(); + // if (result == null) { + // return null; + // } - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); - if (attrs == null) { - return JNI_FALSE; - } + result.smallestScreenWidthDp = config.smallestScreenWidthDp; + result.screenWidthDp = config.screenWidthDp; + result.screenHeightDp = config.screenHeightDp; + return result; + } - int[] values = null; - int values_len = 0; - if (java_values != null) { - values_len = java_values.length; - values = java_values; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_values, null)); - if (values == null) { - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - return JNI_FALSE; - } - } + // static jobjectArray NativeGetSizeConfigurations(JNIEnv* env, jclass /*clazz*/, jlong ptr) { + @Implementation(minSdk = P) + protected static @Nullable Configuration[] nativeGetSizeConfigurations(long ptr) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Set configurations = + assetmanager.GetResourceConfigurations(true /*exclude_system*/, false /*exclude_mipmap*/); - int[] out_values = out_java_values; - // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_values, null)); - if (out_values == null) { - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - // if (values != null) { - // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); - // } - return JNI_FALSE; - } + Configuration[] array = new Configuration[configurations.size()]; + // env.NewObjectArray(configurations.size(), gConfigurationOffsets.classObject, null); + // if (array == null) { + // return null; + // } - int[] out_indices = null; - if (out_java_indices != null) { - int out_indices_len = out_java_indices.length; - if (out_indices_len > attrs_len) { - out_indices = out_java_indices; - // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_indices, null)); - if (out_indices == null) { - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - // if (values != null) { - // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); - // } - // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT); - return JNI_FALSE; - } - } - } + int idx = 0; + for (ResTable_config configuration : configurations) { + Configuration java_configuration = ConstructConfigurationObject(configuration); + // if (java_configuration == null) { + // return null; + // } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - CHECK(theme.GetAssetManager() == assetmanager); - // (void) assetmanager; - - boolean result = ResolveAttrs( - theme, (int) (def_style_attr), (int) (def_style_resid), - values, values_len, attrs, - attrs_len, out_values, out_indices); - // if (out_indices != null) { - // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); - // } + // env.SetObjectArrayElement(array, idx++, java_configuration); + array[idx++] = java_configuration; + // env.DeleteLocalRef(java_configuration); + } + return array; + } - // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0); - // if (values != null) { - // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); - // } + // static void NativeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, + // jint def_style_attr, jint def_style_resid, jlong xml_parser_ptr, + // jintArray java_attrs, jlong out_values_ptr, jlong out_indices_ptr) + // { + @Implementation(minSdk = P) + protected static void nativeApplyStyle( + long ptr, + long theme_ptr, + @AttrRes int def_style_attr, + @StyleRes int def_style_resid, + long xml_parser_ptr, + @NonNull int[] java_attrs, + long out_values_ptr, + long out_indices_ptr) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + CHECK(theme.GetAssetManager() == assetmanager); + // (void) assetmanager; + + ResXMLParser xml_parser = + xml_parser_ptr == 0 ? null : NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr); + // int[] out_values = reinterpret_cast(out_values_ptr); + // int[] out_indices = reinterpret_cast(out_indices_ptr); + ShadowVMRuntime shadowVMRuntime = Shadow.extract(VMRuntime.getRuntime()); + int[] out_values = (int[]) shadowVMRuntime.getObjectForAddress(out_values_ptr); + int[] out_indices = (int[]) shadowVMRuntime.getObjectForAddress(out_indices_ptr); + + int attrs_len = java_attrs.length; + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + // if (attrs == null) { + // return; + // } + + ApplyStyle( + theme, + xml_parser, + (int) (def_style_attr), + (int) (def_style_resid), + attrs, + attrs_len, + out_values, + out_indices); + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + } + + // static jboolean NativeResolveAttrs(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, + // jint def_style_attr, jint def_style_resid, jintArray + // java_values, + // jintArray java_attrs, jintArray out_java_values, + // jintArray out_java_indices) { + @Implementation(minSdk = P) + protected static boolean nativeResolveAttrs( + long ptr, + long theme_ptr, + @AttrRes int def_style_attr, + @StyleRes int def_style_resid, + @Nullable int[] java_values, + @NonNull int[] java_attrs, + @NonNull int[] out_java_values, + @NonNull int[] out_java_indices) { + int attrs_len = java_attrs.length; + int out_values_len = out_java_values.length; + if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { + throw new IndexOutOfBoundsException("outValues too small"); + } + + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + if (attrs == null) { + return JNI_FALSE; + } + + int[] values = null; + int values_len = 0; + if (java_values != null) { + values_len = java_values.length; + values = + java_values; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_values, null)); + if (values == null) { // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - return result ? JNI_TRUE : JNI_FALSE; - } - - // static jboolean NativeRetrieveAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jlong xml_parser_ptr, jintArray java_attrs, -// jintArray out_java_values, jintArray out_java_indices) { - @Implementation(minSdk = P) - protected static boolean nativeRetrieveAttributes(long ptr, long xml_parser_ptr, - @NonNull int[] java_attrs, @NonNull int[] out_java_values, @NonNull int[] out_java_indices) { - int attrs_len = java_attrs.length; - int out_values_len = out_java_values.length; - if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { - throw new IndexOutOfBoundsException("outValues too small"); - } + return JNI_FALSE; + } + } - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); - if (attrs == null) { - return JNI_FALSE; - } + int[] out_values = out_java_values; + // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_values, null)); + if (out_values == null) { + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + // if (values != null) { + // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); + // } + return JNI_FALSE; + } - int[] out_values = out_java_values; - // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_values, null)); - if (out_values == null) { - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - return JNI_FALSE; + int[] out_indices = null; + if (out_java_indices != null) { + int out_indices_len = out_java_indices.length; + if (out_indices_len > attrs_len) { + out_indices = out_java_indices; + // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_indices, null)); + if (out_indices == null) { + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + // if (values != null) { + // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); + // } + // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT); + return JNI_FALSE; } + } + } - int[] out_indices = null; - if (out_java_indices != null) { - int out_indices_len = out_java_indices.length; - if (out_indices_len > attrs_len) { - out_indices = out_java_indices; - // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_indices, null)); - if (out_indices == null) { - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT); - return JNI_FALSE; - } - } - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + CHECK(theme.GetAssetManager() == assetmanager); + // (void) assetmanager; + + boolean result = + ResolveAttrs( + theme, + (int) (def_style_attr), + (int) (def_style_resid), + values, + values_len, + attrs, + attrs_len, + out_values, + out_indices); + // if (out_indices != null) { + // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); + // } - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - ResXMLParser xml_parser = NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr); + // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0); + // if (values != null) { + // env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT); + // } + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + return result ? JNI_TRUE : JNI_FALSE; + } - boolean result = RetrieveAttributes(assetmanager, xml_parser, - attrs, attrs_len, - out_values, - out_indices); + // static jboolean NativeRetrieveAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, + // jlong xml_parser_ptr, jintArray java_attrs, + // jintArray out_java_values, jintArray out_java_indices) + // { + @Implementation(minSdk = P) + protected static boolean nativeRetrieveAttributes( + long ptr, + long xml_parser_ptr, + @NonNull int[] java_attrs, + @NonNull int[] out_java_values, + @NonNull int[] out_java_indices) { + int attrs_len = java_attrs.length; + int out_values_len = out_java_values.length; + if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { + throw new IndexOutOfBoundsException("outValues too small"); + } - // if (out_indices != null) { - // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); - // } - // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0); - // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); - return result; - } - - // static jlong NativeThemeCreate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { - @Implementation(minSdk = P) - protected static long nativeThemeCreate(long ptr) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - return Registries.NATIVE_THEME9_REGISTRY.register(assetmanager.NewTheme()); - } - - // static void NativeThemeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) { - @Implementation(minSdk = P) - protected static void nativeThemeDestroy(long theme_ptr) { - Registries.NATIVE_THEME9_REGISTRY.unregister(theme_ptr); - } - - // static void NativeThemeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint resid, jboolean force) { - @Implementation(minSdk = P) - protected static void nativeThemeApplyStyle(long ptr, long theme_ptr, @StyleRes int resid, - boolean force) { - // AssetManager is accessed via the theme, so grab an explicit lock here. - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - CHECK(theme.GetAssetManager() == assetmanager); - // (void) assetmanager; - theme.ApplyStyle(resid, force); - - // TODO(adamlesinski): Consider surfacing exception when result is failure. - // CTS currently expects no exceptions from this method. - // std::string error_msg = StringPrintf("Failed to apply style 0x%08x to theme", resid); - // throw new IllegalArgumentException(error_msg.c_str()); - } - - // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_theme_ptr, -// jlong src_theme_ptr) { - @Implementation(minSdk = P, maxSdk = P) - protected static void nativeThemeCopy(long dst_theme_ptr, long src_theme_ptr) { - Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); - Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr); - if (!dst_theme.SetTo(src_theme)) { - throw new IllegalArgumentException("Themes are from different AssetManagers"); - } + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + if (attrs == null) { + return JNI_FALSE; } - // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_asset_manager_ptr, - // jlong dst_theme_ptr, jlong src_asset_manager_ptr, jlong src_theme_ptr) { - @Implementation(minSdk = Q) - protected static void nativeThemeCopy( - long dst_asset_manager_ptr, - long dst_theme_ptr, - long src_asset_manager_ptr, - long src_theme_ptr) { - Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); - Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr); - if (dst_asset_manager_ptr != src_asset_manager_ptr) { - CppAssetManager2 dst_assetmanager = AssetManagerFromLong(dst_asset_manager_ptr); - CHECK(dst_theme.GetAssetManager() == dst_assetmanager); - // (void) dst_assetmanager; - - CppAssetManager2 src_assetmanager = AssetManagerFromLong(src_asset_manager_ptr); - CHECK(src_theme.GetAssetManager() == src_assetmanager); - // (void) src_assetmanager; - - dst_theme.SetTo(src_theme); - } else { - dst_theme.SetTo(src_theme); - } + int[] out_values = out_java_values; + // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_values, null)); + if (out_values == null) { + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + return JNI_FALSE; } - // static void NativeThemeClear(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) { - @Implementation(minSdk = P) - protected static void nativeThemeClear(long themePtr) { - Registries.NATIVE_THEME9_REGISTRY.getNativeObject(themePtr).Clear(); - } - - // static jint NativeThemeGetAttributeValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint resid, jobject typed_value, -// jboolean resolve_references) { - @Implementation(minSdk = P) - protected static int nativeThemeGetAttributeValue(long ptr, long theme_ptr, - @AttrRes int resid, @NonNull TypedValue typed_value, boolean resolve_references) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - CHECK(theme.GetAssetManager() == assetmanager); - // (void) assetmanager; // huh? - - final Ref value = new Ref<>(null); - final Ref flags = new Ref<>(null); - ApkAssetsCookie cookie = theme.GetAttribute(resid, value, flags); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + int[] out_indices = null; + if (out_java_indices != null) { + int out_indices_len = out_java_indices.length; + if (out_indices_len > attrs_len) { + out_indices = out_java_indices; + // reinterpret_cast(env.GetPrimitiveArrayCritical(out_java_indices, null)); + if (out_indices == null) { + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT); + return JNI_FALSE; } + } + } - final Ref ref = new Ref<>(0); - if (resolve_references) { - final Ref selected_config = new Ref<>(null); - cookie = - theme.GetAssetManager().ResolveReference(cookie, value, selected_config, flags, ref); - if (cookie.intValue() == kInvalidCookie) { - return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); - } - } - return CopyValue(cookie, value.get(), ref.get(), flags.get(), null, typed_value); - } - - // static void NativeThemeDump(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint priority, jstring tag, jstring prefix) { - @Implementation(minSdk = P) - protected static void nativeThemeDump(long ptr, long theme_ptr, int priority, String tag, - String prefix) { - CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - CHECK(theme.GetAssetManager() == assetmanager); - // (void) assetmanager; - // (void) theme; - // (void) priority; - // (void) tag; - // (void) prefix; - } - - // static jint NativeThemeGetChangingConfigurations(JNIEnv* /*env*/, jclass /*clazz*/, -// jlong theme_ptr) { - @Implementation(minSdk = P) - protected static @NativeConfig int nativeThemeGetChangingConfigurations(long theme_ptr) { - Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); - return (int) (theme.GetChangingConfigurations()); - } - - // static void NativeAssetDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { - @Implementation(minSdk = P) - protected static void nativeAssetDestroy(long asset_ptr) { - Registries.NATIVE_ASSET_REGISTRY.unregister(asset_ptr); - } - - // static jint NativeAssetReadChar(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { - @Implementation(minSdk = P) - protected static int nativeAssetReadChar(long asset_ptr) { - Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - byte[] b = new byte[1]; - int res = asset.read(b, 1); - return res == 1 ? (int) (b[0]) & 0xff : -1; - } - - // static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray java_buffer, -// jint offset, jint len) { - @Implementation(minSdk = P) - protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int offset, int len) - throws IOException { - if (len == 0) { - return 0; - } + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + ResXMLParser xml_parser = NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr); - int buffer_len = java_buffer.length; - if (offset < 0 || offset >= buffer_len || len < 0 || len > buffer_len || - offset > buffer_len - len) { - throw new IndexOutOfBoundsException(); - } + boolean result = + RetrieveAttributes(assetmanager, xml_parser, attrs, attrs_len, out_values, out_indices); - // ScopedByteArrayRW byte_array(env, java_buffer); - // if (byte_array.get() == null) { - // return -1; - // } + // if (out_indices != null) { + // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); + // } + // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0); + // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); + return result; + } - Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - // sint res = asset.read(byte_array.get() + offset, len); - int res = asset.read(java_buffer, offset, len); - if (res < 0) { - throw new IOException(); - } - return res > 0 ? (int) (res) : -1; + // static jlong NativeThemeCreate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { + @Implementation(minSdk = P) + protected static long nativeThemeCreate(long ptr) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + return Registries.NATIVE_THEME9_REGISTRY.register(assetmanager.NewTheme()); + } + + // static void NativeThemeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) { + @Implementation(minSdk = P) + protected static void nativeThemeDestroy(long theme_ptr) { + Registries.NATIVE_THEME9_REGISTRY.unregister(theme_ptr); + } + + // static void NativeThemeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, + // jint resid, jboolean force) { + @Implementation(minSdk = P) + protected static void nativeThemeApplyStyle( + long ptr, long theme_ptr, @StyleRes int resid, boolean force) { + // AssetManager is accessed via the theme, so grab an explicit lock here. + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + CHECK(theme.GetAssetManager() == assetmanager); + // (void) assetmanager; + theme.ApplyStyle(resid, force); + + // TODO(adamlesinski): Consider surfacing exception when result is failure. + // CTS currently expects no exceptions from this method. + // std::string error_msg = StringPrintf("Failed to apply style 0x%08x to theme", resid); + // throw new IllegalArgumentException(error_msg.c_str()); + } + + // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_theme_ptr, + // jlong src_theme_ptr) { + @Implementation(minSdk = P, maxSdk = P) + protected static void nativeThemeCopy(long dst_theme_ptr, long src_theme_ptr) { + Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); + Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr); + if (!dst_theme.SetTo(src_theme)) { + throw new IllegalArgumentException("Themes are from different AssetManagers"); } + } - // static jlong NativeAssetSeek(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jlong offset, -// jint whence) { - @Implementation(minSdk = P) - protected static long nativeAssetSeek(long asset_ptr, long offset, int whence) { - Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - return asset.seek( - (offset), (whence > 0 ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR))); + // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_asset_manager_ptr, + // jlong dst_theme_ptr, jlong src_asset_manager_ptr, jlong src_theme_ptr) { + @Implementation(minSdk = Q) + protected static void nativeThemeCopy( + long dst_asset_manager_ptr, + long dst_theme_ptr, + long src_asset_manager_ptr, + long src_theme_ptr) { + Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); + Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr); + if (dst_asset_manager_ptr != src_asset_manager_ptr) { + CppAssetManager2 dst_assetmanager = AssetManagerFromLong(dst_asset_manager_ptr); + CHECK(dst_theme.GetAssetManager() == dst_assetmanager); + // (void) dst_assetmanager; + + CppAssetManager2 src_assetmanager = AssetManagerFromLong(src_asset_manager_ptr); + CHECK(src_theme.GetAssetManager() == src_assetmanager); + // (void) src_assetmanager; + + dst_theme.SetTo(src_theme); + } else { + dst_theme.SetTo(src_theme); } + } + + // static void NativeThemeClear(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) { + @Implementation(minSdk = P) + protected static void nativeThemeClear(long themePtr) { + Registries.NATIVE_THEME9_REGISTRY.getNativeObject(themePtr).Clear(); + } - // static jlong NativeAssetGetLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { - @Implementation(minSdk = P) - protected static long nativeAssetGetLength(long asset_ptr) { - Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - return asset.getLength(); + // static jint NativeThemeGetAttributeValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong + // theme_ptr, + // jint resid, jobject typed_value, + // jboolean resolve_references) { + @Implementation(minSdk = P) + protected static int nativeThemeGetAttributeValue( + long ptr, + long theme_ptr, + @AttrRes int resid, + @NonNull TypedValue typed_value, + boolean resolve_references) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + CHECK(theme.GetAssetManager() == assetmanager); + // (void) assetmanager; // huh? + + final Ref value = new Ref<>(null); + final Ref flags = new Ref<>(null); + ApkAssetsCookie cookie = theme.GetAttribute(resid, value, flags); + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); } - // static jlong NativeAssetGetRemainingLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { - @Implementation(minSdk = P) - protected static long nativeAssetGetRemainingLength(long asset_ptr) { - Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - return asset.getRemainingLength(); + final Ref ref = new Ref<>(0); + if (resolve_references) { + final Ref selected_config = new Ref<>(null); + cookie = theme.GetAssetManager().ResolveReference(cookie, value, selected_config, flags, ref); + if (cookie.intValue() == kInvalidCookie) { + return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); + } } + return CopyValue(cookie, value.get(), ref.get(), flags.get(), null, typed_value); + } -// ---------------------------------------------------------------------------- + // static void NativeThemeDump(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jlong theme_ptr, + // jint priority, jstring tag, jstring prefix) { + @Implementation(minSdk = P) + protected static void nativeThemeDump( + long ptr, long theme_ptr, int priority, String tag, String prefix) { + CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + CHECK(theme.GetAssetManager() == assetmanager); + // (void) assetmanager; + // (void) theme; + // (void) priority; + // (void) tag; + // (void) prefix; + } - // JNI registration. - // static JNINativeMethod gAssetManagerMethods[] = { - // // AssetManager setup methods. - // {"nativeCreate", "()J", (void*)NativeCreate}, - // {"nativeDestroy", "(J)V", (void*)NativeDestroy}, - // {"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;Z)V", (void*)NativeSetApkAssets}, - // {"nativeSetConfiguration", "(JIILjava/lang/String;IIIIIIIIIIIIIII)V", - // (void*)NativeSetConfiguration}, - // {"nativeGetAssignedPackageIdentifiers", "(J)Landroid/util/SparseArray;", - // (void*)NativeGetAssignedPackageIdentifiers}, - // - // // AssetManager file methods. - // {"nativeList", "(JLjava/lang/String;)[Ljava/lang/String;", (void*)NativeList}, - // {"nativeOpenAsset", "(JLjava/lang/String;I)J", (void*)NativeOpenAsset}, - // {"nativeOpenAssetFd", "(JLjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", - // (void*)NativeOpenAssetFd}, - // {"nativeOpenNonAsset", "(JILjava/lang/String;I)J", (void*)NativeOpenNonAsset}, - // {"nativeOpenNonAssetFd", "(JILjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", - // (void*)NativeOpenNonAssetFd}, - // {"nativeOpenXmlAsset", "(JILjava/lang/String;)J", (void*)NativeOpenXmlAsset}, - // - // // AssetManager resource methods. - // {"nativeGetResourceValue", "(JISLandroid/util/TypedValue;Z)I", (void*)NativeGetResourceValue}, - // {"nativeGetResourceBagValue", "(JIILandroid/util/TypedValue;)I", - // (void*)NativeGetResourceBagValue}, - // {"nativeGetStyleAttributes", "(JI)[I", (void*)NativeGetStyleAttributes}, - // {"nativeGetResourceStringArray", "(JI)[Ljava/lang/String;", - // (void*)NativeGetResourceStringArray}, - // {"nativeGetResourceStringArrayInfo", "(JI)[I", (void*)NativeGetResourceStringArrayInfo}, - // {"nativeGetResourceIntArray", "(JI)[I", (void*)NativeGetResourceIntArray}, - // {"nativeGetResourceArraySize", "(JI)I", (void*)NativeGetResourceArraySize}, - // {"nativeGetResourceArray", "(JI[I)I", (void*)NativeGetResourceArray}, - // - // // AssetManager resource name/ID methods. - // {"nativeGetResourceIdentifier", "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", - // (void*)NativeGetResourceIdentifier}, - // {"nativeGetResourceName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceName}, - // {"nativeGetResourcePackageName", "(JI)Ljava/lang/String;", (void*)NativeGetResourcePackageName}, - // {"nativeGetResourceTypeName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceTypeName}, - // {"nativeGetResourceEntryName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceEntryName}, - // {"nativeGetLocales", "(JZ)[Ljava/lang/String;", (void*)NativeGetLocales}, - // {"nativeGetSizeConfigurations", "(J)[Landroid/content/res/Configuration;", - // (void*)NativeGetSizeConfigurations}, - // - // // Style attribute related methods. - // {"nativeApplyStyle", "(JJIIJ[IJJ)V", (void*)NativeApplyStyle}, - // {"nativeResolveAttrs", "(JJII[I[I[I[I)Z", (void*)NativeResolveAttrs}, - // {"nativeRetrieveAttributes", "(JJ[I[I[I)Z", (void*)NativeRetrieveAttributes}, - // - // // Theme related methods. - // {"nativeThemeCreate", "(J)J", (void*)NativeThemeCreate}, - // {"nativeThemeDestroy", "(J)V", (void*)NativeThemeDestroy}, - // {"nativeThemeApplyStyle", "(JJIZ)V", (void*)NativeThemeApplyStyle}, - // {"nativeThemeCopy", "(JJ)V", (void*)NativeThemeCopy}, - // {"nativeThemeClear", "(J)V", (void*)NativeThemeClear}, - // {"nativeThemeGetAttributeValue", "(JJILandroid/util/TypedValue;Z)I", - // (void*)NativeThemeGetAttributeValue}, - // {"nativeThemeDump", "(JJILjava/lang/String;Ljava/lang/String;)V", (void*)NativeThemeDump}, - // {"nativeThemeGetChangingConfigurations", "(J)I", (void*)NativeThemeGetChangingConfigurations}, - // - // // AssetInputStream methods. - // {"nativeAssetDestroy", "(J)V", (void*)NativeAssetDestroy}, - // {"nativeAssetReadChar", "(J)I", (void*)NativeAssetReadChar}, - // {"nativeAssetRead", "(J[BII)I", (void*)NativeAssetRead}, - // {"nativeAssetSeek", "(JJI)J", (void*)NativeAssetSeek}, - // {"nativeAssetGetLength", "(J)J", (void*)NativeAssetGetLength}, - // {"nativeAssetGetRemainingLength", "(J)J", (void*)NativeAssetGetRemainingLength}, - // - // // System/idmap related methods. - // {"nativeVerifySystemIdmaps", "()V", (void*)NativeVerifySystemIdmaps}, - // - // // Global management/debug methods. - // {"getGlobalAssetCount", "()I", (void*)NativeGetGlobalAssetCount}, - // {"getAssetAllocations", "()Ljava/lang/String;", (void*)NativeGetAssetAllocations}, - // {"getGlobalAssetManagerCount", "()I", (void*)NativeGetGlobalAssetManagerCount}, - // }; - // - // int register_android_content_AssetManager(JNIEnv* env) { - // jclass apk_assets_class = FindClassOrDie(env, "android/content/res/ApkAssets"); - // gApkAssetsFields.native_ptr = GetFieldIDOrDie(env, apk_assets_class, "mNativePtr", "J"); - // - // jclass typedValue = FindClassOrDie(env, "android/util/TypedValue"); - // gTypedValueOffsets.mType = GetFieldIDOrDie(env, typedValue, "type", "I"); - // gTypedValueOffsets.mData = GetFieldIDOrDie(env, typedValue, "data", "I"); - // gTypedValueOffsets.mString = - // GetFieldIDOrDie(env, typedValue, "string", "Ljava/lang/CharSequence;"); - // gTypedValueOffsets.mAssetCookie = GetFieldIDOrDie(env, typedValue, "assetCookie", "I"); - // gTypedValueOffsets.mResourceId = GetFieldIDOrDie(env, typedValue, "resourceId", "I"); - // gTypedValueOffsets.mChangingConfigurations = - // GetFieldIDOrDie(env, typedValue, "changingConfigurations", "I"); - // gTypedValueOffsets.mDensity = GetFieldIDOrDie(env, typedValue, "density", "I"); - // - // jclass assetFd = FindClassOrDie(env, "android/content/res/AssetFileDescriptor"); - // gAssetFileDescriptorOffsets.mFd = - // GetFieldIDOrDie(env, assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;"); - // gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset", "J"); - // gAssetFileDescriptorOffsets.mLength = GetFieldIDOrDie(env, assetFd, "mLength", "J"); - // - // jclass assetManager = FindClassOrDie(env, "android/content/res/AssetManager"); - // gAssetManagerOffsets.mObject = GetFieldIDOrDie(env, assetManager, "mObject", "J"); - // - // jclass stringClass = FindClassOrDie(env, "java/lang/String"); - // g_stringClass = MakeGlobalRefOrDie(env, stringClass); - // - // jclass sparseArrayClass = FindClassOrDie(env, "android/util/SparseArray"); - // gSparseArrayOffsets.classObject = MakeGlobalRefOrDie(env, sparseArrayClass); - // gSparseArrayOffsets.constructor = - // GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "", "()V"); - // gSparseArrayOffsets.put = - // GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "put", "(ILjava/lang/Object;)V"); - // - // jclass configurationClass = FindClassOrDie(env, "android/content/res/Configuration"); - // gConfigurationOffsets.classObject = MakeGlobalRefOrDie(env, configurationClass); - // gConfigurationOffsets.constructor = GetMethodIDOrDie(env, configurationClass, "", "()V"); - // gConfigurationOffsets.mSmallestScreenWidthDpOffset = - // GetFieldIDOrDie(env, configurationClass, "smallestScreenWidthDp", "I"); - // gConfigurationOffsets.mScreenWidthDpOffset = - // GetFieldIDOrDie(env, configurationClass, "screenWidthDp", "I"); - // gConfigurationOffsets.mScreenHeightDpOffset = - // GetFieldIDOrDie(env, configurationClass, "screenHeightDp", "I"); - // - // return RegisterMethodsOrDie(env, "android/content/res/AssetManager", gAssetManagerMethods, - // NELEM(gAssetManagerMethods)); - // } + // static jint NativeThemeGetChangingConfigurations(JNIEnv* /*env*/, jclass /*clazz*/, + // jlong theme_ptr) { + @Implementation(minSdk = P) + protected static @NativeConfig int nativeThemeGetChangingConfigurations(long theme_ptr) { + Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); + return (int) (theme.GetChangingConfigurations()); + } + + // static void NativeAssetDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { + @Implementation(minSdk = P) + protected static void nativeAssetDestroy(long asset_ptr) { + Registries.NATIVE_ASSET_REGISTRY.unregister(asset_ptr); + } + + // static jint NativeAssetReadChar(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { + @Implementation(minSdk = P) + protected static int nativeAssetReadChar(long asset_ptr) { + Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); + byte[] b = new byte[1]; + int res = asset.read(b, 1); + return res == 1 ? (int) (b[0]) & 0xff : -1; + } + + // static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray + // java_buffer, + // jint offset, jint len) { + @Implementation(minSdk = P) + protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int offset, int len) + throws IOException { + if (len == 0) { + return 0; + } + + int buffer_len = java_buffer.length; + if (offset < 0 + || offset >= buffer_len + || len < 0 + || len > buffer_len + || offset > buffer_len - len) { + throw new IndexOutOfBoundsException(); + } + + // ScopedByteArrayRW byte_array(env, java_buffer); + // if (byte_array.get() == null) { + // return -1; + // } + + Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); + // sint res = asset.read(byte_array.get() + offset, len); + int res = asset.read(java_buffer, offset, len); + if (res < 0) { + throw new IOException(); + } + return res > 0 ? (int) (res) : -1; + } + + // static jlong NativeAssetSeek(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jlong offset, + // jint whence) { + @Implementation(minSdk = P) + protected static long nativeAssetSeek(long asset_ptr, long offset, int whence) { + Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); + return asset.seek((offset), (whence > 0 ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR))); + } + + // static jlong NativeAssetGetLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { + @Implementation(minSdk = P) + protected static long nativeAssetGetLength(long asset_ptr) { + Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); + return asset.getLength(); + } + + // static jlong NativeAssetGetRemainingLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) + // { + @Implementation(minSdk = P) + protected static long nativeAssetGetRemainingLength(long asset_ptr) { + Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); + return asset.getRemainingLength(); + } + + // ---------------------------------------------------------------------------- + + // JNI registration. + // static JNINativeMethod gAssetManagerMethods[] = { + // // AssetManager setup methods. + // {"nativeCreate", "()J", (void*)NativeCreate}, + // {"nativeDestroy", "(J)V", (void*)NativeDestroy}, + // {"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;Z)V", (void*)NativeSetApkAssets}, + // {"nativeSetConfiguration", "(JIILjava/lang/String;IIIIIIIIIIIIIII)V", + // (void*)NativeSetConfiguration}, + // {"nativeGetAssignedPackageIdentifiers", "(J)Landroid/util/SparseArray;", + // (void*)NativeGetAssignedPackageIdentifiers}, + // + // // AssetManager file methods. + // {"nativeList", "(JLjava/lang/String;)[Ljava/lang/String;", (void*)NativeList}, + // {"nativeOpenAsset", "(JLjava/lang/String;I)J", (void*)NativeOpenAsset}, + // {"nativeOpenAssetFd", "(JLjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", + // (void*)NativeOpenAssetFd}, + // {"nativeOpenNonAsset", "(JILjava/lang/String;I)J", (void*)NativeOpenNonAsset}, + // {"nativeOpenNonAssetFd", "(JILjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;", + // (void*)NativeOpenNonAssetFd}, + // {"nativeOpenXmlAsset", "(JILjava/lang/String;)J", (void*)NativeOpenXmlAsset}, + // + // // AssetManager resource methods. + // {"nativeGetResourceValue", "(JISLandroid/util/TypedValue;Z)I", + // (void*)NativeGetResourceValue}, + // {"nativeGetResourceBagValue", "(JIILandroid/util/TypedValue;)I", + // (void*)NativeGetResourceBagValue}, + // {"nativeGetStyleAttributes", "(JI)[I", (void*)NativeGetStyleAttributes}, + // {"nativeGetResourceStringArray", "(JI)[Ljava/lang/String;", + // (void*)NativeGetResourceStringArray}, + // {"nativeGetResourceStringArrayInfo", "(JI)[I", (void*)NativeGetResourceStringArrayInfo}, + // {"nativeGetResourceIntArray", "(JI)[I", (void*)NativeGetResourceIntArray}, + // {"nativeGetResourceArraySize", "(JI)I", (void*)NativeGetResourceArraySize}, + // {"nativeGetResourceArray", "(JI[I)I", (void*)NativeGetResourceArray}, + // + // // AssetManager resource name/ID methods. + // {"nativeGetResourceIdentifier", "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", + // (void*)NativeGetResourceIdentifier}, + // {"nativeGetResourceName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceName}, + // {"nativeGetResourcePackageName", "(JI)Ljava/lang/String;", + // (void*)NativeGetResourcePackageName}, + // {"nativeGetResourceTypeName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceTypeName}, + // {"nativeGetResourceEntryName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceEntryName}, + // {"nativeGetLocales", "(JZ)[Ljava/lang/String;", (void*)NativeGetLocales}, + // {"nativeGetSizeConfigurations", "(J)[Landroid/content/res/Configuration;", + // (void*)NativeGetSizeConfigurations}, + // + // // Style attribute related methods. + // {"nativeApplyStyle", "(JJIIJ[IJJ)V", (void*)NativeApplyStyle}, + // {"nativeResolveAttrs", "(JJII[I[I[I[I)Z", (void*)NativeResolveAttrs}, + // {"nativeRetrieveAttributes", "(JJ[I[I[I)Z", (void*)NativeRetrieveAttributes}, + // + // // Theme related methods. + // {"nativeThemeCreate", "(J)J", (void*)NativeThemeCreate}, + // {"nativeThemeDestroy", "(J)V", (void*)NativeThemeDestroy}, + // {"nativeThemeApplyStyle", "(JJIZ)V", (void*)NativeThemeApplyStyle}, + // {"nativeThemeCopy", "(JJ)V", (void*)NativeThemeCopy}, + // {"nativeThemeClear", "(J)V", (void*)NativeThemeClear}, + // {"nativeThemeGetAttributeValue", "(JJILandroid/util/TypedValue;Z)I", + // (void*)NativeThemeGetAttributeValue}, + // {"nativeThemeDump", "(JJILjava/lang/String;Ljava/lang/String;)V", (void*)NativeThemeDump}, + // {"nativeThemeGetChangingConfigurations", "(J)I", + // (void*)NativeThemeGetChangingConfigurations}, + // + // // AssetInputStream methods. + // {"nativeAssetDestroy", "(J)V", (void*)NativeAssetDestroy}, + // {"nativeAssetReadChar", "(J)I", (void*)NativeAssetReadChar}, + // {"nativeAssetRead", "(J[BII)I", (void*)NativeAssetRead}, + // {"nativeAssetSeek", "(JJI)J", (void*)NativeAssetSeek}, + // {"nativeAssetGetLength", "(J)J", (void*)NativeAssetGetLength}, + // {"nativeAssetGetRemainingLength", "(J)J", (void*)NativeAssetGetRemainingLength}, + // + // // System/idmap related methods. + // {"nativeVerifySystemIdmaps", "()V", (void*)NativeVerifySystemIdmaps}, + // + // // Global management/debug methods. + // {"getGlobalAssetCount", "()I", (void*)NativeGetGlobalAssetCount}, + // {"getAssetAllocations", "()Ljava/lang/String;", (void*)NativeGetAssetAllocations}, + // {"getGlobalAssetManagerCount", "()I", (void*)NativeGetGlobalAssetManagerCount}, + // }; + // + // int register_android_content_AssetManager(JNIEnv* env) { + // jclass apk_assets_class = FindClassOrDie(env, "android/content/res/ApkAssets"); + // gApkAssetsFields.native_ptr = GetFieldIDOrDie(env, apk_assets_class, "mNativePtr", "J"); + // + // jclass typedValue = FindClassOrDie(env, "android/util/TypedValue"); + // gTypedValueOffsets.mType = GetFieldIDOrDie(env, typedValue, "type", "I"); + // gTypedValueOffsets.mData = GetFieldIDOrDie(env, typedValue, "data", "I"); + // gTypedValueOffsets.mString = + // GetFieldIDOrDie(env, typedValue, "string", "Ljava/lang/CharSequence;"); + // gTypedValueOffsets.mAssetCookie = GetFieldIDOrDie(env, typedValue, "assetCookie", "I"); + // gTypedValueOffsets.mResourceId = GetFieldIDOrDie(env, typedValue, "resourceId", "I"); + // gTypedValueOffsets.mChangingConfigurations = + // GetFieldIDOrDie(env, typedValue, "changingConfigurations", "I"); + // gTypedValueOffsets.mDensity = GetFieldIDOrDie(env, typedValue, "density", "I"); + // + // jclass assetFd = FindClassOrDie(env, "android/content/res/AssetFileDescriptor"); + // gAssetFileDescriptorOffsets.mFd = + // GetFieldIDOrDie(env, assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;"); + // gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset", + // "J"); + // gAssetFileDescriptorOffsets.mLength = GetFieldIDOrDie(env, assetFd, "mLength", "J"); + // + // jclass assetManager = FindClassOrDie(env, "android/content/res/AssetManager"); + // gAssetManagerOffsets.mObject = GetFieldIDOrDie(env, assetManager, "mObject", "J"); + // + // jclass stringClass = FindClassOrDie(env, "java/lang/String"); + // g_stringClass = MakeGlobalRefOrDie(env, stringClass); + // + // jclass sparseArrayClass = FindClassOrDie(env, "android/util/SparseArray"); + // gSparseArrayOffsets.classObject = MakeGlobalRefOrDie(env, sparseArrayClass); + // gSparseArrayOffsets.constructor = + // GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "", "()V"); + // gSparseArrayOffsets.put = + // GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "put", "(ILjava/lang/Object;)V"); + // + // jclass configurationClass = FindClassOrDie(env, "android/content/res/Configuration"); + // gConfigurationOffsets.classObject = MakeGlobalRefOrDie(env, configurationClass); + // gConfigurationOffsets.constructor = GetMethodIDOrDie(env, configurationClass, "", + // "()V"); + // gConfigurationOffsets.mSmallestScreenWidthDpOffset = + // GetFieldIDOrDie(env, configurationClass, "smallestScreenWidthDp", "I"); + // gConfigurationOffsets.mScreenWidthDpOffset = + // GetFieldIDOrDie(env, configurationClass, "screenWidthDp", "I"); + // gConfigurationOffsets.mScreenHeightDpOffset = + // GetFieldIDOrDie(env, configurationClass, "screenHeightDp", "I"); + // + // return RegisterMethodsOrDie(env, "android/content/res/AssetManager", gAssetManagerMethods, + // NELEM(gAssetManagerMethods)); + // } -}; // namespace android +} +; // namespace android diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager9.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager9.java index 969ccb7c311..ec9678e52ce 100644 --- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager9.java +++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager9.java @@ -1,6 +1,7 @@ package org.robolectric.shadows; -// transliterated from https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/core/jni/android_util_AssetManager.cpp +// transliterated from +// https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/core/jni/android_util_AssetManager.cpp import static android.os.Build.VERSION_CODES.P; import static android.os.Build.VERSION_CODES.Q; @@ -220,8 +221,9 @@ protected static void createSystemAssetsInZygoteLocked() { _assetManagerStatic_.setSystemApkAssets(cachedSystemApkAssets); _assetManagerStatic_.setSystemApkAssetsSet(cachedSystemApkAssetsSet); - sSystem = ReflectionHelpers.callConstructor(AssetManager.class, - ClassParameter.from(boolean.class, true /*sentinel*/)); + sSystem = + ReflectionHelpers.callConstructor( + AssetManager.class, ClassParameter.from(boolean.class, true /*sentinel*/)); sSystem.setApkAssets(cachedSystemApkAssets, false /*invalidateCaches*/); ReflectionHelpers.setStaticField(AssetManager.class, "sSystem", sSystem); } @@ -229,7 +231,8 @@ protected static void createSystemAssetsInZygoteLocked() { @Resetter public static void reset() { - // todo: ShadowPicker doesn't discriminate properly between concrete shadow classes for resetters... + // todo: ShadowPicker doesn't discriminate properly between concrete shadow classes for + // resetters... if (!useLegacy() && RuntimeEnvironment.getApiLevel() >= P) { _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class); _assetManagerStatic_.setSystemApkAssetsSet(null); @@ -344,8 +347,13 @@ protected static String[] nativeCreateIdmapsForStaticOverlaysTargetingAndroid() return new String[0]; } - static int CopyValue(/*JNIEnv* env,*/ ApkAssetsCookie cookie, Res_value value, int ref, - int type_spec_flags, ResTable_config config, TypedValue out_typed_value) { + static int CopyValue( + /*JNIEnv* env,*/ ApkAssetsCookie cookie, + Res_value value, + int ref, + int type_spec_flags, + ResTable_config config, + TypedValue out_typed_value) { out_typed_value.type = value.dataType; out_typed_value.assetCookie = ApkAssetsCookieToJavaCookie(cookie); out_typed_value.data = value.data; @@ -369,8 +377,10 @@ Collection getAllAssetDirs() { ArrayList assetDirs = new ArrayList<>(); for (ApkAssets apkAssets : apkAssetsArray) { - long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); - CppApkAssets cppApkAssets = Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr); + long apk_assets_native_ptr = + ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); + CppApkAssets cppApkAssets = + Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr); if (new File(cppApkAssets.GetPath()).isFile()) { assetDirs.add(Fs.forJar(Paths.get(cppApkAssets.GetPath())).getPath("assets")); @@ -386,7 +396,7 @@ List getAssetPaths() { return AssetManagerForJavaObject(realAssetManager).getAssetPaths(); } -// ---------------------------------------------------------------------------- + // ---------------------------------------------------------------------------- // interface AAssetManager {} // @@ -395,10 +405,12 @@ List getAssetPaths() { // CppAssetManager2 guarded_assetmanager = new CppAssetManager2(); // } - static CppAssetManager2 NdkAssetManagerForJavaObject(/* JNIEnv* env,*/ AssetManager jassetmanager) { + static CppAssetManager2 NdkAssetManagerForJavaObject( + /* JNIEnv* env,*/ AssetManager jassetmanager) { // long assetmanager_handle = env.GetLongField(jassetmanager, gAssetManagerOffsets.mObject); long assetmanager_handle = ReflectionHelpers.getField(jassetmanager, "mObject"); - CppAssetManager2 am = Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(assetmanager_handle); + CppAssetManager2 am = + Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(assetmanager_handle); if (am == null) { throw new IllegalStateException("AssetManager has been finalized!"); } @@ -414,8 +426,8 @@ static CppAssetManager2 AssetManagerFromLong(long ptr) { return Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(ptr); } - static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/ Asset asset, - long[] out_offsets) throws FileNotFoundException { + static ParcelFileDescriptor ReturnParcelFileDescriptor( + /* JNIEnv* env,*/ Asset asset, long[] out_offsets) throws FileNotFoundException { final Ref start_offset = new Ref<>(0L); final Ref length = new Ref<>(0L); FileDescriptor fd = asset.openFileDescriptor(start_offset, length); @@ -426,7 +438,8 @@ static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/ Asset a "This file can not be opened as a file descriptor; it is probably compressed"); } - long[] offsets = out_offsets; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_offsets, 0)); + long[] offsets = + out_offsets; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_offsets, 0)); if (offsets == null) { // close(fd); return null; @@ -448,9 +461,7 @@ static ParcelFileDescriptor ReturnParcelFileDescriptor(/* JNIEnv* env,*/ Asset a return ParcelFileDescriptor.open(asset.getFile(), ParcelFileDescriptor.MODE_READ_ONLY); } - /** - * Used for the creation of system assets. - */ + /** Used for the creation of system assets. */ @Implementation(minSdk = P) protected void __constructor__(boolean sentinel) { inNonSystemConstructor = true; @@ -489,7 +500,8 @@ protected static int getGlobalAssetManagerCount() { // static jlong NativeCreate(JNIEnv* /*env*/, jclass /*clazz*/) { @Implementation(minSdk = P) protected static long nativeCreate() { - // AssetManager2 needs to be protected by a lock. To avoid cache misses, we allocate the lock and + // AssetManager2 needs to be protected by a lock. To avoid cache misses, we allocate the lock + // and // AssetManager2 in a contiguous block (GuardedAssetManager). // return reinterpret_cast(new GuardedAssetManager()); @@ -512,7 +524,7 @@ protected static long nativeCreate() { return cppAssetManagerRef; } - // static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { + // static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { @Implementation(minSdk = P) protected static void nativeDestroy(long ptr) { if (ptr == systemCppAssetManager2Ref) { @@ -525,9 +537,11 @@ protected static void nativeDestroy(long ptr) { } // static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jobjectArray apk_assets_array, jboolean invalidate_caches) { + // jobjectArray apk_assets_array, jboolean invalidate_caches) { @Implementation(minSdk = P) - protected static void nativeSetApkAssets(long ptr, @NonNull android.content.res.ApkAssets[] apk_assets_array, + protected static void nativeSetApkAssets( + long ptr, + @NonNull android.content.res.ApkAssets[] apk_assets_array, boolean invalidate_caches) { ATRACE_NAME("AssetManager::SetApkAssets"); @@ -535,12 +549,14 @@ protected static void nativeSetApkAssets(long ptr, @NonNull android.content.res. List apk_assets = new ArrayList<>(); // apk_assets.reserve(apk_assets_len); for (int i = 0; i < apk_assets_len; i++) { - android.content.res.ApkAssets apkAssets = apk_assets_array[i]; // env.GetObjectArrayElement(apk_assets_array, i); + android.content.res.ApkAssets apkAssets = + apk_assets_array[i]; // env.GetObjectArrayElement(apk_assets_array, i); if (apkAssets == null) { throw new NullPointerException(String.format("ApkAssets at index %d is null", i)); } - long apk_assets_native_ptr = ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); + long apk_assets_native_ptr = + ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr(); // if (env.ExceptionCheck()) { // return; // } @@ -551,19 +567,36 @@ protected static void nativeSetApkAssets(long ptr, @NonNull android.content.res. assetmanager.SetApkAssets(apk_assets, invalidate_caches); } - // static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint mnc, -// jstring locale, jint orientation, jint touchscreen, jint density, -// jint keyboard, jint keyboard_hidden, jint navigation, -// jint screen_width, jint screen_height, -// jint smallest_screen_width_dp, jint screen_width_dp, -// jint screen_height_dp, jint screen_layout, jint ui_mode, -// jint color_mode, jint major_version) { + // static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint + // mnc, + // jstring locale, jint orientation, jint touchscreen, jint + // density, + // jint keyboard, jint keyboard_hidden, jint navigation, + // jint screen_width, jint screen_height, + // jint smallest_screen_width_dp, jint screen_width_dp, + // jint screen_height_dp, jint screen_layout, jint ui_mode, + // jint color_mode, jint major_version) { @Implementation(minSdk = P) - protected static void nativeSetConfiguration(long ptr, int mcc, int mnc, - @Nullable String locale, int orientation, int touchscreen, int density, int keyboard, - int keyboard_hidden, int navigation, int screen_width, int screen_height, - int smallest_screen_width_dp, int screen_width_dp, int screen_height_dp, int screen_layout, - int ui_mode, int color_mode, int major_version) { + protected static void nativeSetConfiguration( + long ptr, + int mcc, + int mnc, + @Nullable String locale, + int orientation, + int touchscreen, + int density, + int keyboard, + int keyboard_hidden, + int navigation, + int screen_width, + int screen_height, + int smallest_screen_width_dp, + int screen_width_dp, + int screen_height_dp, + int screen_layout, + int ui_mode, + int color_mode, + int major_version) { ATRACE_NAME("AssetManager::SetConfiguration"); ResTable_config configuration = new ResTable_config(); @@ -611,25 +644,25 @@ protected static void nativeSetConfiguration(long ptr, int mcc, int mnc, protected static @NonNull SparseArray nativeGetAssignedPackageIdentifiers(long ptr) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - SparseArray sparse_array = - new SparseArray<>(); + SparseArray sparse_array = new SparseArray<>(); if (sparse_array == null) { // An exception is pending. return null; } - assetmanager.ForEachPackage((String package_name, byte package_id) -> { - String jpackage_name = package_name; // env.NewStringUTF(package_name); - if (jpackage_name == null) { - // An exception is pending. - return; - } + assetmanager.ForEachPackage( + (String package_name, byte package_id) -> { + String jpackage_name = package_name; // env.NewStringUTF(package_name); + if (jpackage_name == null) { + // An exception is pending. + return; + } - // env.CallVoidMethod(sparse_array, gSparseArrayOffsets.put, (int) (package_id), - // jpackage_name); - sparse_array.put(package_id, jpackage_name); - }); + // env.CallVoidMethod(sparse_array, gSparseArrayOffsets.put, (int) (package_id), + // jpackage_name); + sparse_array.put(package_id, jpackage_name); + }); return sparse_array; } @@ -644,8 +677,7 @@ protected static void nativeSetConfiguration(long ptr, int mcc, int mnc, } CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - AssetDir asset_dir = - assetmanager.OpenDir(path_utf8); + AssetDir asset_dir = assetmanager.OpenDir(path_utf8); if (asset_dir == null) { throw new FileNotFoundException(path_utf8); } @@ -676,7 +708,7 @@ protected static void nativeSetConfiguration(long ptr, int mcc, int mnc, } // static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, -// jint access_mode) { + // jint access_mode) { @Implementation(minSdk = P) protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode) throws FileNotFoundException { @@ -696,8 +728,7 @@ protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int } CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); - Asset asset = - assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); + Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); if (!isTruthy(asset)) { throw new FileNotFoundException(asset_path_utf8); } @@ -705,10 +736,10 @@ protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int } // static jobject NativeOpenAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path, -// jlongArray out_offsets) { + // jlongArray out_offsets) { @Implementation(minSdk = P) - protected static ParcelFileDescriptor nativeOpenAssetFd(long ptr, - @NonNull String asset_path, long[] out_offsets) throws IOException { + protected static ParcelFileDescriptor nativeOpenAssetFd( + long ptr, @NonNull String asset_path, long[] out_offsets) throws IOException { String asset_path_utf8 = asset_path; if (asset_path_utf8 == null) { // This will throw NPE. @@ -726,10 +757,11 @@ protected static ParcelFileDescriptor nativeOpenAssetFd(long ptr, } // static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path, jint access_mode) { + // jstring asset_path, jint access_mode) { @Implementation(minSdk = P) - protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String asset_path, - int access_mode) throws FileNotFoundException { + protected static long nativeOpenNonAsset( + long ptr, int jcookie, @NonNull String asset_path, int access_mode) + throws FileNotFoundException { ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); String asset_path_utf8 = asset_path; if (asset_path_utf8 == null) { @@ -749,11 +781,10 @@ protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Asset asset; if (cookie.intValue() != kInvalidCookie) { - asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, - Asset.AccessMode.fromInt(access_mode)); + asset = + assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.fromInt(access_mode)); } else { - asset = assetmanager.OpenNonAsset(asset_path_utf8, - Asset.AccessMode.fromInt(access_mode)); + asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.fromInt(access_mode)); } if (!isTruthy(asset)) { @@ -763,10 +794,11 @@ protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String } // static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path, jlongArray out_offsets) { + // jstring asset_path, jlongArray out_offsets) { @Implementation(minSdk = P) - protected static @Nullable ParcelFileDescriptor nativeOpenNonAssetFd(long ptr, int jcookie, - @NonNull String asset_path, @NonNull long[] out_offsets) throws IOException { + protected static @Nullable ParcelFileDescriptor nativeOpenNonAssetFd( + long ptr, int jcookie, @NonNull String asset_path, @NonNull long[] out_offsets) + throws IOException { ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie); String asset_path_utf8 = asset_path; if (asset_path_utf8 == null) { @@ -791,7 +823,7 @@ protected static long nativeOpenNonAsset(long ptr, int jcookie, @NonNull String } // static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie, -// jstring asset_path) { + // jstring asset_path) { @Implementation(minSdk = P) protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path) throws FileNotFoundException { @@ -832,18 +864,22 @@ protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String } // static jint NativeGetResourceValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jshort density, jobject typed_value, -// jboolean resolve_references) { + // jshort density, jobject typed_value, + // jboolean resolve_references) { @Implementation(minSdk = P) - protected static int nativeGetResourceValue(long ptr, @AnyRes int resid, short density, - @NonNull TypedValue typed_value, boolean resolve_references) { + protected static int nativeGetResourceValue( + long ptr, + @AnyRes int resid, + short density, + @NonNull TypedValue typed_value, + boolean resolve_references) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); final Ref value = new Ref<>(null); final Ref selected_config = new Ref<>(null); final Ref flags = new Ref<>(0); ApkAssetsCookie cookie = - assetmanager.GetResource(resid, false /*may_be_bag*/, - (short) (density), value, selected_config, flags); + assetmanager.GetResource( + resid, false /*may_be_bag*/, (short) (density), value, selected_config, flags); if (cookie.intValue() == kInvalidCookie) { return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); } @@ -855,14 +891,15 @@ protected static int nativeGetResourceValue(long ptr, @AnyRes int resid, short d return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); } } - return CopyValue(cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value); + return CopyValue( + cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value); } // static jint NativeGetResourceBagValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jint bag_entry_id, jobject typed_value) { + // jint bag_entry_id, jobject typed_value) { @Implementation(minSdk = P) - protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int bag_entry_id, - @NonNull TypedValue typed_value) { + protected static int nativeGetResourceBagValue( + long ptr, @AnyRes int resid, int bag_entry_id, @NonNull TypedValue typed_value) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResolvedBag bag = assetmanager.GetBag(resid); if (bag == null) { @@ -895,10 +932,11 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int return CopyValue(cookie, value.get(), ref.get(), type_spec_flags.get(), null, typed_value); } - // static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + // static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { @Implementation(minSdk = P) - protected static @Nullable @AttrRes int[] nativeGetStyleAttributes(long ptr, - @StyleRes int resid) { + protected static @Nullable @AttrRes int[] nativeGetStyleAttributes( + long ptr, @StyleRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResolvedBag bag = assetmanager.GetBag(resid); if (bag == null) { @@ -919,10 +957,9 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int } // static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jint resid) { + // jint resid) { @Implementation(minSdk = P) - protected static @Nullable String[] nativeGetResourceStringArray(long ptr, - @ArrayRes int resid) { + protected static @Nullable String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResolvedBag bag = assetmanager.GetBag(resid); if (bag == null) { @@ -979,10 +1016,9 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int } // static jintArray NativeGetResourceStringArrayInfo(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jint resid) { + // jint resid) { @Implementation(minSdk = P) - protected static @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, - @ArrayRes int resid) { + protected static @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, @ArrayRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResolvedBag bag = assetmanager.GetBag(resid); if (bag == null) { @@ -994,7 +1030,7 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int // return null; // } - int[] buffer = array; //reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); + int[] buffer = array; // reinterpret_cast(env.GetPrimitiveArrayCritical(array, null)); // if (buffer == null) { // return null; // } @@ -1024,7 +1060,8 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int return array; } - // static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + // static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint + // resid) { @Implementation(minSdk = P) protected static @Nullable int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); @@ -1056,7 +1093,8 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int return null; } - if (value.get().dataType >= Res_value.TYPE_FIRST_INT && value.get().dataType <= Res_value.TYPE_LAST_INT) { + if (value.get().dataType >= Res_value.TYPE_FIRST_INT + && value.get().dataType <= Res_value.TYPE_LAST_INT) { buffer[i] = (int) (value.get().data); } } @@ -1064,7 +1102,8 @@ protected static int nativeGetResourceBagValue(long ptr, @AnyRes int resid, int return array; } - // static jint NativeGetResourceArraySize(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jint resid) { + // static jint NativeGetResourceArraySize(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jint + // resid) { @Implementation(minSdk = P) protected static int nativeGetResourceArraySize(long ptr, @ArrayRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); @@ -1076,10 +1115,10 @@ protected static int nativeGetResourceArraySize(long ptr, @ArrayRes int resid) { } // static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid, -// jintArray out_data) { + // jintArray out_data) { @Implementation(minSdk = P) - protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, - @NonNull int[] out_data) { + protected static int nativeGetResourceArray( + long ptr, @ArrayRes int resid, @NonNull int[] out_data) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResolvedBag bag = assetmanager.GetBag(resid); if (bag == null) { @@ -1095,7 +1134,8 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, throw new IllegalArgumentException("Input array is not large enough"); } - int[] buffer = out_data; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_data, null)); + int[] buffer = + out_data; // reinterpret_cast(env.GetPrimitiveArrayCritical(out_data, null)); if (buffer == null) { return -1; } @@ -1134,10 +1174,10 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, } // static jint NativeGetResourceIdentifier(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring name, -// jstring def_type, jstring def_package) { + // jstring def_type, jstring def_package) { @Implementation(minSdk = P) - protected static @AnyRes int nativeGetResourceIdentifier(long ptr, @NonNull String name, - @Nullable String def_type, @Nullable String def_package) { + protected static @AnyRes int nativeGetResourceIdentifier( + long ptr, @NonNull String name, @Nullable String def_type, @Nullable String def_package) { String name_utf8 = name; if (name_utf8 == null) { // This will throw NPE. @@ -1172,7 +1212,7 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, StringBuilder result = new StringBuilder(); if (name.package_ != null) { - result.append(name.package_/*, name.package_len*/); + result.append(name.package_ /*, name.package_len*/); } if (name.type != null /*|| name.type16 != null*/) { @@ -1181,7 +1221,7 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, } // if (name.type != null) { - result.append(name.type/*, name.type_len*/); + result.append(name.type /*, name.type_len*/); // } else { // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.type16 /*, name.type_len))*/); // } @@ -1193,7 +1233,7 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, } // if (name.entry != null) { - result.append(name.entry/*, name.entry_len*/); + result.append(name.entry /*, name.entry_len*/); // } else { // result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.entry16 /*, name.entry_len)*/); // } @@ -1201,10 +1241,10 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, return result.toString(); } - // static jstring NativeGetResourcePackageName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + // static jstring NativeGetResourcePackageName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint + // resid) { @Implementation(minSdk = P) - protected static @Nullable String nativeGetResourcePackageName(long ptr, - @AnyRes int resid) { + protected static @Nullable String nativeGetResourcePackageName(long ptr, @AnyRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); final ResourceName name = new ResourceName(); if (!assetmanager.GetResourceName(resid, name)) { @@ -1217,7 +1257,8 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, return null; } - // static jstring NativeGetResourceTypeName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + // static jstring NativeGetResourceTypeName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { @Implementation(minSdk = P) protected static @Nullable String nativeGetResourceTypeName(long ptr, @AnyRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); @@ -1228,13 +1269,15 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, if (name.type != null) { return name.type; - // } else if (name.get().type16 != null) { - // return name.get().type16; // env.NewString(reinterpret_cast(name.type16), name.type_len); + // } else if (name.get().type16 != null) { + // return name.get().type16; // env.NewString(reinterpret_cast(name.type16), + // name.type_len); } return null; } - // static jstring NativeGetResourceEntryName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) { + // static jstring NativeGetResourceEntryName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) + // { @Implementation(minSdk = P) protected static @Nullable String nativeGetResourceEntryName(long ptr, @AnyRes int resid) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); @@ -1245,21 +1288,23 @@ protected static int nativeGetResourceArray(long ptr, @ArrayRes int resid, if (name.entry != null) { return name.entry; - // } else if (name.entry16 != null) { - // return name.entry16; // env.NewString(reinterpret_cast(name.entry16), name.entry_len); + // } else if (name.entry16 != null) { + // return name.entry16; // env.NewString(reinterpret_cast(name.entry16), + // name.entry_len); } return null; } // static jobjectArray NativeGetLocales(JNIEnv* env, jclass /*class*/, jlong ptr, -// jboolean exclude_system) { + // jboolean exclude_system) { @Implementation(minSdk = P) protected static @Nullable String[] nativeGetLocales(long ptr, boolean exclude_system) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Set locales = assetmanager.GetResourceLocales(exclude_system, true /*merge_equivalent_languages*/); - String[] array = new String[locales.size()]; // env.NewObjectArray(locales.size(), g_stringClass, null); + String[] array = + new String[locales.size()]; // env.NewObjectArray(locales.size(), g_stringClass, null); // if (array == null) { // return null; // } @@ -1319,12 +1364,19 @@ static Configuration ConstructConfigurationObject(/* JNIEnv* env,*/ ResTable_con } // static void NativeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint def_style_attr, jint def_style_resid, jlong xml_parser_ptr, -// jintArray java_attrs, jlong out_values_ptr, jlong out_indices_ptr) { + // jint def_style_attr, jint def_style_resid, jlong xml_parser_ptr, + // jintArray java_attrs, jlong out_values_ptr, jlong out_indices_ptr) + // { @Implementation(minSdk = P) - protected static void nativeApplyStyle(long ptr, long theme_ptr, @AttrRes int def_style_attr, - @StyleRes int def_style_resid, long xml_parser_ptr, @NonNull int[] java_attrs, - long out_values_ptr, long out_indices_ptr) { + protected static void nativeApplyStyle( + long ptr, + long theme_ptr, + @AttrRes int def_style_attr, + @StyleRes int def_style_resid, + long xml_parser_ptr, + @NonNull int[] java_attrs, + long out_values_ptr, + long out_indices_ptr) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); CHECK(theme.GetAssetManager() == assetmanager); @@ -1335,36 +1387,51 @@ protected static void nativeApplyStyle(long ptr, long theme_ptr, @AttrRes int de // int[] out_values = reinterpret_cast(out_values_ptr); // int[] out_indices = reinterpret_cast(out_indices_ptr); ShadowVMRuntime shadowVMRuntime = Shadow.extract(VMRuntime.getRuntime()); - int[] out_values = (int[])shadowVMRuntime.getObjectForAddress(out_values_ptr); - int[] out_indices = (int[])shadowVMRuntime.getObjectForAddress(out_indices_ptr); + int[] out_values = (int[]) shadowVMRuntime.getObjectForAddress(out_values_ptr); + int[] out_indices = (int[]) shadowVMRuntime.getObjectForAddress(out_indices_ptr); int attrs_len = java_attrs.length; - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); // if (attrs == null) { // return; // } - ApplyStyle(theme, xml_parser, (int) (def_style_attr), - (int) (def_style_resid), attrs, attrs_len, - out_values, out_indices); + ApplyStyle( + theme, + xml_parser, + (int) (def_style_attr), + (int) (def_style_resid), + attrs, + attrs_len, + out_values, + out_indices); // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); } // static jboolean NativeResolveAttrs(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint def_style_attr, jint def_style_resid, jintArray java_values, -// jintArray java_attrs, jintArray out_java_values, -// jintArray out_java_indices) { + // jint def_style_attr, jint def_style_resid, jintArray + // java_values, + // jintArray java_attrs, jintArray out_java_values, + // jintArray out_java_indices) { @Implementation(minSdk = P) - protected static boolean nativeResolveAttrs(long ptr, long theme_ptr, - @AttrRes int def_style_attr, @StyleRes int def_style_resid, @Nullable int[] java_values, - @NonNull int[] java_attrs, @NonNull int[] out_java_values, @NonNull int[] out_java_indices) { + protected static boolean nativeResolveAttrs( + long ptr, + long theme_ptr, + @AttrRes int def_style_attr, + @StyleRes int def_style_resid, + @Nullable int[] java_values, + @NonNull int[] java_attrs, + @NonNull int[] out_java_values, + @NonNull int[] out_java_indices) { int attrs_len = java_attrs.length; int out_values_len = out_java_values.length; if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { throw new IndexOutOfBoundsException("outValues too small"); } - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); if (attrs == null) { return JNI_FALSE; } @@ -1373,7 +1440,8 @@ protected static boolean nativeResolveAttrs(long ptr, long theme_ptr, int values_len = 0; if (java_values != null) { values_len = java_values.length; - values = java_values; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_values, null)); + values = + java_values; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_values, null)); if (values == null) { // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT); return JNI_FALSE; @@ -1412,10 +1480,17 @@ protected static boolean nativeResolveAttrs(long ptr, long theme_ptr, CHECK(theme.GetAssetManager() == assetmanager); // (void) assetmanager; - boolean result = ResolveAttrs( - theme, (int) (def_style_attr), (int) (def_style_resid), - values, values_len, attrs, - attrs_len, out_values, out_indices); + boolean result = + ResolveAttrs( + theme, + (int) (def_style_attr), + (int) (def_style_resid), + values, + values_len, + attrs, + attrs_len, + out_values, + out_indices); // if (out_indices != null) { // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); // } @@ -1429,18 +1504,24 @@ protected static boolean nativeResolveAttrs(long ptr, long theme_ptr, } // static jboolean NativeRetrieveAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, -// jlong xml_parser_ptr, jintArray java_attrs, -// jintArray out_java_values, jintArray out_java_indices) { + // jlong xml_parser_ptr, jintArray java_attrs, + // jintArray out_java_values, jintArray out_java_indices) + // { @Implementation(minSdk = P) - protected static boolean nativeRetrieveAttributes(long ptr, long xml_parser_ptr, - @NonNull int[] java_attrs, @NonNull int[] out_java_values, @NonNull int[] out_java_indices) { + protected static boolean nativeRetrieveAttributes( + long ptr, + long xml_parser_ptr, + @NonNull int[] java_attrs, + @NonNull int[] out_java_values, + @NonNull int[] out_java_indices) { int attrs_len = java_attrs.length; int out_values_len = out_java_values.length; if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) { throw new IndexOutOfBoundsException("outValues too small"); } - int[] attrs = java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); + int[] attrs = + java_attrs; // reinterpret_cast(env.GetPrimitiveArrayCritical(java_attrs, null)); if (attrs == null) { return JNI_FALSE; } @@ -1469,10 +1550,8 @@ protected static boolean nativeRetrieveAttributes(long ptr, long xml_parser_ptr, CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); ResXMLParser xml_parser = NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr); - boolean result = RetrieveAttributes(assetmanager, xml_parser, - attrs, attrs_len, - out_values, - out_indices); + boolean result = + RetrieveAttributes(assetmanager, xml_parser, attrs, attrs_len, out_values, out_indices); // if (out_indices != null) { // env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0); @@ -1496,10 +1575,10 @@ protected static void nativeThemeDestroy(long theme_ptr) { } // static void NativeThemeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint resid, jboolean force) { + // jint resid, jboolean force) { @Implementation(minSdk = P) - protected static void nativeThemeApplyStyle(long ptr, long theme_ptr, @StyleRes int resid, - boolean force) { + protected static void nativeThemeApplyStyle( + long ptr, long theme_ptr, @StyleRes int resid, boolean force) { // AssetManager is accessed via the theme, so grab an explicit lock here. CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); @@ -1514,7 +1593,7 @@ protected static void nativeThemeApplyStyle(long ptr, long theme_ptr, @StyleRes } // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_theme_ptr, -// jlong src_theme_ptr) { + // jlong src_theme_ptr) { @Implementation(minSdk = P, maxSdk = P) protected static void nativeThemeCopy(long dst_theme_ptr, long src_theme_ptr) { Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); @@ -1524,11 +1603,14 @@ protected static void nativeThemeCopy(long dst_theme_ptr, long src_theme_ptr) { } } -// static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_asset_manager_ptr, -// jlong dst_theme_ptr, jlong src_asset_manager_ptr, jlong src_theme_ptr) { + // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_asset_manager_ptr, + // jlong dst_theme_ptr, jlong src_asset_manager_ptr, jlong src_theme_ptr) { @Implementation(minSdk = Build.VERSION_CODES.Q) - protected static void nativeThemeCopy(long dst_asset_manager_ptr, long dst_theme_ptr, - long src_asset_manager_ptr, long src_theme_ptr) { + protected static void nativeThemeCopy( + long dst_asset_manager_ptr, + long dst_theme_ptr, + long src_asset_manager_ptr, + long src_theme_ptr) { Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr); Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr); @@ -1551,12 +1633,17 @@ protected static void nativeThemeClear(long themePtr) { Registries.NATIVE_THEME9_REGISTRY.getNativeObject(themePtr).Clear(); } - // static jint NativeThemeGetAttributeValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint resid, jobject typed_value, -// jboolean resolve_references) { + // static jint NativeThemeGetAttributeValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong + // theme_ptr, + // jint resid, jobject typed_value, + // jboolean resolve_references) { @Implementation(minSdk = P) - protected static int nativeThemeGetAttributeValue(long ptr, long theme_ptr, - @AttrRes int resid, @NonNull TypedValue typed_value, boolean resolve_references) { + protected static int nativeThemeGetAttributeValue( + long ptr, + long theme_ptr, + @AttrRes int resid, + @NonNull TypedValue typed_value, + boolean resolve_references) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); CHECK(theme.GetAssetManager() == assetmanager); @@ -1572,8 +1659,7 @@ protected static int nativeThemeGetAttributeValue(long ptr, long theme_ptr, final Ref ref = new Ref<>(0); if (resolve_references) { final Ref selected_config = new Ref<>(null); - cookie = - theme.GetAssetManager().ResolveReference(cookie, value, selected_config, flags, ref); + cookie = theme.GetAssetManager().ResolveReference(cookie, value, selected_config, flags, ref); if (cookie.intValue() == kInvalidCookie) { return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE); } @@ -1582,10 +1668,10 @@ protected static int nativeThemeGetAttributeValue(long ptr, long theme_ptr, } // static void NativeThemeDump(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jlong theme_ptr, -// jint priority, jstring tag, jstring prefix) { + // jint priority, jstring tag, jstring prefix) { @Implementation(minSdk = P) - protected static void nativeThemeDump(long ptr, long theme_ptr, int priority, String tag, - String prefix) { + protected static void nativeThemeDump( + long ptr, long theme_ptr, int priority, String tag, String prefix) { CppAssetManager2 assetmanager = AssetManagerFromLong(ptr); Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); CHECK(theme.GetAssetManager() == assetmanager); @@ -1597,7 +1683,7 @@ protected static void nativeThemeDump(long ptr, long theme_ptr, int priority, St } // static jint NativeThemeGetChangingConfigurations(JNIEnv* /*env*/, jclass /*clazz*/, -// jlong theme_ptr) { + // jlong theme_ptr) { @Implementation(minSdk = P) protected static @NativeConfig int nativeThemeGetChangingConfigurations(long theme_ptr) { Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr); @@ -1619,8 +1705,9 @@ protected static int nativeAssetReadChar(long asset_ptr) { return res == 1 ? (int) (b[0]) & 0xff : -1; } - // static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray java_buffer, -// jint offset, jint len) { + // static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray + // java_buffer, + // jint offset, jint len) { @Implementation(minSdk = P) protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int offset, int len) throws IOException { @@ -1629,8 +1716,11 @@ protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int off } int buffer_len = java_buffer.length; - if (offset < 0 || offset >= buffer_len || len < 0 || len > buffer_len || - offset > buffer_len - len) { + if (offset < 0 + || offset >= buffer_len + || len < 0 + || len > buffer_len + || offset > buffer_len - len) { throw new IndexOutOfBoundsException(); } @@ -1649,12 +1739,11 @@ protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int off } // static jlong NativeAssetSeek(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jlong offset, -// jint whence) { + // jint whence) { @Implementation(minSdk = P) protected static long nativeAssetSeek(long asset_ptr, long offset, int whence) { Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); - return asset.seek( - (offset), (whence > 0 ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR))); + return asset.seek((offset), (whence > 0 ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR))); } // static jlong NativeAssetGetLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { @@ -1664,14 +1753,15 @@ protected static long nativeAssetGetLength(long asset_ptr) { return asset.getLength(); } - // static jlong NativeAssetGetRemainingLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) { + // static jlong NativeAssetGetRemainingLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) + // { @Implementation(minSdk = P) protected static long nativeAssetGetRemainingLength(long asset_ptr) { Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr); return asset.getRemainingLength(); } -// ---------------------------------------------------------------------------- + // ---------------------------------------------------------------------------- // JNI registration. // static JNINativeMethod gAssetManagerMethods[] = { @@ -1695,7 +1785,8 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // {"nativeOpenXmlAsset", "(JILjava/lang/String;)J", (void*)NativeOpenXmlAsset}, // // // AssetManager resource methods. - // {"nativeGetResourceValue", "(JISLandroid/util/TypedValue;Z)I", (void*)NativeGetResourceValue}, + // {"nativeGetResourceValue", "(JISLandroid/util/TypedValue;Z)I", + // (void*)NativeGetResourceValue}, // {"nativeGetResourceBagValue", "(JIILandroid/util/TypedValue;)I", // (void*)NativeGetResourceBagValue}, // {"nativeGetStyleAttributes", "(JI)[I", (void*)NativeGetStyleAttributes}, @@ -1710,7 +1801,8 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // {"nativeGetResourceIdentifier", "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", // (void*)NativeGetResourceIdentifier}, // {"nativeGetResourceName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceName}, - // {"nativeGetResourcePackageName", "(JI)Ljava/lang/String;", (void*)NativeGetResourcePackageName}, + // {"nativeGetResourcePackageName", "(JI)Ljava/lang/String;", + // (void*)NativeGetResourcePackageName}, // {"nativeGetResourceTypeName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceTypeName}, // {"nativeGetResourceEntryName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceEntryName}, // {"nativeGetLocales", "(JZ)[Ljava/lang/String;", (void*)NativeGetLocales}, @@ -1731,7 +1823,8 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // {"nativeThemeGetAttributeValue", "(JJILandroid/util/TypedValue;Z)I", // (void*)NativeThemeGetAttributeValue}, // {"nativeThemeDump", "(JJILjava/lang/String;Ljava/lang/String;)V", (void*)NativeThemeDump}, - // {"nativeThemeGetChangingConfigurations", "(J)I", (void*)NativeThemeGetChangingConfigurations}, + // {"nativeThemeGetChangingConfigurations", "(J)I", + // (void*)NativeThemeGetChangingConfigurations}, // // // AssetInputStream methods. // {"nativeAssetDestroy", "(J)V", (void*)NativeAssetDestroy}, @@ -1768,7 +1861,8 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // jclass assetFd = FindClassOrDie(env, "android/content/res/AssetFileDescriptor"); // gAssetFileDescriptorOffsets.mFd = // GetFieldIDOrDie(env, assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;"); - // gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset", "J"); + // gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset", + // "J"); // gAssetFileDescriptorOffsets.mLength = GetFieldIDOrDie(env, assetFd, "mLength", "J"); // // jclass assetManager = FindClassOrDie(env, "android/content/res/AssetManager"); @@ -1786,7 +1880,8 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // // jclass configurationClass = FindClassOrDie(env, "android/content/res/Configuration"); // gConfigurationOffsets.classObject = MakeGlobalRefOrDie(env, configurationClass); - // gConfigurationOffsets.constructor = GetMethodIDOrDie(env, configurationClass, "", "()V"); + // gConfigurationOffsets.constructor = GetMethodIDOrDie(env, configurationClass, "", + // "()V"); // gConfigurationOffsets.mSmallestScreenWidthDpOffset = // GetFieldIDOrDie(env, configurationClass, "smallestScreenWidthDp", "I"); // gConfigurationOffsets.mScreenWidthDpOffset = @@ -1798,4 +1893,5 @@ protected static long nativeAssetGetRemainingLength(long asset_ptr) { // NELEM(gAssetManagerMethods)); // } -}; // namespace android +} +; // namespace android