QQ浏览器 v12.2.3.7053版本的 MD5 值为:8285ab3059e5c8b521a264dfbc5c3685

以下内容为反编译后的 QBPatchAgent.java 源代码,内容仅作参考


package com.tencent.mtt.patch;

import MTT.GetHotPatchReq;
import MTT.GetHotPatchRsp;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.os.SystemClock;
import android.text.TextUtils;
import android.widget.Toast;
import com.facebook.common.util.UriUtil;
import com.huawei.hms.framework.common.ContainerUtils;
import com.tencent.common.http.Apn;
import com.tencent.common.http.NetUtils;
import com.tencent.common.threadpool.BrowserExecutorSupplier;
import com.tencent.common.utils.FileUtils;
import com.tencent.common.utils.Md5Utils;
import com.tencent.common.utils.StorageDirs;
import com.tencent.common.utils.UrlUtils;
import com.tencent.common.wup.IWUPRequestCallBack;
import com.tencent.common.wup.WUPRequestBase;
import com.tencent.common.wup.WUPResponseBase;
import com.tencent.common.wup.WUPTaskProxy;
import com.tencent.map.geolocation.util.DateUtils;
import com.tencent.mtt.ContextHolder;
import com.tencent.mtt.base.account.facade.IUserServiceExtension;
import com.tencent.mtt.base.functionwindow.ActivityHandler;
import com.tencent.mtt.base.stat.StatManager;
import com.tencent.mtt.base.utils.CommonUtils;
import com.tencent.mtt.base.utils.DeviceUtils;
import com.tencent.mtt.base.utils.GetTask;
import com.tencent.mtt.base.utils.HookDetector;
import com.tencent.mtt.base.utils.MttFileUtils;
import com.tencent.mtt.base.wup.GUIDManager;
import com.tencent.mtt.base.wup.WUPRequest;
import com.tencent.mtt.browser.download.core.facade.IBusinessDownloadService;
import com.tencent.mtt.browser.download.core.facade.IFlowCtrlCallback;
import com.tencent.mtt.browser.download.core.impl.DownloadServiceManager;
import com.tencent.mtt.browser.download.engine.DownloadErrorDetail;
import com.tencent.mtt.browser.download.engine.DownloadInfo;
import com.tencent.mtt.browser.download.engine.DownloadTask;
import com.tencent.mtt.browser.download.engine.DownloadTaskListener;
import com.tencent.mtt.browser.download.engine.NetworkPolicy;
import com.tencent.mtt.browser.download.engine.PauseReason;
import com.tencent.mtt.browser.download.engine.RemovePolicy;
import com.tencent.mtt.browser.engine.AppBroadcastObserver;
import com.tencent.mtt.browser.engine.AppBroadcastReceiver;
import com.tencent.mtt.debug.SimpleCostTime;
import com.tencent.mtt.javaswitch.QBPatchSwitch;
import com.tencent.mtt.log.access.Logs;
import com.tencent.mtt.qbinfo.QUAUtils;
import com.tencent.tinker.lib.service.AbstractResultService;
import com.tencent.tinker.lib.service.PatchResult;
import com.tencent.tinker.lib.service.TinkerPatchService;
import com.tencent.tinker.lib.tinker.TinkerApplicationHelper;
import com.tencent.tinker.lib.util.UpgradePatchRetry;
import com.tencent.tinker.loader.shareutil.ShareConstants;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;
import com.xiaomi.mipush.sdk.Constants;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import tmsdk.common.gourd.vine.IMessageCenter;

public class QBPatchAgent implements Handler.Callback, IWUPRequestCallBack, ActivityHandler.ApplicationStateListener, IFlowCtrlCallback, DownloadTaskListener, AppBroadcastObserver {

    private static int f69227d = (int) (System.currentTimeMillis() & 32767);

    private Handler f69229b;
    private QBPatchInfo e;

    private volatile Handler f69230c = null;
    private String f = null;
    private QBPatchDownloadInfo g = null;
    private QBPatchStat h = null;
    private Map<Integer, Integer> i = new ConcurrentHashMap();
    private BackwardDelay j = new BackwardDelay(DateUtils.ONE_MINUTE, 18000000, 2.5d);
    private BackwardDelay k = new BackwardDelay(Constants.ASSEMBLE_PUSH_NETWORK_INTERVAL, DateUtils.ONE_HOUR, 2.0d);
    private BackwardDelay l = new BackwardDelay(DateUtils.TEN_SECOND, 1800000, 5.0d);
    private long m = 0;
    private boolean n = false;
    private boolean o = false;
    private HashSet<Integer> p = new HashSet<>();

    final Object f69228a = {"applicationIsShuttingDown"};

    static class AnonymousClass1 {

        static final int[] f69231a = new int[ActivityHandler.State.values().length];

        static {
            try {
                f69231a[ActivityHandler.State.background.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f69231a[ActivityHandler.State.finish.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                f69231a[ActivityHandler.State.foreground.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    public QBPatchAgent() {
        Logs.c("QBPatch.Agent", "QBPatchAgent.<init>()");
        this.e = QBPatchInfo.a("patch_info_");
        this.f69229b = new Handler(Looper.getMainLooper(), this);
        Logs.c("QBPatch.Agent", "QBPatchAgent.<init>() mPatch=" + this.e);
    }

    private int a(DownloadTask downloadTask, String[] strArr) {
        if (a() == null) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: dir not exists");
            return -6;
        }
        File a2 = a(this.e);
        if (!a2.exists()) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: file not exists");
            if (downloadTask == null || !(downloadTask instanceof DownloadTask)) {
                return -1;
            }
            File file = new File(downloadTask.O());
            if (!file.exists()) {
                Logs.c("QBPatch.Agent", "checkDownloadResult: alter file not exists");
                if (strArr != null && strArr.length > 0) {
                    strArr[0] = "alter=none";
                }
                return -1;
            }
            if (!FileUtils.a(file, a2)) {
                Logs.c("QBPatch.Agent", "checkDownloadResult: rename fail");
                if (strArr != null && strArr.length > 0) {
                    strArr[0] = "alter=renamefail";
                }
                return -1;
            }
            Logs.c("QBPatch.Agent", "checkDownloadResult: rename done");
        }
        if (!a2.isFile()) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: not a file");
            return -2;
        }
        long length = a2.length();
        if (length != 0 && length != this.e.f) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: filesize not match, size=" + length + " expect=" + this.e.f);
            if (strArr == null || strArr.length <= 0) {
                return -3;
            }
            strArr[0] = "len=" + length + "&exp=" + this.e.f;
            return -3;
        }
        String a3 = Md5Utils.a(a2);
        if (TextUtils.isEmpty(a3)) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: unexpected empty md5");
            return -4;
        }
        if (a3.equalsIgnoreCase(this.e.g)) {
            Logs.c("QBPatch.Agent", "checkDownloadResult: ok");
            return 0;
        }
        Logs.c("QBPatch.Agent", "checkDownloadResult: md5 not match, md5=" + a3 + " expect=" + this.e.g);
        if (strArr == null || strArr.length <= 0) {
            return -5;
        }
        strArr[0] = "md5=" + a3 + "&exp=" + this.e.g;
        return -5;
    }

    static File a() {
        return FileUtils.a(StorageDirs.b(), "hotpatch");
    }

    public static <T> T a(Object obj, Class<T> cls) {
        if (!(obj instanceof Object[])) {
            if (cls.isInstance(obj)) {
                return obj;
            }
            Logs.a("QBPatch.Agent", (Throwable) new IllegalArgumentException("意料之外的array类型" + obj + ",保存日志给phantomqi看"));
            return null;
        }
        for (Object obj2 : (Object[]) obj) {
            T t = (T) obj2;
            if (cls.isInstance(t)) {
                return t;
            }
        }
        Logs.a("QBPatch.Agent", (Throwable) new IllegalArgumentException("未在array中找到" + cls.getName() + "的对象,保存日志给phantomqi看"));
        return null;
    }

