package epson.print.pdf;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import com.adobe.mps.MPS;
import epson.common.ExternalFileUtils;
import epson.image.epsonImage;
import epson.print.Util.EPLog;
import java.io.File;
import java.nio.ByteBuffer;

public class AdobePdfContainer {
    private static final String TAG = "AdobePdfContainer";
    private MPS adpbeMPSInterface = new MPS();

    /* renamed from: bm */
    private Bitmap f385bm = null;
    int colorComponent = 4;
    private final int colorspace_ARGB = 1;
    private Context mContext = null;
    private int pageCount = 0;
    ByteBuffer rawBuffer;

    public class PdfException extends Exception {
        public PdfException() {
        }
    }

    public class PasswordException extends PdfException {
        public PasswordException() {
            super();
        }
    }

    public class NoPdfException extends PdfException {
        public NoPdfException() {
            super();
        }
    }

    public class ParameterException extends PdfException {
        public ParameterException() {
            super();
        }
    }

    public AdobePdfContainer(Context context) {
        this.mContext = context;
        EPLog.v(TAG, "adpbeMPSInterface.MPSInit()");
        this.adpbeMPSInterface.MPSInit("com/adobe/mps/ARAESCryptor", "com/adobe/mps/ARSHADigest");
    }

    protected void finalize() {
        EPLog.v(TAG, "finalize()");
    }

    public void LoadDocument(String str, String str2) throws PasswordException, NoPdfException {
        EPLog.v(TAG, "call LoadDocument()");
        if (str2 == null || str2.length() == 0) {
            this.pageCount = this.adpbeMPSInterface.PDFDocInit(str);
        } else {
            this.pageCount = this.adpbeMPSInterface.PDFDocInit(str, str2);
        }
        switch (this.pageCount) {
            case -2:
                throw new PasswordException();
            case -1:
                throw new PasswordException();
            case 0:
                throw new NoPdfException();
            default:
                return;
        }
    }

    public int CountPages() {
        return this.pageCount;
    }

    public int hasPrintPermission() {
        return this.adpbeMPSInterface.PDFHasPrintPermission((String) null);
    }

    public int GetPageSizeX(int i) throws ParameterException {
        if (CountPages() >= i) {
            return this.adpbeMPSInterface.PDFGetPageAttributes(i)[0];
        }
        throw new ParameterException();
    }

    public int GetPageSizeY(int i) throws ParameterException {
        if (CountPages() >= i) {
            return this.adpbeMPSInterface.PDFGetPageAttributes(i)[1];
        }
        throw new ParameterException();
    }

    public Bitmap getPageBitmap(int i, int i2, int i3, int i4) throws Exception {
        EPLog.v(TAG, "getPageBitmap() Start");
        if (CountPages() >= i) {
            Bitmap bitmap = this.f385bm;
            if (bitmap != null) {
                bitmap.recycle();
                this.f385bm = null;
            }
            System.gc();
            this.f385bm = Bitmap.createBitmap(i2, i3, Bitmap.Config.ARGB_8888);
            this.rawBuffer = ByteBuffer.allocateDirect(i2 * i3 * this.colorComponent);
            int[] iArr = {0, 0, 0, 0};
            ByteBuffer byteBuffer = this.rawBuffer;
            if (byteBuffer != null) {
                if (this.adpbeMPSInterface.PDFPageRender(i, i2, i3, 1, byteBuffer, 0, iArr) == 0) {
                    this.f385bm.copyPixelsFromBuffer(this.rawBuffer);
                    if (i4 != 0) {
                        Matrix matrix = new Matrix();
                        matrix.postRotate((float) i4);
                        this.f385bm = Bitmap.createBitmap(this.f385bm, 0, 0, i2, i3, matrix, false);
                    }
                }
                this.rawBuffer.clear();
                this.rawBuffer = null;
            } else {
                EPLog.e(TAG, "Failed ByteBuffer.allocateDirect()");
            }
            EPLog.v(TAG, "getPageBitmap() End");
            return this.f385bm;
        }
        throw new ParameterException();
    }

    public String getPageBitmapFile(int i, int i2, int i3, boolean z) throws Exception {
        String str;
        EPLog.v(TAG, "getPageBitmapFile() Start");
        if (CountPages() >= i) {
            String path = new File(ExternalFileUtils.getInstance(this.mContext).getPrintDir(), "tmpPDF.jpg").getPath();
            if (this.adpbeMPSInterface.PDFPagetoImage(i, path, i2, i3, 0) != 0) {
                return null;
            }
            if (z) {
                str = new File(ExternalFileUtils.getInstance(this.mContext).getPrintDir(), "tmpPDF90.jpg").getPath();
                new epsonImage().epsmpRotateImage2(path, str);
            } else {
                str = path;
            }
            EPLog.v(TAG, "getPageBitmapFile() End");
            return str;
        }
        throw new ParameterException();
    }

    public boolean decodeToJpegFile(String str, int i, int i2, int i3) throws ParameterException {
        if (CountPages() >= i) {
            return this.adpbeMPSInterface.PDFPagetoImage(i, str, i2, i3, 0) == 0;
        }
        throw new ParameterException();
    }
}
