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

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


package com.tencent.mtt.docscan.plugin;

import android.text.TextUtils;
import com.tencent.common.plugin.exports.IQBPluginSystem;
import com.tencent.common.plugin.exports.IQBPluginSystemCallback;
import com.tencent.common.plugin.exports.QBPlugin;
import com.tencent.common.plugin.exports.QBPluginItemInfo;
import com.tencent.common.threadpool.BrowserExecutorSupplier;
import com.tencent.common.utils.FileUtils;
import com.tencent.mtt.ContextHolder;
import com.tencent.mtt.browser.download.core.facade.IBusinessDownloadService;
import com.tencent.mtt.browser.download.core.facade.OverwritePolicy;
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.PauseReason;
import com.tencent.mtt.browser.download.engine.RemovePolicy;
import com.tencent.mtt.docscan.pagebase.DocScanLogHelper;
import com.tencent.mtt.docscan.utils.DocScanUtils;
import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.tensorflow.lite.QBTFliteManager;

public class DocScanLibServiceProxy implements DownloadTaskListener, QBTFliteManager.TflitePluginListener {
    private static float f = 0.75f;
    private static float g = 0.14f;
    private static float h = 0.1f;
    private static final Lock i = new ReentrantLock();

    private boolean f49968a;

    private volatile boolean f49969b;

    private volatile int f49970c;

    private String f49971d;
    private volatile int e;
    private final List<IDocScanLibPluginListener> j;
    private final AtomicInteger k;
    private final AtomicInteger l;
    private final AtomicInteger m;

    public static class InstanceHolder {

        private static final DocScanLibServiceProxy f49974a = new DocScanLibServiceProxy();

        private InstanceHolder() {
        }
    }

    @interface PluginFrom {
    }

    private DocScanLibServiceProxy() {
        this.f49968a = false;
        this.f49969b = false;
        this.f49970c = 0;
        this.f49971d = "";
        this.e = 0;
        this.j = new LinkedList();
        this.k = new AtomicInteger(0);
        this.l = new AtomicInteger(0);
        this.m = new AtomicInteger();
    }

    public static DocScanLibServiceProxy a() {
        return InstanceHolder.f49974a;
    }

    public void a(int i2, String str) {
        i.lock();
        try {
            this.f49968a = false;
            Iterator<IDocScanLibPluginListener> it = c(true).iterator();
            while (it.hasNext()) {
                it.next().a(i2, str);
            }
            DocScanLogHelper.a("DocScanLibServiceProxy", "Load plugin error, status=" + i2 + "\n Reason:" + str);
        } finally {
            i.unlock();
        }
    }

    public void a(File file) {
        String str;
        if (file == null) {
            str = "From dir not valid.";
        } else {
            File file2 = new File(FileUtils.a(ContextHolder.getAppContext()).getAbsolutePath(), "DocScanPluginDebug");
            if (file2.exists() || file2.mkdirs() || file2.exists()) {
                if (!FileUtils.a(file.getAbsolutePath(), file2.getAbsolutePath())) {
                    a(-1, "Cannot copy files from " + file + " to " + file2);
                    return;
                }
                i.lock();
                try {
                    this.f49971d = file2.getAbsolutePath();
                    i.unlock();
                    i();
                    return;
                } catch (Throwable th) {
                    i.unlock();
                    throw th;
                }
            }
            str = "Cannot make toDir " + file2;
        }
        a(-1, str);
    }

    public void b(int i2) {
        int round = Math.round(i2 * f);
        this.f49970c = round;
        Iterator<IDocScanLibPluginListener> it = c(false).iterator();
        while (it.hasNext()) {
            it.next().a(round);
        }
    }

    private void b(boolean z) {
        IQBPluginSystem pluginSystem = QBPlugin.getPluginSystem();
        if (pluginSystem == null) {
            a(-1, "Cannot get pluginSystem!");
        } else {
            if (pluginSystem.getPluginInfo("com.tencent.qb.plugin.docscan", 1) == null) {
                a(-1, "PluginInfo is not available.");
                return;
            }
            IQBPluginSystemCallback h2 = h();
            DocScanLogHelper.a("DocScanLibServiceProxy", "loadPluginFromPluginSystem:正式加载");
            pluginSystem.usePluginAsync("com.tencent.qb.plugin.docscan", 1, h2, null, null, 1);
        }
    }

    public List<IDocScanLibPluginListener> c(boolean z) {
        i.lock();
        try {
            LinkedList linkedList = new LinkedList(this.j);
            if (z) {
                this.j.clear();
            }
            return linkedList;
        } finally {
            i.unlock();
        }
    }

