package com.google.android.gms.internal.appset;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import com.google.android.gms.common.api.internal.i0;
import com.google.android.gms.common.api.internal.v0;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import w8.h;
import w8.i;
import z7.b;

/* JADX INFO: loaded from: classes2.dex */
public final class a implements z7.a {

    @Nullable
    @GuardedBy("InternalAppSetAppSideClientImpl.class")
    private static z7.a zza;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f19900a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final ExecutorService f19901b;

    public a(Context context) {
        ScheduledExecutorService scheduledExecutorServiceNewSingleThreadScheduledExecutor = Executors.newSingleThreadScheduledExecutor();
        this.f19901b = Executors.newSingleThreadExecutor();
        this.f19900a = context;
        scheduledExecutorServiceNewSingleThreadScheduledExecutor.scheduleAtFixedRate(new i0(this), 0L, 86400L, TimeUnit.SECONDS);
    }

    public static synchronized z7.a b(Context context) {
        try {
            if (context == null) {
                throw new NullPointerException("Context must not be null");
            }
            if (zza == null) {
                zza = new a(context.getApplicationContext());
            }
        } catch (Throwable th) {
            throw th;
        }
        return zza;
    }

    public static final void d(Context context) throws zzk {
        SharedPreferences sharedPreferences = context.getSharedPreferences("app_set_id_storage", 0);
        if (sharedPreferences.edit().putLong("app_set_id_last_used_time", System.currentTimeMillis()).commit()) {
            return;
        }
        String strValueOf = String.valueOf(context.getPackageName());
        if (strValueOf.length() != 0) {
            "Failed to store app set ID last used time for App ".concat(strValueOf);
        }
        throw new zzk("Failed to store the app set ID last used time.");
    }

    @Override // z7.a
    public final h a() {
        i iVar = new i();
        this.f19901b.execute(new v0(this, iVar, 1, 0));
        return iVar.f34224a;
    }

    public final void c(i iVar) {
        Context context = this.f19900a;
        String string = context.getSharedPreferences("app_set_id_storage", 0).getString("app_set_id", null);
        long j10 = context.getSharedPreferences("app_set_id_storage", 0).getLong("app_set_id_last_used_time", -1L);
        long j11 = j10 != -1 ? 33696000000L + j10 : -1L;
        if (string == null || System.currentTimeMillis() > j11) {
            string = UUID.randomUUID().toString();
            try {
                if (!context.getSharedPreferences("app_set_id_storage", 0).edit().putString("app_set_id", string).commit()) {
                    String strValueOf = String.valueOf(context.getPackageName());
                    if (strValueOf.length() != 0) {
                        "Failed to store app set ID generated for App ".concat(strValueOf);
                    }
                    throw new zzk("Failed to store the app set ID.");
                }
                d(context);
                if (!context.getSharedPreferences("app_set_id_storage", 0).edit().putLong("app_set_id_creation_time", System.currentTimeMillis()).commit()) {
                    String strValueOf2 = String.valueOf(context.getPackageName());
                    if (strValueOf2.length() != 0) {
                        "Failed to store app set ID creation time for App ".concat(strValueOf2);
                    }
                    throw new zzk("Failed to store the app set ID creation time.");
                }
            } catch (zzk e10) {
                iVar.a(e10);
                return;
            }
        } else {
            try {
                d(context);
            } catch (zzk e11) {
                iVar.a(e11);
                return;
            }
        }
        iVar.setResult(new b(string, 1));
    }
}
