Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 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 | |
| 17 | package android.os.health; |
| 18 | |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 19 | import android.annotation.TestApi; |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | import android.util.ArrayMap; |
| 23 | |
| 24 | import java.util.Arrays; |
| 25 | import 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 Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 39 | @TestApi |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 40 | public class HealthStatsParceler implements Parcelable { |
| 41 | private HealthStatsWriter mWriter; |
| 42 | private HealthStats mHealthStats; |
| 43 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame^] | 44 | public static final @android.annotation.NonNull Parcelable.Creator<HealthStatsParceler> CREATOR |
Joe Onorato | 713fec8 | 2016-03-04 10:34:02 -0800 | [diff] [blame] | 45 | = 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 | |