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

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


package com.tencent.mtt.external.pagetoolbox.pdf;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import com.tencent.common.task.Continuation;
import com.tencent.common.task.QBTask;
import com.tencent.common.threadpool.BrowserExecutorSupplier;
import com.tencent.common.utils.FileUtils;
import com.tencent.common.utils.IFileStore;
import com.tencent.common.utils.UrlUtils;
import com.tencent.mtt.ContextHolder;
import com.tencent.mtt.base.notification.facade.IMessageBubbleService;
import com.tencent.mtt.base.notification.facade.NormalMessageBundle;
import com.tencent.mtt.base.skin.MttResources;
import com.tencent.mtt.base.stat.StatManager;
import com.tencent.mtt.base.utils.DeviceUtils;
import com.tencent.mtt.base.utils.permission.PermissionRequest;
import com.tencent.mtt.base.utils.permission.PermissionUtils;
import com.tencent.mtt.browser.file.FileCoreModule;
import com.tencent.mtt.browser.plugin.ui.newskin.BoxLoadingView;
import com.tencent.mtt.browser.window.IWebView;
import com.tencent.mtt.browser.window.UrlParams;
import com.tencent.mtt.browser.window.templayer.QBWebviewWrapper;
import com.tencent.mtt.businesscenter.facade.IFrameworkDelegate;
import com.tencent.mtt.qbcontext.core.QBContext;
import com.tencent.mtt.view.dialog.newui.SimpleDialogBuilder;
import com.tencent.mtt.view.dialog.newui.builder.api.ICommonDialogBuilder;
import com.tencent.mtt.view.dialog.newui.builder.api.base.IDialogBuilderInterface;
import com.tencent.mtt.view.dialog.newui.dialog.DialogBase;
import com.tencent.mtt.view.dialog.newui.view.ViewOnClickListener;
import com.tencent.mtt.view.toast.MttToaster;
import com.xiaomi.mipush.sdk.Constants;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.Callable;

