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

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


package com.tencent.mtt.external.novel.pirate;

import android.text.TextUtils;
import com.tencent.common.featuretoggle.FeatureToggle;
import com.tencent.common.task.QBTask;
import com.tencent.common.utils.FileUtils;
import com.tencent.common.utils.StringUtils;
import com.tencent.common.utils.UrlUtils;
import com.tencent.library.BuildConfig;
import com.tencent.mtt.ContextHolder;
import com.tencent.mtt.base.wup.PreferenceData;
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.facade.ResultCallback;
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.external.archiver.IMttArchiver;
import com.tencent.mtt.external.reader.facade.IMttArchiverService;
import com.tencent.mtt.log.access.Logs;
import com.tencent.mtt.qbcontext.core.QBContext;
import com.tencent.mtt.setting.PublicSettingManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SogouNovelJsManager implements DownloadTaskListener {

    static String f55345a = "SogouNovelJsManager";
    static volatile SogouNovelJsManager e;
    private File i;
    private File j;

    String f55346b = "365d0190d53c6dec53360d8e7af1a187";

    int f55347c = 3;

    String f55348d = "";
    private volatile Map<String, String> h = new ConcurrentHashMap();
    volatile JSONObject g = null;
    IBusinessDownloadService f = DownloadServiceManager.a();

    private SogouNovelJsManager() {
        this.f.addTaskListener("https://transrules.sogoucdn.com/trans_rules.json.zip", this);
        this.i = FileUtils.f("sogou_novel_js");
        this.j = FileUtils.f("sogou_novel_js_download");
        c();
        f();
    }

    public static SogouNovelJsManager a() {
        if (e == null) {
            synchronized (SogouNovelJsManager.class) {
                if (e == null) {
                    e = new SogouNovelJsManager();
                }
            }
        }
        return e;
    }

    private String a(File file) {
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            String a2 = a(fileInputStream, "UTF-8");
            fileInputStream.close();
            return a2;
        } catch (FileNotFoundException | IOException unused) {
            return "";
        }
    }

    private String a(InputStream inputStream, String str) {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] bArr = new byte[8192];
            while (true) {
                int read = inputStream.read(bArr);
                if (-1 == read) {
                    return new String(byteArrayOutputStream.toByteArray(), str);
                }
                byteArrayOutputStream.write(bArr, 0, read);
            }
        } catch (IOException unused) {
            return null;
        }
    }

    public static boolean b() {
        return FeatureToggle.a(BuildConfig.FEATURE_TOGGLE_PIRATED_NOVEL_SGRULES);
    }

    private void c() {
        String a2 = PreferenceData.a("KEY_SOGOU_JS_DOWNLOADFILE_UNZIP_PASSWORD");
        if (!TextUtils.isEmpty(a2)) {
            this.f55346b = a2;
        }
        this.f55347c = StringUtils.b(PreferenceData.a("KEY_SOGOU_JS_FILE_DOWNLOAD_INTERVAL"), 3);
        String a3 = PreferenceData.a("KEY_SOGOU_JS_LOADFILE_DOWNLOAD_URL");
        if (TextUtils.isEmpty(a3)) {
            a3 = "https://transrules.sogoucdn.com/trans_rules.json.zip";
        }
        this.f55348d = a3;
    }

    public void d() {
        this.f.removeDownloadTask(this.f55348d, RemovePolicy.DELETE_TASK_AND_FILE);
        File file = this.j;
        if (file == null || !file.exists()) {
            return;
        }
        try {
            FileUtils.c(this.j);
        } catch (IOException unused) {
        }
        DownloadInfo downloadInfo = new DownloadInfo();
        downloadInfo.j = false;
        downloadInfo.h = false;
        downloadInfo.l = false;
        downloadInfo.r = 0;
        downloadInfo.f37604a = this.f55348d;
        downloadInfo.k = false;
        downloadInfo.f = this.j.getAbsolutePath();
        downloadInfo.E = NetworkPolicy.ALL_NETWORK;
        downloadInfo.H |= 32;
        Logs.c(f55345a, "downloadJSFile");
        this.f.startDownloadTask(downloadInfo, OverwritePolicy.DIRECTED_OVER_WRITE, new ResultCallback<DownloadTask>() {
            @Override
            public void onResult(ResultCallback.Result result, DownloadTask downloadTask) {
                Logs.c(SogouNovelJsManager.f55345a, "downloadJSFile onResult" + result.ordinal());
            }
        });
    }

    public boolean e() {
        long abs = Math.abs(System.currentTimeMillis() - PublicSettingManager.a().getLong("sogou_trans_rules_file_update_time", 0L));
        long j = this.f55347c * 86400000;
        String str = f55345a;
        if (abs > j) {
            Logs.c(str, "isNeedUpdate = true");
            return true;
        }
        Logs.c(str, "isNeedUpdate = false");
        return false;
    }

    private void f() {
        QBTask.c(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                if (SogouNovelJsManager.this.e() || !SogouNovelJsManager.this.g()) {
                    SogouNovelJsManager sogouNovelJsManager = SogouNovelJsManager.this;
                    sogouNovelJsManager.g = null;
                    sogouNovelJsManager.d();
                }
                return null;
            }
        });
    }

    public boolean g() {
        File file = this.i;
        if (file == null) {
            return false;
        }
        File file2 = new File(file.getAbsolutePath(), "trans_rules.json");
        if (!file2.exists()) {
            return false;
        }
        try {
            this.g = new JSONObject(a(file2));
            return true;
        } catch (JSONException unused) {
            return true;
        }
    }

    protected void a(final DownloadTask downloadTask) {
        Logs.c(f55345a, "onJsFileDownloadSucc");
        PublicSettingManager.a().setLong("sogou_trans_rules_file_update_time", System.currentTimeMillis());
        QBTask.c(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                try {
                    SogouNovelJsManager.this.a(downloadTask.O());
                    return null;
                } catch (IOException unused) {
                    return null;
                }
            }
        });
    }

    protected void a(String str) throws IOException {
        try {
            if (this.i != null && this.i.exists()) {
                FileUtils.c(this.i);
            }
        } catch (IOException unused) {
        }
        File file = this.i;
        if (file == null || !file.exists()) {
            return;
        }
        IMttArchiver createMttArchiverInstance = ((IMttArchiverService) QBContext.getInstance().getService(IMttArchiverService.class)).createMttArchiverInstance(ContextHolder.getAppContext(), str);
        boolean z = false;
        createMttArchiverInstance.setPassword(this.f55346b);
        if (createMttArchiverInstance != null) {
            try {
                try {
                    if (createMttArchiverInstance.openFile() == 8) {
                        Iterator<IMttArchiver> it = createMttArchiverInstance.childrens().iterator();
                        while (true) {
                            if (!it.hasNext()) {
                                break;
                            }
                            IMttArchiver next = it.next();
                            next.setPassword(this.f55346b);
                            if (next.extract(this.i.getAbsolutePath()) != 0) {
                                z = true;
                                break;
                            }
                        }
                    }
                } catch (IOException e2) {
                    throw e2;
                }
            } finally {
                createMttArchiverInstance.closeFile();
            }
        }
        Logs.c(f55345a, "unZip error = " + z);
        File file2 = new File(this.i.getAbsolutePath(), "trans_rules.json");
        if (file2.exists()) {
            try {
                Logs.c(f55345a, "try to readJsonFile");
                this.g = new JSONObject(a(file2));
            } catch (JSONException unused2) {
            }
        }
    }

    public String b(String str) throws JSONException {
        boolean z;
        Logs.c(f55345a, "getMatchRules url = " + str);
        JSONObject jSONObject = new JSONObject();
        String hostNew = UrlUtils.getHostNew(str);
        if (this.h.containsKey(hostNew)) {
            String str2 = this.h.get(hostNew);
            Logs.c(f55345a, "getMatchRules from cache url = " + str + " cacheRules = " + str2);
            return str2;
        }
        if (this.g == null) {
            return "";
        }
        JSONObject jSONObject2 = this.g;
        int i = 0;
        if (jSONObject2.has("detail_rules")) {
            JSONArray optJSONArray = jSONObject2.optJSONArray("detail_rules");
            JSONArray jSONArray = new JSONArray();
            int i2 = 0;
            while (true) {
                if (i2 >= optJSONArray.length()) {
                    break;
                }
                JSONObject optJSONObject = optJSONArray.optJSONObject(i2);
                if (optJSONObject != null && optJSONObject.has("host") && optJSONObject.optString("host", "").equals(hostNew)) {
                    jSONArray.put(optJSONObject);
                    break;
                }
                i2++;
            }
            if (jSONArray.length() > 0) {
                jSONObject.put("detail_rules", jSONArray);
                z = true;
                if (jSONObject2.has("directory_rules")) {
                    JSONArray optJSONArray2 = jSONObject2.optJSONArray("directory_rules");
                    JSONArray jSONArray2 = new JSONArray();
                    while (true) {
                        if (i >= optJSONArray2.length()) {
                            break;
                        }
                        JSONObject optJSONObject2 = optJSONArray2.optJSONObject(i);
                        if (optJSONObject2 != null && optJSONObject2.has("host") && optJSONObject2.optString("host", "").equals(hostNew)) {
                            jSONArray2.put(optJSONObject2);
                            break;
                        }
                        i++;
                    }
                    if (jSONArray2.length() > 0) {
                        jSONObject.put("directory_rules", jSONArray2);
                        z = true;
                    }
                }
                if (z) {
                    return "";
                }
                String jSONObject3 = jSONObject.toString(2);
                Logs.c(f55345a, "getMatchRules from parse url = " + str + " cacheRules = " + jSONObject3);
                this.h.put(hostNew, jSONObject3);
                return jSONObject3;
            }
        }
        z = false;
        if (jSONObject2.has("directory_rules")) {
        }
        if (z) {
        }
    }

    @Override
    public void onTaskCompleted(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskCompleted");
        a(downloadTask);
    }

    @Override
    public void onTaskCreated(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskCreated");
    }

    @Override
    public void onTaskFailed(DownloadTask downloadTask, DownloadErrorDetail downloadErrorDetail) {
        Logs.c(f55345a, "onTaskFailed");
    }

    @Override
    public void onTaskPaused(DownloadTask downloadTask, PauseReason pauseReason) {
        Logs.c(f55345a, "onTaskPaused");
    }

    @Override
    public void onTaskProgress(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskProgress");
    }

    @Override
    public void onTaskRemoved(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskRemoved");
    }

    @Override
    public void onTaskStarted(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskStarted");
    }

    @Override
    public void onTaskWaiting(DownloadTask downloadTask) {
        Logs.c(f55345a, "onTaskWaiting");
    }
}