Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02b0db19 authored by Stefan Andonian's avatar Stefan Andonian
Browse files

Migrating ViewCapture tool from Protobuf-nano to Protobuf-lite.

Bug: 224595733
Test: Verified that functionality still works on device by printing
ViewCapture data via command line.

Change-Id: I55d6b357cbd7749994cd5d02e66e1c3198abe2f1
parent 0e9853b1
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -20,12 +20,12 @@ java_library {
    name: "view_capture_proto",
    srcs: ["src/com/android/app/viewcapture/proto/*.proto"],
    proto: {
        type: "nano",
        type: "lite",
        local_include_dirs:[
            "src/com/android/app/viewcapture/proto"
        ],
    },
    static_libs: ["libprotobuf-java-nano"],
    static_libs: ["libprotobuf-java-lite"],
    java_version: "1.8",
}

+16 −10
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ android {

dependencies {
    implementation "androidx.core:core:1.9.0"
    implementation PROTOBUF_DEPENDENCY
    implementation "com.google.protobuf:protobuf-lite:${protobuf_version}"
    androidTestImplementation project(':SharedTestLib')
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation "androidx.test:rules:1.4.0"
@@ -45,14 +45,20 @@ protobuf {
    // Configure the protoc executable
    protoc {
        artifact = "com.google.protobuf:protoc:${protobuf_version}${PROTO_ARCH_SUFFIX}"
    }
    plugins {
        javalite {
            // The codegen for lite comes as a separate artifact
            artifact = "com.google.protobuf:protoc-gen-javalite:${protobuf_version}${PROTO_ARCH_SUFFIX}"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
                    javanano {
                        option "enum_style=c"
                    }
            }
            task.plugins {
                javalite { }
            }
        }
    }
+34 −36
Original line number Diff line number Diff line
@@ -40,11 +40,9 @@ import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;

import com.android.app.viewcapture.data.nano.ExportedData;
import com.android.app.viewcapture.data.nano.FrameData;
import com.android.app.viewcapture.data.nano.ViewNode;

import com.google.protobuf.nano.MessageNano;
import com.android.app.viewcapture.data.ExportedData;
import com.android.app.viewcapture.data.FrameData;
import com.android.app.viewcapture.data.ViewNode;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
@@ -178,7 +176,7 @@ public abstract class ViewCapture {
                ExportedData data = pair.second.get();
                OutputStream encodedOS = new GZIPOutputStream(new Base64OutputStream(os,
                        Base64.NO_CLOSE | Base64.NO_PADDING | Base64.NO_WRAP));
                encodedOS.write(MessageNano.toByteArray(data));
                data.writeTo(encodedOS);
                encodedOS.close();
                os.flush();
            } catch (Exception e) {
@@ -401,21 +399,21 @@ public abstract class ViewCapture {
        @WorkerThread
        private ExportedData dumpToProto(ViewIdProvider idProvider) {
            int size = (mNodesBg[mMemorySize - 1] == null) ? mFrameIndexBg + 1 : mMemorySize;
            ExportedData exportedData = new ExportedData();
            exportedData.frameData = new FrameData[size];
            ExportedData.Builder exportedDataBuilder = ExportedData.newBuilder();
            ArrayList<Class> classList = new ArrayList<>();

            for (int i = size - 1; i >= 0; i--) {
                int index = (mMemorySize + mFrameIndexBg - i) % mMemorySize;
                ViewNode node = new ViewNode();
                mNodesBg[index].toProto(idProvider, classList, node);
                FrameData frameData = new FrameData();
                frameData.node = node;
                frameData.timestamp = mFrameTimesNanosBg[index];
                exportedData.frameData[size - i - 1] = frameData;
                ViewNode.Builder nodeBuilder = ViewNode.newBuilder();
                mNodesBg[index].toProto(idProvider, classList, nodeBuilder);
                FrameData.Builder frameDataBuilder = FrameData.newBuilder()
                        .setNode(nodeBuilder)
                        .setTimestamp(mFrameTimesNanosBg[index]);
                exportedDataBuilder.addFrameData(frameDataBuilder);
            }
            exportedData.classname = classList.stream().map(Class::getName).toArray(String[]::new);
            return exportedData;
            return exportedDataBuilder
                    .addAllClassname(classList.stream().map(Class::getName).collect(toList()))
                    .build();
        }

        private ViewRef captureViewTree(View view, ViewRef start) {
@@ -500,35 +498,35 @@ public abstract class ViewCapture {
         * at the end of the iteration.
         */
        public ViewPropertyRef toProto(ViewIdProvider idProvider, ArrayList<Class> classList,
                ViewNode viewNode) {
                ViewNode.Builder viewNode) {
            int classnameIndex = classList.indexOf(clazz);
            if (classnameIndex < 0) {
                classnameIndex = classList.size();
                classList.add(clazz);
            }
            viewNode.classnameIndex = classnameIndex;
            viewNode.hashcode = hashCode;
            viewNode.id = idProvider.getName(id);
            viewNode.left = left;
            viewNode.top = top;
            viewNode.width = right - left;
            viewNode.height = bottom - top;
            viewNode.translationX = translateX;
            viewNode.translationY = translateY;
            viewNode.scaleX = scaleX;
            viewNode.scaleY = scaleY;
            viewNode.alpha = alpha;
            viewNode.visibility = visibility;
            viewNode.willNotDraw = willNotDraw;
            viewNode.elevation = elevation;
            viewNode.clipChildren = clipChildren;

            viewNode.setClassnameIndex(classnameIndex)
                    .setHashcode(hashCode)
                    .setId(idProvider.getName(id))
                    .setLeft(left)
                    .setTop(top)
                    .setWidth(right - left)
                    .setHeight(bottom - top)
                    .setTranslationX(translateX)
                    .setTranslationY(translateY)
                    .setScaleX(scaleX)
                    .setScaleY(scaleY)
                    .setAlpha(alpha)
                    .setVisibility(visibility)
                    .setWillNotDraw(willNotDraw)
                    .setElevation(elevation)
                    .setClipChildren(clipChildren);

            ViewPropertyRef result = next;
            viewNode.children = new ViewNode[childCount];
            for (int i = 0; (i < childCount) && (result != null); i++) {
                ViewNode childViewNode = new ViewNode();
                ViewNode.Builder childViewNode = ViewNode.newBuilder();
                result = result.toProto(idProvider, classList, childViewNode);
                viewNode.children[i] = childViewNode;
                viewNode.addChildren(childViewNode);
            }
            return result;
        }
+2 −2
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ class SettingsAwareViewCaptureTest {
                rootView.viewTreeObserver.dispatchOnDraw()

                assertEquals(0, viewCapture.getDumpTask(
                        activity.findViewById(android.R.id.content)).get().get().frameData.size)
                        activity.findViewById(android.R.id.content)).get().get().frameDataList.size)
                closeable.close()
            }
        }
@@ -75,7 +75,7 @@ class SettingsAwareViewCaptureTest {
                rootView.viewTreeObserver.dispatchOnDraw()

                assertEquals(1, viewCapture.getDumpTask(activity.findViewById(
                        android.R.id.content)).get().get().frameData.size)
                        android.R.id.content)).get().get().frameDataList.size)

                closeable.close()
            }
