summaryrefslogtreecommitdiff
path: root/core/java/android/os/health/HealthStatsWriter.java
blob: d4d10b056c5c1cf306dd1d017a57f95141faddb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.os.health;

import android.annotation.TestApi;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.ArrayMap;

import java.util.Map;

/**
 * Class to write the health stats data into a parcel, so it can then be
 * retrieved via a {@link HealthStats} object.
 *
 * There is an attempt to keep this class as low overhead as possible, for
 * example storing an int[] and a long[] instead of a TimerStat[].
 *
 * @hide
 */
@TestApi
public class HealthStatsWriter {
    private final HealthKeys.Constants mConstants;

    // TimerStat fields
    private final boolean[] mTimerFields;
    private final int[] mTimerCounts;
    private final long[] mTimerTimes;

    // Measurement fields
    private final boolean[] mMeasurementFields;
    private final long[] mMeasurementValues;

    // Stats fields
    private final ArrayMap<String,HealthStatsWriter>[] mStatsValues;

    // Timers fields
    private final ArrayMap<String,TimerStat>[] mTimersValues;

    // Measurements fields
    private final ArrayMap<String,Long>[] mMeasurementsValues;

    /**
     * Construct a HealthStatsWriter object with the given constants.
     *
     * The "getDataType()" of the resulting HealthStats object will be the
     * short name of the java class that the Constants object was initalized
     * with.
     */
    public HealthStatsWriter(HealthKeys.Constants constants) {
        mConstants = constants;

        // TimerStat
        final int timerCount = constants.getSize(HealthKeys.TYPE_TIMER);
        mTimerFields = new boolean[timerCount];
        mTimerCounts = new int[timerCount];
        mTimerTimes = new long[timerCount];

        // Measurement
        final int measurementCount = constants.getSize(HealthKeys.TYPE_MEASUREMENT);
        mMeasurementFields = new boolean[measurementCount];
        mMeasurementValues = new long[measurementCount];

        // Stats
        final int statsCount = constants.getSize(HealthKeys.TYPE_STATS);
        mStatsValues = new ArrayMap[statsCount];

        // Timers
        final int timersCount = constants.getSize(HealthKeys.TYPE_TIMERS);
        mTimersValues = new ArrayMap[timersCount];

        // Measurements
        final int measurementsCount = constants.getSize(HealthKeys.TYPE_MEASUREMENTS);
        mMeasurementsValues = new ArrayMap[measurementsCount];
    }

    /**
     * Add a timer for the given key.
     */
    public void addTimer(int timerId, int count, long time) {
        final int index = mConstants.getIndex(HealthKeys.TYPE_TIMER, timerId);

        mTimerFields[index] = true;
        mTimerCounts[index] = count;
        mTimerTimes[index] = time;
    }

    /**
     * Add a measurement for the given key.
     */
    public void addMeasurement(int measurementId, long value) {
        final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENT, measurementId);

