// Path: android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/exception/PageRenderingException.java
// public class PageRenderingException extends Exception {
//     private final int page;
// 
//     public PageRenderingException(int page, Throwable cause) {
//         super(cause);
//         this.page = page;
//     }
// 
//     public int getPage() {
//         return page;
//     }
// }
// 
// Path: android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/model/PagePart.java
// public class PagePart {
// 
//     private int page;
// 
//     private Bitmap renderedBitmap;
// 
//     private RectF pageRelativeBounds;
// 
//     private boolean thumbnail;
// 
//     private int cacheOrder;
// 
//     public PagePart(int page, Bitmap renderedBitmap, RectF pageRelativeBounds, boolean thumbnail, int cacheOrder) {
//         super();
//         this.page = page;
//         this.renderedBitmap = renderedBitmap;
//         this.pageRelativeBounds = pageRelativeBounds;
//         this.thumbnail = thumbnail;
//         this.cacheOrder = cacheOrder;
//     }
// 
//     public int getCacheOrder() {
//         return cacheOrder;
//     }
// 
//     public int getPage() {
//         return page;
//     }
// 
//     public Bitmap getRenderedBitmap() {
//         return renderedBitmap;
//     }
// 
//     public RectF getPageRelativeBounds() {
//         return pageRelativeBounds;
//     }
// 
//     public boolean isThumbnail() {
//         return thumbnail;
//     }
// 
//     public void setCacheOrder(int cacheOrder) {
//         this.cacheOrder = cacheOrder;
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (!(obj instanceof PagePart)) {
//             return false;
//         }
// 
//         PagePart part = (PagePart) obj;
//         return part.getPage() == page
//                 && part.getPageRelativeBounds().left == pageRelativeBounds.left
//                 && part.getPageRelativeBounds().right == pageRelativeBounds.right
//                 && part.getPageRelativeBounds().top == pageRelativeBounds.top
//                 && part.getPageRelativeBounds().bottom == pageRelativeBounds.bottom;
//     }
// 
// }


import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.github.barteksc.pdfviewer.exception.PageRenderingException;
import com.github.barteksc.pdfviewer.model.PagePart;

    private boolean running = false;

    RenderingHandler(Looper looper, PDFView pdfView) {
        super(looper);
        this.pdfView = pdfView;
    }

    void addRenderingTask(int page, float width, float height, RectF bounds, boolean thumbnail, int cacheOrder, boolean bestQuality, boolean annotationRendering) {
        RenderingTask task = new RenderingTask(width, height, bounds, page, thumbnail, cacheOrder, bestQuality, annotationRendering);
        Message msg = obtainMessage(MSG_RENDER_TASK, task);
        sendMessage(msg);
    }

    @Override
    public void handleMessage(Message message) {
        RenderingTask task = (RenderingTask) message.obj;
        try {
            final PagePart part = proceed(task);
            if (part != null) {
                if (running) {
                    pdfView.post(new Runnable() {
                        @Override
                        public void run() {
                            pdfView.onBitmapRendered(part);
                        }
                    });
                } else {
                    part.getRenderedBitmap().recycle();
                }
            }

// Path: android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/exception/PageRenderingException.java
// public class PageRenderingException extends Exception {
//     private final int page;
// 
//     public PageRenderingException(int page, Throwable cause) {
//         super(cause);
//         this.page = page;
//     }
// 
//     public int getPage() {
//         return page;
//     }
// }
// 
// Path: android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/model/PagePart.java
// public class PagePart {
// 
//     private int page;
// 
//     private Bitmap renderedBitmap;
// 
//     private RectF pageRelativeBounds;
// 
//     private boolean thumbnail;
// 
//     private int cacheOrder;
// 
//     public PagePart(int page, Bitmap renderedBitmap, RectF pageRelativeBounds, boolean thumbnail, int cacheOrder) {
//         super();
//         this.page = page;
//         this.renderedBitmap = renderedBitmap;
//         this.pageRelativeBounds = pageRelativeBounds;
//         this.thumbnail = thumbnail;
//         this.cacheOrder = cacheOrder;
//     }
// 
//     public int getCacheOrder() {
//         return cacheOrder;
//     }
// 
//     public int getPage() {
//         return page;
//     }
// 
//     public Bitmap getRenderedBitmap() {
//         return renderedBitmap;
//     }
// 
//     public RectF getPageRelativeBounds() {
//         return pageRelativeBounds;
//     }
// 
//     public boolean isThumbnail() {
//         return thumbnail;
//     }
// 
//     public void setCacheOrder(int cacheOrder) {
//         this.cacheOrder = cacheOrder;
//     }
// 
//     @Override
//     public boolean equals(Object obj) {
//         if (!(obj instanceof PagePart)) {
//             return false;
//         }
// 
//         PagePart part = (PagePart) obj;
//         return part.getPage() == page
//                 && part.getPageRelativeBounds().left == pageRelativeBounds.left
//                 && part.getPageRelativeBounds().right == pageRelativeBounds.right
//                 && part.getPageRelativeBounds().top == pageRelativeBounds.top
//                 && part.getPageRelativeBounds().bottom == pageRelativeBounds.bottom;
//     }
// 
// }


// Path: android-pdf-viewer/src/main/java/com/github/barteksc/pdfviewer/RenderingHandler.java
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import com.github.barteksc.pdfviewer.exception.PageRenderingException;
import com.github.barteksc.pdfviewer.model.PagePart;

    private boolean running = false;

    RenderingHandler(Looper looper, PDFView pdfView) {
        super(looper);
        this.pdfView = pdfView;
    }

    void addRenderingTask(int page, float width, float height, RectF bounds, boolean thumbnail, int cacheOrder, boolean bestQuality, boolean annotationRendering) {
        RenderingTask task = new RenderingTask(width, height, bounds, page, thumbnail, cacheOrder, bestQuality, annotationRendering);
        Message msg = obtainMessage(MSG_RENDER_TASK, task);
        sendMessage(msg);
    }

    @Override
    public void handleMessage(Message message) {
        RenderingTask task = (RenderingTask) message.obj;
        try {
            final PagePart part = proceed(task);
            if (part != null) {
                if (running) {
                    pdfView.post(new Runnable() {
                        @Override
                        public void run() {
                            pdfView.onBitmapRendered(part);
                        }
                    });
                } else {
                    part.getRenderedBitmap().recycle();
                }
            }

        } catch (final PageRenderingException ex) {
