package com.ss.android.ugc.aweme.fe.method;

import X.0CB;
import X.0CH;
import X.0CN;
import X.0Gf;
import X.0Gp;
import X.0H0;
import X.0H5;
import X.2Pr;
import X.2sG;
import X.2te;
import X.2th;
import X.6Is;
import X.FIu;
import X.IPG;
import X.IPI;
import X.JMP;
import X.JMQ;
import X.JXH;
import X.KKi;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.socialbase.downloader.depend.IDownloadListener;
import com.ss.android.socialbase.downloader.downloader.Downloader;
import com.ss.android.socialbase.downloader.exception.BaseException;
import com.ss.android.socialbase.downloader.model.DownloadInfo;
import com.ss.android.ugc.aweme.fe.base.BaseCommonJavaMethod;
import java.io.File;
import java.lang.ref.WeakReference;
import kotlin.h.b.n;
import kotlin.o.y;
import org.json.JSONObject;

public final class DownloadFileMethod extends BaseCommonJavaMethod implements IDownloadListener, 6Is {
    public static final JMP LJ = new JMP((byte) 0);
    public String LIZ;
    public String LIZIZ;
    public Integer LIZJ;
    public String LIZLLL;

    static {
        Covode.recordClassIndex(76058);
    }

    public DownloadFileMethod(byte b) {
        this();
    }

    public final void onFirstStart(DownloadInfo downloadInfo) {
    }

    public final void onFirstSuccess(DownloadInfo downloadInfo) {
    }

    public final void onPrepare(DownloadInfo downloadInfo) {
    }

    public final void onStateChanged(0CH r2, 0CB r3) {
        DownloadFileMethod.super.onStateChanged(r2, r3);
        if (r3 == 0CB.ON_DESTROY) {
            onDestroy();
        }
    }

    public /* synthetic */ DownloadFileMethod() {
        this((KKi) null);
    }

    @0CN(LIZ = 0CB.ON_DESTROY)
    public final void onDestroy() {
        Integer num = this.LIZJ;
        if (num != null) {
            Downloader.getInstance((Context) this.mContextRef.get()).cancel(num.intValue());
        }
    }

    public DownloadFileMethod(KKi kKi) {
        super(kKi);
    }

    public final void LIZ(String str) {
        Context context = (Context) this.mContextRef.get();
        if (context != null) {
            n.LIZIZ(context, "");
            File file = new File(str);
            2sG r2 = new 2sG();
            r2.element = 0H0.LIZJ(context, file.getName(), (String) null);
            if (r2.element == null) {
                r2.element = 0H0.LIZ(context, file.getName(), "image/jpeg");
            }
            0Gp.LIZ(new IPG(r2, file));
        }
    }

    public final BaseCommonJavaMethod attach(WeakReference<Context> weakReference) {
        0CH r1;
        if (!(weakReference == null || (r1 = (Context) weakReference.get()) == null || !(r1 instanceof 0CH))) {
            r1.getLifecycle().LIZ(this);
        }
        DownloadFileMethod.super.attach(weakReference);
        n.LIZIZ(this, "");
        return this;
    }

    public final void onCanceled(DownloadInfo downloadInfo) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "cancel");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }

    public final void onPause(DownloadInfo downloadInfo) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "pause");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }

    public final void onProgress(DownloadInfo downloadInfo) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("progress", downloadInfo);
        jSONObject2.put("type", "progress");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }

    public final void onStart(DownloadInfo downloadInfo) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "start");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }

    public final void onSuccessed(DownloadInfo downloadInfo) {
        if (downloadInfo != null && downloadInfo.getSavePath() != null && downloadInfo.getName() != null) {
            String str = null;
            String savePath = downloadInfo.getSavePath();
            n.LIZIZ(savePath, "");
            if (y.LIZIZ(savePath, "content://", false)) {
                try {
                    str = 0H5.LIZ((Context) this.mContextRef.get(), Uri.parse(downloadInfo.getSavePath()));
                    2te.constructor-impl(2Pr.LIZ);
                } catch (Throwable th) {
                    2te.constructor-impl(2th.LIZ(th));
                }
            } else {
                str = new File(downloadInfo.getSavePath(), downloadInfo.getName()).getPath();
                if (TextUtils.equals(this.LIZIZ, "image") && str != null) {
                    LIZ(str);
                }
            }
            JSONObject jSONObject = new JSONObject();
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("type", "success");
            if (str != null) {
                jSONObject2.put("path", str);
            }
            jSONObject.put("data", jSONObject2);
            jSONObject.put("id", this.LIZLLL);
            jSONObject.put("eventName", "download_status_change");
            LIZ("notification", jSONObject);
        }
    }

    public final void LIZ(String str, JSONObject jSONObject) {
        0Gp.LIZ(new IPI(this, str, jSONObject), 0Gp.LIZIZ, (0Gf) null);
    }

    public final void handle(JSONObject jSONObject, FIu fIu) {
        Context actContext = getActContext();
        if (!(actContext instanceof Activity)) {
            actContext = null;
        }
        Activity activity = (Activity) actContext;
        if (activity != null) {
            JXH.LIZ(activity, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, new JMQ(this, jSONObject, fIu, activity));
        }
    }

    public final void onFailed(DownloadInfo downloadInfo, BaseException baseException) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "error");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
        if (TextUtils.equals(this.LIZIZ, "image")) {
            this.mContextRef.get();
        }
    }

    public final void onRetry(DownloadInfo downloadInfo, BaseException baseException) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "restart");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }

    public final void onRetryDelay(DownloadInfo downloadInfo, BaseException baseException) {
        JSONObject jSONObject = new JSONObject();
        JSONObject jSONObject2 = new JSONObject();
        jSONObject2.put("type", "restart");
        jSONObject.put("data", jSONObject2);
        jSONObject.put("id", this.LIZLLL);
        jSONObject.put("eventName", "download_status_change");
        LIZ("notification", jSONObject);
    }
}
