package epson.print.phlayout;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.AnyThread;



import android.support.p000v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import epson.common.Info_paper;
import epson.print.R;
import epson.print.EPImage;
import epson.print.EPImageCreator;
import epson.print.EPImageList;
import epson.print.IprintApplication;
import epson.print.phlayout.PhotoPreviewImageList;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.locks.ReentrantLock;

public class PhotoPreview extends View {
    private static final int ALL_REFRESH_DRAW_END = 4;
    private static final int CREATE_PREVIEW = 1;
    static final int FIT_MODE_CIRCUMSCRIBED = 1;
    static final int FIT_MODE_FREE = 0;
    static final int FIT_MODE_INSCRIBED = 2;
    private static final int INVALID_IMAGE_NO = -1;
    private static final String LOG_TAG = "previewView";
    private static final float MAX_MAGNIFICATION_FACTOR = 8.0f;
    private static final float MINI_MAGNIFICATION_FACTOR = 0.25f;
    private static final double MOVEMENT_MARGIN_RATE = 0.2d;
    public static final int RESET_PREVIEW = 2;
    public static final int RESTORE_PREVIEW = 0;
    private static final int ZOOM_CONTROL = 2;
    private float baseScaleFactor = 1.0f;
    private int color = 0;
    private Handler drawEndHandler = null;
    private PhotoPreviewImageList imageList = null;
    private volatile int imageNo = 0;
    private final Object lockObj = new Object();
    private Bitmap mBitmap = null;
    private final Rect mBitmapDrawRect = new Rect();
    private final ReentrantLock mDrawLock = new ReentrantLock();
    private final FitLayout mFitLayout = new FitLayout();
    private volatile int mFitMode;
    private GestureDetectorCompat mGestureDetector;
    private volatile ILayoutPosition mLayoutPosition = null;
    private int mPaperSizeId = 0;
    private final Path mPath = new Path();
    private volatile PreviewBitmapMaker mPreviewBitmapMaker;
    private ScaleGestureDetector mScaleGestureDetector = null;
    private int[] mViewSize = new int[2];
    private ViewSizeChangeListener mViewSizeChangeListener;
    private boolean maskExpandPreview = true;
    private int maxBitmapHeight = -1;
    private int maxBitmapWidth = -1;
    private final Paint paint = new Paint();
    private float scaleFactor = 1.0f;
    private final Point trans = new Point();
    private Handler zoomControlHandler = null;

    @Retention(RetentionPolicy.SOURCE)
    @interface FitMode {
    }

