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.NonNull; 20 import android.os.Bundle; 21 22 import java.util.Objects; 23 24 /** The response class of {@code AppSearchSession#getStorageInfo}. */ 25 public class StorageInfo { 26 27 private static final String SIZE_BYTES_FIELD = "sizeBytes"; 28 private static final String ALIVE_DOCUMENTS_COUNT = "aliveDocumentsCount"; 29 private static final String ALIVE_NAMESPACES_COUNT = "aliveNamespacesCount"; 30 31 private final Bundle mBundle; 32 StorageInfo(@onNull Bundle bundle)33 StorageInfo(@NonNull Bundle bundle) { 34 mBundle = Objects.requireNonNull(bundle); 35 } 36 37 /** 38 * Returns the {@link Bundle} populated by this builder. 39 * 40 * @hide 41 */ 42 @NonNull getBundle()43 public Bundle getBundle() { 44 return mBundle; 45 } 46 47 /** Returns the estimated size of the session's database in bytes. */ getSizeBytes()48 public long getSizeBytes() { 49 return mBundle.getLong(SIZE_BYTES_FIELD); 50 } 51 52 /** 53 * Returns the number of alive documents in the current session. 54 * 55 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 56 * set in {@link GenericDocument.Builder#setTtlMillis}. 57 */ getAliveDocumentsCount()58 public int getAliveDocumentsCount() { 59 return mBundle.getInt(ALIVE_DOCUMENTS_COUNT); 60 } 61 62 /** 63 * Returns the number of namespaces that have at least one alive document in the current 64 * session's database. 65 * 66 * <p>Alive documents are documents that haven't been deleted and haven't exceeded the ttl as 67 * set in {@link GenericDocument.Builder#setTtlMillis}. 68 */ getAliveNamespacesCount()69 public int getAliveNamespacesCount() { 70 return mBundle.getInt(ALIVE_NAMESPACES_COUNT); 71 } 72 73 /** Builder for {@link StorageInfo} objects. */ 74 public static final class Builder { 75 private long mSizeBytes; 76 private int mAliveDocumentsCount; 77 private int mAliveNamespacesCount; 78 79 /** Sets the size in bytes. */ 80 @NonNull setSizeBytes(long sizeBytes)81 public StorageInfo.Builder setSizeBytes(long sizeBytes) { 82 mSizeBytes = sizeBytes; 83 return this; 84 } 85 86 /** Sets the number of alive documents. */ 87 @NonNull setAliveDocumentsCount(int aliveDocumentsCount)88 public StorageInfo.Builder setAliveDocumentsCount(int aliveDocumentsCount) { 89 mAliveDocumentsCount = aliveDocumentsCount; 90 return this; 91 } 92 93 /** Sets the number of alive namespaces. */ 94 @NonNull setAliveNamespacesCount(int aliveNamespacesCount)95 public StorageInfo.Builder setAliveNamespacesCount(int aliveNamespacesCount) { 96 mAliveNamespacesCount = aliveNamespacesCount; 97 return this; 98 } 99 100 /** Builds a {@link StorageInfo} object. */ 101 @NonNull build()102 public StorageInfo build() { 103 Bundle bundle = new Bundle(); 104 bundle.putLong(SIZE_BYTES_FIELD, mSizeBytes); 105 bundle.putInt(ALIVE_DOCUMENTS_COUNT, mAliveDocumentsCount); 106 bundle.putInt(ALIVE_NAMESPACES_COUNT, mAliveNamespacesCount); 107 return new StorageInfo(bundle); 108 } 109 } 110 } 111