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.restore;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.health.connect.HealthConnectException;
22 import android.health.connect.aidl.HealthConnectExceptionParcel;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.util.ArrayMap;
26 
27 import java.util.Collections;
28 import java.util.Map;
29 import java.util.Objects;
30 
31 /**
32  * Exception specifically encountered while staging HealthConnect data. Wraps errors encountered for
33  * all files while staging all the files.
34  *
35  * @hide
36  */
37 @SystemApi
38 public final class StageRemoteDataException extends RuntimeException implements Parcelable {
39     private final Map<String, HealthConnectException> mExceptionsByFileNames = new ArrayMap<>();
40 
StageRemoteDataException(Parcel in)41     private StageRemoteDataException(Parcel in) {
42         int size = in.readInt();
43         for (int i = 0; i < size; ++i) {
44             mExceptionsByFileNames.put(
45                     in.readString(),
46                     in.readParcelable(
47                                     HealthConnectExceptionParcel.class.getClassLoader(),
48                                     HealthConnectExceptionParcel.class)
49                             .getHealthConnectException());
50         }
51     }
52 
53     /** @hide */
StageRemoteDataException( @onNull Map<String, HealthConnectException> exceptionsByFileNames)54     public StageRemoteDataException(
55             @NonNull Map<String, HealthConnectException> exceptionsByFileNames) {
56         super("StageRemoteDataException");
57         Objects.requireNonNull(exceptionsByFileNames);
58         mExceptionsByFileNames.putAll(exceptionsByFileNames);
59     }
60 
61     @NonNull
62     public static final Creator<StageRemoteDataException> CREATOR =
63             new Creator<>() {
64                 @Override
65                 public StageRemoteDataException createFromParcel(Parcel in) {
66                     return new StageRemoteDataException(in);
67                 }
68 
69                 @Override
70                 public StageRemoteDataException[] newArray(int size) {
71                     return new StageRemoteDataException[size];
72                 }
73             };
74 
75     /** Returns a {@link Map} which maps the file name with the error encountered for that file. */
76     @NonNull
getExceptionsByFileNames()77     public Map<String, HealthConnectException> getExceptionsByFileNames() {
78         return Collections.unmodifiableMap(mExceptionsByFileNames);
79     }
80 
81     @Override
describeContents()82     public int describeContents() {
83         return 0;
84     }
85 
86     @Override
writeToParcel(@onNull Parcel dest, int flags)87     public void writeToParcel(@NonNull Parcel dest, int flags) {
88         dest.writeInt(mExceptionsByFileNames.size());
89         for (var fileNamePfd : mExceptionsByFileNames.entrySet()) {
90             dest.writeString(fileNamePfd.getKey());
91             dest.writeParcelable(
92                     new HealthConnectExceptionParcel(fileNamePfd.getValue()), /* parcelableFlags */
93                     0);
94         }
95     }
96 }
97