    private void a(WUPRequestBase wUPRequestBase, QBPatchInfo qBPatchInfo) {
        String str;
        if (qBPatchInfo == null) {
            QBPatchStat.a("BONPH_NC_C_5");
        }
        QBPatchStat qBPatchStat = (QBPatchStat) wUPRequestBase.getBindObject();
        int a2 = QBPatchUtils.a(qBPatchInfo);
        if (a2 != 0) {
            Logs.c("QBPatch.Agent", "onRecvNewPatchInfo() nomore config or config not applicable. code=" + a2);
            if (a2 == -5) {
                qBPatchStat.a(qBPatchInfo).a(IUserServiceExtension.SERVICE_WX_READ, null, null);
            } else if (a2 == -4) {
                qBPatchStat.a(qBPatchInfo).a(IUserServiceExtension.SERVICE_TYPE_COMIC, "" + a2, null);
            } else if (a2 != -1) {
                String str2 = "expmd5=" + qBPatchInfo.f69239d + "&expbuild=" + qBPatchInfo.f69238c;
                qBPatchStat.a(qBPatchInfo).a(IUserServiceExtension.SERVICE_TYPE_LIVE, "" + a2, str2);
            } else {
                String str3 = "infoMD5=" + QBPatchUtils.a(qBPatchInfo.f69236a, 8) + "&patchState=" + qBPatchInfo.f69237b + "&expBase=" + qBPatchInfo.f69238c + "&expBaseDex=" + QBPatchUtils.a(qBPatchInfo.f69239d, 8) + "&patchVer=" + qBPatchInfo.e + "&patchFlag=" + qBPatchInfo.j + "&patchNoConfig=" + this.e.e;
                ArrayList<Object> requestParams = wUPRequestBase.getRequestParams();
                if (requestParams != null && !requestParams.isEmpty() && (requestParams.get(0) instanceof GetHotPatchReq)) {
                    GetHotPatchReq getHotPatchReq = (GetHotPatchReq) requestParams.get(0);
                    str3 = str3 + "&req=" + QBPatchUtils.a(getHotPatchReq.sMessageMD5, 8) + "&base=" + getHotPatchReq.iBaseBuildNo + "&local=" + getHotPatchReq.iLocalBuildNo + "&dex=" + QBPatchUtils.a(getHotPatchReq.sBaseClassDexMD5, 8) + "&guid=" + QBPatchUtils.a(getHotPatchReq.sGuid, 8);
                }
                qBPatchStat.a(IUserServiceExtension.SERVICE_TYPE_SHOPPING, null, str3 + "&apk=" + QBPatchContext.f69232a.getPackageCodePath());
                QBPatchStat.a("BONPH_RQ_NONE");
                QBPatchStat.a("BONPH_RQ_NONE_" + this.e.e);
                if (this.e.e <= QBTinkerUtils.a(QBTinkerUtils.c())) {
                    QBPatchStat.a("BONPH_NC_C_0");
                    QBPatchStat.a("BONPH_NC_C_" + CommonUtils.a(0L, "yyyyMMdd"));
                }
            }
        } else if (qBPatchInfo.f69237b == 2 && qBPatchInfo.f69236a.equalsIgnoreCase(this.e.f69236a)) {
            Logs.c("QBPatch.Agent", "onRecvNewPatchInfo() same md5, config duplicated");
            qBPatchStat.a(qBPatchInfo).a(IUserServiceExtension.SERVICE_TYPE_COMIC, null, null);
            if (this.e.f69237b == 1) {
                this.e.f69237b = 2;
            }
            QBPatchStat.a("BONPH_RQ_OK");
        } else {
            QBPatchDownloadInfo qBPatchDownloadInfo = this.g;
            if (qBPatchDownloadInfo != null && (str = qBPatchDownloadInfo.f37604a) != null && !str.equals(qBPatchInfo.h) && !str.equals(qBPatchInfo.i)) {
                Logs.c("QBPatch.Agent", "onRecvNewPatchInfo() cancel previous download task: " + str);
                DownloadServiceManager.a().removeDownloadTask(str, RemovePolicy.DELETE_TASK_AND_FILE);
                qBPatchDownloadInfo.Y.a("2004", null, null);
                this.g = null;
            }
            if (qBPatchInfo.f69237b == 5 && !TextUtils.isEmpty(this.f)) {
                this.f = "rollback";
            }
            qBPatchStat.a(qBPatchInfo).a();
            QBPatchStat.a("BONPH_RQ_OK");
            QBPatchInfo.a(qBPatchInfo, "patch_info_");
            this.e = qBPatchInfo;
            this.l.c();
            this.k.c();
        }
        g();
    }

    private void a(DownloadTask downloadTask) {
        QBPatchDownloadInfo qBPatchDownloadInfo = this.g;
        if (qBPatchDownloadInfo == null) {
            return;
        }
        QBPatchStat qBPatchStat = qBPatchDownloadInfo.Y;
        this.g = null;
        String k = downloadTask.k();
        Logs.c("QBPatch.Agent", "downloadCompleted: " + k);
        String[] strArr = {""};
        int a2 = a(downloadTask, strArr);
        if (a2 == 0) {
            a("热补丁下载成功");
            QBPatchStat.a("BONPH_DL_FIN");
            qBPatchStat.a();
            QBPatchStat.a("BONPH_DL_OK");
            this.k.c();
            if (this.e.f69237b > 2) {
                Logs.d("QBPatch.Agent", "not expecting any downloads: info=" + this.e);
                QBPatchStat.a("BONPH_NC_1");
                g();
                return;
            }
            String g = QBTinkerUtils.g();
            if (!g.equalsIgnoreCase(this.e.g)) {
                QBPatchInfo qBPatchInfo = this.e;
                qBPatchInfo.f69237b = 3;
                QBPatchInfo.a(qBPatchInfo, "patch_info_");
                g();
                return;
            }
            Logs.d("QBPatch.Agent", "patch seems already installed. ignoring this package. md5=" + g);
            QBPatchStat.a("BONPH_NC_2");
            return;
        }
        a("热补丁下载失败[" + a2 + "] detail=" + strArr[0]);
        if ((a2 == -5 || a2 == -3) && !TextUtils.isEmpty(this.e.i)) {
            if (k.startsWith(NetUtils.SCHEME_HTTP)) {
                if (a(true)) {
                    return;
                }
            } else if (!k.equals(this.e.i)) {
                Logs.c("QBPatch.Agent", "found path download url changed.\nold url: " + k + "\nnew url: " + this.e.i + "\ncurrent state: " + this.e.f69237b);
                g();
                return;
            }
        }
        QBPatchStat.a("BONPH_DL_FIN");
        if ((a2 != -5 && a2 != -3) || k == null || k.equals(this.e.h) || k.equals(this.e.i)) {
            qBPatchStat.a("2005", "" + a2, strArr[0] + "&url=" + k);
        } else {
            qBPatchStat.a("2004", "", strArr[0] + "&url=" + k);
        }
        if (this.k.a()) {
            b().sendEmptyMessageDelayed(2, this.k.b());
        }
    }

    private void a(String str) {
        Logs.c("QBPatch.Agent", str);
        if (QBPatchTestAssist.a("patch_debug_show_toast")) {
            this.f69229b.obtainMessage(1, str).sendToTarget();
        }
    }

