1 /* 2 * Copyright (C) 2022 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.GenericDocument; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 import java.util.Objects; 27 28 /** 29 * The Parcelable object contains a List of {@link GenericDocument}. 30 * 31 * <P>This class will batch a list of {@link GenericDocument}. If the number of documents is too 32 * large for a transact, they will be put to Android Shared Memory. 33 * 34 * @see Parcel#writeBlob(byte[]) 35 * @hide 36 */ 37 public final class DocumentsParcel implements Parcelable { 38 private final List<GenericDocument> mDocuments; 39 DocumentsParcel(@onNull List<GenericDocument> documents)40 public DocumentsParcel(@NonNull List<GenericDocument> documents) { 41 mDocuments = Objects.requireNonNull(documents); 42 } 43 DocumentsParcel(@onNull Parcel in)44 private DocumentsParcel(@NonNull Parcel in) { 45 this(in.readBlob()); 46 } 47 DocumentsParcel(@onNull byte[] dataBlob)48 private DocumentsParcel(@NonNull byte[] dataBlob) { 49 // Create a parcel object to un-serialize the byte array we are reading from 50 // Parcel.readBlob(). Parcel.WriteBlob() could take care of whether to pass data via 51 // binder directly or Android shared memory if the data is large. 52 Parcel unmarshallParcel = Parcel.obtain(); 53 try { 54 unmarshallParcel.unmarshall(dataBlob, 0, dataBlob.length); 55 unmarshallParcel.setDataPosition(0); 56 // read the number of document that stored in here. 57 int size = unmarshallParcel.readInt(); 58 mDocuments = new ArrayList<>(size); 59 for (int i = 0; i < size; i++) { 60 // Read document's bundle and convert them. 61 mDocuments.add(new GenericDocument(unmarshallParcel.readBundle())); 62 } 63 } finally { 64 unmarshallParcel.recycle(); 65 } 66 } 67 68 public static final Creator<DocumentsParcel> CREATOR = new Creator<DocumentsParcel>() { 69 @Override 70 public DocumentsParcel createFromParcel(Parcel in) { 71 return new DocumentsParcel(in); 72 } 73 74 @Override 75 public DocumentsParcel[] newArray(int size) { 76 return new DocumentsParcel[size]; 77 } 78 }; 79 80 @Override describeContents()81 public int describeContents() { 82 return 0; 83 } 84 85 @Override writeToParcel(Parcel dest, int flags)86 public void writeToParcel(Parcel dest, int flags) { 87 dest.writeBlob(serializeToByteArray()); 88 } 89 90 /** 91 * Serializes the whole object, So that we can use Parcel.writeBlob() to send data. WriteBlob() 92 * could take care of whether to pass data via binder directly or Android shared memory if the 93 * data is large. 94 */ 95 @NonNull serializeToByteArray()96 private byte[] serializeToByteArray() { 97 byte[] bytes; 98 Parcel data = Parcel.obtain(); 99 try { 100 // Save the number documents to the temporary Parcel object. 101 data.writeInt(mDocuments.size()); 102 // Save all document's bundle to the temporary Parcel object. 103 for (int i = 0; i < mDocuments.size(); i++) { 104 data.writeBundle(mDocuments.get(i).getBundle()); 105 } 106 bytes = data.marshall(); 107 } finally { 108 data.recycle(); 109 } 110 return bytes; 111 } 112 113 /** Returns the List of {@link GenericDocument} of this object. */ 114 @NonNull getDocuments()115 public List<GenericDocument> getDocuments() { 116 return mDocuments; 117 } 118 } 119