package com.google.firebase.appcheck.internal;

import android.app.Application;
import android.content.Context;
import com.google.android.gms.common.api.internal.BackgroundDetector;
import com.google.android.gms.common.internal.Preconditions;
import com.google.firebase.annotations.concurrent.Blocking;
import com.google.firebase.annotations.concurrent.Lightweight;
import com.google.firebase.appcheck.AppCheckToken;
import com.google.firebase.appcheck.internal.util.Clock;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService;

/* loaded from: classes3.dex */
public final class TokenRefreshManager {
    private static final long FIVE_MINUTES_IN_MILLIS = 300000;
    private static final long REFRESH_BUFFER_ABSOLUTE_MILLIS = 60000;
    private static final double REFRESH_BUFFER_FRACTION = 0.5d;
    private static final long UNSET_REFRESH_TIME = -1;
    private final Clock clock;
    private volatile int currentListenerCount;
    private volatile boolean isAutoRefreshEnabled;
    private volatile boolean isBackgrounded;
    private volatile long nextRefreshTimeMillis;
    private final DefaultTokenRefresher tokenRefresher;

    /* JADX INFO: Access modifiers changed from: package-private */
    public TokenRefreshManager(Context context, DefaultFirebaseAppCheck defaultFirebaseAppCheck, @Lightweight Executor executor, @Blocking ScheduledExecutorService scheduledExecutorService) {
        this((Context) Preconditions.checkNotNull(context), new DefaultTokenRefresher((DefaultFirebaseAppCheck) Preconditions.checkNotNull(defaultFirebaseAppCheck), executor, scheduledExecutorService), new Clock.DefaultClock());
    }

    /* JADX INFO: Access modifiers changed from: private */
    public boolean shouldScheduleRefresh() {
        return this.isAutoRefreshEnabled && !this.isBackgrounded && this.currentListenerCount > 0 && this.nextRefreshTimeMillis != -1;
    }

    public void maybeScheduleTokenRefresh(AppCheckToken appCheckToken) {
        DefaultAppCheckToken constructFromRawToken = appCheckToken instanceof DefaultAppCheckToken ? (DefaultAppCheckToken) appCheckToken : DefaultAppCheckToken.constructFromRawToken(appCheckToken.getToken());
        this.nextRefreshTimeMillis = constructFromRawToken.getReceivedAtTimestamp() + ((long) (constructFromRawToken.getExpiresInMillis() * REFRESH_BUFFER_FRACTION)) + FIVE_MINUTES_IN_MILLIS;
        if (this.nextRefreshTimeMillis > constructFromRawToken.getExpireTimeMillis()) {
            this.nextRefreshTimeMillis = constructFromRawToken.getExpireTimeMillis() - REFRESH_BUFFER_ABSOLUTE_MILLIS;
        }
        if (shouldScheduleRefresh()) {
            this.tokenRefresher.scheduleRefresh(this.nextRefreshTimeMillis - this.clock.currentTimeMillis());
        }
    }

    public void onListenerCountChanged(int i10) {
        if (this.currentListenerCount == 0 && i10 > 0) {
            this.currentListenerCount = i10;
            if (shouldScheduleRefresh()) {
                this.tokenRefresher.scheduleRefresh(this.nextRefreshTimeMillis - this.clock.currentTimeMillis());
            }
        } else if (this.currentListenerCount > 0 && i10 == 0) {
            this.tokenRefresher.cancel();
        }
        this.currentListenerCount = i10;
    }

    public void setIsAutoRefreshEnabled(boolean z10) {
        this.isAutoRefreshEnabled = z10;
    }

    TokenRefreshManager(Context context, final DefaultTokenRefresher defaultTokenRefresher, final Clock clock) {
        this.tokenRefresher = defaultTokenRefresher;
        this.clock = clock;
        this.nextRefreshTimeMillis = -1L;
        BackgroundDetector.initialize((Application) context.getApplicationContext());
        BackgroundDetector.getInstance().addListener(new BackgroundDetector.BackgroundStateChangeListener() { // from class: com.google.firebase.appcheck.internal.TokenRefreshManager.1
            @Override // com.google.android.gms.common.api.internal.BackgroundDetector.BackgroundStateChangeListener
            public void onBackgroundStateChanged(boolean z10) {
                TokenRefreshManager.this.isBackgrounded = z10;
                if (z10) {
                    defaultTokenRefresher.cancel();
                } else if (TokenRefreshManager.this.shouldScheduleRefresh()) {
                    defaultTokenRefresher.scheduleRefresh(TokenRefreshManager.this.nextRefreshTimeMillis - clock.currentTimeMillis());
                }
            }
        });
    }
}
