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 import android.util.Pair; 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 List<String> mRemovedIds = new ArrayList<>(); 39 // Contacts 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, new TestContactsIndexerConfig()); 49 mDocLimit = docLimit; 50 mDeleteLimit = deleteLimit; 51 } 52 clear()53 void clear() { 54 mRemovedIds.clear(); 55 mIndexedContacts.clear(); 56 mExistingContacts.clear(); 57 } 58 setExistingContacts(@onNull Collection<Person> contacts)59 public void setExistingContacts(@NonNull Collection<Person> contacts) { 60 for (Person contact : contacts) { 61 mExistingContacts.put(contact.getId(), contact); 62 } 63 } 64 65 @Override indexContactsAsync(@onNull Collection<Person> contacts, @NonNull ContactsUpdateStats updateStats)66 public CompletableFuture<Void> indexContactsAsync(@NonNull Collection<Person> contacts, 67 @NonNull ContactsUpdateStats updateStats) { 68 CompletableFuture<Void> future = new CompletableFuture<>(); 69 for (Person person : contacts) { 70 if (mIndexedContacts.size() >= mDocLimit) { 71 future.completeExceptionally(new AppSearchException( 72 AppSearchResult.RESULT_OUT_OF_SPACE, "Reached document limit.")); 73 return future; 74 } else { 75 mExistingContacts.put(person.getId(), person); 76 mIndexedContacts.add(person); 77 } 78 } 79 future.complete(null); 80 return future; 81 } 82 83 @Override removeContactsByIdAsync(@onNull Collection<String> ids, @NonNull ContactsUpdateStats updateStats)84 public CompletableFuture<Void> removeContactsByIdAsync(@NonNull Collection<String> ids, 85 @NonNull ContactsUpdateStats updateStats) { 86 mRemovedIds.addAll(ids); 87 CompletableFuture<Void> future = new CompletableFuture<>(); 88 future.complete(null); 89 return future; 90 } 91 92 @Override getContactsWithFingerprintsAsync( @onNull List<String> ids)93 public CompletableFuture<List<GenericDocument>> getContactsWithFingerprintsAsync( 94 @NonNull List<String> ids) { 95 return CompletableFuture.supplyAsync(() -> { 96 List<GenericDocument> result = new ArrayList<>(); 97 for (String id : ids) { 98 result.add(mExistingContacts.get(id)); 99 } 100 return result; 101 }); 102 } 103 } 104