package i4;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.work.NetworkType;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.background.systemalarm.ConstraintProxyUpdateReceiver;
import androidx.work.impl.background.systemalarm.SystemAlarmService;
import androidx.work.u;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import n4.o;
import n4.r;
import xyz.stream.utils.t;

/* JADX INFO: loaded from: classes.dex */
public final class b implements androidx.work.impl.b {

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final /* synthetic */ int f26538d = 0;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final HashMap f26540b = new HashMap();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final Object f26541c = new Object();

    static {
        u.e("CommandHandler");
    }

    public b(Context context) {
        this.f26539a = context;
    }

    public static Intent a(Context context, String str) {
        Intent intent = new Intent(context, (Class<?>) SystemAlarmService.class);
        intent.setAction("ACTION_DELAY_MET");
        intent.putExtra("KEY_WORKSPEC_ID", str);
        return intent;
    }

    public static Intent b(Context context, String str) {
        Intent intent = new Intent(context, (Class<?>) SystemAlarmService.class);
        intent.setAction("ACTION_SCHEDULE_WORK");
        intent.putExtra("KEY_WORKSPEC_ID", str);
        return intent;
    }

    private static boolean hasKeys(@Nullable Bundle bundle, @NonNull String... strArr) {
        if (bundle == null || bundle.isEmpty()) {
            return false;
        }
        for (String str : strArr) {
            if (bundle.get(str) == null) {
                return false;
            }
        }
        return true;
    }

