package com.google.firebase.firestore; import android.app.Activity; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; import com.google.android.gms.tasks.Tasks; import com.google.firebase.firestore.Filter; import com.google.firebase.firestore.FirebaseFirestoreException; import com.google.firebase.firestore.core.ActivityScope; import com.google.firebase.firestore.core.AsyncEventListener; import com.google.firebase.firestore.core.CompositeFilter; import com.google.firebase.firestore.core.EventManager; import com.google.firebase.firestore.core.FieldFilter; import com.google.firebase.firestore.core.ListenerRegistrationImpl; import com.google.firebase.firestore.core.OrderBy; import com.google.firebase.firestore.core.Query; import com.google.firebase.firestore.core.ViewSnapshot; import com.google.firebase.firestore.model.DocumentKey; import com.google.firebase.firestore.model.ResourcePath; import com.google.firebase.firestore.model.Values; import com.google.firebase.firestore.util.Assert; import com.google.firebase.firestore.util.Executors; import com.google.firebase.firestore.util.Preconditions; import com.google.firebase.firestore.util.Util; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import o.sbb; import o.uOO; /* loaded from: classes2.dex */ public class Query { final FirebaseFirestore firestore; final com.google.firebase.firestore.core.Query query; /* loaded from: classes2.dex */ public enum Direction { ASCENDING, DESCENDING } /* JADX INFO: Access modifiers changed from: package-private */ public Query(com.google.firebase.firestore.core.Query query, FirebaseFirestore firebaseFirestore) { this.query = (com.google.firebase.firestore.core.Query) Preconditions.checkNotNull(query); this.firestore = (FirebaseFirestore) Preconditions.checkNotNull(firebaseFirestore); } public Query whereLessThan(String str, Object obj) { return where(Filter.lessThan(str, obj)); } public Query whereGreaterThanOrEqualTo(String str, Object obj) { return where(Filter.greaterThanOrEqualTo(str, obj)); } private FieldFilter parseFieldFilter(Filter.UnaryFilter unaryFilter) { sbb parseQueryValue; FieldPath field = unaryFilter.getField(); FieldFilter.Operator operator = unaryFilter.getOperator(); Object value = unaryFilter.getValue(); Preconditions.checkNotNull(field, "Provided field path must not be null."); Preconditions.checkNotNull(operator, "Provided op must not be null."); if (field.getInternalPath().isKeyField()) { if (operator == FieldFilter.Operator.ARRAY_CONTAINS || operator == FieldFilter.Operator.ARRAY_CONTAINS_ANY) { StringBuilder sb = new StringBuilder("Invalid query. You can't perform '"); sb.append(operator.toString()); sb.append("' queries on FieldPath.documentId()."); throw new IllegalArgumentException(sb.toString()); } if (operator == FieldFilter.Operator.IN || operator == FieldFilter.Operator.NOT_IN) { validateDisjunctiveFilterElements(value, operator); uOO.IeS a = uOO.a(); Iterator it = ((List) value).iterator(); while (it.hasNext()) { a.a(parseDocumentIdValue(it.next())); } parseQueryValue = sbb.d().b(a).build(); } else { parseQueryValue = parseDocumentIdValue(value); } } else { if (operator == FieldFilter.Operator.IN || operator == FieldFilter.Operator.NOT_IN || operator == FieldFilter.Operator.ARRAY_CONTAINS_ANY) { validateDisjunctiveFilterElements(value, operator); } parseQueryValue = this.firestore.getUserDataReader().parseQueryValue(value, operator == FieldFilter.Operator.IN || operator == FieldFilter.Operator.NOT_IN); } return FieldFilter.create(field.getInternalPath(), operator, parseQueryValue); } private com.google.firebase.firestore.core.Filter parseCompositeFilter(Filter.CompositeFilter compositeFilter) { ArrayList arrayList = new ArrayList(); Iterator it = compositeFilter.getFilters().iterator(); while (it.hasNext()) { com.google.firebase.firestore.core.Filter parseFilter = parseFilter(it.next()); if (!parseFilter.getFilters().isEmpty()) { arrayList.add(parseFilter); } } if (arrayList.size() == 1) { return (com.google.firebase.firestore.core.Filter) arrayList.get(0); } return new CompositeFilter(arrayList, compositeFilter.getOperator()); } private com.google.firebase.firestore.core.Filter parseFilter(Filter filter) { boolean z = filter instanceof Filter.UnaryFilter; Assert.hardAssert(z || (filter instanceof Filter.CompositeFilter), "Parsing is only supported for Filter.UnaryFilter and Filter.CompositeFilter.", new Object[0]); if (z) { return parseFieldFilter((Filter.UnaryFilter) filter); } return parseCompositeFilter((Filter.CompositeFilter) filter); } Query where(Filter filter) { com.google.firebase.firestore.core.Filter parseFilter = parseFilter(filter); if (parseFilter.getFilters().isEmpty()) { return this; } validateNewFilter(parseFilter); return new Query(this.query.filter(parseFilter), this.firestore); } private void validateOrderByField(com.google.firebase.firestore.model.FieldPath fieldPath) { com.google.firebase.firestore.model.FieldPath inequalityField = this.query.inequalityField(); if (this.query.getFirstOrderByField() != null || inequalityField == null) { return; } validateOrderByFieldMatchesInequality(fieldPath, inequalityField); } private sbb parseDocumentIdValue(Object obj) { if (obj instanceof String) { String str = (String) obj; if (str.isEmpty()) { throw new IllegalArgumentException("Invalid query. When querying with FieldPath.documentId() you must provide a valid document ID, but it was an empty string."); } if (!this.query.isCollectionGroupQuery() && str.contains("/")) { StringBuilder sb = new StringBuilder("Invalid query. When querying a collection by FieldPath.documentId() you must provide a plain document ID, but '"); sb.append(str); sb.append("' contains a '/' character."); throw new IllegalArgumentException(sb.toString()); } ResourcePath append = this.query.getPath().append(ResourcePath.fromString(str)); if (!DocumentKey.isDocumentKey(append)) { StringBuilder sb2 = new StringBuilder("Invalid query. When querying a collection group by FieldPath.documentId(), the value provided must result in a valid document path, but '"); sb2.append(append); sb2.append("' is not because it has an odd number of segments ("); sb2.append(append.length()); sb2.append(")."); throw new IllegalArgumentException(sb2.toString()); } return Values.refValue(getFirestore().getDatabaseId(), DocumentKey.fromPath(append)); } if (obj instanceof DocumentReference) { return Values.refValue(getFirestore().getDatabaseId(), ((DocumentReference) obj).getKey()); } StringBuilder sb3 = new StringBuilder("Invalid query. When querying with FieldPath.documentId() you must provide a valid String or DocumentReference, but it was of type: "); sb3.append(Util.typeName(obj)); throw new IllegalArgumentException(sb3.toString()); } private void validateDisjunctiveFilterElements(Object obj, FieldFilter.Operator operator) { if (obj instanceof List) { List list = (List) obj; if (list.size() != 0) { if (list.size() <= 10) { return; } StringBuilder sb = new StringBuilder("Invalid Query. '"); sb.append(operator.toString()); sb.append("' filters support a maximum of 10 elements in the value array."); throw new IllegalArgumentException(sb.toString()); } } StringBuilder sb2 = new StringBuilder("Invalid Query. A non-empty array is required for '"); sb2.append(operator.toString()); sb2.append("' filters."); throw new IllegalArgumentException(sb2.toString()); } private void validateOrderByFieldMatchesInequality(com.google.firebase.firestore.model.FieldPath fieldPath, com.google.firebase.firestore.model.FieldPath fieldPath2) { if (fieldPath.equals(fieldPath2)) { return; } String canonicalString = fieldPath2.canonicalString(); throw new IllegalArgumentException(String.format("Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field '%s' and so you must also have '%s' as your first orderBy() field, but your first orderBy() is currently on field '%s' instead.", canonicalString, canonicalString, fieldPath.canonicalString())); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: com.google.firebase.firestore.Query$1, reason: invalid class name */ /* loaded from: classes2.dex */ public static /* synthetic */ class AnonymousClass1 { static final int[] $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator; static { int[] iArr = new int[FieldFilter.Operator.values().length]; $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator = iArr; try { iArr[FieldFilter.Operator.NOT_EQUAL.ordinal()] = 1; } catch (NoSuchFieldError unused) { } try { $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator[FieldFilter.Operator.ARRAY_CONTAINS.ordinal()] = 2; } catch (NoSuchFieldError unused2) { } try { $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator[FieldFilter.Operator.IN.ordinal()] = 3; } catch (NoSuchFieldError unused3) { } try { $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator[FieldFilter.Operator.ARRAY_CONTAINS_ANY.ordinal()] = 4; } catch (NoSuchFieldError unused4) { } try { $SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator[FieldFilter.Operator.NOT_IN.ordinal()] = 5; } catch (NoSuchFieldError unused5) { } } } private List conflictingOps(FieldFilter.Operator operator) { int i = AnonymousClass1.$SwitchMap$com$google$firebase$firestore$core$FieldFilter$Operator[operator.ordinal()]; if (i == 1) { return Arrays.asList(FieldFilter.Operator.NOT_EQUAL, FieldFilter.Operator.NOT_IN); } if (i == 2) { return Arrays.asList(FieldFilter.Operator.ARRAY_CONTAINS, FieldFilter.Operator.ARRAY_CONTAINS_ANY, FieldFilter.Operator.NOT_IN); } if (i == 3) { return Arrays.asList(FieldFilter.Operator.ARRAY_CONTAINS_ANY, FieldFilter.Operator.IN, FieldFilter.Operator.NOT_IN); } if (i == 4) { return Arrays.asList(FieldFilter.Operator.ARRAY_CONTAINS, FieldFilter.Operator.ARRAY_CONTAINS_ANY, FieldFilter.Operator.IN, FieldFilter.Operator.NOT_IN); } if (i == 5) { return Arrays.asList(FieldFilter.Operator.ARRAY_CONTAINS, FieldFilter.Operator.ARRAY_CONTAINS_ANY, FieldFilter.Operator.IN, FieldFilter.Operator.NOT_IN, FieldFilter.Operator.NOT_EQUAL); } return new ArrayList(); } private void validateNewFieldFilter(com.google.firebase.firestore.core.Query query, FieldFilter fieldFilter) { FieldFilter.Operator operator = fieldFilter.getOperator(); if (fieldFilter.isInequality()) { com.google.firebase.firestore.model.FieldPath inequalityField = query.inequalityField(); com.google.firebase.firestore.model.FieldPath field = fieldFilter.getField(); if (inequalityField != null && !inequalityField.equals(field)) { throw new IllegalArgumentException(String.format("All where filters with an inequality (notEqualTo, notIn, lessThan, lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the same field. But you have filters on '%s' and '%s'", inequalityField.canonicalString(), field.canonicalString())); } com.google.firebase.firestore.model.FieldPath firstOrderByField = query.getFirstOrderByField(); if (firstOrderByField != null) { validateOrderByFieldMatchesInequality(firstOrderByField, field); } } FieldFilter.Operator findOpInsideFilters = findOpInsideFilters(query.getFilters(), conflictingOps(operator)); if (findOpInsideFilters != null) { if (findOpInsideFilters == operator) { StringBuilder sb = new StringBuilder("Invalid Query. You cannot use more than one '"); sb.append(operator.toString()); sb.append("' filter."); throw new IllegalArgumentException(sb.toString()); } StringBuilder sb2 = new StringBuilder("Invalid Query. You cannot use '"); sb2.append(operator.toString()); sb2.append("' filters with '"); sb2.append(findOpInsideFilters.toString()); sb2.append("' filters."); throw new IllegalArgumentException(sb2.toString()); } } private void validateNewFilter(com.google.firebase.firestore.core.Filter filter) { com.google.firebase.firestore.core.Query query = this.query; for (FieldFilter fieldFilter : filter.getFlattenedFilters()) { validateNewFieldFilter(query, fieldFilter); query = query.filter(fieldFilter); } } private FieldFilter.Operator findOpInsideFilters(List list, List list2) { Iterator it = list.iterator(); while (it.hasNext()) { for (FieldFilter fieldFilter : it.next().getFlattenedFilters()) { if (list2.contains(fieldFilter.getOperator())) { return fieldFilter.getOperator(); } } } return null; } public Query orderBy(String str) { return orderBy(FieldPath.fromDotSeparatedPath(str), Direction.ASCENDING); } public Query orderBy(String str, Direction direction) { return orderBy(FieldPath.fromDotSeparatedPath(str), direction); } public Query orderBy(FieldPath fieldPath, Direction direction) { Preconditions.checkNotNull(fieldPath, "Provided field path must not be null."); return orderBy(fieldPath.getInternalPath(), direction); } private Query orderBy(com.google.firebase.firestore.model.FieldPath fieldPath, Direction direction) { OrderBy.Direction direction2; Preconditions.checkNotNull(direction, "Provided direction must not be null."); if (this.query.getStartAt() != null) { throw new IllegalArgumentException("Invalid query. You must not call Query.startAt() or Query.startAfter() before calling Query.orderBy()."); } if (this.query.getEndAt() != null) { throw new IllegalArgumentException("Invalid query. You must not call Query.endAt() or Query.endBefore() before calling Query.orderBy()."); } validateOrderByField(fieldPath); if (direction == Direction.ASCENDING) { direction2 = OrderBy.Direction.ASCENDING; } else { direction2 = OrderBy.Direction.DESCENDING; } return new Query(this.query.orderBy(OrderBy.getInstance(direction2, fieldPath)), this.firestore); } public Query limitToLast(long j) { if (j <= 0) { StringBuilder sb = new StringBuilder("Invalid Query. Query limitToLast ("); sb.append(j); sb.append(") is invalid. Limit must be positive."); throw new IllegalArgumentException(sb.toString()); } return new Query(this.query.limitToLast(j), this.firestore); } public Task get() { return get(Source.DEFAULT); } public Task get(Source source) { validateHasExplicitOrderByForLimitToLast(); if (source == Source.CACHE) { return this.firestore.getClient().getDocumentsFromLocalCache(this.query).continueWith(Executors.DIRECT_EXECUTOR, new Continuation(this) { // from class: com.google.firebase.firestore.Query$$ExternalSyntheticLambda1 public final Query f$0; @Override // com.google.android.gms.tasks.Continuation public final Object then(Task task) { return this.f$0.m170lambda$get$0$comgooglefirebasefirestoreQuery(task); } { this.f$0 = this; } }); } return getViaSnapshotListener(source); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$get$0$com-google-firebase-firestore-Query, reason: not valid java name */ public /* synthetic */ QuerySnapshot m170lambda$get$0$comgooglefirebasefirestoreQuery(Task task) throws Exception { return new QuerySnapshot(new Query(this.query, this.firestore), (ViewSnapshot) task.getResult(), this.firestore); } private Task getViaSnapshotListener(final Source source) { final TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); final TaskCompletionSource taskCompletionSource2 = new TaskCompletionSource(); EventManager.ListenOptions listenOptions = new EventManager.ListenOptions(); listenOptions.includeDocumentMetadataChanges = true; listenOptions.includeQueryMetadataChanges = true; listenOptions.waitForSyncWhenOnline = true; taskCompletionSource2.setResult(addSnapshotListenerInternal(Executors.DIRECT_EXECUTOR, listenOptions, null, new EventListener(taskCompletionSource, taskCompletionSource2, source) { // from class: com.google.firebase.firestore.Query$$ExternalSyntheticLambda0 public final TaskCompletionSource f$0; public final TaskCompletionSource f$1; public final Source f$2; @Override // com.google.firebase.firestore.EventListener public final void onEvent(Object obj, FirebaseFirestoreException firebaseFirestoreException) { Query.lambda$getViaSnapshotListener$1(this.f$0, this.f$1, this.f$2, (QuerySnapshot) obj, firebaseFirestoreException); } { this.f$0 = taskCompletionSource; this.f$1 = taskCompletionSource2; this.f$2 = source; } })); return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: package-private */ public static /* synthetic */ void lambda$getViaSnapshotListener$1(TaskCompletionSource taskCompletionSource, TaskCompletionSource taskCompletionSource2, Source source, QuerySnapshot querySnapshot, FirebaseFirestoreException firebaseFirestoreException) { if (firebaseFirestoreException != null) { taskCompletionSource.setException(firebaseFirestoreException); return; } try { ((ListenerRegistration) Tasks.await(taskCompletionSource2.getTask())).remove(); if (querySnapshot.getMetadata().isFromCache() && source == Source.SERVER) { taskCompletionSource.setException(new FirebaseFirestoreException("Failed to get documents from server. (However, these documents may exist in the local cache. Run again without setting source to SERVER to retrieve the cached documents.)", FirebaseFirestoreException.Code.UNAVAILABLE)); } else { taskCompletionSource.setResult(querySnapshot); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw Assert.fail(e, "Failed to register a listener for a query result", new Object[0]); } catch (ExecutionException e2) { throw Assert.fail(e2, "Failed to register a listener for a query result", new Object[0]); } } public ListenerRegistration addSnapshotListener(EventListener eventListener) { return addSnapshotListener(MetadataChanges.EXCLUDE, eventListener); } public ListenerRegistration addSnapshotListener(MetadataChanges metadataChanges, EventListener eventListener) { return addSnapshotListener(Executors.DEFAULT_CALLBACK_EXECUTOR, metadataChanges, eventListener); } public ListenerRegistration addSnapshotListener(Executor executor, MetadataChanges metadataChanges, EventListener eventListener) { Preconditions.checkNotNull(executor, "Provided executor must not be null."); Preconditions.checkNotNull(metadataChanges, "Provided MetadataChanges value must not be null."); Preconditions.checkNotNull(eventListener, "Provided EventListener must not be null."); return addSnapshotListenerInternal(executor, internalOptions(metadataChanges), null, eventListener); } private ListenerRegistration addSnapshotListenerInternal(Executor executor, EventManager.ListenOptions listenOptions, Activity activity, final EventListener eventListener) { validateHasExplicitOrderByForLimitToLast(); AsyncEventListener asyncEventListener = new AsyncEventListener(executor, new EventListener(this, eventListener) { // from class: com.google.firebase.firestore.Query$$ExternalSyntheticLambda2 public final Query f$0; public final EventListener f$1; @Override // com.google.firebase.firestore.EventListener public final void onEvent(Object obj, FirebaseFirestoreException firebaseFirestoreException) { this.f$0.m169xb1c289cb(this.f$1, (ViewSnapshot) obj, firebaseFirestoreException); } { this.f$0 = this; this.f$1 = eventListener; } }); return ActivityScope.bind(activity, new ListenerRegistrationImpl(this.firestore.getClient(), this.firestore.getClient().listen(this.query, listenOptions, asyncEventListener), asyncEventListener)); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$addSnapshotListenerInternal$2$com-google-firebase-firestore-Query, reason: not valid java name */ public /* synthetic */ void m169xb1c289cb(EventListener eventListener, ViewSnapshot viewSnapshot, FirebaseFirestoreException firebaseFirestoreException) { if (firebaseFirestoreException != null) { eventListener.onEvent(null, firebaseFirestoreException); } else { Assert.hardAssert(viewSnapshot != null, "Got event without value or error set", new Object[0]); eventListener.onEvent(new QuerySnapshot(this, viewSnapshot, this.firestore), null); } } private void validateHasExplicitOrderByForLimitToLast() { if (this.query.getLimitType().equals(Query.LimitType.LIMIT_TO_LAST) && this.query.getExplicitOrderBy().isEmpty()) { throw new IllegalStateException("limitToLast() queries require specifying at least one orderBy() clause"); } } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Query)) { return false; } Query query = (Query) obj; return this.query.equals(query.query) && this.firestore.equals(query.firestore); } public int hashCode() { return (this.query.hashCode() * 31) + this.firestore.hashCode(); } private static EventManager.ListenOptions internalOptions(MetadataChanges metadataChanges) { EventManager.ListenOptions listenOptions = new EventManager.ListenOptions(); listenOptions.includeDocumentMetadataChanges = metadataChanges == MetadataChanges.INCLUDE; listenOptions.includeQueryMetadataChanges = metadataChanges == MetadataChanges.INCLUDE; listenOptions.waitForSyncWhenOnline = false; return listenOptions; } public FirebaseFirestore getFirestore() { return this.firestore; } }