    private void f() {
        BrowserExecutorSupplier.forIoTasks().execute(new Runnable() {
            @Override
            public void run() {
                DocScanLibServiceProxy.this.b(0);
                DocScanLibServiceProxy.this.a(new File(FileUtils.b(), "DocScanPlugin"));
                DocScanLibServiceProxy.this.i();
            }
        });
    }

    private void g() {
        this.f49970c = 0;
        Iterator<IDocScanLibPluginListener> it = c(false).iterator();
        while (it.hasNext()) {
            it.next().a(0);
        }
        this.k.set(0);
        this.l.set(0);
        this.m.set(0);
        IBusinessDownloadService a2 = DownloadServiceManager.a();
        a2.addTaskListener(this);
        a2.removeDownloadTask("https://static.res.qq.com/nav/file/docScanPlugins/libqbDocScan.so", RemovePolicy.DELETE_TASK_AND_FILE);
        a2.removeDownloadTask("https://static.res.qq.com/nav/file/docScanPlugins/ocr_lite_model.tflite", RemovePolicy.DELETE_TASK_AND_FILE);
        File file = new File(DocScanUtils.c(), "DebugPlugin");
        DownloadInfo downloadInfo = new DownloadInfo();
        downloadInfo.f37604a = "https://static.res.qq.com/nav/file/docScanPlugins/libqbDocScan.so";
        downloadInfo.j = false;
        downloadInfo.f = file.getAbsolutePath();
        downloadInfo.H |= 32;
        a2.startDownloadTask(downloadInfo, OverwritePolicy.DIRECTED_OVER_WRITE, null);
        DownloadInfo downloadInfo2 = new DownloadInfo();
        downloadInfo2.f37604a = "https://static.res.qq.com/nav/file/docScanPlugins/ocr_lite_model.tflite";
        downloadInfo2.j = false;
        downloadInfo2.H |= 32;
        downloadInfo2.f = file.getAbsolutePath();
        a2.startDownloadTask(downloadInfo2, OverwritePolicy.DIRECTED_OVER_WRITE, null);
    }

    private IQBPluginSystemCallback h() {
        return new IQBPluginSystemCallback() {
            @Override
            public void onDownloadCreateed(String str, String str2) {
            }

            @Override
            public void onDownloadProgress(String str, int i2, int i3) {
                DocScanLibServiceProxy.this.b(i3);
            }

            @Override
            public void onDownloadStart(String str, int i2) {
            }

            @Override
            public void onDownloadSuccessed(String str, String str2) {
                int round = Math.round(DocScanLibServiceProxy.f * 100.0f);
                DocScanLibServiceProxy.this.f49970c = round;
                Iterator it = DocScanLibServiceProxy.this.c(false).iterator();
                while (it.hasNext()) {
                    ((IDocScanLibPluginListener) it.next()).a(round);
                }
            }

            @Override
            public void onNeedDownloadNotify(String str, boolean z) {
            }

            @Override
            public void onPrepareFinished(String str, QBPluginItemInfo qBPluginItemInfo, int i2, int i3) {
                if (i3 != 0) {
                    DocScanLibServiceProxy.this.a(i3, "DocScanLib so加载失败,errorCode=" + i3 + ", status=" + i2);
                    return;
                }
                if (qBPluginItemInfo == null) {
                    DocScanLibServiceProxy.this.a(i3, "ErrorCode=0, but pluginInfo==null! Status=" + i2);
                    return;
                }
                if (TextUtils.isEmpty(qBPluginItemInfo.mUnzipDir)) {
                    DocScanLibServiceProxy.this.a(i3, "ErrorCode=0, but unzipPath is empty! Status=" + i2);
                    return;
                }
                DocScanLibServiceProxy.i.lock();
                try {
                    DocScanLibServiceProxy.this.f49971d = qBPluginItemInfo.mUnzipDir;
                    DocScanLibServiceProxy.i.unlock();
                    DocScanLibServiceProxy.this.i();
                } catch (Throwable th) {
                    DocScanLibServiceProxy.i.unlock();
                    throw th;
                }
            }

            @Override
            public void onPrepareStart(String str) {
                DocScanLibServiceProxy.this.b(0);
            }
        };
    }

    public void i() {
        int round = Math.round((f + g) * 100.0f);
        this.f49970c = round;
        Iterator<IDocScanLibPluginListener> it = c(false).iterator();
        while (it.hasNext()) {
            it.next().a(round);
        }
        QBTFliteManager.getInstance().loadPlugin(ContextHolder.getAppContext(), this);
    }

    public DocScanEngine a(boolean z) {
        DocScanLogHelper.a("DocScanLibServiceProxy", "newEngine needClassifier: ");
        return new DocScanEngine(z);
    }

    public void a(int i2) {
        if (this.e != i2) {
            synchronized (this) {
                this.e = i2;
                this.f49969b = false;
            }
        }
    }

    public void a(DocScanEngine docScanEngine) {
        docScanEngine.c();
    }

