package com.seewo.ota.v.s.q;

import com.seewo.ota.k.f;
import com.seewo.ota.v.s.o;
import f.e0.s;
import f.t;
import f.z.d.k;
import f.z.d.l;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;

/* JADX INFO: compiled from: LogCatchHandler.kt */
/* JADX INFO: loaded from: classes.dex */
public final class c implements o.b {

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final String f2432f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static final f.e f2433g;

    /* JADX INFO: compiled from: LogCatchHandler.kt */
    public /* synthetic */ class a {
        public static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[o.c.values().length];
            iArr[o.c.UPDATE_STATE_START.ordinal()] = 1;
            iArr[o.c.UPDATE_STATE_FAIL.ordinal()] = 2;
            iArr[o.c.UPDATE_STATE_SUCCESS.ordinal()] = 3;
            a = iArr;
        }
    }

    /* JADX INFO: compiled from: LogCatchHandler.kt */
    static final class b extends l implements f.z.c.a<File> {

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

        b() {
            super(0);
        }

        @Override // f.z.c.a
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final File invoke() {
            return c.f2431b.c();
        }
    }

    static {
        String string = new Date().toString();
        k.d(string, "Date().toString()");
        f2432f = s.h(s.h(string, " ", "_", false, 4, null), ":", "-", false, 4, null);
        f2433g = f.g.a(b.f2434b);
    }

    private c() {
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final File c() throws IOException {
        File file = new File("/data/anr/anr_ota_update_device.log");
        if (!file.exists()) {
            File parentFile = file.getParentFile();
            boolean z = false;
            if (parentFile != null && !parentFile.exists()) {
                z = true;
            }
            if (z) {
                parentFile.mkdirs();
            }
            file.createNewFile();
        }
        d(file);
        return file;
    }

    private final void d(File file) throws IOException {
        if (!file.exists() || file.length() <= 4194304) {
            return;
        }
        long j2 = 0;
        long length = file.length() / ((long) 2);
        int length2 = 1024;
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
        while (length < file.length()) {
            try {
                randomAccessFile.seek(length);
                if (file.length() - length < length2) {
                    length2 = (int) (file.length() - length);
                }
                byte[] bArr = new byte[length2];
                randomAccessFile.read(bArr);
                randomAccessFile.seek(j2);
                randomAccessFile.write(bArr);
                long j3 = length2;
                j2 += j3;
                length += j3;
            } catch (Throwable th) {
                try {
                    throw th;
                } catch (Throwable th2) {
                    f.y.b.a(randomAccessFile, th);
                    throw th2;
                }
            }
        }
        randomAccessFile.setLength(j2);
        t tVar = t.a;
        f.y.b.a(randomAccessFile, null);
    }

    private final File e() {
        return (File) f2433g.getValue();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final void g(String str) throws IOException {
        c cVar = f2431b;
        k.d(str, "it");
        cVar.h(str);
    }

    private final void h(String str) throws IOException {
        f.y.h.c(e(), str + '\n', null, 2, null);
    }

    @Override // com.seewo.ota.v.s.o.b
    public void a(o.c cVar) throws IOException {
        k.e(cVar, "state");
        int i2 = a.a[cVar.ordinal()];
        if (i2 != 1) {
            if (i2 == 2 || i2 == 3) {
                com.seewo.ota.k.f.e().d();
                com.seewo.ota.k.f.e().o(false);
                com.seewo.ota.k.f.e().n(true);
                return;
            }
            return;
        }
        com.seewo.ota.k.f.e().o(true);
        com.seewo.ota.k.f.e().n(false);
        h("Start log catch at " + f2432f);
        com.seewo.ota.k.f.e().l(new f.b() { // from class: com.seewo.ota.v.s.q.a
            @Override // com.seewo.ota.k.f.b
            public final void a(String str) throws IOException {
                c.g(str);
            }
        });
        com.seewo.ota.k.f.e().p();
    }
}
