Ensure native allocation/free is correctly registered

This CL fixes an issue where one of the constructors creates a
native tree object without registering the allocation of its
root group with the NativeAllocationRegistry.

BUG: 28943866
Change-Id: Ic8db3f2fa8036dfabdbc1a1fcbd58d08a0379e2d
diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java
index 4f6368c..dc1d18f 100644
--- a/graphics/java/android/graphics/drawable/VectorDrawable.java
+++ b/graphics/java/android/graphics/drawable/VectorDrawable.java
@@ -235,7 +235,7 @@
     private final Rect mTmpBounds = new Rect();
 
     public VectorDrawable() {
-        this(new VectorDrawableState(), null);
+        this(new VectorDrawableState(null), null);
     }
 
     /**
@@ -830,7 +830,8 @@
 
         private static final int NATIVE_ALLOCATION_SIZE = 316;
 
-        // Deep copy for mutate() or implicitly mutate.
+        // If copy is not null, deep copy the given VectorDrawableState. Otherwise, create a
+        // native vector drawable tree with an empty root group.
         public VectorDrawableState(VectorDrawableState copy) {
             if (copy != null) {
                 mThemeAttrs = copy.mThemeAttrs;
@@ -851,8 +852,11 @@
                 if (copy.mRootName != null) {
                     mVGTargetsMap.put(copy.mRootName, this);
                 }
-                onTreeConstructionFinished();
+            } else {
+                mRootGroup = new VGroup();
+                createNativeTree(mRootGroup);
             }
+            onTreeConstructionFinished();
         }
 
         private void createNativeTree(VGroup rootGroup) {
@@ -870,7 +874,8 @@
             VMRuntime.getRuntime().registerNativeAllocation(NATIVE_ALLOCATION_SIZE);
         }
 
-
+        // This should be called every time after a new RootGroup and all its subtrees are created
+        // (i.e. in constructors of VectorDrawableState and in inflate).
         void onTreeConstructionFinished() {
             mRootGroup.setTree(mNativeTree);
             mAllocationOfAllNodes = mRootGroup.getNativeSize();
@@ -918,11 +923,6 @@
                     || super.canApplyTheme();
         }
 
-        public VectorDrawableState() {
-            mRootGroup = new VGroup();
-            createNativeTree(mRootGroup);
-        }
-
         @Override
         public Drawable newDrawable() {
             return new VectorDrawable(this, null);