package com.google.firebase.crashlytics.internal.common; import android.os.Looper; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; import java.io.File; import java.io.FilenameFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* loaded from: classes.dex */ public final class Utils { private static final FilenameFilter ALL_FILES_FILTER = new FilenameFilter() { // from class: com.google.firebase.crashlytics.internal.common.Utils.1 @Override // java.io.FilenameFilter public boolean accept(File file, String str) { return true; } }; private static final ExecutorService TASK_CONTINUATION_EXECUTOR_SERVICE = ExecutorUtils.buildSingleThreadExecutorService("awaitEvenIfOnMainThread task continuation executor"); private Utils() { } /* JADX INFO: Access modifiers changed from: package-private */ public static int capSessionCount(File file, File file2, int i, Comparator comparator) { ArrayList arrayList = new ArrayList(); File[] listFiles = file.listFiles(); File[] listFiles2 = file2.listFiles(ALL_FILES_FILTER); if (listFiles == null) { listFiles = new File[0]; } if (listFiles2 == null) { listFiles2 = new File[0]; } arrayList.addAll(Arrays.asList(listFiles)); arrayList.addAll(Arrays.asList(listFiles2)); return capFileCount(arrayList, i, comparator); } /* JADX INFO: Access modifiers changed from: package-private */ public static int capFileCount(File file, int i, Comparator comparator) { return capFileCount(file, ALL_FILES_FILTER, i, comparator); } /* JADX INFO: Access modifiers changed from: package-private */ public static int capFileCount(File file, FilenameFilter filenameFilter, int i, Comparator comparator) { File[] listFiles = file.listFiles(filenameFilter); if (listFiles == null) { return 0; } return capFileCount((List) Arrays.asList(listFiles), i, comparator); } static int capFileCount(List list, int i, Comparator comparator) { int size = list.size(); Collections.sort(list, comparator); for (File file : list) { if (size <= i) { return size; } recursiveDelete(file); size--; } return size; } public static Task race(Task task, Task task2) { TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); Continuation continuation = new Continuation(taskCompletionSource) { // from class: com.google.firebase.crashlytics.internal.common.Utils.2 final TaskCompletionSource val$result; { this.val$result = taskCompletionSource; } @Override // com.google.android.gms.tasks.Continuation public Void then(Task task3) throws Exception { if (task3.isSuccessful()) { this.val$result.trySetResult(task3.getResult()); return null; } this.val$result.trySetException(task3.getException()); return null; } }; task.continueWith(continuation); task2.continueWith(continuation); return taskCompletionSource.getTask(); } public static Task callTask(Executor executor, Callable> callable) { TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); executor.execute(new AnonymousClass3(callable, taskCompletionSource)); return taskCompletionSource.getTask(); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: com.google.firebase.crashlytics.internal.common.Utils$3, reason: invalid class name */ /* loaded from: classes2.dex */ public class AnonymousClass3 implements Runnable { final Callable val$callable; final TaskCompletionSource val$tcs; AnonymousClass3(Callable callable, TaskCompletionSource taskCompletionSource) { this.val$callable = callable; this.val$tcs = taskCompletionSource; } @Override // java.lang.Runnable public void run() { try { ((Task) this.val$callable.call()).continueWith(new Continuation(this) { // from class: com.google.firebase.crashlytics.internal.common.Utils.3.1 final AnonymousClass3 this$0; { this.this$0 = this; } @Override // com.google.android.gms.tasks.Continuation public Void then(Task task) throws Exception { if (task.isSuccessful()) { this.this$0.val$tcs.setResult(task.getResult()); return null; } this.this$0.val$tcs.setException(task.getException()); return null; } }); } catch (Exception e) { this.val$tcs.setException(e); } } } public static T awaitEvenIfOnMainThread(Task task) throws InterruptedException, TimeoutException { CountDownLatch countDownLatch = new CountDownLatch(1); task.continueWith(TASK_CONTINUATION_EXECUTOR_SERVICE, new Continuation(countDownLatch) { // from class: com.google.firebase.crashlytics.internal.common.Utils.4 final CountDownLatch val$latch; { this.val$latch = countDownLatch; } @Override // com.google.android.gms.tasks.Continuation public Object then(Task task2) throws Exception { this.val$latch.countDown(); return null; } }); if (Looper.getMainLooper() == Looper.myLooper()) { countDownLatch.await(4L, TimeUnit.SECONDS); } else { countDownLatch.await(); } if (task.isComplete()) { return task.getResult(); } throw new TimeoutException(); } private static void recursiveDelete(File file) { if (file.isDirectory()) { for (File file2 : file.listFiles()) { recursiveDelete(file2); } } file.delete(); } }