package w8;

import android.os.Looper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.gms.common.api.internal.v0;
import com.google.android.gms.common.internal.w;
import e.v;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/* JADX INFO: loaded from: classes2.dex */
public abstract class k {
    public static Object a(h hVar) throws InterruptedException {
        w.e("Must not be called on the main application thread");
        Looper looperMyLooper = Looper.myLooper();
        if (looperMyLooper != null && Objects.equals(looperMyLooper.getThread().getName(), "GoogleApiHandler")) {
            throw new IllegalStateException("Must not be called on GoogleApiHandler thread.");
        }
        if (hVar == null) {
            throw new NullPointerException("Task must not be null");
        }
        if (hVar.i()) {
            return g(hVar);
        }
        l lVar = new l(0);
        v vVar = j.f34226b;
        hVar.d(vVar, lVar);
        hVar.c(vVar, lVar);
        t tVar = (t) hVar;
        tVar.f34251b.f(new q(vVar, (c) lVar));
        tVar.m();
        Object obj = lVar.f34228b;
        switch (1) {
            case 1:
                ((CountDownLatch) obj).await();
                break;
            default:
                ((t) obj).zze(null);
                break;
        }
        return g(hVar);
    }

    public static Object b(h hVar, long j10, TimeUnit timeUnit) throws TimeoutException {
        w.e("Must not be called on the main application thread");
        Looper looperMyLooper = Looper.myLooper();
        if (looperMyLooper != null && Objects.equals(looperMyLooper.getThread().getName(), "GoogleApiHandler")) {
            throw new IllegalStateException("Must not be called on GoogleApiHandler thread.");
        }
        if (hVar == null) {
            throw new NullPointerException("Task must not be null");
        }
        if (timeUnit == null) {
            throw new NullPointerException("TimeUnit must not be null");
        }
        if (hVar.i()) {
            return g(hVar);
        }
        l lVar = new l(0);
        v vVar = j.f34226b;
        hVar.d(vVar, lVar);
        hVar.c(vVar, lVar);
        t tVar = (t) hVar;
        tVar.f34251b.f(new q(vVar, (c) lVar));
        tVar.m();
        if (((CountDownLatch) lVar.f34228b).await(j10, timeUnit)) {
            return g(hVar);
        }
        throw new TimeoutException("Timed out waiting for Task");
    }

    public static t c(Callable callable, Executor executor) {
        if (executor == null) {
            throw new NullPointerException("Executor must not be null");
        }
        if (callable == null) {
            throw new NullPointerException("Callback must not be null");
        }
        t tVar = new t();
        executor.execute(new v0(tVar, callable, 8, 0));
        return tVar;
    }

    public static t d(Exception exc) {
        t tVar = new t();
        tVar.k(exc);
        return tVar;
    }

    public static t e(Object obj) {
        t tVar = new t();
        tVar.zzb(obj);
        return tVar;
    }

    public static t f(h hVar, long j10, TimeUnit timeUnit) {
        if (hVar == null) {
            throw new NullPointerException("Task must not be null");
        }
        w.b(j10 > 0, "Timeout must be positive");
        if (timeUnit == null) {
            throw new NullPointerException("TimeUnit must not be null");
        }
        l lVar = new l();
        i iVar = new i(lVar);
        o8.e eVar = new o8.e(Looper.getMainLooper(), 3);
        eVar.postDelayed(new p(iVar, 1), timeUnit.toMillis(j10));
        hVar.b(new xyz.stream.utils.t(eVar, iVar, lVar, 16));
        return iVar.f34224a;
    }

    public static Object g(h hVar) throws ExecutionException {
        if (hVar.j()) {
            return hVar.g();
        }
        if (((t) hVar).f34253d) {
            throw new CancellationException("Task is already canceled");
        }
        throw new ExecutionException(hVar.getException());
    }

    @NonNull
    public static h whenAll(@Nullable Collection<? extends h> collection) {
        if (collection == null || collection.isEmpty()) {
            return e(null);
        }
        Iterator<? extends h> it = collection.iterator();
        while (it.hasNext()) {
            if (it.next() == null) {
                throw new NullPointerException("null tasks are not accepted");
            }
        }
        t tVar = new t();
        n nVar = new n(collection.size(), tVar);
        for (h hVar : collection) {
            v vVar = j.f34226b;
            hVar.d(vVar, nVar);
            hVar.c(vVar, nVar);
            t tVar2 = (t) hVar;
            tVar2.f34251b.f(new q(vVar, (c) nVar));
            tVar2.m();
        }
        return tVar;
    }

    @NonNull
    public static h whenAllComplete(@Nullable Collection<? extends h> collection) {
        return whenAllComplete(j.f34225a, collection);
    }

    @NonNull
    public static <TResult> h whenAllSuccess(@Nullable Collection<? extends h> collection) {
        return whenAllSuccess(j.f34225a, collection);
    }

    @NonNull
    public static h whenAllComplete(@NonNull Executor executor, @Nullable Collection<? extends h> collection) {
        return (collection == null || collection.isEmpty()) ? e(Collections.emptyList()) : whenAll(collection).f(executor, new m(1, collection));
    }

    @NonNull
    public static <TResult> h whenAllSuccess(@NonNull Executor executor, @Nullable Collection<? extends h> collection) {
        return (collection == null || collection.isEmpty()) ? e(Collections.emptyList()) : whenAll(collection).e(executor, new m(0, collection));
    }

    @NonNull
    public static h whenAllComplete(@NonNull Executor executor, @Nullable h... hVarArr) {
        if (hVarArr != null && hVarArr.length != 0) {
            return whenAllComplete(executor, Arrays.asList(hVarArr));
        }
        return e(Collections.emptyList());
    }

    @NonNull
    public static <TResult> h whenAllSuccess(@NonNull Executor executor, @Nullable h... hVarArr) {
        if (hVarArr != null && hVarArr.length != 0) {
            return whenAllSuccess(executor, Arrays.asList(hVarArr));
        }
        return e(Collections.emptyList());
    }

    @NonNull
    public static h whenAllComplete(@Nullable h... hVarArr) {
        if (hVarArr != null && hVarArr.length != 0) {
            return whenAllComplete(Arrays.asList(hVarArr));
        }
        return e(Collections.emptyList());
    }

    @NonNull
    public static <TResult> h whenAllSuccess(@Nullable h... hVarArr) {
        if (hVarArr != null && hVarArr.length != 0) {
            return whenAllSuccess(Arrays.asList(hVarArr));
        }
        return e(Collections.emptyList());
    }

    @NonNull
    public static h whenAll(@Nullable h... hVarArr) {
        if (hVarArr != null && hVarArr.length != 0) {
            return whenAll(Arrays.asList(hVarArr));
        }
        return e(null);
    }
}
