• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.IntRange;
20 import android.annotation.NonNull;
21 import android.os.Bundle;
22 import android.util.ArraySet;
23 
24 import java.util.ArrayList;
25 import java.util.Objects;
26 import java.util.Set;
27 
28 /** The response class of {@link AppSearchSession#getSchema} */
29 public final class GetSchemaResponse {
30     private static final String VERSION_FIELD = "version";
31     private static final String SCHEMAS_FIELD = "schemas";
32 
33     private final Bundle mBundle;
34 
GetSchemaResponse(@onNull Bundle bundle)35     GetSchemaResponse(@NonNull Bundle bundle) {
36         mBundle = Objects.requireNonNull(bundle);
37     }
38 
39     /**
40      * Returns the {@link Bundle} populated by this builder.
41      *
42      * @hide
43      */
44     @NonNull
getBundle()45     public Bundle getBundle() {
46         return mBundle;
47     }
48 
49     /**
50      * Returns the overall database schema version.
51      *
52      * <p>If the database is empty, 0 will be returned.
53      */
54     @IntRange(from = 0)
getVersion()55     public int getVersion() {
56         return mBundle.getInt(VERSION_FIELD);
57     }
58 
59     /**
60      * Return the schemas most recently successfully provided to {@link AppSearchSession#setSchema}.
61      *
62      * <p>It is inefficient to call this method repeatedly.
63      */
64     @NonNull
getSchemas()65     public Set<AppSearchSchema> getSchemas() {
66         ArrayList<Bundle> schemaBundles = mBundle.getParcelableArrayList(SCHEMAS_FIELD);
67         Set<AppSearchSchema> schemas = new ArraySet<>(schemaBundles.size());
68         for (int i = 0; i < schemaBundles.size(); i++) {
69             schemas.add(new AppSearchSchema(schemaBundles.get(i)));
70         }
71         return schemas;
72     }
73 
74     /** Builder for {@link GetSchemaResponse} objects. */
75     public static final class Builder {
76         private int mVersion = 0;
77         private ArrayList<Bundle> mSchemaBundles = new ArrayList<>();
78         private boolean mBuilt = false;
79 
80         /**
81          * Sets the database overall schema version.
82          *
83          * <p>Default version is 0
84          */
85         @NonNull
setVersion(@ntRangefrom = 0) int version)86         public Builder setVersion(@IntRange(from = 0) int version) {
87             resetIfBuilt();
88             mVersion = version;
89             return this;
90         }
91 
92         /** Adds one {@link AppSearchSchema} to the schema list. */
93         @NonNull
addSchema(@onNull AppSearchSchema schema)94         public Builder addSchema(@NonNull AppSearchSchema schema) {
95             Objects.requireNonNull(schema);
96             resetIfBuilt();
97             mSchemaBundles.add(schema.getBundle());
98             return this;
99         }
100 
101         /** Builds a {@link GetSchemaResponse} object. */
102         @NonNull
build()103         public GetSchemaResponse build() {
104             Bundle bundle = new Bundle();
105             bundle.putInt(VERSION_FIELD, mVersion);
106             bundle.putParcelableArrayList(SCHEMAS_FIELD, mSchemaBundles);
107             mBuilt = true;
108             return new GetSchemaResponse(bundle);
109         }
110 
resetIfBuilt()111         private void resetIfBuilt() {
112             if (mBuilt) {
113                 mSchemaBundles = new ArrayList<>(mSchemaBundles);
114                 mBuilt = false;
115             }
116         }
117     }
118 }
119