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