        mMeasurementFields[index] = true;
        mMeasurementValues[index] = value;
    }

    /**
     * Add a recursive HealthStats object for the given key and string name. The value
     * is stored as a HealthStatsWriter until this object is written to a parcel, so
     * don't attempt to reuse the HealthStatsWriter.
     *
     * The value field should not be null.
     */
    public void addStats(int key, String name, HealthStatsWriter value) {
        final int index = mConstants.getIndex(HealthKeys.TYPE_STATS, key);

        ArrayMap<String,HealthStatsWriter> map = mStatsValues[index];
        if (map == null) {
            map = mStatsValues[index] = new ArrayMap<String,HealthStatsWriter>(1);
        }
        map.put(name, value);
    }

    /**
     * Add a TimerStat for the given key and string name.
     *
     * The value field should not be null.
     */
    public void addTimers(int key, String name, TimerStat value) {
        final int index = mConstants.getIndex(HealthKeys.TYPE_TIMERS, key);

        ArrayMap<String,TimerStat> map = mTimersValues[index];
        if (map == null) {
            map = mTimersValues[index] = new ArrayMap<String,TimerStat>(1);
        }
        map.put(name, value);
    }

    /**
     * Add a measurement for the given key and string name.
     */
    public void addMeasurements(int key, String name, long value) {
        final int index = mConstants.getIndex(HealthKeys.TYPE_MEASUREMENTS, key);

        ArrayMap<String,Long> map = mMeasurementsValues[index];
        if (map == null) {
            map = mMeasurementsValues[index] = new ArrayMap<String,Long>(1);
        }
        map.put(name, value);
    }

    /**
     * Flattens the data in this HealthStatsWriter to the Parcel format
     * that can be unparceled into a HealthStat.
     * @more
     * (Called flattenToParcel because this HealthStatsWriter itself is
     * not parcelable and we don't flatten all the business about the
     * HealthKeys.Constants, only the values that were actually supplied)
     */
    public void flattenToParcel(Parcel out) {
        int[] keys;

        // Header fields
        out.writeString(mConstants.getDataType());

        // TimerStat fields
        out.writeInt(countBooleanArray(mTimerFields));
        keys = mConstants.getKeys(HealthKeys.TYPE_TIMER);
        for (int i=0; i<keys.length; i++) {
            if (mTimerFields[i]) {
                out.writeInt(keys[i]);
                out.writeInt(mTimerCounts[i]);
                out.writeLong(mTimerTimes[i]);
            }
        }

        // Measurement fields
        out.writeInt(countBooleanArray(mMeasurementFields));
        keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENT);
        for (int i=0; i<keys.length; i++) {
            if (mMeasurementFields[i]) {
                out.writeInt(keys[i]);
                out.writeLong(mMeasurementValues[i]);
            }
        }

        // Stats
        out.writeInt(countObjectArray(mStatsValues));
        keys = mConstants.getKeys(HealthKeys.TYPE_STATS);
        for (int i=0; i<keys.length; i++) {
            if (mStatsValues[i] != null) {
                out.writeInt(keys[i]);
                writeHealthStatsWriterMap(out, mStatsValues[i]);
            }
        }

        // Timers
        out.writeInt(countObjectArray(mTimersValues));
        keys = mConstants.getKeys(HealthKeys.TYPE_TIMERS);
        for (int i=0; i<keys.length; i++) {
            if (mTimersValues[i] != null) {
                out.writeInt(keys[i]);
                writeParcelableMap(out, mTimersValues[i]);
            }
        }

        // Measurements
        out.writeInt(countObjectArray(mMeasurementsValues));
        keys = mConstants.getKeys(HealthKeys.TYPE_MEASUREMENTS);
        for (int i=0; i<keys.length; i++) {
            if (mMeasurementsValues[i] != null) {
                out.writeInt(keys[i]);
                writeLongsMap(out, mMeasurementsValues[i]);
            }
        }
    }

    /**
     * Count how many of the fields have been set.
     */
    private static int countBooleanArray(boolean[] fields) {
        int count = 0;
        final int N = fields.length;
        for (int i=0; i<N; i++) {
            if (fields[i]) {
                count++;
            }
        }
        return count;
    }

    /**
     * Count how many of the fields have been set.
     */
    private static <T extends Object> int countObjectArray(T[] fields) {
        int count = 0;
        final int N = fields.length;
        for (int i=0; i<N; i++) {
            if (fields[i] != null) {
                count++;
            }
        }
        return count;
    }

    /**
     * Write a map of String to HealthStatsWriter to the Parcel.
     */
    private static void writeHealthStatsWriterMap(Parcel out,
            ArrayMap<String,HealthStatsWriter> map) {
        final int N = map.size();
        out.writeInt(N);
        for (int i=0; i<N; i++) {
            out.writeString(map.keyAt(i));
            map.valueAt(i).flattenToParcel(out);
        }
    }

    /**
     * Write a map of String to Parcelables to the Parcel.
     */
    private static <T extends Parcelable> void writeParcelableMap(Parcel out,
            ArrayMap<String,T> map) {
        final int N = map.size();
        out.writeInt(N);
        for (int i=0; i<N; i++) {
            out.writeString(map.keyAt(i));
            map.valueAt(i).writeToParcel(out, 0);
        }
    }

    /**
     * Write a map of String to Longs to the Parcel.
     */
    private static void writeLongsMap(Parcel out, ArrayMap<String,Long> map) {
        final int N = map.size();
        out.writeInt(N);
        for (int i=0; i<N; i++) {
            out.writeString(map.keyAt(i));
            out.writeLong(map.valueAt(i));
        }
    }
}