package com.google.firebase.appcheck.playintegrity.internal;

import com.google.android.gms.tasks.SuccessContinuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.appcheck.AppCheckProvider;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.internal.AppCheckTokenResponse;
import com.google.firebase.appcheck.internal.DefaultAppCheckToken;
import com.google.firebase.appcheck.internal.NetworkClient;
import com.google.firebase.appcheck.internal.RetryManager;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;

/* loaded from: classes3.dex */
public class PlayIntegrityAppCheckProvider implements AppCheckProvider {
    private static final String UTF_8 = "UTF-8";
    private final Executor blockingExecutor;
    private final wj.a integrityManager;
    private final Executor liteExecutor;
    private final NetworkClient networkClient;
    private final String projectNumber;
    private final RetryManager retryManager;

    public PlayIntegrityAppCheckProvider(FirebaseApp firebaseApp, @Lightweight Executor executor, @Blocking Executor executor2) {
        this(firebaseApp.getOptions().getGcmSenderId(), wj.b.a(firebaseApp.getApplicationContext()), new NetworkClient(firebaseApp), executor, executor2, new RetryManager());
    }

    private Task<wj.e> getPlayIntegrityAttestation() {
        final GeneratePlayIntegrityChallengeRequest generatePlayIntegrityChallengeRequest = new GeneratePlayIntegrityChallengeRequest();
        return Tasks.call(this.blockingExecutor, new Callable() { // from class: com.google.firebase.appcheck.playintegrity.internal.a
            @Override // java.util.concurrent.Callable
            public final Object call() {
                GeneratePlayIntegrityChallengeResponse lambda$getPlayIntegrityAttestation$3;
                lambda$getPlayIntegrityAttestation$3 = PlayIntegrityAppCheckProvider.this.lambda$getPlayIntegrityAttestation$3(generatePlayIntegrityChallengeRequest);
                return lambda$getPlayIntegrityAttestation$3;
            }
        }).onSuccessTask(this.liteExecutor, new SuccessContinuation() { // from class: com.google.firebase.appcheck.playintegrity.internal.b
            @Override // com.google.android.gms.tasks.SuccessContinuation
            public final Task then(Object obj) {
                Task lambda$getPlayIntegrityAttestation$4;
                lambda$getPlayIntegrityAttestation$4 = PlayIntegrityAppCheckProvider.this.lambda$getPlayIntegrityAttestation$4((GeneratePlayIntegrityChallengeResponse) obj);
                return lambda$getPlayIntegrityAttestation$4;
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ GeneratePlayIntegrityChallengeResponse lambda$getPlayIntegrityAttestation$3(GeneratePlayIntegrityChallengeRequest generatePlayIntegrityChallengeRequest) throws Exception {
        return GeneratePlayIntegrityChallengeResponse.fromJsonString(this.networkClient.generatePlayIntegrityChallenge(generatePlayIntegrityChallengeRequest.toJsonString().getBytes(UTF_8), this.retryManager));
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ Task lambda$getPlayIntegrityAttestation$4(GeneratePlayIntegrityChallengeResponse generatePlayIntegrityChallengeResponse) throws Exception {
        return this.integrityManager.a(wj.d.b().b(Long.parseLong(this.projectNumber)).c(generatePlayIntegrityChallengeResponse.getChallenge()).a());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ AppCheckTokenResponse lambda$getToken$0(ExchangePlayIntegrityTokenRequest exchangePlayIntegrityTokenRequest) throws Exception {
        return this.networkClient.exchangeAttestationForAppCheckToken(exchangePlayIntegrityTokenRequest.toJsonString().getBytes(UTF_8), 3, this.retryManager);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ Task lambda$getToken$1(wj.e eVar) throws Exception {
        final ExchangePlayIntegrityTokenRequest exchangePlayIntegrityTokenRequest = new ExchangePlayIntegrityTokenRequest(eVar.a());
        return Tasks.call(this.blockingExecutor, new Callable() { // from class: com.google.firebase.appcheck.playintegrity.internal.c
            @Override // java.util.concurrent.Callable
            public final Object call() {
                AppCheckTokenResponse lambda$getToken$0;
                lambda$getToken$0 = PlayIntegrityAppCheckProvider.this.lambda$getToken$0(exchangePlayIntegrityTokenRequest);
                return lambda$getToken$0;
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ Task lambda$getToken$2(AppCheckTokenResponse appCheckTokenResponse) throws Exception {
        return Tasks.forResult(DefaultAppCheckToken.constructFromAppCheckTokenResponse(appCheckTokenResponse));
    }

    @Override // com.google.firebase.appcheck.AppCheckProvider
    public Task<AppCheckToken> getToken() {
        return getPlayIntegrityAttestation().onSuccessTask(this.liteExecutor, new SuccessContinuation() { // from class: com.google.firebase.appcheck.playintegrity.internal.d
            @Override // com.google.android.gms.tasks.SuccessContinuation
            public final Task then(Object obj) {
                Task lambda$getToken$1;
                lambda$getToken$1 = PlayIntegrityAppCheckProvider.this.lambda$getToken$1((wj.e) obj);
                return lambda$getToken$1;
            }
        }).onSuccessTask(this.liteExecutor, new SuccessContinuation() { // from class: com.google.firebase.appcheck.playintegrity.internal.e
            @Override // com.google.android.gms.tasks.SuccessContinuation
            public final Task then(Object obj) {
                Task lambda$getToken$2;
                lambda$getToken$2 = PlayIntegrityAppCheckProvider.lambda$getToken$2((AppCheckTokenResponse) obj);
                return lambda$getToken$2;
            }
        });
    }

    PlayIntegrityAppCheckProvider(String str, wj.a aVar, NetworkClient networkClient, Executor executor, Executor executor2, RetryManager retryManager) {
        this.projectNumber = str;
        this.integrityManager = aVar;
        this.networkClient = networkClient;
        this.liteExecutor = executor;
        this.blockingExecutor = executor2;
        this.retryManager = retryManager;
    }
}
