• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.app.appsearch.aidl;
18 
19 import android.annotation.NonNull;
20 import android.app.appsearch.AppSearchBatchResult;
21 import android.app.appsearch.ParcelableUtil;
22 import android.app.appsearch.AppSearchResult;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.util.Map;
27 import java.util.Objects;
28 
29 /**
30  * Parcelable wrapper around {@link AppSearchBatchResult}.
31  *
32  * <p>{@link AppSearchBatchResult} can contain any type of key and value, including non-parcelable
33  * values. For the specific case of sending {@link AppSearchBatchResult} across Binder, this class
34  * wraps an {@link AppSearchBatchResult} that has String keys and Parcelable values. It provides
35  * parcelability of the whole structure.
36  *
37  * @param <ValueType> The type of result object for successful calls. Must be a parcelable type.
38  * @hide
39  */
40 public final class AppSearchBatchResultParcel<ValueType> implements Parcelable {
41     private final AppSearchBatchResult<String, ValueType> mResult;
42 
43     /** Creates a new {@link AppSearchBatchResultParcel} from the given result. */
AppSearchBatchResultParcel(@onNull AppSearchBatchResult<String, ValueType> result)44     public AppSearchBatchResultParcel(@NonNull AppSearchBatchResult<String, ValueType> result) {
45         mResult = Objects.requireNonNull(result);
46     }
47 
AppSearchBatchResultParcel(@onNull Parcel in)48     private AppSearchBatchResultParcel(@NonNull Parcel in) {
49         Parcel unmarshallParcel = Parcel.obtain();
50         try {
51             byte[] dataBlob = Objects.requireNonNull(ParcelableUtil.readBlob(in));
52             unmarshallParcel.unmarshall(dataBlob, 0, dataBlob.length);
53             unmarshallParcel.setDataPosition(0);
54             AppSearchBatchResult.Builder<String, ValueType> builder =
55                     new AppSearchBatchResult.Builder<>();
56             int size = unmarshallParcel.dataSize();
57             while (unmarshallParcel.dataPosition() < size) {
58                 String key = Objects.requireNonNull(unmarshallParcel.readString());
59                 builder.setResult(key, (AppSearchResult<ValueType>) AppSearchResultParcel
60                         .directlyReadFromParcel(unmarshallParcel));
61             }
62             mResult = builder.build();
63         } finally {
64             unmarshallParcel.recycle();
65         }
66     }
67 
68     @NonNull
getResult()69     public AppSearchBatchResult<String, ValueType> getResult() {
70         return mResult;
71     }
72 
73     /** @hide */
74     @Override
writeToParcel(@onNull Parcel dest, int flags)75     public void writeToParcel(@NonNull Parcel dest, int flags) {
76         byte[] bytes;
77         // Create a parcel object to serialize results. So that we can use Parcel.writeBlob() to
78         // send data. WriteBlob() could take care of whether to pass data via binder directly or
79         // Android shared memory if the data is large.
80         Parcel data = Parcel.obtain();
81         try {
82             for (Map.Entry<String, AppSearchResult<ValueType>> entry
83                     : mResult.getAll().entrySet()) {
84                 data.writeString(entry.getKey());
85                 AppSearchResultParcel.directlyWriteToParcel(data, entry.getValue());
86             }
87             bytes = data.marshall();
88         } finally {
89             data.recycle();
90         }
91         ParcelableUtil.writeBlob(dest, bytes);
92     }
93 
94     /** @hide */
95     @Override
describeContents()96     public int describeContents() {
97         return 0;
98     }
99 
100     /** @hide */
101     @NonNull
102     public static final Creator<AppSearchBatchResultParcel<?>> CREATOR =
103             new Creator<AppSearchBatchResultParcel<?>>() {
104                 @NonNull
105                 @Override
106                 public AppSearchBatchResultParcel<?> createFromParcel(@NonNull Parcel in) {
107                     return new AppSearchBatchResultParcel<>(in);
108                 }
109 
110                 @NonNull
111                 @Override
112                 public AppSearchBatchResultParcel<?>[] newArray(int size) {
113                     return new AppSearchBatchResultParcel<?>[size];
114                 }
115             };
116 }
117