public class PDFUtil {
    public static String a(String str, String str2, String str3) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return null;
        }
        File file = new File(str, str2 + str3);
        String name = file.getName();
        int i = 1;
        if (!TextUtils.equals(name, str2)) {
            file = new File(str, name);
            str2 = file.getName().replace(".pdf", "");
        }
        while (file.exists()) {
            StringBuilder sb = new StringBuilder();
            sb.append(str2);
            sb.append("(");
            sb.append(i);
            sb.append(")");
            i++;
            file = new File(str, sb.toString().replaceAll("%", "") + str3);
        }
        return file.getName();
    }

    public static void a(final IWebView iWebView) {
        if (iWebView instanceof QBWebviewWrapper) {
            PermissionUtils.a(PermissionUtils.a(4), new PermissionRequest.Callback() {
                @Override
                public void onPermissionRequestGranted(boolean z) {
                    PDFUtil.b(IWebView.this);
                }

                @Override
                public void onPermissionRevokeCanceled() {
                    MttToaster.show("SD卡存储”权限被拒绝,无法导出", 1);
                }
            }, true, "SD卡存储”权限被拒绝,无法导出");
        }
    }

    public static boolean a(Bitmap bitmap, File file) {
        FileOutputStream fileOutputStream;
        if (Build.VERSION.SDK_INT >= 19 && bitmap != null) {
            PdfDocument.PageInfo create = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
            PdfDocument pdfDocument = new PdfDocument();
            PdfDocument.Page startPage = pdfDocument.startPage(create);
            startPage.getCanvas().drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null);
            pdfDocument.finishPage(startPage);
            if (file.exists()) {
                file.delete();
            }
            try {
                fileOutputStream = new FileOutputStream(file);
                try {
                    pdfDocument.writeTo(fileOutputStream);
                    fileOutputStream.flush();
                    try {
                        fileOutputStream.close();
                        pdfDocument.close();
                    } catch (IOException unused) {
                    }
                    return true;
                } catch (FileNotFoundException unused2) {
                } catch (IOException unused3) {
                } catch (Throwable th) {
                    th = th;
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException unused4) {
                            throw th;
                        }
                    }
                    pdfDocument.close();
                    throw th;
                }
            } catch (FileNotFoundException unused5) {
                fileOutputStream = null;
            } catch (IOException unused6) {
                fileOutputStream = null;
            } catch (Throwable th2) {
                th = th2;
                fileOutputStream = null;
            }
        }
        return false;
    }

    public static void b(final IWebView iWebView) {
        if (iWebView instanceof QBWebviewWrapper) {
            NormalMessageBundle normalMessageBundle = new NormalMessageBundle();
            BoxLoadingView boxLoadingView = new BoxLoadingView(ContextHolder.getAppContext(), (byte) 3, (byte) 4, (byte) 2);
            boxLoadingView.a(MttResources.s(24), MttResources.s(24), 1);
            boxLoadingView.setText("");
            normalMessageBundle.j = boxLoadingView;
            normalMessageBundle.f32713d = "正在将网页保存为PDF,待网页完全加载完成再保存效果更好";
            final long showNormalBubble = ((IMessageBubbleService) QBContext.getInstance().getService(IMessageBubbleService.class)).showNormalBubble(normalMessageBundle, null);
            QBWebviewWrapper qBWebviewWrapper = (QBWebviewWrapper) iWebView;
            final int af = DeviceUtils.ai() ? DeviceUtils.af() / 3 : DeviceUtils.af() / 2;
            final String url = iWebView.getUrl();
            if (TextUtils.isEmpty(url)) {
                url = "qb://home/?opt=1";
            }
            String pageTitle = qBWebviewWrapper.getPageTitle();
            if (TextUtils.isEmpty(pageTitle)) {
                pageTitle = "网页PDF_" + new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss").format(new Date());
            }
            String absolutePath = FileUtils.n().getAbsolutePath();
            String a2 = a(absolutePath, pageTitle, ".pdf");
            if (TextUtils.isEmpty(a2)) {
                ((IMessageBubbleService) QBContext.getInstance().getService(IMessageBubbleService.class)).closeMyBubble(showNormalBubble);
                MttToaster.show("保存失败,请重试", 0);
                return;
            }
            final File file = new File(absolutePath + File.separator + a2.replaceAll("%", "").replaceAll(Constants.COLON_SEPARATOR, Constants.ACCEPT_TIME_SEPARATOR_SERVER).replaceAll("\\|", "_"));
            QBTask.b((Callable) new Callable<Bitmap>() {
                @Override
                public Bitmap call() throws Exception {
                    return IWebView.this.snapshotWholePageUsingBitmap(af, 0, IWebView.RatioRespect.RESPECT_WIDTH, 5);
                }
            }).a(new Continuation<Bitmap, Object>() {
                @Override
                public Object then(QBTask<Bitmap> qBTask) throws Exception {
                    if (qBTask != null && qBTask.e() != null) {
                        if (qBTask.e() == null || qBTask.e().getByteCount() < 10240 || !PDFUtil.a(qBTask.e(), file)) {
                            MttToaster.show("保存失败,请重试", 0);
                        } else {
                            IFileStore d2 = FileCoreModule.d();
                            if (d2 != null) {
                                d2.b(file);
                                d2.b(file.getAbsolutePath());
                            }
                            PDFUtil.b(url);
                            PDFUtil.b(url, "SavemodulePopup_Show");
                        }
                        ((IMessageBubbleService) QBContext.getInstance().getService(IMessageBubbleService.class)).closeMyBubble(showNormalBubble);
                    }
                    return null;
                }
            }, 0);
        }
    }

    public static void b(final String str) {
        BrowserExecutorSupplier.forMainThreadTasks().execute(new Runnable() {
            @Override
            public void run() {
                ICommonDialogBuilder a2 = SimpleDialogBuilder.a();
                a2.a(IDialogBuilderInterface.ImageStyle.MATCH_MARGIN).a("https://static.res.qq.com/nav/toolbox/save_web2pdf_notify.png").d("PDF已保存").e("可在“文件>文档”查看").a(IDialogBuilderInterface.ButtonOrientation.VERTICAL).a((CharSequence) "去看看").c("好的");
                a2.a_(new ViewOnClickListener() {
                    @Override
                    public void onClick(View view, DialogBase dialogBase) {
                        ((IFrameworkDelegate) QBContext.getInstance().getService(IFrameworkDelegate.class)).doLoad(new UrlParams("qb://tab/file?callFrom=SV_PDF&entry=true&target=5&whichTimesShowBubble=1").d(true));
                        PDFUtil.b(str, "SavemodulePopup_Gotosee_Click");
                        dialogBase.dismiss();
                    }
                });
                a2.c(new ViewOnClickListener() {
                    @Override
                    public void onClick(View view, DialogBase dialogBase) {
                        PDFUtil.b(str, "SavemodulePopup_OkFine_Click");
                        dialogBase.dismiss();
                    }
                });
                a2.e();
            }
        });
    }

    public static void b(String str, String str2) {
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
            return;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("domain", UrlUtils.getHost(str));
        hashMap.put("FileType", "PDF");
        StatManager.b().b(str2, hashMap);
    }
}