Override calls to ActivityThread in Animation

Replacing the call requires recomputing the frames in ASM. That means we
need to pass all the framework classes and dependencies in the classpath
of the layoutlib create tool.

Bug: 74062470
Test: layoutlib create
Change-Id: Id6421446edb5f32ae7ded26ba98d0fd1c78596fd
diff --git a/.idea/runConfigurations/Create.xml b/.idea/runConfigurations/Create.xml
index 8fa90ff..a66f155 100644
--- a/.idea/runConfigurations/Create.xml
+++ b/.idea/runConfigurations/Create.xml
@@ -4,7 +4,7 @@
     <option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
     <option name="MAIN_CLASS_NAME" value="com.android.tools.layoutlib.create.Main" />
     <module name="create" />
-    <option name="PROGRAM_PARAMETERS" value="--create-stub out/soong/.temp/temp_layoutlib.jar out/soong/.intermediates/libcore/core-libart/android_common/withres/core-libart.jar out/soong/.intermediates/frameworks/base/framework/android_common/jarjar/framework.jar out/soong/.intermediates/frameworks/base/ext/android_common/withres/ext.jar out/soong/.intermediates/external/icu/icu4j/icu4j-icudata-jarjar/linux_glibc_common/jarjar/icu4j-icudata-jarjar.jar out/soong/.intermediates/external/icu/icu4j/icu4j-icutzdata-jarjar/linux_glibc_common/jarjar/icu4j-icutzdata-jarjar.jar" />
+    <option name="PROGRAM_PARAMETERS" value="--create-stub out/soong/.temp/temp_layoutlib.jar out/soong/.intermediates/prebuilts/misc/common/atf/atf-prebuilt-jars-371374941/linux_glibc_common/combined/atf-prebuilt-jars-371374941.jar out/soong/.intermediates/external/icu/android_icu4j/core-icu4j-for-host/android_common/withres/core-icu4j-for-host.jar out/soong/.intermediates/libcore/core-libart/android_common/javac/core-libart.jar out/soong/.intermediates/frameworks/base/framework-all/android_common/combined/framework-all.jar out/soong/.intermediates/frameworks/base/ext/android_common/withres/ext.jar out/soong/.intermediates/external/icu/icu4j/icu4j-icudata-jarjar/linux_glibc_common/jarjar/icu4j-icudata-jarjar.jar out/soong/.intermediates/external/icu/icu4j/icu4j-icutzdata-jarjar/linux_glibc_common/jarjar/icu4j-icutzdata-jarjar.jar" />
     <option name="VM_PARAMETERS" value="-ea" />
     <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../.." />
     <RunnerSettings RunnerId="Debug">
diff --git a/Android.bp b/Android.bp
index bb68eae..05037d3 100644
--- a/Android.bp
+++ b/Android.bp
@@ -73,3 +73,16 @@
     ],
     cmd: "rm -f $(out) && $(location layoutlib_create) --create-native-only-delegates $(out) $(in)",
 }
+
+java_device_for_host {
+    name: "layoutlib_create-classpath",
+    libs: [
+        "conscrypt-for-host",
+        "core-icu4j-for-host",
+        "core-libart-for-host",
+        "ext",
+        "framework-all",
+        "icu4j-icudata-jarjar",
+        "icu4j-icutzdata-jarjar",
+    ],
+}
diff --git a/bridge/src/android/app/ActivityThread_Delegate.java b/bridge/src/android/app/ActivityThread_Delegate.java
new file mode 100644
index 0000000..da81134
--- /dev/null
+++ b/bridge/src/android/app/ActivityThread_Delegate.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.app;
+
+import com.android.layoutlib.bridge.impl.RenderAction;
+
+import android.content.Context;
+
+public class ActivityThread_Delegate {
+    public static Context getSystemUiContext() {
+        return RenderAction.getCurrentContext();
+    }
+}
diff --git a/create/Android.bp b/create/Android.bp
index 0fc219d..bbbbe5e 100644
--- a/create/Android.bp
+++ b/create/Android.bp
@@ -35,5 +35,7 @@
         "asm-commons-6.0",
         "guava",
         "layoutlib-common",
+        "layoutlib_create-classpath",
+        "atf-prebuilt-371374941",
     ],
 }
diff --git a/create/src/com/android/tools/layoutlib/create/AsmAnalyzer.java b/create/src/com/android/tools/layoutlib/create/AsmAnalyzer.java
index 6a555cb..e5d1088 100644
--- a/create/src/com/android/tools/layoutlib/create/AsmAnalyzer.java
+++ b/create/src/com/android/tools/layoutlib/create/AsmAnalyzer.java
@@ -488,7 +488,7 @@
             try {
                 // exclude classes that are part of the default JRE (the one executing this program)
                 if (className.startsWith("java.") || className.startsWith("sun.") ||
-                        getClass().getClassLoader().loadClass(className) != null) {
+                        getClass().getClassLoader().getParent().loadClass(className) != null) {
                     return;
                 }
             } catch (ClassNotFoundException e) {
diff --git a/create/src/com/android/tools/layoutlib/create/AsmGenerator.java b/create/src/com/android/tools/layoutlib/create/AsmGenerator.java
index 500f207..98055e3 100644
--- a/create/src/com/android/tools/layoutlib/create/AsmGenerator.java
+++ b/create/src/com/android/tools/layoutlib/create/AsmGenerator.java
@@ -367,7 +367,7 @@
 
         // Rewrite the new class from scratch, without reusing the constant pool from the
         // original class reader.
-        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
+        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
 
         ClassVisitor cv = cw;
 
diff --git a/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index 34d5c62..eff7b67 100644
--- a/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -147,6 +147,7 @@
         new ProcessInitializerInitSchedReplacer(),
         new ValidateNinePatchChunkReplacer(),
         new NativeInitPathReplacer(),
+        new ActivityThreadInAnimationReplacer(),
     };
 
     /**
@@ -753,4 +754,20 @@
             mi.opcode = Opcodes.INVOKESTATIC;
         }
     }
+
+    public static class ActivityThreadInAnimationReplacer implements MethodReplacer {
+        @Override
+        public boolean isNeeded(String owner, String name, String desc, String sourceClass) {
+            return ("android/app/ActivityThread").equals(owner) &&
+                    name.equals("getSystemUiContext") &&
+                    sourceClass.equals("android/view/animation/Animation");
+        }
+
+        @Override
+        public void replace(MethodInformation mi) {
+            mi.owner = "android/app/ActivityThread_Delegate";
+            mi.opcode = Opcodes.INVOKESTATIC;
+            mi.desc = "()Landroid/content/Context;";
+        }
+    }
 }