package pi;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.PersistableBundle;
import android.util.Base64;
import com.google.android.datatransport.runtime.scheduling.jobscheduling.JobInfoSchedulerService;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.zip.Adler32;

/* loaded from: classes.dex */
public class d implements x {

    /* renamed from: a, reason: collision with root package name */
    private final Context f32760a;

    /* renamed from: b, reason: collision with root package name */
    private final qi.d f32761b;

    /* renamed from: c, reason: collision with root package name */
    private final f f32762c;

    public d(Context context, qi.d dVar, f fVar) {
        this.f32760a = context;
        this.f32761b = dVar;
        this.f32762c = fVar;
    }

    private boolean d(JobScheduler jobScheduler, int i10, int i11) {
        for (JobInfo jobInfo : jobScheduler.getAllPendingJobs()) {
            int i12 = jobInfo.getExtras().getInt("attemptNumber");
            if (jobInfo.getId() == i10) {
                return i12 >= i11;
            }
        }
        return false;
    }

    @Override // pi.x
    public void a(ii.p pVar, int i10) {
        b(pVar, i10, false);
    }

    @Override // pi.x
    public void b(ii.p pVar, int i10, boolean z10) {
        ComponentName componentName = new ComponentName(this.f32760a, (Class<?>) JobInfoSchedulerService.class);
        JobScheduler jobScheduler = (JobScheduler) this.f32760a.getSystemService("jobscheduler");
        int c10 = c(pVar);
        if (!z10 && d(jobScheduler, c10, i10)) {
            mi.a.a("JobInfoScheduler", "Upload for context %s is already scheduled. Returning...", pVar);
            return;
        }
        long I0 = this.f32761b.I0(pVar);
        JobInfo.Builder c11 = this.f32762c.c(new JobInfo.Builder(c10, componentName), pVar.d(), I0, i10);
        PersistableBundle persistableBundle = new PersistableBundle();
        persistableBundle.putInt("attemptNumber", i10);
        persistableBundle.putString("backendName", pVar.b());
        persistableBundle.putInt("priority", ti.a.a(pVar.d()));
        if (pVar.c() != null) {
            persistableBundle.putString("extras", Base64.encodeToString(pVar.c(), 0));
        }
        c11.setExtras(persistableBundle);
        mi.a.b("JobInfoScheduler", "Scheduling upload for context %s with jobId=%d in %dms(Backend next call timestamp %d). Attempt %d", pVar, Integer.valueOf(c10), Long.valueOf(this.f32762c.g(pVar.d(), I0, i10)), Long.valueOf(I0), Integer.valueOf(i10));
        jobScheduler.schedule(c11.build());
    }

    int c(ii.p pVar) {
        Adler32 adler32 = new Adler32();
        adler32.update(this.f32760a.getPackageName().getBytes(Charset.forName("UTF-8")));
        adler32.update(pVar.b().getBytes(Charset.forName("UTF-8")));
        adler32.update(ByteBuffer.allocate(4).putInt(ti.a.a(pVar.d())).array());
        if (pVar.c() != null) {
            adler32.update(pVar.c());
        }
        return (int) adler32.getValue();
    }
}
