1 /* 2 * Copyright (C) 2023 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.health.connect; 18 19 import android.annotation.NonNull; 20 import android.health.connect.datatypes.DataOrigin; 21 import android.os.Parcel; 22 import android.util.ArraySet; 23 24 import java.time.ZoneOffset; 25 import java.util.List; 26 import java.util.Objects; 27 import java.util.Set; 28 29 /** 30 * A class to represent the results of {@link HealthConnectManager} aggregate APIs 31 * 32 * @hide 33 */ 34 public final class AggregateResult<T> { 35 private final T mResult; 36 private ZoneOffset mZoneOffset; 37 private Set<DataOrigin> mDataOrigins; 38 AggregateResult(T result)39 public AggregateResult(T result) { 40 mResult = result; 41 } 42 putToParcel(@onNull Parcel parcel)43 public void putToParcel(@NonNull Parcel parcel) { 44 if (mResult instanceof Long) { 45 parcel.writeLong((Long) mResult); 46 } else if (mResult instanceof Double) { 47 parcel.writeDouble((Double) mResult); 48 } 49 } 50 51 /** 52 * @return {@link ZoneOffset} for the underlying record, null if aggregation was derived from 53 * multiple records 54 */ getZoneOffset()55 public ZoneOffset getZoneOffset() { 56 return mZoneOffset; 57 } 58 59 /** Sets the {@link ZoneOffset} for the underlying record. */ setZoneOffset(ZoneOffset zoneOffset)60 public AggregateResult<T> setZoneOffset(ZoneOffset zoneOffset) { 61 mZoneOffset = zoneOffset; 62 return this; 63 } 64 65 /** Returns set of {@link DataOrigin} that contributed to the aggregation result */ 66 @NonNull getDataOrigins()67 public Set<DataOrigin> getDataOrigins() { 68 return mDataOrigins; 69 } 70 71 /** Sets a Set of {@link DataOrigin} that contributed to the aggregation result. */ setDataOrigins(@onNull List<String> packageNameList)72 public AggregateResult<T> setDataOrigins(@NonNull List<String> packageNameList) { 73 Objects.requireNonNull(packageNameList); 74 75 mDataOrigins = new ArraySet<>(); 76 for (String packageName : packageNameList) { 77 mDataOrigins.add(new DataOrigin.Builder().setPackageName(packageName).build()); 78 } 79 return this; 80 } 81 82 /** 83 * @return an Object representing the result of an aggregation. 84 */ 85 @NonNull getResult()86 T getResult() { 87 return mResult; 88 } 89 } 90