1 /* 2 * Copyright (C) 2022 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 com.android.server.appsearch.contactsindexer; 18 19 import android.annotation.NonNull; 20 import android.app.appsearch.AppSearchResult; 21 import android.app.appsearch.GenericDocument; 22 import android.app.appsearch.exceptions.AppSearchException; 23 import android.content.Context; 24 import android.util.ArrayMap; 25 26 import com.android.server.appsearch.contactsindexer.appsearchtypes.Person; 27 28 import java.util.ArrayList; 29 import java.util.Collection; 30 import java.util.List; 31 import java.util.Map; 32 import java.util.concurrent.CompletableFuture; 33 34 public final class FakeAppSearchHelper extends AppSearchHelper { 35 final int mDocLimit; 36 final int mDeleteLimit; 37 // Contacts that have been deleted during the test 38 List<String> mRemovedIds = new ArrayList<>(); 39 // Contacts that have been updated/inserted during the test 40 List<Person> mIndexedContacts = new ArrayList<>(); 41 Map<String, Person> mExistingContacts = new ArrayMap<>(); 42 FakeAppSearchHelper(@onNull Context context)43 public FakeAppSearchHelper(@NonNull Context context) { 44 this(context, Integer.MAX_VALUE, Integer.MAX_VALUE); 45 } 46 FakeAppSearchHelper(@onNull Context context, int docLimit, int deleteLimit)47 public FakeAppSearchHelper(@NonNull Context context, int docLimit, int deleteLimit) { 48 super(context, Runnable::run); 49 mDocLimit = docLimit; 50 mDeleteLimit = deleteLimit; 51 } 52 setExistingContacts(@onNull Collection<Person> contacts)53 public void setExistingContacts(@NonNull Collection<Person> contacts) { 54 for (Person contact : contacts) { 55 mExistingContacts.put(contact.getId(), contact); 56 } 57 } 58 59 @Override indexContactsAsync(@onNull Collection<Person> contacts, @NonNull ContactsUpdateStats updateStats, boolean shouldKeepUpdatingOnErrorFlagEnabled)60 public CompletableFuture<Void> indexContactsAsync(@NonNull Collection<Person> contacts, 61 @NonNull ContactsUpdateStats updateStats, 62 boolean shouldKeepUpdatingOnErrorFlagEnabled) { 63 for (Person person : contacts) { 64 if (mIndexedContacts.size() >= mDocLimit) { 65 return CompletableFuture.failedFuture( 66 new AppSearchException(AppSearchResult.RESULT_OUT_OF_SPACE, 67 "Reached document limit.")); 68 } else { 69 mExistingContacts.put(person.getId(), person); 70 mIndexedContacts.add(person); 71 } 72 } 73 return CompletableFuture.completedFuture(null); 74 } 75 76 @Override removeContactsByIdAsync(@onNull Collection<String> ids, @NonNull ContactsUpdateStats updateStats, boolean shouldKeepUpdatingOnError)77 public CompletableFuture<Void> removeContactsByIdAsync(@NonNull Collection<String> ids, 78 @NonNull ContactsUpdateStats updateStats, 79 boolean shouldKeepUpdatingOnError) { 80 mRemovedIds.addAll(ids); 81 return CompletableFuture.completedFuture(null); 82 } 83 84 @Override getContactsWithFingerprintsAsync( @onNull List<String> ids, boolean shouldKeepUpdatingOnError, @NonNull ContactsUpdateStats updateStats)85 public CompletableFuture<List<GenericDocument>> getContactsWithFingerprintsAsync( 86 @NonNull List<String> ids, boolean shouldKeepUpdatingOnError, 87 @NonNull ContactsUpdateStats updateStats) { 88 return CompletableFuture.supplyAsync(() -> { 89 List<GenericDocument> result = new ArrayList<>(); 90 for (String id : ids) { 91 result.add(mExistingContacts.get(id)); 92 } 93 return result; 94 }); 95 } 96 } 97