package x3;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.PersistableBundle;
import android.util.Base64;
import android.util.Log;
import com.google.android.datatransport.runtime.scheduling.jobscheduling.JobInfoSchedulerService;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
import java.util.zip.Adler32;
import m3.AbstractC2478m;
import r3.j;
import y3.C3270g;
import y3.InterfaceC3267d;

/* JADX INFO: renamed from: x3.c, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class C3218c {

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final C3216a f26009c;

    public C3218c(Context context, InterfaceC3267d interfaceC3267d, C3216a c3216a) {
        this.f26007a = context;
        this.f26008b = interfaceC3267d;
        this.f26009c = c3216a;
    }

    public final void a(j jVar, int i6, boolean z6) {
        Context context = this.f26007a;
        ComponentName componentName = new ComponentName(context, (Class<?>) JobInfoSchedulerService.class);
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
        Adler32 adler32 = new Adler32();
        adler32.update(context.getPackageName().getBytes(Charset.forName("UTF-8")));
        adler32.update(jVar.f23972a.getBytes(Charset.forName("UTF-8")));
        ByteBuffer byteBufferAllocate = ByteBuffer.allocate(4);
        o3.c cVar = jVar.f23974c;
        adler32.update(byteBufferAllocate.putInt(B3.a.a(cVar)).array());
        byte[] bArr = jVar.f23973b;
        if (bArr != null) {
            adler32.update(bArr);
        }
        int value = (int) adler32.getValue();
        if (!z6) {
            Iterator<JobInfo> it = jobScheduler.getAllPendingJobs().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                JobInfo next = it.next();
                int i7 = next.getExtras().getInt("attemptNumber");
                if (next.getId() == value) {
                    if (i7 >= i6) {
                        AbstractC2478m.g("JobInfoScheduler", "Upload for context %s is already scheduled. Returning...", jVar);
                        return;
                    }
                }
            }
        }
        SQLiteDatabase sQLiteDatabaseA = ((C3270g) this.f26008b).a();
        String strValueOf = String.valueOf(B3.a.a(cVar));
        String str = jVar.f23972a;
        Cursor cursorRawQuery = sQLiteDatabaseA.rawQuery("SELECT next_request_ms FROM transport_contexts WHERE backend_name = ? and priority = ?", new String[]{str, strValueOf});
        try {
            Long lValueOf = cursorRawQuery.moveToNext() ? Long.valueOf(cursorRawQuery.getLong(0)) : 0L;
            cursorRawQuery.close();
            long jLongValue = lValueOf.longValue();
            JobInfo.Builder builder = new JobInfo.Builder(value, componentName);
            C3216a c3216a = this.f26009c;
            builder.setMinimumLatency(c3216a.a(cVar, jLongValue, i6));
            Set set = ((C3217b) c3216a.f26003b.get(cVar)).f26006c;
            if (set.contains(EnumC3219d.f26010a)) {
                builder.setRequiredNetworkType(2);
            } else {
                builder.setRequiredNetworkType(1);
            }
            if (set.contains(EnumC3219d.f26012c)) {
                builder.setRequiresCharging(true);
            }
            if (set.contains(EnumC3219d.f26011b)) {
                builder.setRequiresDeviceIdle(true);
            }
            PersistableBundle persistableBundle = new PersistableBundle();
            persistableBundle.putInt("attemptNumber", i6);
            persistableBundle.putString("backendName", str);
            persistableBundle.putInt("priority", B3.a.a(cVar));
            if (bArr != null) {
                persistableBundle.putString("extras", Base64.encodeToString(bArr, 0));
            }
            builder.setExtras(persistableBundle);
            Object[] objArr = {jVar, Integer.valueOf(value), Long.valueOf(c3216a.a(cVar, jLongValue, i6)), lValueOf, Integer.valueOf(i6)};
            String strL = AbstractC2478m.l("JobInfoScheduler");
            if (Log.isLoggable(strL, 3)) {
                Log.d(strL, String.format("Scheduling upload for context %s with jobId=%d in %dms(Backend next call timestamp %d). Attempt %d", objArr));
            }
            jobScheduler.schedule(builder.build());
        } catch (Throwable th) {
            cursorRawQuery.close();
            throw th;
        }
    }
}
