package o;

import androidx.work.impl.WorkDatabase;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.logging.Logger;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.rq, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final /* synthetic */ class RunnableC2373rq implements Runnable {
    public final /* synthetic */ int l;
    public final /* synthetic */ Object m;
    public final /* synthetic */ Object n;

    /* renamed from: o, reason: collision with root package name */
    public final /* synthetic */ Object f706o;
    public final /* synthetic */ Object p;

    public /* synthetic */ RunnableC2373rq(Object obj, Object obj2, Object obj3, Object obj4, int i) {
        this.l = i;
        this.m = obj;
        this.n = obj2;
        this.f706o = obj3;
        this.p = obj4;
    }

    @Override // java.lang.Runnable
    public final void run() throws Throwable {
        String strE;
        BufferedWriter bufferedWriter;
        BufferedWriter bufferedWriter2;
        String string;
        switch (this.l) {
            case 0:
                C2563tq c2563tq = (C2563tq) this.m;
                C2156pc c2156pc = (C2156pc) this.n;
                String str = c2156pc.a;
                Vg0 vg0 = (Vg0) this.f706o;
                C0338Nb c0338Nb = (C0338Nb) this.p;
                c2563tq.getClass();
                Logger logger = C2563tq.f;
                try {
                    Rg0 rg0A = c2563tq.c.a(str);
                    if (rg0A == null) {
                        String str2 = "Transport backend '" + str + "' is not registered";
                        logger.warning(str2);
                        vg0.b(new IllegalArgumentException(str2));
                    } else {
                        ((L30) c2563tq.e).j(new C3034yo(c2563tq, c2156pc, ((C0369Og) rg0A).a(c0338Nb), 1));
                        vg0.b(null);
                    }
                    return;
                } catch (Exception e) {
                    logger.warning("Error scheduling event " + e.getMessage());
                    vg0.b(e);
                    return;
                }
            case 1:
                List list = (List) this.m;
                C2366rm0 c2366rm0 = (C2366rm0) this.n;
                C1414hl c1414hl = (C1414hl) this.f706o;
                WorkDatabase workDatabase = (WorkDatabase) this.p;
                Iterator it = list.iterator();
                while (it.hasNext()) {
                    ((D40) it.next()).a(c2366rm0.a);
                }
                H40.b(c1414hl, workDatabase, list);
                return;
            default:
                Zf0 zf0 = (Zf0) this.m;
                String str3 = (String) this.n;
                Map map = (Map) this.f706o;
                List list2 = (List) this.p;
                C2710vP c2710vP = (C2710vP) zf0.a;
                AtomicMarkableReference atomicMarkableReference = (AtomicMarkableReference) zf0.g;
                BufferedWriter bufferedWriter3 = null;
                if (((String) atomicMarkableReference.getReference()) != null) {
                    String str4 = (String) atomicMarkableReference.getReference();
                    File fileB = c2710vP.a.b(str3, "user-data");
                    try {
                        C2615uP c2615uP = new C2615uP();
                        c2615uP.put("userId", str4);
                        string = c2615uP.toString();
                        bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileB), C2710vP.b));
                    } catch (Exception unused) {
                        bufferedWriter2 = null;
                    } catch (Throwable th) {
                        th = th;
                    }
                    try {
                        bufferedWriter2.write(string);
                        bufferedWriter2.flush();
                    } catch (Exception unused2) {
                    } catch (Throwable th2) {
                        th = th2;
                        bufferedWriter3 = bufferedWriter2;
                        C1884mi0.p(bufferedWriter3);
                        throw th;
                    }
                    C1884mi0.p(bufferedWriter2);
                }
                if (!map.isEmpty()) {
                    c2710vP.g(str3, map, false);
                }
                if (list2.isEmpty()) {
                    return;
                }
                File fileB2 = c2710vP.a.b(str3, "rollouts-state");
                if (list2.isEmpty()) {
                    if (fileB2.exists() && fileB2.delete()) {
                        fileB2.getAbsolutePath();
                        return;
                    }
                    return;
                }
                try {
                    try {
                        strE = C2710vP.e(list2);
                        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileB2), C2710vP.b));
                    } catch (Exception unused3) {
                    }
                } catch (Throwable th3) {
                    th = th3;
                }
                try {
                    bufferedWriter.write(strE);
                    bufferedWriter.flush();
                    C1884mi0.p(bufferedWriter);
                    return;
                } catch (Exception unused4) {
                    bufferedWriter3 = bufferedWriter;
                    C2710vP.f(fileB2);
                    C1884mi0.p(bufferedWriter3);
                    return;
                } catch (Throwable th4) {
                    th = th4;
                    bufferedWriter3 = bufferedWriter;
                    C1884mi0.p(bufferedWriter3);
                    throw th;
                }
        }
    }
}
