Dump usb as DualDump

This allows to dump the USB state as proto-buf. This in turn allows to
automatically process this data.

Test: adb shell dumpsys usb
      incident_report usb
      No automated test possible as no field is guaranteed to be set
Change-Id: Ifdf22bfaf9c78226c420b11c43278013ce69f849
diff --git a/core/java/com/android/internal/util/dump/DumpUtils.java b/core/java/com/android/internal/util/dump/DumpUtils.java
new file mode 100644
index 0000000..0ee204e
--- /dev/null
+++ b/core/java/com/android/internal/util/dump/DumpUtils.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 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 com.android.internal.util.dump;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.ComponentName;
+import android.content.ComponentNameProto;
+
+public class DumpUtils {
+    /**
+     * Write a string to a proto if the string is not {@code null}.
+     *
+     * @param proto The proto to write to
+     * @param idName Clear text name of the proto-id
+     * @param id The proto-id of the string
+     * @param string The string to write
+     */
+    public static void writeStringIfNotNull(@NonNull DualDumpOutputStream proto, String idName,
+            long id, @Nullable String string) {
+        if (string != null) {
+            proto.write(idName, id, string);
+        }
+    }
+
+    /**
+     * Write a {@link ComponentName} to a proto.
+     *
+     * @param proto The proto to write to
+     * @param idName Clear text name of the proto-id
+     * @param id The proto-id of the component name
+     * @param component The component name to write
+     */
+    public static void writeComponentName(@NonNull DualDumpOutputStream proto, String idName,
+            long id, @NonNull ComponentName component) {
+        long token = proto.start(idName, id);
+        proto.write("package_name", ComponentNameProto.PACKAGE_NAME, component.getPackageName());
+        proto.write("class_name", ComponentNameProto.CLASS_NAME, component.getClassName());
+        proto.end(token);
+    }
+}