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