1 /* 2 * Copyright 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; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.app.appsearch.annotation.CanIgnoreReturnValue; 22 import android.app.appsearch.safeparcel.AbstractSafeParcelable; 23 import android.app.appsearch.safeparcel.SafeParcelable; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import com.android.appsearch.flags.Flags; 28 29 /** The response class of {@code AppSearchSession#getStorageInfo}. */ 30 @SafeParcelable.Class(creator = "StorageInfoCreator") 31 @SuppressWarnings("HiddenSuperclass") 32 public final class StorageInfo extends AbstractSafeParcelable { 33 34 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 35 @NonNull 36 public static final Parcelable.Creator<StorageInfo> CREATOR = new StorageInfoCreator(); 37 38 @Field(id = 1, getter = "getSizeBytes") 39 private long mSizeBytes; 40 41 @Field(id = 2, getter = "getAliveDocumentsCount") 42 private int mAliveDocumentsCount; 43 44 @Field(id = 3, getter = "getAliveNamespacesCount") 45 private int mAliveNamespacesCount; 46 47 @Constructor StorageInfo( @aramid = 1) long sizeBytes, @Param(id = 2) int aliveDocumentsCount, @Param(id = 3) int aliveNamespacesCount)48 StorageInfo( 49 @Param(id = 1) long sizeBytes, 50 @Param(id = 2) int aliveDocumentsCount, 51 @Param(id = 3) int aliveNamespacesCount) { 52 mSizeBytes = sizeBytes; 53 mAliveDocumentsCount = aliveDocumentsCount; 54 mAliveNamespacesCount = aliveNamespacesCount; 55 } 56 57 /** Returns the estimated size of the session's database in bytes. */ getSizeBytes()58 public long getSizeBytes() { 59 return mSizeBytes; 60 } 61 62 /** 63 * Returns the number of alive documents in the current session. 64 * 65 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 66 * set in {@link GenericDocument.Builder#setTtlMillis}. 67 */ getAliveDocumentsCount()68 public int getAliveDocumentsCount() { 69 return mAliveDocumentsCount; 70 } 71 72 /** 73 * Returns the number of namespaces that have at least one alive document in the current 74 * session's database. 75 * 76 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 77 * set in {@link GenericDocument.Builder#setTtlMillis}. 78 */ getAliveNamespacesCount()79 public int getAliveNamespacesCount() { 80 return mAliveNamespacesCount; 81 } 82 83 /** Builder for {@link StorageInfo} objects. */ 84 public static final class Builder { 85 private long mSizeBytes; 86 private int mAliveDocumentsCount; 87 private int mAliveNamespacesCount; 88 89 /** Sets the size in bytes. */ 90 @CanIgnoreReturnValue 91 @NonNull setSizeBytes(long sizeBytes)92 public StorageInfo.Builder setSizeBytes(long sizeBytes) { 93 mSizeBytes = sizeBytes; 94 return this; 95 } 96 97 /** Sets the number of alive documents. */ 98 @CanIgnoreReturnValue 99 @NonNull setAliveDocumentsCount(int aliveDocumentsCount)100 public StorageInfo.Builder setAliveDocumentsCount(int aliveDocumentsCount) { 101 mAliveDocumentsCount = aliveDocumentsCount; 102 return this; 103 } 104 105 /** Sets the number of alive namespaces. */ 106 @CanIgnoreReturnValue 107 @NonNull setAliveNamespacesCount(int aliveNamespacesCount)108 public StorageInfo.Builder setAliveNamespacesCount(int aliveNamespacesCount) { 109 mAliveNamespacesCount = aliveNamespacesCount; 110 return this; 111 } 112 113 /** Builds a {@link StorageInfo} object. */ 114 @NonNull build()115 public StorageInfo build() { 116 return new StorageInfo(mSizeBytes, mAliveDocumentsCount, mAliveNamespacesCount); 117 } 118 } 119 120 @FlaggedApi(Flags.FLAG_ENABLE_SAFE_PARCELABLE_2) 121 @Override writeToParcel(@onNull Parcel dest, int flags)122 public void writeToParcel(@NonNull Parcel dest, int flags) { 123 StorageInfoCreator.writeToParcel(this, dest, flags); 124 } 125 } 126