1713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato/* 2713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Copyright (C) 2016 The Android Open Source Project 3713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 4713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 5713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * you may not use this file except in compliance with the License. 6713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * You may obtain a copy of the License at 7713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 8713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * http://www.apache.org/licenses/LICENSE-2.0 9713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 10713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Unless required by applicable law or agreed to in writing, software 11713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 12713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * See the License for the specific language governing permissions and 14713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * limitations under the License. 15713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 16713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 17713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratopackage android.os.health; 18713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 19713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.Parcel; 20713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.os.Parcelable; 21713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport android.util.ArrayMap; 22713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 23713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratoimport java.util.Map; 24713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 25713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato/** 26713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Class to write the health stats data into a parcel, so it can then be 27713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * retrieved via a {@link HealthStats} object. 28713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 29713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * There is an attempt to keep this class as low overhead as possible, for 30713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * example storing an int[] and a long[] instead of a TimerStat[]. 31713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 32713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @hide 33713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 34713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onoratopublic class HealthStatsWriter { 35713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final HealthKeys.Constants mConstants; 36713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 37713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // TimerStat fields 38713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final boolean[] mTimerFields; 39713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final int[] mTimerCounts; 40713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final long[] mTimerTimes; 41713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 42713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurement fields 43713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final boolean[] mMeasurementFields; 44713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final long[] mMeasurementValues; 45713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 46713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Stats fields 47713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final ArrayMap<String,HealthStatsWriter>[] mStatsValues; 48713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 49713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Timers fields 50713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final ArrayMap<String,TimerStat>[] mTimersValues; 51713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 52713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurements fields 53713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private final ArrayMap<String,Long>[] mMeasurementsValues; 54713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 55713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 56713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Construct a HealthStatsWriter object with the given constants. 57713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 58713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * The "getDataType()" of the resulting HealthStats object will be the 59713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * short name of the java class that the Constants object was initalized 60713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * with. 61713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 62713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public HealthStatsWriter(HealthKeys.Constants constants) { 63713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mConstants = constants; 64713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 65713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // TimerStat 66713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int timerCount = constants.getSize(HealthKeys.TYPE_TIMER); 67713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerFields = new boolean[timerCount]; 68713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerCounts = new int[timerCount]; 69713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerTimes = new long[timerCount]; 70713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 71713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurement 72713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int measurementCount = constants.getSize(HealthKeys.TYPE_MEASUREMENT); 73713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mMeasurementFields = new boolean[measurementCount]; 74713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mMeasurementValues = new long[measurementCount]; 75713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 76713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Stats 77713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int statsCount = constants.getSize(HealthKeys.TYPE_STATS); 78713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mStatsValues = new ArrayMap[statsCount]; 79713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 80713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Timers 81713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int timersCount = constants.getSize(HealthKeys.TYPE_TIMERS); 82713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimersValues = new ArrayMap[timersCount]; 83713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 84713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurements 85713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int measurementsCount = constants.getSize(HealthKeys.TYPE_MEASUREMENTS); 86713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mMeasurementsValues = new ArrayMap[measurementsCount]; 87713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 88713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 89713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 90713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Add a timer for the given key. 91713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 92713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void addTimer(int timerId, int count, long time) { 93713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int index = mConstants.getIndex(HealthKeys.TYPE_TIMER, timerId); 94713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 95713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerFields[index] = true; 96713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerCounts[index] = count; 97713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mTimerTimes[index] = time; 98713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 99713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 100713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 101713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Add a measurement for the given key. 102713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 103713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void addMeasurement(int measurementId, long value) { 104713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENT, measurementId); 105713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 106713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mMeasurementFields[index] = true; 107713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato mMeasurementValues[index] = value; 108713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 109713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 110713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 111713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Add a recursive HealthStats object for the given key and string name. The value 112713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * is stored as a HealthStatsWriter until this object is written to a parcel, so 113713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * don't attempt to reuse the HealthStatsWriter. 114713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 115713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * The value field should not be null. 116713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 117713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void addStats(int key, String name, HealthStatsWriter value) { 118713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int index = mConstants.getIndex(HealthKeys.TYPE_STATS, key); 119713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 120713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ArrayMap<String,HealthStatsWriter> map = mStatsValues[index]; 121713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (map == null) { 122713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map = mStatsValues[index] = new ArrayMap<String,HealthStatsWriter>(1); 123713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 124713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map.put(name, value); 125713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 126713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 127713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 128713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Add a TimerStat for the given key and string name. 129713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * 130713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * The value field should not be null. 131713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 132713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void addTimers(int key, String name, TimerStat value) { 133713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int index = mConstants.getIndex(HealthKeys.TYPE_TIMERS, key); 134713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 135713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ArrayMap<String,TimerStat> map = mTimersValues[index]; 136713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (map == null) { 137713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map = mTimersValues[index] = new ArrayMap<String,TimerStat>(1); 138713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 139713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map.put(name, value); 140713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 141713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 142713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 143713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Add a measurement for the given key and string name. 144713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 145713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void addMeasurements(int key, String name, long value) { 146713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENTS, key); 147713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 148713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ArrayMap<String,Long> map = mMeasurementsValues[index]; 149713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (map == null) { 150713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map = mMeasurementsValues[index] = new ArrayMap<String,Long>(1); 151713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 152713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map.put(name, value); 153713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 154713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 155713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 156713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Flattens the data in this HealthStatsWriter to the Parcel format 157713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * that can be unparceled into a HealthStat. 158713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * @more 159713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * (Called flattenToParcel because this HealthStatsWriter itself is 160713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * not parcelable and we don't flatten all the business about the 161713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * HealthKeys.Constants, only the values that were actually supplied) 162713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 163713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato public void flattenToParcel(Parcel out) { 164713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato int[] keys; 165713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 166713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Header fields 167713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeString(mConstants.getDataType()); 168713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 169713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // TimerStat fields 170713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(countBooleanArray(mTimerFields)); 171713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato keys = mConstants.getKeys(HealthKeys.TYPE_TIMER); 172713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<keys.length; i++) { 173713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (mTimerFields[i]) { 174713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(keys[i]); 175713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(mTimerCounts[i]); 176713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeLong(mTimerTimes[i]); 177713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 178713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 179713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 180713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurement fields 181713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(countBooleanArray(mMeasurementFields)); 182713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENT); 183713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<keys.length; i++) { 184713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (mMeasurementFields[i]) { 185713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(keys[i]); 186713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeLong(mMeasurementValues[i]); 187713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 188713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 189713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 190713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Stats 191713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(countObjectArray(mStatsValues)); 192713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato keys = mConstants.getKeys(HealthKeys.TYPE_STATS); 193713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<keys.length; i++) { 194713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (mStatsValues[i] != null) { 195713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(keys[i]); 196713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato writeHealthStatsWriterMap(out, mStatsValues[i]); 197713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 198713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 199713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 200713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Timers 201713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(countObjectArray(mTimersValues)); 202713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato keys = mConstants.getKeys(HealthKeys.TYPE_TIMERS); 203713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<keys.length; i++) { 204713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (mTimersValues[i] != null) { 205713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(keys[i]); 206713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato writeParcelableMap(out, mTimersValues[i]); 207713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 208713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 209713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 210713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato // Measurements 211713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(countObjectArray(mMeasurementsValues)); 212713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENTS); 213713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<keys.length; i++) { 214713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (mMeasurementsValues[i] != null) { 215713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(keys[i]); 216713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato writeLongsMap(out, mMeasurementsValues[i]); 217713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 218713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 219713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 220713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 221713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 222713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Count how many of the fields have been set. 223713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 224713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private static int countBooleanArray(boolean[] fields) { 225713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato int count = 0; 226713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = fields.length; 227713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 228713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (fields[i]) { 229713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato count++; 230713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 231713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 232713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return count; 233713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 234713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 235713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 236713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Count how many of the fields have been set. 237713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 238713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private static <T extends Object> int countObjectArray(T[] fields) { 239713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato int count = 0; 240713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = fields.length; 241713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 242713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato if (fields[i] != null) { 243713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato count++; 244713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 245713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 246713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato return count; 247713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 248713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 249713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 250713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Write a map of String to HealthStatsWriter to the Parcel. 251713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 252713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private static void writeHealthStatsWriterMap(Parcel out, 253713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ArrayMap<String,HealthStatsWriter> map) { 254713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = map.size(); 255713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(N); 256713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 257713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeString(map.keyAt(i)); 258713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map.valueAt(i).flattenToParcel(out); 259713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 260713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 261713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 262713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 263713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Write a map of String to Parcelables to the Parcel. 264713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 265713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private static <T extends Parcelable> void writeParcelableMap(Parcel out, 266713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato ArrayMap<String,T> map) { 267713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = map.size(); 268713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(N); 269713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 270713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeString(map.keyAt(i)); 271713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato map.valueAt(i).writeToParcel(out, 0); 272713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 273713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 274713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 275713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato /** 276713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato * Write a map of String to Longs to the Parcel. 277713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato */ 278713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato private static void writeLongsMap(Parcel out, ArrayMap<String,Long> map) { 279713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato final int N = map.size(); 280713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeInt(N); 281713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato for (int i=0; i<N; i++) { 282713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeString(map.keyAt(i)); 283713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato out.writeLong(map.valueAt(i)); 284713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 285713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato } 286713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato} 287713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 288713fec85b8612256211f09c62b8958a99fe5b9dbJoe Onorato 289