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
diff --git a/viewcapturelib/Android.bp b/viewcapturelib/Android.bp
index d5700e5..33da2dd 100644
--- a/viewcapturelib/Android.bp
+++ b/viewcapturelib/Android.bp
@@ -20,12 +20,12 @@
     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",
 }
 
diff --git a/viewcapturelib/build.gradle b/viewcapturelib/build.gradle
index 7f56819..3f40ad6 100644
--- a/viewcapturelib/build.gradle
+++ b/viewcapturelib/build.gradle
@@ -35,7 +35,7 @@
 
 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,15 +45,21 @@
     // Configure the protoc executable
     protoc {
         artifact = "com.google.protobuf:protoc:${protobuf_version}${PROTO_ARCH_SUFFIX}"
-        generateProtoTasks {
-            all().each { task ->
-                task.builtins {
-                    remove java
-                    javanano {
-                        option "enum_style=c"
-                    }
-                }
+    }
+    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
+            }
+            task.plugins {
+                javalite { }
             }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/viewcapturelib/src/com/android/app/viewcapture/ViewCapture.java b/viewcapturelib/src/com/android/app/viewcapture/ViewCapture.java
index abc265c..afd2736 100644
--- a/viewcapturelib/src/com/android/app/viewcapture/ViewCapture.java
+++ b/viewcapturelib/src/com/android/app/viewcapture/ViewCapture.java
@@ -40,11 +40,9 @@
 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 @@
                 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 @@
         @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 @@
          * 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;
         }
diff --git a/viewcapturelib/tests/com/android/app/viewcapture/SettingsAwareViewCaptureTest.kt b/viewcapturelib/tests/com/android/app/viewcapture/SettingsAwareViewCaptureTest.kt
index c931eeb..e08b549 100644
--- a/viewcapturelib/tests/com/android/app/viewcapture/SettingsAwareViewCaptureTest.kt
+++ b/viewcapturelib/tests/com/android/app/viewcapture/SettingsAwareViewCaptureTest.kt
@@ -55,7 +55,7 @@
                 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 @@
                 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()
             }
diff --git a/viewcapturelib/tests/com/android/app/viewcapture/ViewCaptureTest.kt b/viewcapturelib/tests/com/android/app/viewcapture/ViewCaptureTest.kt
index 56840ca..b0fcca1 100644
--- a/viewcapturelib/tests/com/android/app/viewcapture/ViewCaptureTest.kt
+++ b/viewcapturelib/tests/com/android/app/viewcapture/ViewCaptureTest.kt
@@ -27,7 +27,7 @@
 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 @@
                 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 @@
                 // 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 @@
     }
 
     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)
             )
         }
     }