| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os.health; |
| 18 |
|
| 19 |
import android.annotation.TestApi; |
| 20 |
import android.os.Parcel; |
| 21 |
import android.os.Parcelable; |
| 22 |
import android.util.ArrayMap; |
| 23 |
|
| 24 |
import java.util.Map; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@TestApi |
| 36 |
public class HealthStatsWriter { |
| 37 |
private final HealthKeys.Constants mConstants; |
| 38 |
|
| 39 |
|
| 40 |
private final boolean[] mTimerFields; |
| 41 |
private final int[] mTimerCounts; |
| 42 |
private final long[] mTimerTimes; |
| 43 |
|
| 44 |
|
| 45 |
private final boolean[] mMeasurementFields; |
| 46 |
private final long[] mMeasurementValues; |
| 47 |
|
| 48 |
|
| 49 |
private final ArrayMap<String,HealthStatsWriter>[] mStatsValues; |
| 50 |
|
| 51 |
|
| 52 |
private final ArrayMap<String,TimerStat>[] mTimersValues; |
| 53 |
|
| 54 |
|
| 55 |
private final ArrayMap<String,Long>[] mMeasurementsValues; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public HealthStatsWriter(HealthKeys.Constants constants) { |
| 65 |
mConstants = constants; |
| 66 |
|
| 67 |
|
| 68 |
final int timerCount = constants.getSize(HealthKeys.TYPE_TIMER); |
| 69 |
mTimerFields = new boolean[timerCount]; |
| 70 |
mTimerCounts = new int[timerCount]; |
| 71 |
mTimerTimes = new long[timerCount]; |
| 72 |
|
| 73 |
|
| 74 |
final int measurementCount = constants.getSize(HealthKeys.TYPE_MEASUREMENT); |
| 75 |
mMeasurementFields = new boolean[measurementCount]; |
| 76 |
mMeasurementValues = new long[measurementCount]; |
| 77 |
|
| 78 |
|
| 79 |
final int statsCount = constants.getSize(HealthKeys.TYPE_STATS); |
| 80 |
mStatsValues = new ArrayMap[statsCount]; |
| 81 |
|
| 82 |
|
| 83 |
final int timersCount = constants.getSize(HealthKeys.TYPE_TIMERS); |
| 84 |
mTimersValues = new ArrayMap[timersCount]; |
| 85 |
|
| 86 |
|
| 87 |
final int measurementsCount = constants.getSize(HealthKeys.TYPE_MEASUREMENTS); |
| 88 |
mMeasurementsValues = new ArrayMap[measurementsCount]; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public void addTimer(int timerId, int count, long time) { |
| 95 |
final int index = mConstants.getIndex(HealthKeys.TYPE_TIMER, timerId); |
| 96 |
|
| 97 |
mTimerFields[index] = true; |
| 98 |
mTimerCounts[index] = count; |
| 99 |
mTimerTimes[index] = time; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public void addMeasurement(int measurementId, long value) { |
| 106 |
final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENT, measurementId); |
| 107 |
|
| 108 |
mMeasurementFields[index] = true; |
| 109 |
mMeasurementValues[index] = value; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void addStats(int key, String name, HealthStatsWriter value) { |
| 120 |
final int index = mConstants.getIndex(HealthKeys.TYPE_STATS, key); |
| 121 |
|
| 122 |
ArrayMap<String,HealthStatsWriter> map = mStatsValues[index]; |
| 123 |
if (map == null) { |
| 124 |
map = mStatsValues[index] = new ArrayMap<String,HealthStatsWriter>(1); |
| 125 |
} |
| 126 |
map.put(name, value); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void addTimers(int key, String name, TimerStat value) { |
| 135 |
final int index = mConstants.getIndex(HealthKeys.TYPE_TIMERS, key); |
| 136 |
|
| 137 |
ArrayMap<String,TimerStat> map = mTimersValues[index]; |
| 138 |
if (map == null) { |
| 139 |
map = mTimersValues[index] = new ArrayMap<String,TimerStat>(1); |
| 140 |
} |
| 141 |
map.put(name, value); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void addMeasurements(int key, String name, long value) { |
| 148 |
final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENTS, key); |
| 149 |
|
| 150 |
ArrayMap<String,Long> map = mMeasurementsValues[index]; |
| 151 |
if (map == null) { |
| 152 |
map = mMeasurementsValues[index] = new ArrayMap<String,Long>(1); |
| 153 |
} |
| 154 |
map.put(name, value); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public void flattenToParcel(Parcel out) { |
| 166 |
int[] keys; |
| 167 |
|
| 168 |
|
| 169 |
out.writeString(mConstants.getDataType()); |
| 170 |
|
| 171 |
|
| 172 |
out.writeInt(countBooleanArray(mTimerFields)); |
| 173 |
keys = mConstants.getKeys(HealthKeys.TYPE_TIMER); |
| 174 |
for (int i=0; i<keys.length; i++) { |
| 175 |
if (mTimerFields[i]) { |
| 176 |
out.writeInt(keys[i]); |
| 177 |
out.writeInt(mTimerCounts[i]); |
| 178 |
out.writeLong(mTimerTimes[i]); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
out.writeInt(countBooleanArray(mMeasurementFields)); |
| 184 |
keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENT); |
| 185 |
for (int i=0; i<keys.length; i++) { |
| 186 |
if (mMeasurementFields[i]) { |
| 187 |
out.writeInt(keys[i]); |
| 188 |
out.writeLong(mMeasurementValues[i]); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
out.writeInt(countObjectArray(mStatsValues)); |
| 194 |
keys = mConstants.getKeys(HealthKeys.TYPE_STATS); |
| 195 |
for (int i=0; i<keys.length; i++) { |
| 196 |
if (mStatsValues[i] != null) { |
| 197 |
out.writeInt(keys[i]); |
| 198 |
writeHealthStatsWriterMap(out, mStatsValues[i]); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
out.writeInt(countObjectArray(mTimersValues)); |
| 204 |
keys = mConstants.getKeys(HealthKeys.TYPE_TIMERS); |
| 205 |
for (int i=0; i<keys.length; i++) { |
| 206 |
if (mTimersValues[i] != null) { |
| 207 |
out.writeInt(keys[i]); |
| 208 |
writeParcelableMap(out, mTimersValues[i]); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
out.writeInt(countObjectArray(mMeasurementsValues)); |
| 214 |
keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENTS); |
| 215 |
for (int i=0; i<keys.length; i++) { |
| 216 |
if (mMeasurementsValues[i] != null) { |
| 217 |
out.writeInt(keys[i]); |
| 218 |
writeLongsMap(out, mMeasurementsValues[i]); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
private static int countBooleanArray(boolean[] fields) { |
| 227 |
int count = 0; |
| 228 |
final int N = fields.length; |
| 229 |
for (int i=0; i<N; i++) { |
| 230 |
if (fields[i]) { |
| 231 |
count++; |
| 232 |
} |
| 233 |
} |
| 234 |
return count; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
private static <T extends Object> int countObjectArray(T[] fields) { |
| 241 |
int count = 0; |
| 242 |
final int N = fields.length; |
| 243 |
for (int i=0; i<N; i++) { |
| 244 |
if (fields[i] != null) { |
| 245 |
count++; |
| 246 |
} |
| 247 |
} |
| 248 |
return count; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
private static void writeHealthStatsWriterMap(Parcel out, |
| 255 |
ArrayMap<String,HealthStatsWriter> map) { |
| 256 |
final int N = map.size(); |
| 257 |
out.writeInt(N); |
| 258 |
for (int i=0; i<N; i++) { |
| 259 |
out.writeString(map.keyAt(i)); |
| 260 |
map.valueAt(i).flattenToParcel(out); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
private static <T extends Parcelable> void writeParcelableMap(Parcel out, |
| 268 |
ArrayMap<String,T> map) { |
| 269 |
final int N = map.size(); |
| 270 |
out.writeInt(N); |
| 271 |
for (int i=0; i<N; i++) { |
| 272 |
out.writeString(map.keyAt(i)); |
| 273 |
map.valueAt(i).writeToParcel(out, 0); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private static void writeLongsMap(Parcel out, ArrayMap<String,Long> map) { |
| 281 |
final int N = map.size(); |
| 282 |
out.writeInt(N); |
| 283 |
for (int i=0; i<N; i++) { |
| 284 |
out.writeString(map.keyAt(i)); |
| 285 |
out.writeLong(map.valueAt(i)); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|