package com.seewo.ota.v.u;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemProperties;
import android.util.Log;
import com.seewo.ota.R;
import com.seewo.ota.util.ImageCheckUtil;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/* JADX INFO: compiled from: ShellUpgradeProcessor.java */
/* JADX INFO: loaded from: classes.dex */
public class q extends n {

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static final String f2617f = "/cache/recovery" + File.separator + "command";

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private String f2618c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private s f2619d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final Handler f2620e;

    public q(Context context, s sVar) {
        super(context, sVar);
        this.f2620e = new Handler(Looper.getMainLooper());
    }

    private void f(final boolean z) {
        this.f2620e.post(new Runnable() { // from class: com.seewo.ota.v.u.g
            @Override // java.lang.Runnable
            public final void run() {
                this.f2594b.g(z);
            }
        });
    }

    private void j() {
        d.e.h.e.f.a("sync");
        d.e.h.e.f.a("reboot recovery");
    }

    private void k(Context context) {
        if (context.getResources().getBoolean(R.bool.support_reset_project_id)) {
            SystemProperties.set("persist.sys.projectid.index", "811");
        }
    }

    private void l() {
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e2) {
            Log.e("ShellUpgradeProcessor", "safeSleep: ", e2);
        }
    }

    private void m() {
        new Thread(new Runnable() { // from class: com.seewo.ota.v.u.f
            @Override // java.lang.Runnable
            public final void run() {
                this.f2593b.h();
            }
        }).start();
    }

    private void n(Context context, s sVar) {
        this.f2619d = sVar;
        com.seewo.vcommons.data.c.t(context, true);
        k(context);
        if (com.seewo.ota.util.h.a() && ImageCheckUtil.a(this.f2618c)) {
            Log.d("ShellUpgradeProcessor", "upgradeSystemWithImg");
            m();
        } else {
            Log.d("ShellUpgradeProcessor", "upgradeSystemWithZip");
            o();
        }
    }

    private void o() {
        new Thread(new Runnable() { // from class: com.seewo.ota.v.u.h
            @Override // java.lang.Runnable
            public final void run() {
                this.f2596b.i();
            }
        }).start();
    }

    private void p(String str) {
        String str2 = str + this.f2618c;
        d.e.c.a.a.d("/cache/recovery");
        d.e.c.a.a.c(f2617f);
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(f2617f));
            try {
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                try {
                    bufferedOutputStream.write(str2.getBytes());
                    bufferedOutputStream.flush();
                    f(true);
                    bufferedOutputStream.close();
                    fileOutputStream.close();
                } finally {
                }
            } finally {
            }
        } catch (IOException e2) {
            Log.e("ShellUpgradeProcessor", "ota IOException", e2);
            f(false);
        }
    }

    @Override // com.seewo.ota.v.u.o
    public void a(String str, boolean z) {
        this.f2618c = str;
        n(this.a, this.f2619d);
    }

    public /* synthetic */ void g(boolean z) {
        if (this.f2619d != null) {
            int i2 = z ? 5 : 3;
            this.f2619d.c(i2, Integer.valueOf(i2));
        }
    }

    public /* synthetic */ void h() {
        l();
        p("--update_rkimage=");
        j();
    }

    public /* synthetic */ void i() {
        l();
        p("--update_package=");
        j();
    }
}
