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.testutil;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.app.appsearch.AppSearchSchema;
22 import android.app.appsearch.AppSearchSchema.PropertyConfig;
23 import android.app.appsearch.AppSearchSchema.StringPropertyConfig;
24 import android.app.appsearch.GenericDocument;
25 import android.app.appsearch.annotation.CanIgnoreReturnValue;
26 
27 /**
28  * Encapsulates a {@link GenericDocument} that represent an email.
29  *
30  * <p>This class is a higher level implement of {@link GenericDocument}.
31  *
32  * @hide
33  */
34 public class AppSearchEmail extends GenericDocument {
35     /** The name of the schema type for {@link AppSearchEmail} documents. */
36     public static final String SCHEMA_TYPE = "builtin:Email";
37 
38     private static final String KEY_FROM = "from";
39     private static final String KEY_TO = "to";
40     private static final String KEY_CC = "cc";
41     private static final String KEY_BCC = "bcc";
42     private static final String KEY_SUBJECT = "subject";
43     private static final String KEY_BODY = "body";
44 
45     public static final AppSearchSchema SCHEMA =
46             new AppSearchSchema.Builder(SCHEMA_TYPE)
47                     .addProperty(
48                             new StringPropertyConfig.Builder(KEY_FROM)
49                                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
50                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
51                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
52                                     .build())
53                     .addProperty(
54                             new StringPropertyConfig.Builder(KEY_TO)
55                                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
56                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
57                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
58                                     .build())
59                     .addProperty(
60                             new StringPropertyConfig.Builder(KEY_CC)
61                                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
62                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
63                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
64                                     .build())
65                     .addProperty(
66                             new StringPropertyConfig.Builder(KEY_BCC)
67                                     .setCardinality(PropertyConfig.CARDINALITY_REPEATED)
68                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
69                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
70                                     .build())
71                     .addProperty(
72                             new StringPropertyConfig.Builder(KEY_SUBJECT)
73                                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
74                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
75                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
76                                     .build())
77                     .addProperty(
78                             new StringPropertyConfig.Builder(KEY_BODY)
79                                     .setCardinality(PropertyConfig.CARDINALITY_OPTIONAL)
80                                     .setTokenizerType(StringPropertyConfig.TOKENIZER_TYPE_PLAIN)
81                                     .setIndexingType(StringPropertyConfig.INDEXING_TYPE_PREFIXES)
82                                     .build())
83                     .build();
84 
85     /**
86      * Creates a new {@link AppSearchEmail} from the contents of an existing {@link
87      * GenericDocument}.
88      *
89      * @param document The {@link GenericDocument} containing the email content.
90      */
AppSearchEmail(@onNull GenericDocument document)91     public AppSearchEmail(@NonNull GenericDocument document) {
92         super(document);
93     }
94 
95     /**
96      * Gets the from address of {@link AppSearchEmail}.
97      *
98      * @return The subject of {@link AppSearchEmail} or {@code null} if it's not been set yet.
99      */
100     @Nullable
getFrom()101     public String getFrom() {
102         return getPropertyString(KEY_FROM);
103     }
104 
105     /**
106      * Gets the destination addresses of {@link AppSearchEmail}.
107      *
108      * @return The destination addresses of {@link AppSearchEmail} or {@code null} if it's not been
109      *     set yet.
110      */
111     @Nullable
getTo()112     public String[] getTo() {
113         return getPropertyStringArray(KEY_TO);
114     }
115 
116     /**
117      * Gets the CC list of {@link AppSearchEmail}.
118      *
119      * @return The CC list of {@link AppSearchEmail} or {@code null} if it's not been set yet.
120      */
121     @Nullable
getCc()122     public String[] getCc() {
123         return getPropertyStringArray(KEY_CC);
124     }
125 
126     /**
127      * Gets the BCC list of {@link AppSearchEmail}.
128      *
129      * @return The BCC list of {@link AppSearchEmail} or {@code null} if it's not been set yet.
130      */
131     @Nullable
getBcc()132     public String[] getBcc() {
133         return getPropertyStringArray(KEY_BCC);
134     }
135 
136     /**
137      * Gets the subject of {@link AppSearchEmail}.
138      *
139      * @return The value subject of {@link AppSearchEmail} or {@code null} if it's not been set yet.
140      */
141     @Nullable
getSubject()142     public String getSubject() {
143         return getPropertyString(KEY_SUBJECT);
144     }
145 
146     /**
147      * Gets the body of {@link AppSearchEmail}.
148      *
149      * @return The body of {@link AppSearchEmail} or {@code null} if it's not been set yet.
150      */
151     @Nullable
getBody()152     public String getBody() {
153         return getPropertyString(KEY_BODY);
154     }
155 
156     /** The builder class for {@link AppSearchEmail}. */
157     public static class Builder extends GenericDocument.Builder<Builder> {
158         /**
159          * Creates a new {@link Builder}
160          *
161          * @param namespace The namespace of the Email.
162          * @param id The ID of the Email.
163          */
Builder(@onNull String namespace, @NonNull String id)164         public Builder(@NonNull String namespace, @NonNull String id) {
165             super(namespace, id, SCHEMA_TYPE);
166         }
167 
168         /** Sets the from address of {@link AppSearchEmail} */
169         @CanIgnoreReturnValue
170         @NonNull
setFrom(@onNull String from)171         public Builder setFrom(@NonNull String from) {
172             return setPropertyString(KEY_FROM, from);
173         }
174 
175         /** Sets the destination address of {@link AppSearchEmail} */
176         @CanIgnoreReturnValue
177         @NonNull
setTo(@onNull String... to)178         public Builder setTo(@NonNull String... to) {
179             return setPropertyString(KEY_TO, to);
180         }
181 
182         /** Sets the CC list of {@link AppSearchEmail} */
183         @CanIgnoreReturnValue
184         @NonNull
setCc(@onNull String... cc)185         public Builder setCc(@NonNull String... cc) {
186             return setPropertyString(KEY_CC, cc);
187         }
188 
189         /** Sets the BCC list of {@link AppSearchEmail} */
190         @CanIgnoreReturnValue
191         @NonNull
setBcc(@onNull String... bcc)192         public Builder setBcc(@NonNull String... bcc) {
193             return setPropertyString(KEY_BCC, bcc);
194         }
195 
196         /** Sets the subject of {@link AppSearchEmail} */
197         @CanIgnoreReturnValue
198         @NonNull
setSubject(@onNull String subject)199         public Builder setSubject(@NonNull String subject) {
200             return setPropertyString(KEY_SUBJECT, subject);
201         }
202 
203         /** Sets the body of {@link AppSearchEmail} */
204         @CanIgnoreReturnValue
205         @NonNull
setBody(@onNull String body)206         public Builder setBody(@NonNull String body) {
207             return setPropertyString(KEY_BODY, body);
208         }
209 
210         /** Builds the {@link AppSearchEmail} object. */
211         @NonNull
212         @Override
build()213         public AppSearchEmail build() {
214             return new AppSearchEmail(super.build());
215         }
216     }
217 }
218