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 19 import android.annotation.TestApi; 20 import android.compat.annotation.UnsupportedAppUsage; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * Class to allow sending the HealthStats through aidl generated glue. 26 * 27 * The alternative would be to send a HealthStats object, which would 28 * require constructing one, and then immediately flattening it. This 29 * saves that step at the cost of doing the extra flattening when 30 * accessed in the same process as the writer. 31 * 32 * The HealthStatsWriter passed in the constructor is retained, so don't 33 * reuse them. 34 * @hide 35 */ 36 @TestApi 37 public class HealthStatsParceler implements Parcelable { 38 private HealthStatsWriter mWriter; 39 private HealthStats mHealthStats; 40 41 @UnsupportedAppUsage 42 public static final @android.annotation.NonNull 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 HealthStatsParceler(HealthStatsWriter writer)53 public HealthStatsParceler(HealthStatsWriter writer) { 54 mWriter = writer; 55 } 56 HealthStatsParceler(Parcel in)57 public HealthStatsParceler(Parcel in) { 58 mHealthStats = new HealthStats(in); 59 } 60 describeContents()61 public int describeContents() { 62 return 0; 63 } 64 writeToParcel(Parcel out, int flags)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 getHealthStats()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