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