Merge "Refactor ddm ChunkHandler utility methods from libcore to framework"
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 65f2c02..71cc11b 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -6381,7 +6381,7 @@
             VMDebug.setAllocTrackerStackDepth(Integer.parseInt(property));
         }
         if (data.trackAllocation) {
-            DdmVmInternal.enableRecentAllocations(true);
+            DdmVmInternal.setRecentAllocationsTrackingEnabled(true);
         }
         // Note when this process has started.
         Process.setStartTimes(SystemClock.elapsedRealtime(), SystemClock.uptimeMillis());
diff --git a/core/java/android/ddm/DdmHandle.java b/core/java/android/ddm/DdmHandle.java
new file mode 100644
index 0000000..0505fee
--- /dev/null
+++ b/core/java/android/ddm/DdmHandle.java
@@ -0,0 +1,51 @@
+/*
+ * 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.ddm;
+
+import org.apache.harmony.dalvik.ddmc.ChunkHandler;
+
+import java.nio.ByteBuffer;
+
+/**
+ * Contains utility methods for chunk serialization and deserialization.
+ */
+public abstract class DdmHandle extends ChunkHandler {
+
+    /**
+     * Utility function to copy a String out of a ByteBuffer.
+     *
+     * This is here because multiple chunk handlers can make use of it,
+     * and there's nowhere better to put it.
+     */
+    public static String getString(ByteBuffer buf, int len) {
+        char[] data = new char[len];
+        for (int i = 0; i < len; i++) {
+            data[i] = buf.getChar();
+        }
+        return new String(data);
+    }
+
+    /**
+     * Utility function to copy a String into a ByteBuffer.
+     */
+    public static void putString(ByteBuffer buf, String str) {
+        int len = str.length();
+        for (int i = 0; i < len; i++) {
+            buf.putChar(str.charAt(i));
+        }
+    }
+
+}
diff --git a/core/java/android/ddm/DdmHandleAppName.java b/core/java/android/ddm/DdmHandleAppName.java
index 35da062..e19f19f 100644
--- a/core/java/android/ddm/DdmHandleAppName.java
+++ b/core/java/android/ddm/DdmHandleAppName.java
@@ -30,9 +30,9 @@
 /**
  * Track our app name.  We don't (currently) handle any inbound packets.
  */
