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