+8 −8
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import com.android.app.viewcapture.TestActivity.Companion.TEXT_VIEW_COUNT
import com.android.app.viewcapture.data.nano.ExportedData
import com.android.app.viewcapture.data.ExportedData
import junit.framework.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
@@ -57,7 +57,7 @@ class ViewCaptureTest {
                val rootView = activity.findViewById<View>(android.R.id.content)
                val exportedData = viewCapture.getDumpTask(rootView).get().get()

                assertEquals(1, exportedData.frameData.size)
                assertEquals(1, exportedData.frameDataList.size)
                verifyTestActivityViewHierarchy(exportedData)
                closeable.close()
            }
@@ -75,7 +75,7 @@ class ViewCaptureTest {
                // since ViewCapture MEMORY_SIZE is [viewCaptureMemorySize], only
                // [viewCaptureMemorySize] frames are exported, although the view is invalidated
                // [viewCaptureMemorySize + 5] times
                assertEquals(memorySize, exportedData.frameData.size)
                assertEquals(memorySize, exportedData.frameDataList.size)
                verifyTestActivityViewHierarchy(exportedData)
                closeable.close()
            }
@@ -97,19 +97,19 @@ class ViewCaptureTest {
    }

    private fun verifyTestActivityViewHierarchy(exportedData: ExportedData) {
        for (frame in exportedData.frameData) {
        for (frame in exportedData.frameDataList) {
            val testActivityRoot =
                frame.node // FrameLayout (android.R.id.content)
                    .children
                    .childrenList
                    .first() // LinearLayout (set by setContentView())
            assertEquals(TEXT_VIEW_COUNT, testActivityRoot.children.size)
            assertEquals(TEXT_VIEW_COUNT, testActivityRoot.childrenList.size)
            assertEquals(
                LinearLayout::class.qualifiedName,
                exportedData.classname[testActivityRoot.classnameIndex]
                exportedData.getClassname(testActivityRoot.classnameIndex)
            )
            assertEquals(
                TextView::class.qualifiedName,
                exportedData.classname[testActivityRoot.children.first().classnameIndex]
                exportedData.getClassname(testActivityRoot.childrenList.first().classnameIndex)
            )
        }
    }