1/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.util.dump;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.ComponentName;
22import android.content.ComponentNameProto;
23
24public class DumpUtils {
25    /**
26     * Write a string to a proto if the string is not {@code null}.
27     *
28     * @param proto The proto to write to
29     * @param idName Clear text name of the proto-id
30     * @param id The proto-id of the string
31     * @param string The string to write
32     */
33    public static void writeStringIfNotNull(@NonNull DualDumpOutputStream proto, String idName,
34            long id, @Nullable String string) {
35        if (string != null) {
36            proto.write(idName, id, string);
37        }
38    }
39
40    /**
41     * Write a {@link ComponentName} to a proto.
42     *
43     * @param proto The proto to write to
44     * @param idName Clear text name of the proto-id
45     * @param id The proto-id of the component name
46     * @param component The component name to write
47     */
48    public static void writeComponentName(@NonNull DualDumpOutputStream proto, String idName,
49            long id, @NonNull ComponentName component) {
50        long token = proto.start(idName, id);
51        proto.write("package_name", ComponentNameProto.PACKAGE_NAME, component.getPackageName());
52        proto.write("class_name", ComponentNameProto.CLASS_NAME, component.getClassName());
53        proto.end(token);
54    }
55}
56