    public final void c(Intent intent, int i10, h hVar) {
        String action = intent.getAction();
        if ("ACTION_CONSTRAINTS_CHANGED".equals(action)) {
            u uVarC = u.c();
            String.format("Handling constraints changed %s", intent);
            uVarC.a(new Throwable[0]);
            d dVar = new d(this.f26539a, i10, hVar);
            ArrayList<o> arrayListK = ((r) hVar.f26563e.f4625c.workSpecDao()).k();
            int i11 = c.f26542a;
            Iterator it = arrayListK.iterator();
            boolean z10 = false;
            boolean z11 = false;
            boolean z12 = false;
            boolean z13 = false;
            while (it.hasNext()) {
                androidx.work.e eVar = ((o) it.next()).f30827j;
                z10 |= eVar.f4552d;
                z11 |= eVar.f4550b;
                z12 |= eVar.f4553e;
                z13 |= eVar.f4549a != NetworkType.f4506a;
                if (z10 && z11 && z12 && z13) {
                    break;
                }
            }
            int i12 = ConstraintProxyUpdateReceiver.f4570a;
            Intent intent2 = new Intent("androidx.work.impl.background.systemalarm.UpdateProxies");
            Context context = dVar.f26544a;
            intent2.setComponent(new ComponentName(context, (Class<?>) ConstraintProxyUpdateReceiver.class));
            intent2.putExtra("KEY_BATTERY_NOT_LOW_PROXY_ENABLED", z10).putExtra("KEY_BATTERY_CHARGING_PROXY_ENABLED", z11).putExtra("KEY_STORAGE_NOT_LOW_PROXY_ENABLED", z12).putExtra("KEY_NETWORK_STATE_PROXY_ENABLED", z13);
            context.sendBroadcast(intent2);
            k4.d dVar2 = dVar.f26546c;
            dVar2.d(arrayListK);
            ArrayList arrayList = new ArrayList(arrayListK.size());
            long jCurrentTimeMillis = System.currentTimeMillis();
            for (o oVar : arrayListK) {
                String str = oVar.f30818a;
                if (jCurrentTimeMillis >= oVar.a() && (!oVar.b() || dVar2.a(str))) {
                    arrayList.add(oVar);
                }
            }
            Iterator it2 = arrayList.iterator();
            while (it2.hasNext()) {
                String str2 = ((o) it2.next()).f30818a;
                Intent intentA = a(context, str2);
                u uVarC2 = u.c();
                String.format("Creating a delay_met command for workSpec with id (%s)", str2);
                int i13 = d.f26543d;
                uVarC2.a(new Throwable[0]);
                hVar.e(new androidx.browser.customtabs.f(hVar, intentA, dVar.f26545b));
            }
            dVar2.e();
            return;
        }
        if ("ACTION_RESCHEDULE".equals(action)) {
            u uVarC3 = u.c();
            String.format("Handling reschedule %s, %s", intent, Integer.valueOf(i10));
            uVarC3.a(new Throwable[0]);
            hVar.f26563e.e();
            return;
        }
        if (!hasKeys(intent.getExtras(), "KEY_WORKSPEC_ID")) {
            u uVarC4 = u.c();
            String.format("Invalid request for %s, requires %s.", action, "KEY_WORKSPEC_ID");
            uVarC4.b(new Throwable[0]);
            return;
        }
        if ("ACTION_SCHEDULE_WORK".equals(action)) {
            String string = intent.getExtras().getString("KEY_WORKSPEC_ID");
            u uVarC5 = u.c();
            String.format("Handling schedule work for %s", string);
            uVarC5.a(new Throwable[0]);
            WorkDatabase workDatabase = hVar.f26563e.f4625c;
            workDatabase.beginTransaction();
            try {
                o oVarP = ((r) workDatabase.workSpecDao()).p(string);
                if (oVarP == null) {
                    u.c().f(new Throwable[0]);
                } else if (oVarP.f30819b.a()) {
                    u.c().f(new Throwable[0]);
                } else {
                    long jA = oVarP.a();
                    boolean zB = oVarP.b();
                    Context context2 = this.f26539a;
                    androidx.work.impl.o oVar2 = hVar.f26563e;
                    if (zB) {
                        u uVarC6 = u.c();
                        String.format("Opportunistically setting an alarm for %s at %s", string, Long.valueOf(jA));
                        uVarC6.a(new Throwable[0]);
                        a.b(context2, oVar2, string, jA);
                        Intent intent3 = new Intent(context2, (Class<?>) SystemAlarmService.class);
                        intent3.setAction("ACTION_CONSTRAINTS_CHANGED");
                        hVar.e(new androidx.browser.customtabs.f(hVar, intent3, i10));
                    } else {
                        u uVarC7 = u.c();
                        String.format("Setting up Alarms for %s at %s", string, Long.valueOf(jA));
                        uVarC7.a(new Throwable[0]);
                        a.b(context2, oVar2, string, jA);
                    }
                    workDatabase.setTransactionSuccessful();
                }
                workDatabase.endTransaction();
                return;
            } catch (Throwable th) {
                workDatabase.endTransaction();
                throw th;
            }
        }
        if ("ACTION_DELAY_MET".equals(action)) {
            Bundle extras = intent.getExtras();
            synchronized (this.f26541c) {
                try {
                    String string2 = extras.getString("KEY_WORKSPEC_ID");
                    u uVarC8 = u.c();
                    String.format("Handing delay met for %s", string2);
                    uVarC8.a(new Throwable[0]);
                    if (this.f26540b.containsKey(string2)) {
                        u uVarC9 = u.c();
                        String.format("WorkSpec %s is already being handled for ACTION_DELAY_MET", string2);
                        uVarC9.a(new Throwable[0]);
                    } else {
                        e eVar2 = new e(this.f26539a, i10, string2, hVar);
                        this.f26540b.put(string2, eVar2);
                        eVar2.b();
                    }
                } finally {
                }
            }
            return;
        }
        if (!"ACTION_STOP_WORK".equals(action)) {
            if (!"ACTION_EXECUTION_COMPLETED".equals(action)) {
                u uVarC10 = u.c();
                String.format("Ignoring intent %s", intent);
                uVarC10.f(new Throwable[0]);
                return;
            }
            Bundle extras2 = intent.getExtras();
            String string3 = extras2.getString("KEY_WORKSPEC_ID");
            boolean z14 = extras2.getBoolean("KEY_NEEDS_RESCHEDULE");
            u uVarC11 = u.c();
            String.format("Handling onExecutionCompleted %s, %s", intent, Integer.valueOf(i10));
            uVarC11.a(new Throwable[0]);
            onExecuted(string3, z14);
            return;
        }
        String string4 = intent.getExtras().getString("KEY_WORKSPEC_ID");
        u uVarC12 = u.c();
        String.format("Handing stopWork work for %s", string4);
        uVarC12.a(new Throwable[0]);
        hVar.f26563e.f(string4);
        int i14 = a.f26537a;
        t tVar = (t) hVar.f26563e.f4625c.systemIdInfoDao();
        n4.g systemIdInfo = tVar.getSystemIdInfo(string4);
        if (systemIdInfo != null) {
            a.a(this.f26539a, systemIdInfo.f30809b, string4);
            u uVarC13 = u.c();
            String.format("Removing SystemIdInfo for workSpecId (%s)", string4);
            uVarC13.a(new Throwable[0]);
            tVar.v(string4);
        }
        hVar.onExecuted(string4, false);
    }

    @Override // androidx.work.impl.b
    public final void onExecuted(String str, boolean z10) {
        synchronized (this.f26541c) {
            try {
                androidx.work.impl.b bVar = (androidx.work.impl.b) this.f26540b.remove(str);
                if (bVar != null) {
                    bVar.onExecuted(str, z10);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
