1 /* 2 * Copyright (C) 2017 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 17 package android.util.proto; 18 19 import android.util.AggStats; 20 import android.util.Duration; 21 22 /** 23 * This class contains a list of helper functions to write common proto in 24 * //frameworks/base/core/proto/android/base directory 25 */ 26 public class ProtoUtils { 27 28 /** 29 * Dump AggStats to ProtoOutputStream 30 * @hide 31 */ toAggStatsProto(ProtoOutputStream proto, long fieldId, long min, long average, long max)32 public static void toAggStatsProto(ProtoOutputStream proto, long fieldId, 33 long min, long average, long max) { 34 final long aggStatsToken = proto.start(fieldId); 35 proto.write(AggStats.MIN, min); 36 proto.write(AggStats.AVERAGE, average); 37 proto.write(AggStats.MAX, max); 38 proto.end(aggStatsToken); 39 } 40 41 /** 42 * Dump Duration to ProtoOutputStream 43 * @hide 44 */ toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs)45 public static void toDuration(ProtoOutputStream proto, long fieldId, long startMs, long endMs) { 46 final long token = proto.start(fieldId); 47 proto.write(Duration.START_MS, startMs); 48 proto.write(Duration.END_MS, endMs); 49 proto.end(token); 50 } 51 52 /** 53 * Helper function to write bit-wise flags to proto as repeated enums 54 * @hide 55 */ writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId, int flags, int[] origEnums, int[] protoEnums)56 public static void writeBitWiseFlagsToProtoEnum(ProtoOutputStream proto, long fieldId, 57 int flags, int[] origEnums, int[] protoEnums) { 58 if (protoEnums.length != origEnums.length) { 59 throw new IllegalArgumentException("The length of origEnums must match protoEnums"); 60 } 61 int len = origEnums.length; 62 for (int i = 0; i < len; i++) { 63 // handle zero flag case. 64 if (origEnums[i] == 0 && flags == 0) { 65 proto.write(fieldId, protoEnums[i]); 66 return; 67 } 68 if ((flags & origEnums[i]) != 0) { 69 proto.write(fieldId, protoEnums[i]); 70 } 71 } 72 } 73 } 74