// 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;

/**
 * Copyright 2016 Bartosz Schiller
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.barteksc.pdfviewer;



/**
 * A {@link Handler} that will process incoming {@link RenderingTask} messages
 * and alert {@link PDFView#onBitmapRendered(PagePart)} when the portion of the
 * PDF is ready to render.
 */
class RenderingHandler extends Handler {
    /**
     * {@link Message#what} kind of message this handler processes.
     */
    static final int MSG_RENDER_TASK = 1;

    private static final String TAG = RenderingHandler.class.getName();

    private PDFView pdfView;

    private RectF renderBounds = new RectF();
    private Rect roundedRenderBounds = new Rect();
    private Matrix renderMatrix = new Matrix();
    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 {

// 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;

/**
 * Copyright 2016 Bartosz Schiller
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.barteksc.pdfviewer;



/**
 * A {@link Handler} that will process incoming {@link RenderingTask} messages
 * and alert {@link PDFView#onBitmapRendered(PagePart)} when the portion of the
 * PDF is ready to render.
 */
class RenderingHandler extends Handler {
    /**
     * {@link Message#what} kind of message this handler processes.
     */
    static final int MSG_RENDER_TASK = 1;

    private static final String TAG = RenderingHandler.class.getName();

    private PDFView pdfView;

    private RectF renderBounds = new RectF();
    private Rect roundedRenderBounds = new Rect();
    private Matrix renderMatrix = new Matrix();
    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);
