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 package android.device.collectors; 17 18 import android.os.Bundle; 19 import androidx.annotation.VisibleForTesting; 20 21 import java.io.File; 22 import java.util.LinkedHashMap; 23 import java.util.Map; 24 25 /** 26 * Object to hold all the data collected by metric collectors. 27 */ 28 public class DataRecord { 29 // TODO: expend type supports to more complex type: Object,etc. 30 private LinkedHashMap<String, String> mCurrentStringMetrics = new LinkedHashMap<>(); 31 private LinkedHashMap<String, File> mCurrentFileMetrics = new LinkedHashMap<>(); 32 private LinkedHashMap<String, byte[]> mCurrentBinaryMetrics = new LinkedHashMap<>(); 33 34 /** 35 * Add a metric to be tracked by a key. 36 * 37 * @param key the key under which to find the metric 38 * @param value the value associated with the key 39 */ addStringMetric(String key, String value)40 public void addStringMetric(String key, String value) { 41 mCurrentStringMetrics.put(key, value); 42 } 43 44 /** 45 * Add a metric file to be tracked under a key. It will be reported in the instrumentation 46 * results as the key and absolute path to the file. 47 * 48 * @param fileKey the key under which the file will be found. 49 * @param value the {@link File} associated to the key. 50 */ addFileMetric(String fileKey, File value)51 public void addFileMetric(String fileKey, File value) { 52 mCurrentFileMetrics.put(fileKey, value); 53 } 54 55 /** 56 * Add a byte[] metric to be tracked by a key. 57 * 58 * @param key the key under which to find the metric 59 * @param value the byte[] value associated with the key 60 */ addBinaryMetric(String key, byte[] value)61 public void addBinaryMetric(String key, byte[] value) { 62 mCurrentBinaryMetrics.put(key, value); 63 } 64 65 /** 66 * Returns True if the {@link DataRecord} already contains some metrics, False otherwise. 67 */ hasMetrics()68 public boolean hasMetrics() { 69 return (mCurrentStringMetrics.size() + mCurrentFileMetrics.size() 70 + mCurrentBinaryMetrics.size()) > 0; 71 } 72 73 /** 74 * Returns all the string and file data received so far to the map of metrics that will be 75 * reported. 76 */ getStringMetrics()77 private Map<String, String> getStringMetrics() { 78 Map<String, String> res = new LinkedHashMap<>(); 79 for (Map.Entry<String, File> entry : mCurrentFileMetrics.entrySet()) { 80 res.put(entry.getKey(), entry.getValue().getAbsolutePath()); 81 } 82 res.putAll(mCurrentStringMetrics); 83 return res; 84 } 85 86 /** 87 * Create a {@link Bundle} and populate it with the metrics, or return null if no metrics are 88 * available. 89 */ createBundleFromMetrics()90 final Bundle createBundleFromMetrics() { 91 Map<String, String> map = getStringMetrics(); 92 Bundle b = createBundle(); 93 for (String key : map.keySet()) { 94 b.putString(key, map.get(key)); 95 } 96 for (String key : mCurrentBinaryMetrics.keySet()) { 97 b.putByteArray(key, mCurrentBinaryMetrics.get(key)); 98 } 99 return b; 100 } 101 102 /** 103 * Create a {@link Bundle} that will hold the metrics. Exposed for testing. 104 */ 105 @VisibleForTesting createBundle()106 Bundle createBundle() { 107 return new Bundle(); 108 } 109 clear()110 final void clear() { 111 mCurrentStringMetrics.clear(); 112 mCurrentFileMetrics.clear(); 113 mCurrentBinaryMetrics.clear(); 114 } 115 } 116