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;
    /* access modifiers changed from: private */
    public Rect canvasRect = new Rect();
    private GestureDetector gesDetect = null;
    /* access modifiers changed from: private */
    public float mOffsetX = 0.0f;
    /* access modifiers changed from: private */
    public float mOffsetY = 0.0f;
    /* access modifiers changed from: private */
    public 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.this.mScaleFactor == 1.0f) {
                float x = (((-ScalableImageView.this.mOffsetX) + motionEvent.getX()) / ScalableImageView.this.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.width());
                float y = (((-ScalableImageView.this.mOffsetY) + motionEvent.getY()) / ScalableImageView.this.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.height());
                float unused = ScalableImageView.this.mScaleFactor = ScalableImageView.MAX_SCALE_FACTOR;
                float width = ((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.this.mScaleFactor;
                float height = ((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.this.mScaleFactor;
                float unused2 = ScalableImageView.this.mOffsetX = motionEvent.getX() - (width * x);
                float unused3 = ScalableImageView.this.mOffsetY = motionEvent.getY() - (height * y);
            } else {
                float unused4 = ScalableImageView.this.mScaleFactor = 1.0f;
                float unused5 = ScalableImageView.this.mOffsetX = 0.0f;
                float unused6 = ScalableImageView.this.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.this.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.this.mScaleFactor));
            }
            EPLog.m305d(ScalableImageView.TAG, "mOffsetX = " + ScalableImageView.this.mOffsetX + " mOffsetY = " + ScalableImageView.this.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) {
            this.focusX = scaleGestureDetector.getFocusX();
            this.focusY = scaleGestureDetector.getFocusY();
            this.perFocusX = (((-ScalableImageView.this.mOffsetX) + this.focusX) / ScalableImageView.this.mScaleFactor) / ((float) ScalableImageView.this.canvasRect.width());
            this.perFocusY = (((-ScalableImageView.this.mOffsetY) + this.focusY) / ScalableImageView.this.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.this.mScaleFactor == 1.0f) {
                float unused3 = ScalableImageView.this.mOffsetX = 0.0f;
                float unused4 = ScalableImageView.this.mOffsetY = 0.0f;
            } else {
                float width = ((float) ScalableImageView.this.canvasRect.width()) * ScalableImageView.this.mScaleFactor;
                float height = ((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.this.mScaleFactor;
                float unused5 = ScalableImageView.this.mOffsetX = this.focusX - (width * this.perFocusX);
                float unused6 = ScalableImageView.this.mOffsetY = this.focusY - (height * this.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.this.mScaleFactor));
                ScalableImageView scalableImageView6 = ScalableImageView.this;
                float unused10 = scalableImageView6.mOffsetY = Math.max(scalableImageView6.mOffsetY, ((float) ScalableImageView.this.canvasRect.height()) - (((float) ScalableImageView.this.canvasRect.height()) * ScalableImageView.this.mScaleFactor));
            }
            EPLog.m305d(ScalableImageView.TAG, "mScaleFactor = " + ScalableImageView.this.mScaleFactor + " mOffsetX = " + ScalableImageView.this.mOffsetX + " mOffsetY = " + ScalableImageView.this.mOffsetY);
            ScalableImageView.this.invalidate();
            return true;
        }
    };
    private ScaleGestureDetector scaleGesDetect = null;

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

    public Bitmap getBitmap() {
        return this.f332bm;
    }

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

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

    public float getmScaleFactor() {
        return this.mScaleFactor;
    }

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

    public float getmOffsetX() {
        return this.mOffsetX;
    }

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

    public float getmOffsetY() {
        return this.mOffsetY;
    }

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

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

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

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

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