1/*
2 * Copyright (C) 2016 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
17package android.os.health;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.ArrayMap;
22
23import java.util.Arrays;
24import java.util.Map;
25
26/**
27 * Class to allow sending the HealthStats through aidl generated glue.
28 *
29 * The alternative would be to send a HealthStats object, which would
30 * require constructing one, and then immediately flattening it. This
31 * saves that step at the cost of doing the extra flattening when
32 * accessed in the same process as the writer.
33 *
34 * The HealthStatsWriter passed in the constructor is retained, so don't
35 * reuse them.
36 * @hide
37 */
38public class HealthStatsParceler implements Parcelable {
39    private HealthStatsWriter mWriter;
40    private HealthStats mHealthStats;
41
42    public static final Parcelable.Creator<HealthStatsParceler> CREATOR
43            = new Parcelable.Creator<HealthStatsParceler>() {
44        public HealthStatsParceler createFromParcel(Parcel in) {
45            return new HealthStatsParceler(in);
46        }
47
48        public HealthStatsParceler[] newArray(int size) {
49            return new HealthStatsParceler[size];
50        }
51    };
52
53    public HealthStatsParceler(HealthStatsWriter writer) {
54        mWriter = writer;
55    }
56
57    public HealthStatsParceler(Parcel in) {
58        mHealthStats = new HealthStats(in);
59    }
60
61    public int describeContents() {
62        return 0;
63    }
64
65    public void writeToParcel(Parcel out, int flags) {
66        // See comment on mWriter declaration above.
67        if (mWriter != null) {
68            mWriter.flattenToParcel(out);
69        } else {
70            throw new RuntimeException("Can not re-parcel HealthStatsParceler that was"
71                    + " constructed from a Parcel");
72        }
73    }
74
75    public HealthStats getHealthStats() {
76        if (mWriter != null) {
77            final Parcel parcel = Parcel.obtain();
78            mWriter.flattenToParcel(parcel);
79            parcel.setDataPosition(0);
80            mHealthStats = new HealthStats(parcel);
81            parcel.recycle();
82        }
83
84        return mHealthStats;
85    }
86}
87
88