    public interface PreviewBitmapMaker {
        Bitmap getPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z);
    }

    public interface ViewSizeChangeListener {
        void onViewSizeChanged();
    }

    public PhotoPreview(Context context) {
        super(context);
        initLocal(context);
    }

    public PhotoPreview(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initLocal(context);
    }

    private void initLocal(Context context) {
        this.mGestureDetector = new GestureDetectorCompat(context, new MyGestureListener(this));
        this.mScaleGestureDetector = new ScaleGestureDetector(context, new MyScaledGestureListener(this));
        this.mFitMode = 1;
    }

    /* access modifiers changed from: private */
    public void startScaleChange() {
        this.maskExpandPreview = false;
        this.baseScaleFactor = this.scaleFactor;
        this.mFitMode = 0;
        invalidate();
    }

    /* access modifiers changed from: private */
    public void changeScale(float f) {
        this.scaleFactor = f * this.baseScaleFactor;
        float f2 = this.scaleFactor;
        if (f2 < MINI_MAGNIFICATION_FACTOR) {
            this.scaleFactor = MINI_MAGNIFICATION_FACTOR;
        } else if (f2 > MAX_MAGNIFICATION_FACTOR) {
            this.scaleFactor = MAX_MAGNIFICATION_FACTOR;
        }
        Handler handler = this.zoomControlHandler;
        if (handler != null) {
            handler.sendEmptyMessage(2);
        }
        adjustTrans(this.trans);
        invalidate();
    }

    /* access modifiers changed from: private */
    public void changeFitModeAndRefresh() {
        this.maskExpandPreview = true;
        changeFitMode();
        invalidate();
        Handler handler = this.zoomControlHandler;
        if (handler != null) {
            handler.sendEmptyMessage(2);
        }
    }

    private void changeFitMode() {
        if (1 == this.mLayoutPosition.getLayoutId()) {
            fitCircumscribed();
        } else if (this.mFitMode == 1) {
            fitInscribed();
        } else {
            fitCircumscribed();
        }
    }

    @WorkerThread
    private void fitCircumscribed() {
        this.scaleFactor = 1.0f;
        this.baseScaleFactor = 1.0f;
        setPaper(this.mPaperSizeId);
        this.mFitMode = 1;
    }

    private void fitInscribed() {
        EPImage currentEpImage;
        this.mFitMode = 2;
        if (this.mLayoutPosition != null && (currentEpImage = getCurrentEpImage()) != null) {
            RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
            double[] fitInscribed = this.mFitLayout.fitInscribed(new double[]{(double) previewImageCircumscribedTargetSize.left, (double) previewImageCircumscribedTargetSize.top, (double) previewImageCircumscribedTargetSize.right, (double) previewImageCircumscribedTargetSize.bottom}, new double[]{(double) currentEpImage.previewWidth, (double) currentEpImage.previewHeight});
            if (fitInscribed != null) {
                Point point = this.trans;
                point.x = (int) fitInscribed[0];
                point.y = (int) fitInscribed[1];
                this.scaleFactor = (float) fitInscribed[2];
            }
        }
    }

    @AnyThread
    private EPImage getCurrentEpImage() {
        if (this.imageNo < 0 || this.imageNo >= this.imageList.size()) {
            return null;
        }
        return this.imageList.get(this.imageNo);
    }

    private PhotoPreviewImageList.Element getCurrentElement() {
        return this.imageList.getElement(this.imageNo);
    }

    @WorkerThread
    private static Bitmap getBitmap(EPImage ePImage, int i, int i2, int i3, boolean z) {
        new EPImageCreator(IprintApplication.getInstance()).createPreviewImage(ePImage, i, i2, i3, z);
        if (ePImage.previewImageFileName != null) {
            return BitmapFactory.decodeFile(ePImage.previewImageFileName);
        }
        return null;
    }

    public void setViewSizeChangeListener(ViewSizeChangeListener viewSizeChangeListener) {
        this.mViewSizeChangeListener = viewSizeChangeListener;
    }

    public void setPreviewBitmapMaker(PreviewBitmapMaker previewBitmapMaker) {
        this.mPreviewBitmapMaker = previewBitmapMaker;
    }

    public EPImageList getImageList() {
        EPImageList ePImageList;
        synchronized (this.lockObj) {
            saveImage(this.imageNo);
            ePImageList = new EPImageList();
            ePImageList.setRenderingMode(1);
            for (int i = 0; i < this.imageList.size(); i++) {
                EPImage ePImage = this.imageList.get(i);
                if (ePImage != null) {
                    RectF calcPreviewImageRect = calcPreviewImageRect(ePImage.previewWidth, ePImage.previewHeight, ePImage.scaleFactor, new Point(ePImage.previewImageRectCenterX, ePImage.previewImageRectCenterY));
                    EPImage ePImage2 = new EPImage(ePImage);
                    ePImage2.previewImageRectLeft = calcPreviewImageRect.left;
                    ePImage2.previewImageRectTop = calcPreviewImageRect.top;
                    ePImage2.previewImageRectRight = calcPreviewImageRect.right;
                    ePImage2.previewImageRectBottom = calcPreviewImageRect.bottom;
                    ePImageList.add(ePImage2);
                }
            }
        }
        return ePImageList;
    }

    public void setImageList(@NonNull PhotoPreviewImageList photoPreviewImageList) {
        this.imageList = photoPreviewImageList;
    }

    public void setLayout(int i, int i2) {
        if (i == 1) {
            this.mLayoutPosition = new BorderlessLayoutPosition();
        } else if (i != 4) {
            this.mLayoutPosition = new BorderedLayoutPosition();
        } else {
            this.mLayoutPosition = new CdLayoutPosition();
        }
        this.mLayoutPosition.setLayoutId(i, i2);
    }

    public void setColor(int i) {
        this.color = i;
    }

    public void setZoomControlHandler(Handler handler) {
        this.zoomControlHandler = handler;
    }

    public void setDrawEndHandler(Handler handler) {
        this.drawEndHandler = handler;
    }

    public boolean getIsPaperLandscape() {
        return this.mLayoutPosition.getIsPaperLandscape();
    }

    @WorkerThread
    public void setIsPaperLandscape(boolean z, Activity activity) {
        synchronized (this.lockObj) {
            this.mLayoutPosition.setPaperLandscape(z);
            if (this.imageList == null || this.imageList.size() <= 0) {
                setPaper(this.mPaperSizeId);
            } else {
                this.scaleFactor = 1.0f;
                this.baseScaleFactor = 1.0f;
                this.mFitMode = 1;
                setImage(this.imageNo, 1, activity);
            }
            postInvalidate();
        }
    }

    @AnyThread
    public boolean setPaper(int i) {
        synchronized (this.lockObj) {
            this.mPaperSizeId = i;
            Info_paper infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), this.mPaperSizeId);
            if (infoPaper == null) {
                return false;
            }
            int[] viewSize = getViewSize();
            if (viewSize == null) {
                return false;
            }
            this.mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], this.trans);
            return true;
        }
    }

    @Nullable
    @AnyThread
    private synchronized int[] getViewSize() {
        if (this.mViewSize[0] > 0) {
            if (this.mViewSize[1] > 0) {
                return this.mViewSize;
            }
        }
        return null;
    }

    @WorkerThread
    private void reCalcPaperPosition() {
        Info_paper infoPaper;
        int[] viewSize = getViewSize();
        if (viewSize != null && (infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), this.mPaperSizeId)) != null) {
            this.mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], new Point());
        }
    }

    @AnyThread
    public void invalidateImageNo() {
        if (this.imageList != null) {
            this.imageNo = -1;
        }
    }

    public void invalidatePreview() {
        if (this.imageList != null) {
            synchronized (this.lockObj) {
                for (int i = 0; i < this.imageList.size(); i++) {
                    this.imageList.get(i).previewImageFileName = null;
                }
            }
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:62:0x0099, code lost:
        r4.mDrawLock.unlock();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:63:0x009e, code lost:
        return false;
     */
    /* JADX WARNING: Missing exception handler attribute for start block: B:45:0x007e */
    /* JADX WARNING: Missing exception handler attribute for start block: B:50:0x0088 */
    @android.support.annotation.WorkerThread
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean setImage(int r5, int r6, android.app.Activity r7) throws java.lang.OutOfMemoryError {
        /*
            r4 = this;
            java.util.concurrent.locks.ReentrantLock r0 = r4.mDrawLock     // Catch:{ all -> 0x00a2 }
            r0.lock()     // Catch:{ all -> 0x00a2 }
            java.lang.Object r0 = r4.lockObj     // Catch:{ all -> 0x00a2 }
            monitor-enter(r0)     // Catch:{ all -> 0x00a2 }
            epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList     // Catch:{ all -> 0x009f }
            r2 = 0
            if (r1 == 0) goto L_0x0098
            epson.print.phlayout.ILayoutPosition r1 = r4.mLayoutPosition     // Catch:{ all -> 0x009f }
            if (r1 != 0) goto L_0x0013
            goto L_0x0098
        L_0x0013:
            epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            int r1 = r1.size()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r3 = 1
            if (r1 > 0) goto L_0x003e
            r5 = 0
            r4.mBitmap = r5     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r5 = 2
            if (r6 != r5) goto L_0x0025
            r4.fitCircumscribed()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
        L_0x0025:
            if (r6 == r3) goto L_0x002f
            epson.print.phlayout.ILayoutPosition r5 = r4.mLayoutPosition     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            boolean r5 = r5.isPreviewImageSizeValid()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            if (r5 != 0) goto L_0x0034
        L_0x002f:
            int r5 = r4.mPaperSizeId     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r4.setPaper(r5)     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
        L_0x0034:
            r4.postInvalidate()     // Catch:{ all -> 0x009f }
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
            r5.unlock()
            return r2
        L_0x003e:
            r4.setImageCore(r5, r6)     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            android.graphics.Bitmap r5 = r4.mBitmap     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            if (r5 == 0) goto L_0x0076
            int r5 = r4.maxBitmapWidth     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r6 = -1
            if (r5 == r6) goto L_0x0054
            android.graphics.Bitmap r5 = r4.mBitmap     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            int r5 = r5.getWidth()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            int r1 = r4.maxBitmapWidth     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            if (r5 > r1) goto L_0x0062
        L_0x0054:
            int r5 = r4.maxBitmapWidth     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            if (r5 == r6) goto L_0x006a
            android.graphics.Bitmap r5 = r4.mBitmap     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            int r5 = r5.getHeight()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            int r6 = r4.maxBitmapHeight     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            if (r5 <= r6) goto L_0x006a
        L_0x0062:
            epson.print.phlayout.PhotoPreview$1 r5 = new epson.print.phlayout.PhotoPreview$1     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r5.<init>()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r7.runOnUiThread(r5)     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
        L_0x006a:
            r4.maskExpandPreview = r3     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r4.postInvalidate()     // Catch:{ all -> 0x009f }
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
            r5.unlock()
            return r3
        L_0x0076:
            java.lang.OutOfMemoryError r5 = new java.lang.OutOfMemoryError     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            r5.<init>()     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
            throw r5     // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
        L_0x007c:
            r5 = move-exception
            goto L_0x0094
        L_0x007e:
            r4.postInvalidate()     // Catch:{ all -> 0x009f }
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
            r5.unlock()
            return r2
        L_0x0088:
            r4.postInvalidate()     // Catch:{ all -> 0x009f }
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
            r5.unlock()
            return r2
        L_0x0092:
            r5 = move-exception
            throw r5     // Catch:{ all -> 0x007c }
        L_0x0094:
            r4.postInvalidate()     // Catch:{ all -> 0x009f }
            throw r5     // Catch:{ all -> 0x009f }
        L_0x0098:
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
            r5.unlock()
            return r2
        L_0x009f:
            r5 = move-exception
            monitor-exit(r0)     // Catch:{ all -> 0x009f }
            throw r5     // Catch:{ all -> 0x00a2 }
        L_0x00a2:
            r5 = move-exception
            java.util.concurrent.locks.ReentrantLock r6 = r4.mDrawLock
            r6.unlock()
            throw r5
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.print.phlayout.PhotoPreview.setImage(int, int, android.app.Activity):boolean");
    }

    @WorkerThread
    private void setImageCore(int i, int i2) throws Exception {
        if (i < 0 || i >= this.imageList.size()) {
            throw new Exception();
        } else if (this.imageNo < 0 || this.imageNo >= this.imageList.size() || saveImage(this.imageNo)) {
            this.imageNo = i;
            PhotoPreviewImageList.Element currentElement = getCurrentElement();
            EPImage currentEpImage = getCurrentEpImage();
            if (i2 == 0) {
                setImageCore_RestorePreview(currentElement);
            } else {
                setImageCore2(currentEpImage, i2 == 2);
            }
        } else {
            throw new Exception();
        }
    }

    @WorkerThread
    private void setImageCore2(EPImage ePImage, boolean z) throws Exception {
        if (z) {
            this.mLayoutPosition.setPaperLandscape(ePImage.srcWidth > ePImage.srcHeight);
            fitCircumscribed();
        } else {
            setPaper(this.mPaperSizeId);
        }
        if (this.mLayoutPosition.isPreviewImageSizeValid()) {
            RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
            this.mBitmap = localGetPreviewBitmap(ePImage, (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), this.color, true, !z);
            return;
        }
        throw new Exception();
    }

    @WorkerThread
    private void setImageCore_RestorePreview(@NonNull PhotoPreviewImageList.Element element) throws Exception {
        restoreImage(element);
        reCalcPaperPosition();
        if (this.mLayoutPosition.isPreviewImageSizeValid()) {
            RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
            this.mBitmap = localGetPreviewBitmap(element.getEPImage(), (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), this.color, false, true);
            return;
        }
        throw new Exception();
    }

    @WorkerThread
    private Bitmap localGetPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z, boolean z2) {
        if (this.mPreviewBitmapMaker != null && z2) {
            this.mBitmap = this.mPreviewBitmapMaker.getPreviewBitmap(ePImage, i, i2, i3, z);
            Bitmap bitmap = this.mBitmap;
            if (bitmap != null) {
                return bitmap;
            }
        }
        this.mBitmap = getBitmap(ePImage, i, i2, i3, z);
        if (this.mBitmap == null) {
            return null;
        }
        Log.i(LOG_TAG, "bitmap <" + this.mBitmap.getWidth() + PreferencesConstants.COOKIE_DELIMITER + this.mBitmap.getHeight() + "> preview WxH <" + ePImage.previewWidth + PreferencesConstants.COOKIE_DELIMITER + ePImage.previewHeight + ">");
        return this.mBitmap;
    }

    @WorkerThread
    private boolean saveImage(int i) {
        try {
            PhotoPreviewImageList.Element element = this.imageList.getElement(i);
            EPImage ePImage = element.getEPImage();
            if (ePImage == null) {
                return false;
            }
            ePImage.previewImageRectCenterX = this.trans.x;
            ePImage.previewImageRectCenterY = this.trans.y;
            this.mLayoutPosition.copyPreviewPrintAreaToEpImage(ePImage);
            RectF calcPreviewImageRect = calcPreviewImageRect(this.mBitmap, this.scaleFactor, this.trans);
            ePImage.previewImageRectLeft = calcPreviewImageRect.left;
            ePImage.previewImageRectTop = calcPreviewImageRect.top;
            ePImage.previewImageRectRight = calcPreviewImageRect.right;
            ePImage.previewImageRectBottom = calcPreviewImageRect.bottom;
            ePImage.scaleFactor = this.scaleFactor;
            this.imageList.set(i, ePImage);
            element.setFitMode(this.mFitMode);
            return true;
        } catch (IndexOutOfBoundsException unused) {
            return false;
        }
    }

    @WorkerThread
    private void restoreImage(@NonNull PhotoPreviewImageList.Element element) {
        EPImage ePImage = element.getEPImage();
        this.trans.x = ePImage.previewImageRectCenterX;
        this.trans.y = ePImage.previewImageRectCenterY;
        this.mLayoutPosition.copyPreviewPrintAreaFromEpImage(ePImage);
        this.scaleFactor = ePImage.scaleFactor;
        this.mFitMode = element.getFitMode();
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        boolean z = this.mGestureDetector.onTouchEvent(motionEvent) || this.mScaleGestureDetector.onTouchEvent(motionEvent);
        if ((motionEvent.getAction() & 255) == 1) {
            onMoveEventEnd();
        }
        if (z || super.onTouchEvent(motionEvent)) {
            return true;
        }
        return false;
    }

    private void onMoveEventEnd() {
        invalidate();
    }

    /* access modifiers changed from: private */
    public void onMoveEventStarted(float f, float f2) {
        this.maskExpandPreview = false;
        invalidate();
    }

    /* access modifiers changed from: private */
    public void onMoving(float f, float f2) {
        this.mFitMode = 0;
        if (this.mBitmap != null) {
            Point point = this.trans;
            point.x = (int) ((-f) + ((float) point.x));
            Point point2 = this.trans;
            point2.y = (int) ((-f2) + ((float) point2.y));
            adjustTrans(this.trans);
            invalidate();
        }
    }

    private void adjustTrans(@NonNull Point point) {
        synchronized (this.lockObj) {
            if (this.mLayoutPosition != null) {
                RectF previewImageCircumscribedTargetSize = this.mLayoutPosition.getPreviewImageCircumscribedTargetSize();
                EPImage currentEpImage = getCurrentEpImage();
                if (currentEpImage != null) {
                    adjustPosition(point, MOVEMENT_MARGIN_RATE, previewImageCircumscribedTargetSize, (double) (((float) currentEpImage.previewWidth) * this.scaleFactor), (double) (((float) currentEpImage.previewHeight) * this.scaleFactor));
                }
            }
        }
    }

    private void adjustPosition(@NonNull Point point, double d, RectF rectF, double d2, double d3) {
        Point point2 = point;
        RectF rectF2 = rectF;
        double d4 = ((double) (rectF2.right - rectF2.left)) * d;
        double d5 = ((double) (rectF2.bottom - rectF2.top)) * d;
        double d6 = d2 / 2.0d;
        if (((double) rectF2.left) + d4 > ((double) point2.x) + d6) {
            point2.x = (int) ((((double) rectF2.left) + d4) - d6);
        }
        double d7 = d3 / 2.0d;
        if (((double) rectF2.top) + d5 > ((double) point2.y) + d7) {
            point2.y = (int) ((((double) rectF2.top) + d5) - d7);
        }
        if (((double) rectF2.right) - d4 < ((double) point2.x) - d6) {
            point2.x = (int) ((((double) rectF2.right) - d4) + d6);
        }
        if (((double) rectF2.bottom) - d5 < ((double) point2.y) - d7) {
            point2.y = (int) ((((double) rectF2.bottom) - d5) + d7);
        }
    }

    public void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        synchronized (this) {
            this.mViewSize[0] = i;
            this.mViewSize[1] = i2;
        }
        ViewSizeChangeListener viewSizeChangeListener = this.mViewSizeChangeListener;
        if (viewSizeChangeListener != null) {
            viewSizeChangeListener.onViewSizeChanged();
        }
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.mDrawLock.tryLock()) {
            try {
                synchronized (this.lockObj) {
                    if (!canvas.isHardwareAccelerated()) {
                        if (this.maxBitmapWidth > 0 && this.maxBitmapHeight > 0) {
                            this.maxBitmapWidth = -1;
                            this.maxBitmapHeight = -1;
                        }
                    } else if (this.maxBitmapWidth < 0 || this.maxBitmapHeight < 0) {
                        this.maxBitmapWidth = canvas.getMaximumBitmapWidth();
                        this.maxBitmapHeight = canvas.getMaximumBitmapHeight();
                    }
                    RectF calcPreviewImageRect = this.mBitmap != null ? calcPreviewImageRect(this.mBitmap, this.scaleFactor, this.trans) : null;
                    if (this.mLayoutPosition.getLayoutId() == 4) {
                        drawCdPreviewAll(canvas, this.paint, this.mBitmap, this.mLayoutPosition, this.mBitmapDrawRect, this.maskExpandPreview, this.mPath, getResources().getColor(R.color.background_preview), calcPreviewImageRect);
                    } else {
                        drawPreview(canvas, this.paint, this.mBitmap, this.mLayoutPosition, this.mBitmapDrawRect, this.maskExpandPreview, getResources().getColor(R.color.background_preview), new int[]{getWidth(), getHeight()}, calcPreviewImageRect);
                    }
                    if (this.drawEndHandler != null) {
                        Message message = new Message();
                        message.what = 4;
                        message.arg1 = this.imageNo;
                        this.drawEndHandler.sendMessage(message);
                    }
                }
                this.mDrawLock.unlock();
            } catch (Throwable th) {
                this.mDrawLock.unlock();
                throw th;
            }
        }
    }

    private void drawPreview(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, int i, int[] iArr, RectF rectF) {
        RectF rectF2;
        Canvas canvas2 = canvas;
        Paint paint3 = paint2;
        Bitmap bitmap2 = bitmap;
        Rect rect2 = rect;
        int layoutId = iLayoutPosition.getLayoutId();
        if (layoutId == 2) {
            rectF2 = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
        } else {
            rectF2 = new RectF(iLayoutPosition.getPreviewPaperRect());
        }
        paint2.setColor(-1);
        canvas.drawRect(rectF2, paint2);
        if (bitmap2 != null) {
            rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
        }
        drawBorderedOutsidePrintingArea(canvas, rectF2, layoutId, paint2, z, iLayoutPosition, i, iArr);
    }

    private static void drawBorderedOutsidePrintingArea(@NonNull Canvas canvas, @NonNull RectF rectF, int i, @NonNull Paint paint2, boolean z, @NonNull ILayoutPosition iLayoutPosition, int i2, @NonNull int[] iArr) {
        RectF rectF2 = rectF;
        int i3 = i;
        Paint paint3 = paint2;
        int i4 = i2;
        if (rectF2.left != 0.0f && rectF2.top != 0.0f && rectF2.right != 0.0f && rectF2.bottom != 0.0f) {
            paint3.setColor(i4);
            if (!z) {
                paint3.setAlpha(221);
            }
            int leftMargin = iLayoutPosition.getLeftMargin();
            int topMargin = iLayoutPosition.getTopMargin();
            int rightMargin = iLayoutPosition.getRightMargin();
            int bottomMargin = iLayoutPosition.getBottomMargin();
            float f = (float) leftMargin;
            Canvas canvas2 = canvas;
            Paint paint4 = paint2;
            canvas2.drawRect(0.0f, 0.0f, rectF2.left - f, (float) iArr[1], paint4);
            float f2 = (float) rightMargin;
            float f3 = (float) topMargin;
            canvas.drawRect(rectF2.left - f, 0.0f, rectF2.right + f2, rectF2.top - f3, paint4);
            canvas.drawRect(rectF2.right + f2, 0.0f, (float) iArr[0], (float) iArr[1], paint4);
            float f4 = (float) bottomMargin;
            canvas.drawRect(rectF2.left - f, rectF2.bottom + f4, rectF2.right + f2, (float) iArr[1], paint4);
            if (i3 == 2) {
                paint3.setColor(-1);
            } else if (i3 == 1) {
                paint3.setColor(i4);
                if (!z) {
                    paint3.setAlpha(221);
                }
            }
            Paint paint5 = paint2;
            canvas.drawRect(rectF2.left - f, rectF2.top - f3, rectF2.left, rectF2.bottom + f4, paint5);
            canvas.drawRect(rectF2.left, rectF2.top - f3, rectF2.right, rectF2.top, paint5);
            canvas.drawRect(rectF2.right, rectF2.top - f3, rectF2.right + f2, rectF2.bottom + f4, paint5);
            canvas.drawRect(rectF2.left, rectF2.bottom, rectF2.right, rectF2.bottom + f4, paint2);
        }
    }

    private void drawCdPreviewAll(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, @NonNull Path path, int i, @NonNull RectF rectF) {
        float f;
        if (bitmap != null) {
            rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
        }
        RectF previewImageCircumscribedTargetSize = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
        paint2.setColor(i);
        if (!z) {
            paint2.setAlpha(221);
        }
        if (previewImageCircumscribedTargetSize.height() > previewImageCircumscribedTargetSize.width()) {
            f = previewImageCircumscribedTargetSize.width() / 2.0f;
        } else {
            f = previewImageCircumscribedTargetSize.height() / 2.0f;
        }
        path.reset();
        canvas.drawCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), (43.0f * f) / 116.0f, paint2);
        path.addCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), f, Path.Direction.CCW);
        canvas.clipPath(path, Region.Op.DIFFERENCE);
        canvas.drawPaint(paint2);
    }

    @WorkerThread
    @Deprecated
    @NonNull
    public RectF calcPreviewImageRect(@NonNull Bitmap bitmap, float f, @NonNull Point point) {
        return calcPreviewImageRect(bitmap.getWidth(), bitmap.getHeight(), f, point);
    }

    @WorkerThread
    @NonNull
    private RectF calcPreviewImageRect(int i, int i2, float f, @NonNull Point point) {
        RectF rectF = new RectF();
        float f2 = ((float) i) * f;
        rectF.left = ((float) point.x) - (f2 / 2.0f);
        float f3 = ((float) i2) * f;
        rectF.top = ((float) point.y) - (f3 / 2.0f);
        rectF.right = rectF.left + f2;
        rectF.bottom = rectF.top + f3;
        return rectF;
    }

    static class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private final PhotoPreview mPreviewView;

        public MyGestureListener(PhotoPreview photoPreview) {
            this.mPreviewView = photoPreview;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            this.mPreviewView.onMoving(f, f2);
            return true;
        }

        public boolean onDown(MotionEvent motionEvent) {
            this.mPreviewView.onMoveEventStarted(motionEvent.getX(), motionEvent.getY());
            return true;
        }

        public boolean onDoubleTap(MotionEvent motionEvent) {
            this.mPreviewView.changeFitModeAndRefresh();
            return true;
        }
    }

    static class MyScaledGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        private final PhotoPreview mPreviewView;

        public MyScaledGestureListener(PhotoPreview photoPreview) {
            this.mPreviewView = photoPreview;
        }

        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            this.mPreviewView.startScaleChange();
            return super.onScaleBegin(scaleGestureDetector);
        }

        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            this.mPreviewView.changeScale(scaleGestureDetector.getScaleFactor());
            return super.onScale(scaleGestureDetector);
        }
    }
}
