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