    private static boolean a(DownloadInfo downloadInfo, DownloadTask downloadTask) {
        String k;
        if (downloadTask == null || downloadInfo == null || (k = downloadTask.k()) == null) {
            return false;
        }
        if (k.equals(downloadInfo.f37604a)) {
            return true;
        }
        if (downloadInfo.f37605b != null && !downloadInfo.f37605b.isEmpty()) {
            Iterator<String> it = downloadInfo.f37605b.iterator();
            while (it.hasNext()) {
                if (k.equals(it.next())) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean a(boolean z) {
        String sb;
        QBPatchStat.a("BONPH_DL_DO_START");
        this.o = false;
        QBPatchInfo qBPatchInfo = this.e;
        String g = QBTinkerUtils.g();
        if (g.equalsIgnoreCase(qBPatchInfo.g)) {
            Logs.d("QBPatch.Agent", "patch already installed. cancel download. retry=" + z + " installed=" + g + " info=" + qBPatchInfo);
            return true;
        }
        if (qBPatchInfo.f69237b != 2) {
            Logs.d("QBPatch.Agent", "not expecting any downlaods. info=" + qBPatchInfo);
            g();
            return true;
        }
        String str = "2";
        QBPatchStat qBPatchStat = new QBPatchStat(qBPatchInfo, "2", null);
        File a2 = a();
        if (a2 == null) {
            Logs.c("QBPatch.Agent", "doStartDownload() can't access download dir: " + a2.getAbsolutePath());
            qBPatchStat.a("2006", null, "file=" + a2.getName() + "&retry=" + (z ? 1 : 0));
            return false;
        }
        File a3 = a(qBPatchInfo);
        if (a3.exists() && !a3.delete()) {
            Logs.c("QBPatch.Agent", "doStartDownload() can't delete targetfile: " + a3.getAbsolutePath());
            qBPatchStat.a("2001", null, "file=" + a3.getName() + "&retry=" + (z ? 1 : 0));
            return false;
        }
        if (!z && qBPatchInfo.j == 1 && !Apn.isWifiMode()) {
            Logs.c("QBPatch.Agent", "doStartDownload() need wifi");
            a("当前热补丁仅wifi能下载");
            qBPatchStat.a("2002", null, "apn=" + Apn.getApnName(Apn.getApnTypeS()));
            QBPatchStat.a("BONPH_DL_NEED_WIFI");
            b().sendEmptyMessageDelayed(2, this.k.b());
            this.o = true;
            return false;
        }
        QBPatchDownloadInfo qBPatchDownloadInfo = new QBPatchDownloadInfo(qBPatchStat);
        if (z) {
            qBPatchDownloadInfo.f37604a = qBPatchInfo.i;
        } else {
            qBPatchDownloadInfo.f37604a = qBPatchInfo.h;
            qBPatchDownloadInfo.f37605b = new ArrayList(Arrays.asList(qBPatchInfo.i));
            if ("softimtt.myapp.com".equals(UrlUtils.getHost(qBPatchDownloadInfo.f37604a))) {
                qBPatchDownloadInfo.f37605b.add(qBPatchDownloadInfo.f37604a.replace("softimtt.myapp.com", "soft.imtt.qq.com"));
            }
        }
        qBPatchDownloadInfo.f37606c = a3.getName();
        qBPatchDownloadInfo.f37607d = qBPatchInfo.f;
        qBPatchDownloadInfo.f = a2.getAbsolutePath();
        qBPatchDownloadInfo.j = false;
        qBPatchDownloadInfo.h = false;
        qBPatchDownloadInfo.l = false;
        qBPatchDownloadInfo.r = 0;
        qBPatchDownloadInfo.k = false;
        qBPatchDownloadInfo.E = NetworkPolicy.ALL_NETWORK;
        qBPatchDownloadInfo.H |= 32;
        if (qBPatchInfo.j == 1) {
            qBPatchDownloadInfo.H |= Integer.MIN_VALUE;
        }
        IBusinessDownloadService a4 = DownloadServiceManager.a();
        a4.addTaskListener(this);
        DownloadTask startDownloadTask = a4.startDownloadTask(qBPatchDownloadInfo, null, null);
        if (startDownloadTask != null) {
            Logs.c("QBPatch.Agent", "doStartDownload(" + z + ") started url=>" + startDownloadTask.k());
            QBPatchDownloadInfo qBPatchDownloadInfo2 = this.g;
            if (qBPatchDownloadInfo2 != null && qBPatchDownloadInfo2.f37604a != null && this.g.f37604a.equals(qBPatchDownloadInfo.f37604a)) {
                qBPatchDownloadInfo.Z = this.g.Z;
            }
            this.g = qBPatchDownloadInfo;
            qBPatchInfo.c();
            QBPatchStat.a("BONPH_DL_STARTED");
            return true;
        }
        Logs.c("QBPatch.Agent", "doStartDownload() can't create download task");
        QBPatchStat.a("BONPH_DL_START_FAIL_0");
        long a5 = MttFileUtils.a((String) null, QBPatchContext.f69232a);
        if (a5 <= qBPatchInfo.f) {
            qBPatchStat.a("2010", null, "space=" + a5 + "&size=" + qBPatchInfo.f);
            sb = "BONPH_DL_START_FAIL_4";
        } else {
            if (TextUtils.isEmpty(qBPatchDownloadInfo.f37604a) || TextUtils.isEmpty(qBPatchDownloadInfo.f37604a.trim())) {
                str = "1";
            } else if (qBPatchInfo.f <= 20971520) {
                str = a5 <= ((long) (qBPatchInfo.f * 2)) ? "3" : "0";
            }
            qBPatchStat.a("2003", str, "retry=" + (z ? 1 : 0) + "&url=" + qBPatchDownloadInfo.f37604a + "&size=" + qBPatchInfo.f);
            StringBuilder sb2 = new StringBuilder();
            sb2.append("BONPH_DL_START_FAIL_");
            sb2.append(str);
            sb = sb2.toString();
        }
        QBPatchStat.a(sb);
        return false;
    }

    private void b(DownloadTask downloadTask) {
        String str;
        String str2;
        QBPatchStat qBPatchStat;
        String str3;
        StringBuilder sb;
        if (downloadTask != null && this.g != null) {
            int aa = downloadTask.aa();
            if (downloadTask instanceof DownloadTask) {
                aa = downloadTask.aa();
                str2 = downloadTask.ab();
            } else {
                str2 = "";
            }
            a("热补丁下载失败(" + aa + Constants.COLON_SEPARATOR + str2 + ")");
            if (this.g.Y != null) {
                if (aa != 34 && aa != 39) {
                    qBPatchStat = this.g.Y;
                    str3 = "" + aa;
                    sb = new StringBuilder();
                } else if (QBPatchUtils.b()) {
                    QBPatchDownloadInfo qBPatchDownloadInfo = this.g;
                    int i = qBPatchDownloadInfo.Z;
                    qBPatchDownloadInfo.Z = i - 1;
                    if (i > 0 && this.g.f37604a != null) {
                        boolean z = false;
                        if (this.g.f37604a.equals(this.e.h)) {
                            z = a(false);
                        } else if (this.g.f37604a.equals(this.e.i)) {
                            z = a(true);
                        }
                        if (z) {
                            str = "下载重试剩余次数=" + this.g.Z;
                        }
                    }
                    qBPatchStat = this.g.Y;
                    str3 = "" + aa;
                    sb = new StringBuilder();
                } else {
                    this.o = true;
                    this.g.Y.a("2009", "", "");
                }
                sb.append("url=");
                sb.append(downloadTask.k());
                sb.append("desc=");
                sb.append(str2);
                qBPatchStat.a("2007", str3, sb.toString());
            }
            this.g = null;
            if (this.k.a()) {
                b().sendEmptyMessageDelayed(2, this.k.b());
                return;
            }
            return;
        }
        str = "downloadFailed() invalid args. task=" + downloadTask + " mDownloadInfo=" + this.g;
        Logs.c("QBPatch.Agent", str);
    }

    private void b(PatchResult patchResult) {
        Logs.c("QBPatch.Agent", "patchReady(" + patchResult + ") info=" + this.e);
        if (patchResult.patchVersion != null) {
            b().removeMessages(5, patchResult.patchVersion);
        }
        QBPatchContext.a().remove("patch_setting_service_start");
        this.h = null;
        if (!patchResult.isSuccess) {
            if (this.e.g == null || this.e.f69237b != 3) {
                return;
            }
            if (patchResult.patchVersion == null || this.e.g.equals(patchResult.patchVersion)) {
                this.e.f69237b = 1;
                return;
            }
            return;
        }
        this.l.c();
        if (this.e.g != null && this.e.g.equals(patchResult.patchVersion)) {
            QBPatchInfo qBPatchInfo = this.e;
            qBPatchInfo.f69237b = 4;
            QBPatchInfo.a(qBPatchInfo, "patch_info_");
            if (QBTinkerUtils.a(QBTinkerUtils.c()) == this.e.e) {
                QBPatchStat.a("BONPH_NC_9");
            }
        }
        g();
    }

    private void b(boolean z) {
        String str;
        boolean z2;
        if (!QBTinkerUtils.f()) {
            Logs.c("QBPatch.Agent", "tinker is disabled");
            return;
        }
        Logs.c("QBPatch.Agent", "notifyApplicationGoingDown() killAllQBProc=" + z + " info=" + this.e);
        QBPatchInfo qBPatchInfo = this.e;
        if (qBPatchInfo.f69237b < 4) {
            a(4, "patchState", "" + qBPatchInfo.f69237b, "killAll", "" + z);
            return;
        }
        if (qBPatchInfo.f69237b == 4) {
            if (QBPatchUtils.a(qBPatchInfo) != 0) {
                str = "notifyApplicationGoingDown: patch not applicable";
                Logs.c("QBPatch.Agent", str);
                z2 = false;
            }
            z2 = true;
        } else {
            if (qBPatchInfo.f69237b == 5 && (QBPatchContext.f69234c == null || !QBPatchContext.f69234c.i())) {
                str = "notifyApplicationGoingDown: tinker not loaded";
                Logs.c("QBPatch.Agent", str);
                z2 = false;
            }
            z2 = true;
        }
        if (!z2) {
            a(4, new String[0]);
            return;
        }
        a(1, new String[0]);
        QBPatchStat.a(z ? "BONPH_KL_BG" : "BONPH_KL_FN");
        QBPatchUtils.a(z);
    }

    private void c() {
        QBPatchInfo qBPatchInfo = this.e;
        if (qBPatchInfo.f69237b == 2 && this.o && Apn.isNetworkConnected()) {
            boolean isWifiMode = Apn.isWifiMode();
            if (isWifiMode || (qBPatchInfo.j != 1 && m())) {
                StringBuilder sb = new StringBuilder();
                sb.append("检测到当前已开启");
                sb.append(isWifiMode ? "wifi" : "网络");
                sb.append(",尝试下载热补丁ver=");
                sb.append(qBPatchInfo.e);
                a(sb.toString());
                g();
            }
        }
    }

    private WUPRequest d() {
        GetHotPatchReq getHotPatchReq = new GetHotPatchReq();
        getHotPatchReq.iBaseBuildNo = QBTinkerUtils.a(QBTinkerUtils.b());
        getHotPatchReq.iLocalBuildNo = QBTinkerUtils.a(QBTinkerUtils.c());
        getHotPatchReq.sBaseClassDexMD5 = QBPatchUtils.a();
        QBPatchInfo qBPatchInfo = this.e;
        getHotPatchReq.sMessageMD5 = qBPatchInfo.f69236a;
        getHotPatchReq.sGuid = GUIDManager.a().f();
        getHotPatchReq.sQua = QUAUtils.a();
        Logs.c("QBPatch.Agent", "createHotPatchInfoRequest() md5=" + getHotPatchReq.sMessageMD5 + " basebuild=" + getHotPatchReq.iBaseBuildNo + " localbuild=" + getHotPatchReq.iLocalBuildNo + " dexmd5=" + getHotPatchReq.sBaseClassDexMD5 + " reqid=" + f69227d + " guid=" + getHotPatchReq.sGuid + " qua=" + getHotPatchReq.sQua);
        HashMap hashMap = new HashMap();
        hashMap.put("sMessageMD5", getHotPatchReq.sMessageMD5);
        hashMap.put("iBaseBuildNo", Integer.valueOf(getHotPatchReq.iBaseBuildNo));
        hashMap.put("iLocalBuildNo", Integer.valueOf(getHotPatchReq.iLocalBuildNo));
        hashMap.put("sBaseClassDexMD5", getHotPatchReq.sBaseClassDexMD5);
        hashMap.put("sGuid", getHotPatchReq.sGuid);
        hashMap.put("sQua", getHotPatchReq.sQua);
        WUPRequest wUPRequest = new WUPRequest(IBusinessDownloadService.FLOWCTRL_APP_UPGRADE, "getHotPatch", this);
        wUPRequest.put("getHotPatchReq", getHotPatchReq);
        wUPRequest.setRequestName("getHotPatch");
        int i = f69227d + 1;
        f69227d = i;
        wUPRequest.setRequstID(i);
        wUPRequest.setBindObject(new QBPatchStat(qBPatchInfo, "1", null));
        this.i.put(Integer.valueOf(wUPRequest.getRequstID()), 1);
        if (QBPatchTestAssist.a("patch_debug_req_env_switch")) {
            Logs.c("QBPatch.Agent", "use debug wup");
            wUPRequest.setUrl("http://14.18.180.112:18000");
            QBPatchStat.a("BONPH_NC_B_5");
            hashMap.put("request.url", wUPRequest.getUrl());
        }
        QBPatchStat.b("request-" + f69227d, hashMap);
        return wUPRequest;
    }

    private void e() {
        f();
        g();
    }

    private void f() {
        if (!this.j.a()) {
            Logs.c("QBPatch.Agent", "delayRequest: no more retry");
            return;
        }
        long b2 = this.j.b();
        Logs.c("QBPatch.Agent", "delayRequest: delay=" + (b2 / DateUtils.ONE_MINUTE) + "min");
        b().sendEmptyMessageDelayed(3, b2);
    }

    private void g() {
        Logs.c("QBPatch.Agent", "goAhead() info=" + this.e);
        if (!TextUtils.isEmpty(this.f)) {
            Logs.c("QBPatch.Agent", "goAhead() waiting flowctrl: " + this.f);
            return;
        }
        QBPatchDownloadInfo qBPatchDownloadInfo = this.g;
        if (qBPatchDownloadInfo != null) {
            Logs.c("QBPatch.Agent", "goAhead() downloading=" + qBPatchDownloadInfo.f37604a);
            return;
        }
        b().removeMessages(2);
        if (HookDetector.a() && this.e.f69237b != 4) {
            Logs.c("QBPatch.Agent", "hook may be detected! pause patch routine");
            a("检测到本地hook,暂停热补丁流程");
            QBPatchStat.a("BONPH_HOOK");
            return;
        }
        int i = this.e.f69237b;
        if (i == 0) {
            a("没有可用的热补丁");
            return;
        }
        if (i == 1) {
            a("补丁安装失败");
            h();
            return;
        }
        if (i == 2) {
            a("检查补丁下载状态...ver=" + this.e.e);
            i();
            return;
        }
        if (i == 3) {
            a("启动本地补丁合成...");
            l();
            return;
        }
        if (i == 4) {
            a("检查合成的补丁是否可用...ver=" + this.e.e);
            StatManager.b().b(true);
            b().removeMessages(110);
            if (!m()) {
                b().sendEmptyMessageDelayed(110, 7000L);
                return;
            }
        } else if (i != 5) {
            return;
        } else {
            a("需要回滚当前补丁");
        }
        j();
    }

    private void h() {
        if (!this.l.a()) {
            a("停止补丁流程");
            return;
        }
        long b2 = this.l.b();
        a((b2 / 1000) + "秒后重试安装");
        b().sendEmptyMessageDelayed(6, b2);
    }

    private void i() {
        Logs.c("QBPatch.Agent", "checkIfDownload() info=" + this.e);
        QBPatchInfo qBPatchInfo = this.e;
        if (qBPatchInfo.f69237b != 2) {
            g();
            return;
        }
        int a2 = QBPatchUtils.a(qBPatchInfo);
        if (a2 != 0) {
            Logs.c("QBPatch.Agent", "checkIfDownload() patchInfo not applicable: " + a2);
            this.e.f69237b = 0;
            qBPatchInfo.f69237b = 0;
            QBPatchInfo.a(qBPatchInfo, "patch_info_");
            g();
            return;
        }
        if (qBPatchInfo.a(DateUtils.ONE_HOUR)) {
            Logs.c("QBPatch.Agent", "checkIfDownload() too many downloads, need break");
            b().sendEmptyMessageDelayed(2, 1800000L);
            return;
        }
        if (QBPatchTestAssist.a("patch_debug_req_env_switch")) {
            onFlowCtrlStartDownload(1);
            return;
        }
        IBusinessDownloadService a3 = DownloadServiceManager.a();
        if (!TextUtils.isEmpty(this.f)) {
            if (this.f.equals(qBPatchInfo.h)) {
                Logs.c("QBPatch.Agent", "checkIfDownload() during flowctrl. wait");
                return;
            }
            a3.removeFlowCtrlTask((byte) 1);
        }
        this.f = qBPatchInfo.h;
        a3.sendFlowCtrl(1, this.f, this);
        b().removeMessages(4);
        b().sendEmptyMessageDelayed(4, Constants.ASSEMBLE_PUSH_NETWORK_INTERVAL);
        QBPatchStat.a("BONPH_DL_SND_FLWCTL");
    }

    private void j() {
        Logs.c("QBPatch.Agent", "checkIfApplicable() info=" + this.e);
        if (!k()) {
            a("热补丁不可用或无新补丁,中止补丁流程\n" + QBTinkerUtils.b() + Constants.ACCEPT_TIME_SEPARATOR_SP + QBTinkerUtils.c() + Constants.ACCEPT_TIME_SEPARATOR_SP + this.e.e + Constants.ACCEPT_TIME_SEPARATOR_SP + this.e.a());
            return;
        }
        if (m()) {
            Logs.c("QBPatch.Agent", "checkIfApplicable: wait for app go background");
            a("热补丁准备就绪,可手动重启浏览器或将浏览器切后台");
            if (this.e.f69237b == 5) {
                QBTinkerUtils.a(false, "recv-rollback");
            }
            if (HookDetector.a()) {
                this.e.f69237b = 5;
                a("检测到本地hook,回滚热补丁");
                QBPatchStat.a("BONPH_HOOK");
                QBTinkerUtils.a(false, "hookdetector");
            }
            QBPatchInfo.a(this.e, "patch_installed_");
            a(2, new String[0]);
            return;
        }
        if (this.e.f69237b == 5) {
            QBTinkerUtils.a(false, "recv-rollback");
        }
        if (HookDetector.a()) {
            this.e.f69237b = 5;
            a("检测到本地hook,回滚热补丁");
            QBPatchStat.a("BONPH_HOOK");
            QBTinkerUtils.a(false, "hookdetector");
        }
        QBPatchInfo.a(this.e, "patch_installed_");
        a(1, new String[0]);
        QBPatchStat.a("BONPH_KL_NT");
        QBPatchUtils.a(true);
    }

    private boolean k() {
        if (this.e.f69237b == 4) {
            return !this.e.g.equals(QBPatchInfo.a("patch_installed_").g) && QBPatchUtils.a(this.e) == 0;
        }
        if (this.e.f69237b == 5) {
            if (QBPatchContext.f69234c != null && QBPatchContext.f69234c.i()) {
                return true;
            }
            Logs.c("QBPatch.Agent", "i am not patched yet!");
        }
        return false;
    }

    private void l() {
        String str;
        String sb;
        int i;
        String str2;
        char c2;
        String str3 = ContainerUtils.FIELD_DELIMITER;
        Logs.c("QBPatch.Agent", "preparePatch() info=" + this.e);
        QBPatchStat.a("BONPH_NT_PREPARE");
        if (QBTinkerUtils.g().equalsIgnoreCase(this.e.g)) {
            if (TinkerApplicationHelper.b(QBPatchContext.f69233b)) {
                Logs.d("QBPatch.Agent", "patch already installed and loaded, no need to run patch service");
                this.e.f69237b = QBTinkerUtils.h() ? 4 : 5;
                QBPatchInfo.a(this.e, "patch_info_");
                QBPatchInfo.a(this.e, "patch_installed_");
                QBPatchStat.a("BONPH_NC_3");
                g();
                return;
            }
            Logs.d("QBPatch.Agent", "patch already installed but not loaded successfully, delete previously installed patch");
            if (QBPatchContext.f69234c != null) {
                QBPatchContext.f69234c.q();
            }
        }
        QBPatchStat qBPatchStat = new QBPatchStat(this.e, "3", null);
        String[] strArr = {""};
        int a2 = a((DownloadTask) null, strArr);
        if (a2 != 0) {
            a("热补丁校验失败[" + a2 + "] detail=" + strArr[0] + ",需要重新下载");
            StringBuilder sb2 = new StringBuilder();
            sb2.append("");
            sb2.append(a2);
            qBPatchStat.a("3001", sb2.toString(), strArr[0]);
            QBPatchInfo qBPatchInfo = this.e;
            qBPatchInfo.f69237b = 2;
            QBPatchInfo.a(qBPatchInfo, "patch_info_");
            g();
            return;
        }
        if (this.e.f69237b == 1) {
            this.e.f69237b = 3;
        }
        String string = QBPatchContext.a().getString("patch_setting_reset_retry_count", "");
        String b2 = CommonUtils.b();
        if (string == null || !string.equals(b2)) {
            Logs.c("QBPatch.Agent", "reset max retry check: " + string + " -> " + b2);
            UpgradePatchRetry.a(QBPatchContext.f69232a).b(this.e.g);
        }
        File a3 = a(this.e);
        Logs.c("QBPatch.Agent", "preparePatch() file=" + a3.getAbsolutePath());
        try {
            TinkerPatchService.a(new QBUpgradePatchProcessor(), (Class<? extends AbstractResultService>) QBPatchResultService.class);
            i = QBPatchContext.f69234c.o().a(a3.getAbsolutePath());
            sb = "";
        } catch (Throwable th) {
            if (DeviceUtils.aC) {
                StringBuilder sb3 = new StringBuilder();
                sb3.append("");
                sb3.append(TextUtils.isEmpty("") ? "" : ContainerUtils.FIELD_DELIMITER);
                sb3.append("oppo=1");
                str = sb3.toString();
            } else {
                str = "";
            }
            if (th.toString().matches(".*user.*0.*restricted.*")) {
                StringBuilder sb4 = new StringBuilder();
                sb4.append(str);
                sb4.append(TextUtils.isEmpty(str) ? "" : ContainerUtils.FIELD_DELIMITER);
                sb4.append("user0=1");
                str = sb4.toString();
            }
            StringBuilder sb5 = new StringBuilder();
            sb5.append(str);
            sb5.append(TextUtils.isEmpty(str) ? "" : ContainerUtils.FIELD_DELIMITER);
            sb5.append("e=");
            sb5.append(th.toString());
            sb = sb5.toString();
            i = -1001;
        }
        if (i == 0) {
            this.h = qBPatchStat;
            QBPatchContext.a().setLong("patch_setting_service_start", System.currentTimeMillis());
            b().sendMessageDelayed(b().obtainMessage(5, this.e.g), 1800000L);
            QBPatchStat.a("BONPH_NT_STARTED");
            return;
        }
        a("热补丁合成启动失败 returnCode=" + i);
        if (i == -3) {
            String tinkerRunningPatchTask = ShareTinkerInternals.tinkerRunningPatchTask(QBPatchContext.f69232a);
            long j = QBPatchContext.a().getLong("patch_setting_service_start", 0L);
            long currentTimeMillis = System.currentTimeMillis() - j;
            Logs.c("QBPatch.Agent", "runningPatchTask = " + tinkerRunningPatchTask + " patchServiceStart = " + j + " elapsed = " + currentTimeMillis);
            if (j <= 0 || currentTimeMillis <= 0 || currentTimeMillis >= SystemClock.elapsedRealtime()) {
                if (tinkerRunningPatchTask != null) {
                }
                c2 = 65535;
            } else {
                if (currentTimeMillis < 1800000) {
                    if (tinkerRunningPatchTask != null && tinkerRunningPatchTask.contains(this.e.g)) {
                        c2 = 0;
                    }
                    c2 = 65535;
                }
                c2 = 1;
            }
            if (c2 == 65535) {
                StringBuilder sb6 = new StringBuilder();
                sb6.append(sb);
                if (TextUtils.isEmpty(sb)) {
                    str3 = "";
                }
                sb6.append(str3);
                sb6.append("task=");
                sb6.append(tinkerRunningPatchTask);
                sb6.append("&elapse=");
                sb6.append(currentTimeMillis);
                qBPatchStat.a("3006", "", sb6.toString());
                return;
            }
            if (c2 == 0) {
                if (b().hasMessages(5)) {
                    return;
                }
                if (this.h == null) {
                    this.h = qBPatchStat;
                }
                b().sendMessageDelayed(b().obtainMessage(5, this.e.g), 1800000 - currentTimeMillis);
                return;
            }
            if (c2 != 1) {
                return;
            }
            if (this.h == null && tinkerRunningPatchTask != null && tinkerRunningPatchTask.contains(this.e.g)) {
                this.h = qBPatchStat;
            }
            b().removeMessages(5);
            b().obtainMessage(5, this.e.g).sendToTarget();
            return;
        }
        if (i == -7) {
            qBPatchStat.a("3008", null, null);
            QBPatchContext.a().setString("patch_setting_reset_retry_count", CommonUtils.b());
            return;
        }
        if (i == -101) {
            String tinkerVersionDisableReason = ShareTinkerInternals.tinkerVersionDisableReason(QBPatchContext.f69232a);
            StringBuilder sb7 = new StringBuilder();
            sb7.append(sb);
            if (TextUtils.isEmpty(sb)) {
                str3 = "";
            }
            sb7.append(str3);
            sb7.append("reason=");
            sb7.append(tinkerVersionDisableReason);
            String sb8 = sb7.toString();
            if (tinkerVersionDisableReason.contains("checkSafeModeCount")) {
                try {
                    String string2 = QBPatchContext.f69232a.getSharedPreferences("tinker_own_config_all", 0).getString(ShareConstants.TINKER_SAFE_MODE_EXCP, "");
                    if (TextUtils.isEmpty(string2)) {
                        string2 = QBPatchContext.f69232a.getSharedPreferences(ShareConstants.TINKER_OWN_PREFERENCE_CONFIG_PREFIX + QBTinkerUtils.d(), 0).getString(ShareConstants.TINKER_SAFE_MODE_EXCP, "");
                    }
                    if (TextUtils.isEmpty(string2)) {
                        str2 = sb8;
                    } else {
                        str2 = sb8 + "&excp=" + string2;
                    }
                    try {
                        QBPatchContext.f69235d.d("QBPatch.Agent", "safeModeExcp=" + string2, new Object[0]);
                        sb = str2;
                    } catch (Throwable th2) {
                        th = th2;
                        sb8 = str2;
                        QBPatchContext.f69235d.e("QBPatch.Agent", "get safe mode exception fail", th);
                        sb = sb8 + "&excp=" + th;
                        qBPatchStat.a("3004", "" + i, sb);
                    }
                } catch (Throwable th3) {
                    th = th3;
                }
            } else {
                sb = sb8;
            }
        }
        qBPatchStat.a("3004", "" + i, sb);
    }

    private static boolean m() {
        return GetTask.a(QBPatchContext.f69232a, 255);
    }

    public WUPRequest a(int i) {
        String str;
        String str2;
        String str3;
        if (!QBTinkerUtils.f()) {
            Logs.c("QBPatch.Agent", "tinker is disabled");
            return null;
        }
        int e = QBTinkerUtils.e();
        String str4 = new String[]{"", "appload", "pushcmd", "overnight", "retry", "debugurl", "appfg", "10sec"}[i];
        Logs.c("QBPatch.Agent", "startAgent(" + str4 + ") proc=" + QBPatchUtils.a(e));
        if (e != 1) {
            return null;
        }
        QBPatchStat.b("startFlagString", str4);
        if (i == 7 && this.m != 0) {
            return null;
        }
        if (HookDetector.a()) {
            QBPatchStat.b("hooked", true);
            Logs.c("QBPatch.Agent", "hit hookdetector");
            if (TinkerApplicationHelper.b(QBPatchContext.f69233b)) {
                j();
            } else {
                QBPatchStat.a("BONPH_HOOK");
            }
            return null;
        }
        AppBroadcastReceiver.a().a(this);
        ActivityHandler.b().a(this);
        QBPatchStat.a("BONPH_RQ_START");
        QBPatchStat.a("BONPH_RQ_START_" + i);
        this.m = System.currentTimeMillis() + 28800000;
        if (i == 5 && QBTinkerUtils.a()) {
            File file = new File(FileUtils.n(), "patch_signed.apk");
            if (file.exists() && file.isFile() && file.length() > 0) {
                if (a() != null) {
                    QBPatchInfo a2 = QBPatchInfo.a(this.e);
                    a2.g = Md5Utils.a(file);
                    a2.e = QBTinkerUtils.a(QBTinkerUtils.c()) + 1;
                    File a3 = a(a2);
                    if (a3.exists() && !a3.delete()) {
                        str2 = "拷贝测试补丁失败[2]";
                    } else {
                        if (FileUtils.c(file.getAbsolutePath(), a3.getAbsolutePath())) {
                            a("加载本地构造的测试数据,绕过拉取配置和下载补丁的步骤...");
                            String a4 = QBPatchUtils.a();
                            if (TextUtils.isEmpty(a2.f69236a)) {
                                str3 = a4;
                            } else {
                                str3 = a2.f69236a.substring(3) + a2.f69236a.substring(0, 3);
                            }
                            a2.f69236a = str3;
                            a2.f69237b = 3;
                            a2.f = (int) a3.length();
                            a2.f69238c = QBTinkerUtils.a(QBTinkerUtils.b());
                            a2.f69239d = a4;
                            a2.i = "https://softimtt.myapp.com/browser/plugin/big_file/SourceHanSansCN-Normal.ttf";
                            a2.j = 1;
                            a2.h = a2.i.replace(UriUtil.HTTPS_SCHEME, UriUtil.HTTP_SCHEME);
                            b().obtainMessage(107, a2).sendToTarget();
                            return null;
                        }
                        str2 = "拷贝测试补丁失败[1]";
                    }
                } else {
                    str2 = "拷贝测试补丁文件失败[3]";
                }
                a(str2);
            } else {
                File file2 = new File(FileUtils.n(), "patch_rollback");
                if (file2.exists()) {
                    file2.delete();
                    a("加载本地构造的测试数据,回滚当前加载的补丁...");
                    QBPatchInfo a5 = QBPatchInfo.a(this.e);
                    String a6 = QBPatchUtils.a();
                    if (TextUtils.isEmpty(a5.f69236a)) {
                        str = a6;
                    } else {
                        str = this.e.f69236a.substring(3) + this.e.f69236a.substring(0, 3);
                    }
                    a5.f69236a = str;
                    a5.f69237b = 5;
                    a5.f = 0;
                    a5.f69238c = QBTinkerUtils.a(QBTinkerUtils.b());
                    a5.f69239d = a6;
                    a5.i = null;
                    a5.e = 0;
                    a5.j = 1;
                    a5.h = null;
                    a5.g = null;
                    b().obtainMessage(102, new Object[]{d(), a5}).sendToTarget();
                    return null;
                }
                Logs.c("QBPatch.Agent", "没有发现测试补丁 qbdir=" + FileUtils.n().getAbsolutePath());
            }
        }
        try {
            if (QBPatchSwitch.openAndDebug()) {
                QBPatchTestAssist.a();
            }
        } catch (Throwable th) {
            Logs.a("QBPatch.Agent", th);
        }
        WUPRequest d2 = d();
        if (i == 1 && !QBPatchTestAssist.a("patch_debug_req_env_switch")) {
            ((QBPatchStat) d2.getBindObject()).a("multi_wup", (Object) "1");
            return d2;
        }
        a(str4 + "触发拉取热补丁配置...");
        ((QBPatchStat) d2.getBindObject()).a("multi_wup", (Object) "2");
        if (i != 4) {
            b().removeMessages(3);
        }
        boolean send = WUPTaskProxy.send(d2);
        if (!send) {
            Logs.c("QBPatch.Agent", "发送wup失败");
        }
        if (!send && this.e != null) {
            Logs.c("QBPatch.Agent", "发送wup失败,流程继续");
            g();
        }
        return null;
    }

    File a(QBPatchInfo qBPatchInfo) {
        String replaceAll = QBTinkerUtils.b().replaceAll("\\.", "");
        return new File(a(), replaceAll + Constants.ACCEPT_TIME_SEPARATOR_SERVER + qBPatchInfo.e + Constants.ACCEPT_TIME_SEPARATOR_SERVER + qBPatchInfo.g + ".apk");
    }

    void a(int i, String... strArr) {
        if (this.p.contains(Integer.valueOf(i))) {
            Logs.c("QBPatch.Agent", "reportKill: duplicate, result = " + i);
            return;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("result", "" + i);
        StatManager.b().b("QBPATCH_KILL", hashMap);
        this.p.add(Integer.valueOf(i));
    }

    public void a(PatchResult patchResult) {
        if (!QBTinkerUtils.f()) {
            Logs.c("QBPatch.Agent", "tinker is disabled");
            return;
        }
        Logs.c("QBPatch.Agent", "notifyPatchReady(" + patchResult + ") info=" + this.e);
        StringBuilder sb = new StringBuilder();
        sb.append("BONPH_NC_");
        sb.append(m() ? "B_3" : "B_4");
        QBPatchStat.a(sb.toString());
        StatManager.b().b(true);
        b().obtainMessage(103, patchResult).sendToTarget();
    }

    Handler b() {
        if (this.f69230c == null) {
            synchronized (this) {
                if (this.f69230c == null) {
                    Looper looperForRunShortTime = BrowserExecutorSupplier.getLooperForRunShortTime();
                    if (looperForRunShortTime == null) {
                        try {
                            looperForRunShortTime = BrowserExecutorSupplier.getBusinessLooper("QBPatch.Agent");
                        } catch (Throwable unused) {
                        }
                    }
                    Logs.c("QBPatch.Agent", "threadHandler tid=" + looperForRunShortTime.getThread().getId());
                    this.f69230c = new Handler(looperForRunShortTime, this);
                }
            }
        }
        return this.f69230c;
    }

    @Override
    public boolean handleMessage(Message message) {
        int i = message.what;
        switch (i) {
            case 1:
                Toast.makeText(ContextHolder.getAppContext(), (String) message.obj, 1).show();
                return true;
            case 2:
                Logs.c("QBPatch.Agent", "MSG_GENERAL_DELAY");
                g();
                return true;
            case 3:
                a(4);
                return true;
            case 4:
                Logs.c("QBPatch.Agent", "MSG_FLOWCTRL_TIMEOUT mFlowCtrlUrl=" + this.f + " patch=" + this.e);
                DownloadServiceManager.a().removeFlowCtrlTask((byte) 1);
                this.f = null;
                if (!a(false) && this.k.a()) {
                    b().sendEmptyMessageDelayed(2, this.k.b());
                }
                return true;
            case 5:
                if (message.obj != null) {
                    try {
                        Logs.c("QBPatch.Agent", "MSG_PATCH_TIMEOUT: " + message.obj);
                        int myPid = Process.myPid();
                        if (QBPatchContext.f69232a != null && myPid > 0) {
                            ActivityManager activityManager = (ActivityManager) QBPatchContext.f69232a.getSystemService("activity");
                            String a2 = QBTinkerUtils.a((Class<? extends Service>) TinkerPatchService.class);
                            Iterator<ActivityManager.RunningAppProcessInfo> it = activityManager.getRunningAppProcesses().iterator();
                            while (true) {
                                if (it.hasNext()) {
                                    ActivityManager.RunningAppProcessInfo next = it.next();
                                    if (next.processName != null && next.processName.equals(a2) && next.pid != myPid) {
                                        Logs.c("QBPatch.Agent", "MSG_PATCH_TIMEOUT: " + message.obj + " pid=" + next.pid);
                                        Process.killProcess(next.pid);
                                        if (this.h != null) {
                                            String tinkerRunningPatchTask = ShareTinkerInternals.tinkerRunningPatchTask(QBPatchContext.f69232a);
                                            long j = QBPatchContext.a().getLong("patch_setting_service_start", 0L);
                                            this.h.a("3005", null, "task=" + tinkerRunningPatchTask + "&start=" + j + "&elapse" + (System.currentTimeMillis() - j));
                                        }
                                        this.h = null;
                                        if (this.e.f69237b == 3) {
                                            this.e.f69237b = 1;
                                            g();
                                        }
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        Logs.e("QBPatch.Agent", "MSG_PATCH_TIMEOUT: " + message.obj + " exception:" + e.getMessage());
                    }
                }
                return true;
            case 6:
                l();
                return true;
            default:
                switch (i) {
                    case 100:
                        c();
                        return true;
                    case 101:
                        e();
                        return true;
                    case 102:
                        SimpleCostTime.a("getHotPatchRsp onWUPTaskSuccess.MSG_CALL_RECV_CFG");
                        a((WUPRequestBase) a(message.obj, WUPRequestBase.class), (QBPatchInfo) a(message.obj, QBPatchInfo.class));
                        SimpleCostTime.a("MultiWUPRequestTimeCost", "getHotPatchRsp onWUPTaskSuccess.MSG_CALL_RECV_CFG", "getHotPatchRsp onWUPTaskSuccess.MSG_CALL_RECV_CFG");
                        return true;
                    case 103:
                        b((PatchResult) a(message.obj, PatchResult.class));
                        return true;
                    case 104:
                        a((DownloadTask) a(message.obj, DownloadTask.class));
                        return true;
                    case 105:
                        b((DownloadTask) a(message.obj, DownloadTask.class));
                        return true;
                    case 106:
                        if (!a(false) && this.k.a()) {
                            b().sendEmptyMessageDelayed(2, this.k.b());
                        }
                        return true;
                    case 107:
                        QBPatchInfo qBPatchInfo = (QBPatchInfo) a(message.obj, QBPatchInfo.class);
                        this.e = qBPatchInfo;
                        QBPatchInfo.a(qBPatchInfo, "patch_info_");
                        g();
                        return true;
                    case 108:
                        if (m()) {
                            a(2, new String[0]);
                        } else {
                            b(true);
                        }
                        return true;
                    case 109:
                        synchronized (this.f69228a) {
                            b(false);
                            this.f69228a.notifyAll();
                        }
                        return true;
                    case 110:
                        j();
                        return true;
                    default:
                        return false;
                }
        }
    }

    @Override
    public void onApplicationState(ActivityHandler.State state) {
        if (QBTinkerUtils.f() && QBTinkerUtils.e() == 1) {
            Logs.c("QBPatch.Agent", "current application state = " + state + " mIsFinishing = " + this.n);
            b().removeMessages(108);
            int i = AnonymousClass1.f69231a[state.ordinal()];
            if (i == 1) {
                if (this.n) {
                    return;
                }
                if (QBTinkerUtils.i() < 3) {
                    Logs.c("QBPatch.Agent", "check application state in 5 seconds");
                    b().sendEmptyMessageDelayed(108, 5000L);
                    return;
                }
                try {
                    a(3, new String[0]);
                    Logs.c("QBPatch.Agent", "fail count too much, won't kill QB process automatically");
                    return;
                } catch (NullPointerException e) {
                    Logs.a("QBPatch.Agent", (Throwable) e);
                    return;
                }
            }
            if (i == 2) {
                this.n = true;
                synchronized (this.f69228a) {
                    b().obtainMessage(109, this.f69228a).sendToTarget();
                    try {
                        this.f69228a.wait(1000L);
                    } catch (InterruptedException unused) {
                    }
                }
                return;
            }
            if (i != 3) {
                return;
            }
            long currentTimeMillis = System.currentTimeMillis() + 28800000;
            Logs.c("QBPatch.Agent", "application foreground, mLastDate=" + CommonUtils.a(this.m, "yyyy-MM-dd_HH:mm") + " current=" + currentTimeMillis);
            if ((currentTimeMillis / DateUtils.ONE_HOUR) - (this.m / DateUtils.ONE_HOUR) < 2) {
                return;
            }
            a(6);
        }
    }

    @Override
    public void onBroadcastReceiver(Intent intent) {
        if (intent.getAction().equals(IMessageCenter.MSG_SYS_CONNECTIVITY_ACTION) && QBTinkerUtils.f()) {
            b().sendEmptyMessage(100);
        }
    }

    @Override
    public void onFlowCtrlDelayed(int i, long j) {
        if (i == 1) {
            Logs.c("QBPatch.Agent", "onFlowCtrlDelayed(" + i + ", " + j + ")");
            b().removeMessages(4);
            b().sendEmptyMessageDelayed(4, j + Constants.ASSEMBLE_PUSH_NETWORK_INTERVAL);
        }
    }

    @Override
    public boolean onFlowCtrlStartDownload(int i) {
        if (i != 1) {
            return false;
        }
        Logs.c("QBPatch.Agent", "onFlowCtrlStartDownload(" + i + ")");
        b().removeMessages(4);
        this.f = null;
        b().sendEmptyMessage(106);
        return true;
    }

    @Override
    public void onTaskCompleted(DownloadTask downloadTask) {
        if (a(this.g, downloadTask)) {
            Logs.c("QBPatch.Agent", "onTaskCompleted: " + downloadTask.k());
            b().obtainMessage(104, downloadTask).sendToTarget();
        }
    }

    @Override
    public void onTaskCreated(DownloadTask downloadTask) {
    }

    @Override
    public void onTaskFailed(DownloadTask downloadTask, DownloadErrorDetail downloadErrorDetail) {
        if (a(this.g, downloadTask)) {
            Logs.c("QBPatch.Agent", "onTaskFailed() url=" + downloadTask.k());
            b().obtainMessage(105, downloadTask).sendToTarget();
            QBPatchStat.a("BONPH_DL_FIN");
        }
    }

    @Override
    public void onTaskPaused(DownloadTask downloadTask, PauseReason pauseReason) {
    }

    @Override
    public void onTaskProgress(DownloadTask downloadTask) {
        if (a(this.g, downloadTask)) {
            Logs.c("QBPatch.Agent", "onTaskProgress: " + downloadTask.V() + " url=" + downloadTask.k());
        }
    }

    @Override
    public void onTaskRemoved(DownloadTask downloadTask) {
    }

    @Override
    public void onTaskStarted(DownloadTask downloadTask) {
        if (a(this.g, downloadTask)) {
            Logs.c("QBPatch.Agent", "onTaskStarted: " + downloadTask.k());
        }
    }

    @Override
    public void onTaskWaiting(DownloadTask downloadTask) {
    }

    @Override
    public void onWUPTaskFail(WUPRequestBase wUPRequestBase) {
        Logs.c("QBPatch.Agent", "onWUPTaskFail() e=" + wUPRequestBase.getErrorCode() + Constants.ACCEPT_TIME_SEPARATOR_SP + wUPRequestBase.getFailedReason());
        boolean b2 = QBPatchUtils.b();
        HashMap hashMap = new HashMap();
        hashMap.put("isNetAvailable", Boolean.valueOf(b2));
        hashMap.put("wupRequestBase.getErrorCode()", Integer.valueOf(wUPRequestBase.getErrorCode()));
        hashMap.put("wupRequestBase.getFailedReason()", wUPRequestBase.getFailedReason());
        if (b2 && wUPRequestBase.getErrorCode() != -2011) {
            int requstID = wUPRequestBase.getRequstID();
            Integer num = this.i.get(Integer.valueOf(requstID));
            hashMap.put("reqId", Integer.valueOf(requstID));
            hashMap.put("retryCnt", num);
            if (num != null && num.intValue() > 0) {
                Integer valueOf = Integer.valueOf(num.intValue() - 1);
                if (valueOf.intValue() > 0) {
                    this.i.put(Integer.valueOf(requstID), valueOf);
                } else {
                    this.i.remove(Integer.valueOf(requstID));
                }
                ((QBPatchStat) wUPRequestBase.getBindObject()).a("multi_wup", (Object) "2");
                if (WUPTaskProxy.send(wUPRequestBase)) {
                    Logs.c("QBPatch.Agent", "wup剩余重试次数@" + requstID + ContainerUtils.KEY_VALUE_DELIMITER + valueOf);
                    QBPatchStat.b("onWUPTaskFail-retry", hashMap);
                    return;
                }
                Logs.c("QBPatch.Agent", "重试wup失败");
                this.i.remove(Integer.valueOf(requstID));
            }
        }
        String str = b2 ? IUserServiceExtension.SERVICE_TYPE_NOVEL : IUserServiceExtension.SERVICE_TYPE_GAME;
        String str2 = "code=" + wUPRequestBase.getErrorCode() + "&reason=" + wUPRequestBase.getFailedReason();
        ((QBPatchStat) wUPRequestBase.getBindObject()).a(str, "" + wUPRequestBase.getErrorCode(), str2);
        QBPatchStat.b("onWUPTaskFail", hashMap);
        b().sendEmptyMessage(101);
    }

    @Override
    public void onWUPTaskSuccess(WUPRequestBase wUPRequestBase, WUPResponseBase wUPResponseBase) {
        SimpleCostTime.a("getHotPatchRsp onWUPTaskSuccess");
        Logs.c("QBPatch.Agent", "onWUPTaskSuccess()");
        if (wUPResponseBase == null) {
            Logs.c("QBPatch.Agent", "response==null");
            ((QBPatchStat) wUPRequestBase.getBindObject()).a(IUserServiceExtension.SERVICE_TYPE_VIDEO, "1", null);
            b().sendEmptyMessage(101);
            return;
        }
        GetHotPatchRsp getHotPatchRsp = (GetHotPatchRsp) wUPResponseBase.get("getHotPatchRsp");
        if (!QBPatchInfo.a(getHotPatchRsp)) {
            Logs.c("QBPatch.Agent", "rsp==null");
            ((QBPatchStat) wUPRequestBase.getBindObject()).a(IUserServiceExtension.SERVICE_TYPE_VIDEO, getHotPatchRsp == null ? "2" : "3", null);
            b().sendEmptyMessage(101);
            return;
        }
        this.j.c();
        this.i.remove(Integer.valueOf(wUPRequestBase.getRequstID()));
        b().removeMessages(3);
        QBPatchInfo b2 = QBPatchInfo.b(getHotPatchRsp);
        Logs.c("QBPatch.Agent", "onWUPTaskSuccess() rsp=" + b2);
        QBPatchStat.b("received", b2);
        b().obtainMessage(102, new Object[]{wUPRequestBase, b2}).sendToTarget();
        SimpleCostTime.a("MultiWUPRequestTimeCost", "getHotPatchRsp onWUPTaskSuccess", "getHotPatchRsp onWUPTaskSuccess");
    }
}