package epson.common;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import epson.print.Util.EPLog;

public class ScalableImageView extends View {
    private static final float MAX_SCALE_FACTOR = 5.0f;
    private static final float MIN_SCALE_FACTOR = 1.0f;
    private static final String TAG = "ScalableImageView";

    /* renamed from: bm */
    private Bitmap f332bm = null;
    private Rect canvasRect = new Rect();
    private GestureDetector gesDetect = null;
    private float mOffsetX = 0.0f;
    private float mOffsetY = 0.0f;
    private float mScaleFactor = 1.0f;
    private final GestureDetector.SimpleOnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener() {
        public boolean onDoubleTap(MotionEvent motionEvent) {
            EPLog.m305d(ScalableImageView.TAG, "DOBULE TAP");
            if (ScalableImageView.mScaleFactor == 1.0f) {
                float x = (((-ScalableImageView.mOffsetX) + motionEvent.getX()) / ScalableImageView.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.width());
                float y = (((-ScalableImageView.mOffsetY) + motionEvent.getY()) / ScalableImageView.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.height());
                float unused = ScalableImageView.mScaleFactor = ScalableImageView.MAX_SCALE_FACTOR;
                float width = ((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.mScaleFactor;
                float height = ((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.mScaleFactor;
                float unused2 = ScalableImageView.mOffsetX = motionEvent.getX() - (width * x);
                float unused3 = ScalableImageView.mOffsetY = motionEvent.getY() - (height * y);
            } else {
                float unused4 = ScalableImageView.mScaleFactor = 1.0f;
                float unused5 = ScalableImageView.mOffsetX = 0.0f;
                float unused6 = ScalableImageView.mOffsetY = 0.0f;
            }
            ScalableImageView.this.invalidate();
            return true;
        }

        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
            float f3 = -f;
            float f4 = -f2;
            if (f3 > 0.0f) {
                ScalableImageView scalableImageView = ScalableImageView.this;
                float unused = scalableImageView.mOffsetX = Math.min(scalableImageView.mOffsetX + f3, 0.0f);
            } else {
                ScalableImageView scalableImageView2 = ScalableImageView.this;
                float unused2 = scalableImageView2.mOffsetX = Math.max(scalableImageView2.mOffsetX + f3, ((float) ScalableImageView.this.canvasRect.width()) - (((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.mScaleFactor));
            }
            if (f4 > 0.0f) {
                ScalableImageView scalableImageView3 = ScalableImageView.this;
                float unused3 = scalableImageView3.mOffsetY = Math.min(scalableImageView3.mOffsetY + f4, 0.0f);
            } else {
                ScalableImageView scalableImageView4 = ScalableImageView.this;
                float unused4 = scalableImageView4.mOffsetY = Math.max(scalableImageView4.mOffsetY + f4, ((float) ScalableImageView.this.canvasRect.height()) - (((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.mScaleFactor));
            }
            EPLog.m305d(ScalableImageView.TAG, "mOffsetX = " + ScalableImageView.mOffsetX + " mOffsetY = " + ScalableImageView.mOffsetY);
            ScalableImageView.this.invalidate();
            return true;
        }
    };
    private final ScaleGestureDetector.SimpleOnScaleGestureListener onScaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        private float focusX = 0.0f;
        private float focusY = 0.0f;
        float perFocusX = 0.0f;
        float perFocusY = 0.0f;

        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
            focusX = scaleGestureDetector.getFocusX();
            focusY = scaleGestureDetector.getFocusY();
            perFocusX = (((-ScalableImageView.mOffsetX) + focusX) / ScalableImageView.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.width());
            perFocusY = (((-ScalableImageView.mOffsetY) + focusY) / ScalableImageView.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.height());
            return super.onScaleBegin(scaleGestureDetector);
        }

        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
            ScalableImageView scalableImageView = ScalableImageView.this;
            float unused = scalableImageView.mScaleFactor = scalableImageView.mScaleFactor * scaleGestureDetector.getScaleFactor();
            ScalableImageView scalableImageView2 = ScalableImageView.this;
            float unused2 = scalableImageView2.mScaleFactor = Math.max(1.0f, Math.min(scalableImageView2.mScaleFactor, ScalableImageView.MAX_SCALE_FACTOR));
            if (ScalableImageView.mScaleFactor == 1.0f) {
                float unused3 = ScalableImageView.mOffsetX = 0.0f;
                float unused4 = ScalableImageView.mOffsetY = 0.0f;
            } else {
                float width = ((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.mScaleFactor;
                float height = ((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.mScaleFactor;
                float unused5 = ScalableImageView.mOffsetX = focusX - (width * perFocusX);
                float unused6 = ScalableImageView.mOffsetY = focusY - (height * perFocusY);
                ScalableImageView scalableImageView3 = ScalableImageView.this;
                float unused7 = scalableImageView3.mOffsetX = Math.min(0.0f, scalableImageView3.mOffsetX);
                ScalableImageView scalableImageView4 = ScalableImageView.this;
                float unused8 = scalableImageView4.mOffsetY = Math.min(0.0f, scalableImageView4.mOffsetY);
                ScalableImageView scalableImageView5 = ScalableImageView.this;
                float unused9 = scalableImageView5.mOffsetX = Math.max(scalableImageView5.mOffsetX, ((float) ScalableImageView.this.canvasRect.width()) - (((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.mScaleFactor));
                ScalableImageView scalableImageView6 = ScalableImageView.this;
                float unused10 = scalableImageView6.mOffsetY = Math.max(scalableImageView6.mOffsetY, ((float) ScalableImageView.this.canvasRect.height()) - (((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.mScaleFactor));
            }
            EPLog.m305d(ScalableImageView.TAG, "mScaleFactor = " + ScalableImageView.mScaleFactor + " mOffsetX = " + ScalableImageView.mOffsetX + " mOffsetY = " + ScalableImageView.mOffsetY);
            ScalableImageView.this.invalidate();
            return true;
        }
    };
    private ScaleGestureDetector scaleGesDetect = null;

    public ScalableImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        scaleGesDetect = new ScaleGestureDetector(context, onScaleGestureListener);
        gesDetect = new GestureDetector(context, onGestureListener);
    }

    public Bitmap getBitmap() {
        return f332bm;
    }

    public void setImageBitmap(Bitmap bitmap) {
        f332bm = bitmap;
    }

    public void SetImageBitmap(Bitmap bitmap, Resources resources) {
        if (this.f332bm != bitmap) {
            f332bm = bitmap;
        }
    }

    public float getmScaleFactor() {
        return mScaleFactor;
    }

    public void setmScaleFactor(float f) {
        mScaleFactor = f;
    }

    public float getmOffsetX() {
        return mOffsetX;
    }

    public void setmOffsetX(float f) {
        mOffsetX = f;
    }

    public float getmOffsetY() {
        return mOffsetY;
    }

    public void setmOffsetY(float f) {
        mOffsetY = f;
    }

    protected void onConfigurationChanged(Configuration configuration) {
        mScaleFactor = 1.0f;
        mOffsetX = 0.0f;
        mOffsetY = 0.0f;
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (this.f332bm != null) {
            canvasRect.right = canvas.getWidth();
            canvasRect.bottom = canvas.getHeight();
            float min = Math.min(((float) canvasRect.width()) / ((float) f332bm.getWidth()), ((float) canvasRect.height()) / ((float) f332bm.getHeight()));
            Rect rect = new Rect((int) ((((float) canvasRect.width()) / 2.0f) - ((((float) f332bm.getWidth()) * min) / 2.0f)), (int) ((((float) canvasRect.height()) / 2.0f) - ((((float) f332bm.getHeight()) * min) / 2.0f)), (int) ((((float) canvasRect.width()) / 2.0f) + ((((float) f332bm.getWidth()) * min) / 2.0f)), (int) ((((float) canvasRect.height()) / 2.0f) + ((((float) f332bm.getHeight()) * min) / 2.0f)));
            canvas.save();
            canvas.translate(mOffsetX, mOffsetY);
            float f = mScaleFactor;
            canvas.scale(f, f);
            Bitmap bitmap = f332bm;
            canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), f332bm.getHeight()), rect, (Paint) null);
            canvas.restore();
        }
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (motionEvent == null) {
            return false;
        }
        scaleGesDetect.onTouchEvent(motionEvent);
        if (this.scaleGesDetect.isInProgress()) {
            return true;
        }
        gesDetect.onTouchEvent(motionEvent);
        return true;
    }

    public void SetRotate(float f) {
        Matrix matrix = new Matrix();
        matrix.postRotate(f);
        Bitmap bitmap = f332bm;
        f332bm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), f332bm.getHeight(), matrix, true);
        invalidate();
    }
}