    public void a(IDocScanLibPluginListener iDocScanLibPluginListener) {
        a(false, iDocScanLibPluginListener);
    }

    public void a(boolean z, IDocScanLibPluginListener iDocScanLibPluginListener) {
        DocScanLogHelper.a("DocScanLibServiceProxy", "loadPluginAsync");
        if (this.f49969b) {
            if (iDocScanLibPluginListener != null) {
                iDocScanLibPluginListener.b();
                return;
            }
            return;
        }
        i.lock();
        try {
            if (!this.f49969b) {
                if (iDocScanLibPluginListener != null) {
                    this.j.add(iDocScanLibPluginListener);
                }
                if (!this.f49968a) {
                    this.f49968a = true;
                    this.f49970c = 0;
                    Iterator<IDocScanLibPluginListener> it = c(false).iterator();
                    while (it.hasNext()) {
                        it.next().a(0);
                    }
                    int i2 = this.e;
                    if (i2 == 0) {
                        DocScanLogHelper.a("DocScanLibServiceProxy", "loadPluginAsync loadPluginFromPluginSystem");
                        b(z);
                    } else if (i2 == 1) {
                        g();
                    } else if (i2 != 2) {
                        this.f49968a = false;
                    } else {
                        f();
                    }
                    return;
                }
                if (iDocScanLibPluginListener != null) {
                    iDocScanLibPluginListener.a(this.f49970c);
                }
            } else if (iDocScanLibPluginListener != null) {
                iDocScanLibPluginListener.b();
            }
        } finally {
            i.unlock();
        }
    }

    public DocScanEngine b() {
        DocScanLogHelper.a("DocScanLibServiceProxy", "newEngine: ");
        return new DocScanEngine();
    }

    public String c() {
        i.lock();
        try {
            return this.f49971d;
        } finally {
            i.unlock();
        }
    }

    @Override
    public void onDownLoadPluginStart() {
    }

    @Override
    public void onInstallPluginStart() {
        int round = Math.round((f + g + h) * 100.0f);
        this.f49970c = round;
        Iterator<IDocScanLibPluginListener> it = c(false).iterator();
        while (it.hasNext()) {
            it.next().a(round);
        }
    }

    @Override
    public void onLoadPluginFailed() {
        a(-1, "TFLitePlugin 加载失败");
    }

    @Override
    public void onLoadPluginSuc() {
        i.lock();
        try {
            this.f49969b = true;
            this.f49968a = false;
            Iterator<IDocScanLibPluginListener> it = c(true).iterator();
            while (it.hasNext()) {
                it.next().b();
            }
        } finally {
            i.unlock();
        }
    }

    @Override
    public void onTaskCompleted(DownloadTask downloadTask) {
        if (this.f49968a) {
            String j = downloadTask.j();
            if (("https://static.res.qq.com/nav/file/docScanPlugins/ocr_lite_model.tflite".equals(j) || "https://static.res.qq.com/nav/file/docScanPlugins/libqbDocScan.so".equals(j)) && this.m.incrementAndGet() == 2) {
                a(new File(DocScanUtils.c(), "DebugPlugin"));
                i();
            }
        }
    }

    @Override
    public void onTaskCreated(DownloadTask downloadTask) {
    }

    @Override
    public void onTaskFailed(DownloadTask downloadTask, DownloadErrorDetail downloadErrorDetail) {
        if (this.f49968a) {
            String j = downloadTask.j();
            if ("https://static.res.qq.com/nav/file/docScanPlugins/ocr_lite_model.tflite".equals(j) || "https://static.res.qq.com/nav/file/docScanPlugins/libqbDocScan.so".equals(j)) {
                a(-1, "DocScanSo 下载失败");
            }
        }
    }

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

    @Override
    public void onTaskProgress(DownloadTask downloadTask) {
        float f2;
        if (this.f49968a) {
            String j = downloadTask.j();
            if ("https://static.res.qq.com/nav/file/docScanPlugins/ocr_lite_model.tflite".equals(j)) {
                int i2 = this.k.get();
                int V = downloadTask.V();
                this.l.set(downloadTask.V());
                f2 = V + i2;
            } else {
                if (!"https://static.res.qq.com/nav/file/docScanPlugins/libqbDocScan.so".equals(j)) {
                    return;
                }
                int i3 = this.k.get();
                int V2 = downloadTask.V();
                this.l.set(downloadTask.V());
                f2 = i3 + V2;
            }
            b(Math.round(f2 * 0.5f));
        }
    }

    @Override
    public void onTaskRemoved(DownloadTask downloadTask) {
    }

    @Override
    public void onTaskStarted(DownloadTask downloadTask) {
    }

    @Override
    public void onTaskWaiting(DownloadTask downloadTask) {
    }
}