-public class DdmHandleAppName extends ChunkHandler {
+public class DdmHandleAppName extends DdmHandle {
 
-    public static final int CHUNK_APNM = type("APNM");
+    public static final int CHUNK_APNM = ChunkHandler.type("APNM");
 
     private static volatile Names sNames = new Names("", "");
 
@@ -51,13 +51,13 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {}
+    public void onConnected() {}
 
     /**
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {}
+    public void onDisconnected() {}
 
     /**
      * Handle a chunk of data.
diff --git a/core/java/android/ddm/DdmHandleExit.java b/core/java/android/ddm/DdmHandleExit.java
index 74ae37a..a9e3d16 100644
--- a/core/java/android/ddm/DdmHandleExit.java
+++ b/core/java/android/ddm/DdmHandleExit.java
@@ -16,18 +16,20 @@
 
 package android.ddm;
 
+import android.util.Log;
+
 import org.apache.harmony.dalvik.ddmc.Chunk;
 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
 import org.apache.harmony.dalvik.ddmc.DdmServer;
-import android.util.Log;
+
 import java.nio.ByteBuffer;
 
 /**
  * Handle an EXIT chunk.
  */
-public class DdmHandleExit extends ChunkHandler {
+public class DdmHandleExit extends DdmHandle {
 
-    public static final int CHUNK_EXIT = type("EXIT");
+    public static final int CHUNK_EXIT = ChunkHandler.type("EXIT");
 
     private static DdmHandleExit mInstance = new DdmHandleExit();
 
@@ -46,13 +48,13 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {}
+    public void onConnected() {}
 
     /**
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {}
+    public void onDisconnected() {}
 
     /**
      * Handle a chunk of data.  We're only registered for "EXIT".
diff --git a/core/java/android/ddm/DdmHandleHeap.java b/core/java/android/ddm/DdmHandleHeap.java
index 8fa2352..2edb5c7 100644
--- a/core/java/android/ddm/DdmHandleHeap.java
+++ b/core/java/android/ddm/DdmHandleHeap.java
@@ -16,21 +16,18 @@
 
 package android.ddm;
 
+import android.util.Log;
+
 import org.apache.harmony.dalvik.ddmc.Chunk;
 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
 import org.apache.harmony.dalvik.ddmc.DdmServer;
-import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
-import android.os.Debug;
-import android.util.Log;
-import java.io.IOException;
-import java.nio.ByteBuffer;
 
 /**
  * Handle native and virtual heap requests.
  */
-public class DdmHandleHeap extends ChunkHandler {
+public class DdmHandleHeap extends DdmHandle {
 
-    public static final int CHUNK_HPGC = type("HPGC");
+    public static final int CHUNK_HPGC = ChunkHandler.type("HPGC");
 
     private static DdmHandleHeap mInstance = new DdmHandleHeap();
 
@@ -49,13 +46,13 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {}
+    public void onConnected() {}
 
     /**
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {}
+    public void onDisconnected() {}
 
     /**
      * Handle a chunk of data.
@@ -68,8 +65,7 @@
         if (type == CHUNK_HPGC) {
             return handleHPGC(request);
         } else {
-            throw new RuntimeException("Unknown packet "
-                + ChunkHandler.name(type));
+            throw new RuntimeException("Unknown packet " + name(type));
         }
     }
 
diff --git a/core/java/android/ddm/DdmHandleHello.java b/core/java/android/ddm/DdmHandleHello.java
index 60dfc8d..4160029 100644
--- a/core/java/android/ddm/DdmHandleHello.java
+++ b/core/java/android/ddm/DdmHandleHello.java
@@ -31,11 +31,11 @@
 /**
  * Handle "hello" messages and feature discovery.
  */
-public class DdmHandleHello extends ChunkHandler {
+public class DdmHandleHello extends DdmHandle {
 
-    public static final int CHUNK_HELO = type("HELO");
-    public static final int CHUNK_WAIT = type("WAIT");
-    public static final int CHUNK_FEAT = type("FEAT");
+    public static final int CHUNK_HELO = ChunkHandler.type("HELO");
+    public static final int CHUNK_WAIT = ChunkHandler.type("WAIT");
+    public static final int CHUNK_FEAT = ChunkHandler.type("FEAT");
 
     private static final int CLIENT_PROTOCOL_VERSION = 1;
 
@@ -61,15 +61,14 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {
+    public void onConnected() {
         if (false)
             Log.v("ddm-hello", "Connected!");
 
         if (false) {
             /* test spontaneous transmission */
             byte[] data = new byte[] { 0, 1, 2, 3, 4, -4, -3, -2, -1, 127 };
-            Chunk testChunk =
-                new Chunk(ChunkHandler.type("TEST"), data, 1, data.length-2);
+            Chunk testChunk = new Chunk(ChunkHandler.type("TEST"), data, 1, data.length - 2);
             DdmServer.sendChunk(testChunk);
         }
     }
@@ -78,7 +77,7 @@
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {
+    public void onDisconnected() {
         if (false)
             Log.v("ddm-hello", "Disconnected!");
     }
@@ -96,8 +95,7 @@
         } else if (type == CHUNK_FEAT) {
             return handleFEAT(request);
         } else {
-            throw new RuntimeException("Unknown packet "
-                + ChunkHandler.name(type));
+            throw new RuntimeException("Unknown packet " + name(type));
         }
     }
 
diff --git a/core/java/android/ddm/DdmHandleNativeHeap.java b/core/java/android/ddm/DdmHandleNativeHeap.java
index 775c570..dfd451c 100644
--- a/core/java/android/ddm/DdmHandleNativeHeap.java
+++ b/core/java/android/ddm/DdmHandleNativeHeap.java
@@ -16,17 +16,18 @@
 
 package android.ddm;
 
+import android.util.Log;
+
 import org.apache.harmony.dalvik.ddmc.Chunk;
 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
 import org.apache.harmony.dalvik.ddmc.DdmServer;
-import android.util.Log;
 
 /**
  * Handle thread-related traffic.
  */
-public class DdmHandleNativeHeap extends ChunkHandler {
+public class DdmHandleNativeHeap extends DdmHandle {
 
-    public static final int CHUNK_NHGT = type("NHGT");
+    public static final int CHUNK_NHGT = ChunkHandler.type("NHGT");
 
     private static DdmHandleNativeHeap mInstance = new DdmHandleNativeHeap();
 
@@ -45,13 +46,13 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {}
+    public void onConnected() {}
 
     /**
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {}
+    public void onDisconnected() {}
 
     /**
      * Handle a chunk of data.
@@ -63,8 +64,7 @@
         if (type == CHUNK_NHGT) {
             return handleNHGT(request);
         } else {
-            throw new RuntimeException("Unknown packet "
-                + ChunkHandler.name(type));
+            throw new RuntimeException("Unknown packet " + name(type));
         }
     }
 
diff --git a/core/java/android/ddm/DdmHandleProfiling.java b/core/java/android/ddm/DdmHandleProfiling.java
index cce4dd2..806e4bd 100644
--- a/core/java/android/ddm/DdmHandleProfiling.java
+++ b/core/java/android/ddm/DdmHandleProfiling.java
@@ -16,25 +16,28 @@
 
 package android.ddm;
 
+
+import android.os.Debug;
+import android.util.Log;
+
 import org.apache.harmony.dalvik.ddmc.Chunk;
 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
 import org.apache.harmony.dalvik.ddmc.DdmServer;
-import android.os.Debug;
-import android.util.Log;
+
 import java.nio.ByteBuffer;
 
 /**
  * Handle profiling requests.
  */
-public class DdmHandleProfiling extends ChunkHandler {
+public class DdmHandleProfiling extends DdmHandle {
 
-    public static final int CHUNK_MPRS = type("MPRS");
-    public static final int CHUNK_MPRE = type("MPRE");
-    public static final int CHUNK_MPSS = type("MPSS");
-    public static final int CHUNK_MPSE = type("MPSE");
-    public static final int CHUNK_MPRQ = type("MPRQ");
-    public static final int CHUNK_SPSS = type("SPSS");
-    public static final int CHUNK_SPSE = type("SPSE");
+    public static final int CHUNK_MPRS = ChunkHandler.type("MPRS");
+    public static final int CHUNK_MPRE = ChunkHandler.type("MPRE");
+    public static final int CHUNK_MPSS = ChunkHandler.type("MPSS");
+    public static final int CHUNK_MPSE = ChunkHandler.type("MPSE");
+    public static final int CHUNK_MPRQ = ChunkHandler.type("MPRQ");
+    public static final int CHUNK_SPSS = ChunkHandler.type("SPSS");
+    public static final int CHUNK_SPSE = ChunkHandler.type("SPSE");
 
     private static final boolean DEBUG = false;
     private static DdmHandleProfiling mInstance = new DdmHandleProfiling();
@@ -60,13 +63,13 @@
      * Called when the DDM server connects.  The handler is allowed to
      * send messages to the server.
      */
-    public void connected() {}
+    public void onConnected() {}
 
     /**
      * Called when the DDM server disconnects.  Can be used to disable
      * periodic transmissions or clean up saved state.
      */
-    public void disconnected() {}
+    public void onDisconnected() {}
 
     /**
      * Handle a chunk of data.
@@ -91,8 +94,7 @@
         } else if (type == CHUNK_SPSE) {
             return handleMPSEOrSPSE(request, "Sample");
         } else {
-            throw new RuntimeException("Unknown packet "
-                + ChunkHandler.name(type));
+            throw new RuntimeException("Unknown packet " + name(type));
         }
     }
 
diff --git a/core/java/android/ddm/DdmHandleViewDebug.java b/core/java/android/ddm/DdmHandleViewDebug.java
index 5539dc9..6b0f78f 100644
--- a/core/java/android/ddm/DdmHandleViewDebug.java
+++ b/core/java/android/ddm/DdmHandleViewDebug.java
@@ -39,12 +39,12 @@
  * Handle various requests related to profiling / debugging of the view system.
  * Support for these features are advertised via {@link DdmHandleHello}.
  */
-public class DdmHandleViewDebug extends ChunkHandler {
+public class DdmHandleViewDebug extends DdmHandle {
     /** List {@link ViewRootImpl}'s of this process. */
-    private static final int CHUNK_VULW = type("VULW");
+    private static final int CHUNK_VULW = ChunkHandler.type("VULW");
 
     /** Operation on view root, first parameter in packet should be one of VURT_* constants */
-    private static final int CHUNK_VURT = type("VURT");
+    private static final int CHUNK_VURT = ChunkHandler.type("VURT");
 
     /** Dump view hierarchy. */
     private static final int VURT_DUMP_HIERARCHY = 1;
@@ -59,7 +59,7 @@
      * Generic View Operation, first parameter in the packet should be one of the
      * VUOP_* constants below.
      */
-    private static final int CHUNK_VUOP = type("VUOP");
+    private static final int CHUNK_VUOP = ChunkHandler.type("VUOP");
 
     /** Capture View. */
     private static final int VUOP_CAPTURE_VIEW = 1;
@@ -99,11 +99,11 @@
     }
 
     @Override
-    public void connected() {
+    public void onConnected() {
     }
 
     @Override
-    public void disconnected() {
+    public void onDisconnected() {
     }
 
     @Override
@@ -154,7 +154,7 @@
                     return createFailChunk(ERR_INVALID_OP, "Unknown view operation: " + op);
             }
         } else {
-            throw new RuntimeException("Unknown packet " + ChunkHandler.name(type));
+            throw new RuntimeException("Unknown packet " + name(type));
         }
     }