package com.ikea.kompis.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.Scroller;
import com.ikea.kompis.products.event.ImageScaledEvent;
import com.ikea.shared.util.L;
import com.squareup.otto.Bus;

public class ZoomableImageView extends ImageView
{
  private static final int DOUBLE_TAP = 4;
  private static final int DRAG = 1;
  private static final int FLYING = 3;
  private static final int NONE = 0;
  private static final int ZOOM = 2;
  private static final float mMaxScaleBoundary = 3.0F;
  private static final float mMinScaleBoundary = 1.0F;
  private boolean isRedraw = false;
  private Bus mBus;
  private final Context mContext;
  private float mCurrImageHeight;
  private float mCurrImageWidth;
  private Matrix mCurrMatrix;
  private int mCurrViewHeight;
  private int mCurrViewWidth;
  private int mDirection;
  private Fling mFling;
  private GestureDetector mGestureDetector;
  private float[] mMatrixArray;
  private Matrix mOriginalMatrix;
  private int mPositionInAdapter;
  private int mPrevViewHeight;
  private int mPrevViewWidth;
  private boolean mRetainedAfterOrientationChange;
  float mSavedScale = 1.0F;
  private ScaleGestureDetector mScaleDetector;
  private final PointF mStartPoint = new PointF();
  private int mode = 0;

  public ZoomableImageView(Context paramContext)
  {
    super(paramContext);
    this.mContext = paramContext;
  }

  public ZoomableImageView(Context paramContext, AttributeSet paramAttributeSet)
  {
    super(paramContext, paramAttributeSet);
    this.mContext = paramContext;
  }

  public ZoomableImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
  {
    super(paramContext, paramAttributeSet, paramInt);
    this.mContext = paramContext;
  }

  private void assignControl(boolean paramBoolean)
  {
    getParent().requestDisallowInterceptTouchEvent(paramBoolean);
  }

  private void compatPostOnAnimation(Runnable paramRunnable)
  {
    ViewCompat.postOnAnimation(this, paramRunnable);
  }

  private void fixScaleTrans()
  {
    fixTrans();
    this.mCurrMatrix.getValues(this.mMatrixArray);
    float f1 = this.mCurrImageWidth * this.mSavedScale;
    float f2 = this.mCurrImageHeight * this.mSavedScale;
    if (f1 < this.mCurrViewWidth)
      this.mMatrixArray[2] = ((this.mCurrViewWidth - f1) / 2.0F);
    if (f2 < this.mCurrViewHeight)
      this.mMatrixArray[5] = ((this.mCurrViewHeight - f2) / 2.0F);
    this.mCurrMatrix.setValues(this.mMatrixArray);
  }

  private void fixTrans()
  {
    this.mCurrMatrix.getValues(this.mMatrixArray);
    float f1 = this.mMatrixArray[2];
    float f2 = this.mMatrixArray[5];
    float f3 = getFixTrans(f1, this.mCurrViewWidth, this.mCurrImageWidth * this.mSavedScale);
    float f4 = getFixTrans(f2, this.mCurrViewHeight, this.mCurrImageHeight * this.mSavedScale);
    if ((f3 != 0.0F) || (f4 != 0.0F))
      this.mCurrMatrix.postTranslate(f3, f4);
  }

  private float getFixTrans(float paramFloat1, float paramFloat2, float paramFloat3)
  {
    float f1;
    if (paramFloat3 <= paramFloat2)
      f1 = 0.0F;
    for (float f2 = paramFloat2 - paramFloat3; paramFloat1 < f1; f2 = 0.0F)
    {
      return f1 + -paramFloat1;
      f1 = paramFloat2 - paramFloat3;
    }
    if (paramFloat1 > f2)
      return f2 + -paramFloat1;
    return 0.0F;
  }

  private PointF transformCoordBitmapToTouch(float paramFloat1, float paramFloat2)
  {
    this.mCurrMatrix.getValues(this.mMatrixArray);
    float f1 = 1.0F;
    float f2 = 1.0F;
    if (getDrawable() != null)
    {
      f1 = getDrawable().getIntrinsicWidth();
      f2 = getDrawable().getIntrinsicHeight();
    }
    float f3 = paramFloat1 / f1;
    float f4 = paramFloat2 / f2;
    return new PointF(this.mMatrixArray[2] + f3 * (this.mCurrImageWidth * this.mSavedScale), this.mMatrixArray[5] + f4 * (this.mCurrImageHeight * this.mSavedScale));
  }

  private PointF transformCoordTouchToBitmap(float paramFloat1, float paramFloat2, boolean paramBoolean)
  {
    this.mCurrMatrix.getValues(this.mMatrixArray);
    Drawable localDrawable = getDrawable();
    float f1 = 0.0F;
    float f2 = 0.0F;
    if (localDrawable != null)
    {
      f2 = getDrawable().getIntrinsicWidth();
      f1 = getDrawable().getIntrinsicHeight();
    }
    float f3 = this.mMatrixArray[2];
    float f4 = this.mMatrixArray[5];
    float f5 = f2 * (paramFloat1 - f3) / (this.mCurrImageWidth * this.mSavedScale);
    float f6 = f1 * (paramFloat2 - f4) / (this.mCurrImageHeight * this.mSavedScale);
    if (paramBoolean)
    {
      f5 = Math.min(Math.max(f5, 0.0F), f2);
      f6 = Math.min(Math.max(f6, 0.0F), f1);
    }
    return new PointF(f5, f6);
  }

  private void zoomImage(double paramDouble, float paramFloat1, float paramFloat2)
  {
    float f1 = (float)paramDouble;
    float f2 = this.mSavedScale;
    this.mSavedScale = (f1 * this.mSavedScale);
    if (this.mSavedScale > 3.0F)
    {
      this.mSavedScale = 3.0F;
      f1 = 3.0F / f2;
      if ((this.mCurrImageWidth * this.mSavedScale > this.mCurrViewWidth) && (this.mCurrImageHeight * this.mSavedScale > this.mCurrViewHeight))
        break label134;
      this.mCurrMatrix.postScale(f1, f1, this.mCurrViewWidth / 2, this.mCurrViewHeight / 2);
    }
    while (true)
    {
      fixScaleTrans();
      return;
      if (this.mSavedScale >= 1.0F)
        break;
      this.mSavedScale = 1.0F;
      f1 = 1.0F / f2;
      break;
      label134: this.mCurrMatrix.postScale(f1, f1, paramFloat1, paramFloat2);
    }
  }

  public float getSavedScale()
  {
    return this.mSavedScale;
  }

  public void init(View.OnClickListener paramOnClickListener, int paramInt, Bus paramBus)
  {
    this.mBus = paramBus;
    this.mPositionInAdapter = paramInt;
    this.mCurrMatrix = new Matrix();
    this.mMatrixArray = new float[9];
    this.mDirection = 0;
    setImageMatrix(this.mCurrMatrix);
    setScaleType(ImageView.ScaleType.MATRIX);
    setOnTouchListener(new TopTouchListener(null));
    setOnClickListener(paramOnClickListener);
  }

  protected void onAttachedToWindow()
  {
    this.mScaleDetector = new ScaleGestureDetector(this.mContext, new ScaleListener(null));
    this.mGestureDetector = new GestureDetector(this.mContext, new GestureListener(null));
    super.onAttachedToWindow();
  }

  protected void onDetachedFromWindow()
  {
    this.mGestureDetector = null;
    this.mScaleDetector = null;
    super.onDetachedFromWindow();
  }

  protected void onMeasure(int paramInt1, int paramInt2)
  {
    super.onMeasure(paramInt1, paramInt2);
    this.mCurrViewWidth = View.MeasureSpec.getSize(paramInt1);
    this.mCurrViewHeight = View.MeasureSpec.getSize(paramInt2);
    L.I("onMeasure :: mCurrViewWidth :: " + this.mCurrViewWidth + ", mCurrViewHeight" + this.mCurrViewHeight);
    if (((this.mPrevViewHeight == this.mCurrViewWidth) && (this.mPrevViewWidth == this.mCurrViewHeight)) || (this.mCurrViewWidth == 0) || (this.mCurrViewHeight == 0));
    do
    {
      return;
      this.mPrevViewHeight = this.mCurrViewHeight;
      this.mPrevViewWidth = this.mCurrViewWidth;
      if (this.mSavedScale != 1.0F)
        break;
      localDrawable = getDrawable();
    }
    while ((localDrawable == null) || (localDrawable.getIntrinsicWidth() == 0) || (localDrawable.getIntrinsicHeight() == 0));
    i = localDrawable.getIntrinsicWidth();
    j = localDrawable.getIntrinsicHeight();
    Log.d("bmSize", "bmWidth: " + i + " bmHeight : " + j);
    f3 = Math.min(this.mCurrViewWidth / i, this.mCurrViewHeight / j);
    this.mCurrMatrix.setScale(f3, f3);
    f4 = this.mCurrViewHeight - f3 * j;
    f5 = this.mCurrViewWidth - f3 * i;
    f6 = f4 / 2.0F;
    f7 = f5 / 2.0F;
    this.mCurrMatrix.postTranslate(f7, f6);
    this.mOriginalMatrix = new Matrix(this.mCurrMatrix);
    this.mCurrImageWidth = (this.mCurrViewWidth - 2.0F * f7);
    this.mCurrImageHeight = (this.mCurrViewHeight - 2.0F * f6);
    setImageMatrix(this.mCurrMatrix);
    while (!this.isRedraw)
    {
      Drawable localDrawable;
      int i;
      int j;
      float f3;
      float f4;
      float f5;
      float f6;
      float f7;
      fixTrans();
      return;
    }
    this.isRedraw = false;
    float f1 = this.mCurrViewHeight - Math.min(this.mCurrViewHeight, this.mCurrViewWidth);
    float f2 = this.mCurrViewWidth - Math.min(this.mCurrViewHeight, this.mCurrViewWidth);
    this.mCurrImageHeight = (this.mCurrViewHeight - f1);
    this.mCurrImageWidth = (this.mCurrViewWidth - f2);
    if (this.mDirection == 2)
      this.mCurrMatrix.setScale(this.mSavedScale, this.mSavedScale, this.mCurrImageWidth, this.mCurrImageHeight / 2.0F);
    while (true)
    {
      fixScaleTrans();
      setImageMatrix(this.mCurrMatrix);
      break;
      if (this.mDirection == 1)
        this.mCurrMatrix.setScale(this.mSavedScale, this.mSavedScale, 0.0F, this.mCurrImageHeight / 2.0F);
      else if (this.mDirection == 0)
        this.mCurrMatrix.setScale(this.mSavedScale, this.mSavedScale, this.mCurrImageWidth / 2.0F, this.mCurrImageHeight / 2.0F);
    }
  }

  public void redrawZoom(float paramFloat, int paramInt, boolean paramBoolean)
  {
    this.mSavedScale = paramFloat;
    this.mDirection = paramInt;
    this.isRedraw = true;
    this.mRetainedAfterOrientationChange = paramBoolean;
  }

  public void resetZoom()
  {
    if (this.mSavedScale != 1.0F)
    {
      this.mSavedScale = 1.0F;
      setImageMatrix(new Matrix(this.mOriginalMatrix));
      invalidate();
    }
  }

  public void setImageBitmap(Bitmap paramBitmap)
  {
    super.setImageBitmap(paramBitmap);
    if (!this.mRetainedAfterOrientationChange)
      resetZoom();
  }

  public void setImageDrawable(Drawable paramDrawable)
  {
    super.setImageDrawable(paramDrawable);
    if (!this.mRetainedAfterOrientationChange)
      resetZoom();
  }

  private class DoubleTapZoom
    implements Runnable
  {
    private static final float ZOOM_TIME = 500.0F;
    private final float bitmapX;
    private final float bitmapY;
    private final PointF endTouch;
    private final AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
    private final long startTime;
    private final PointF startTouch;
    private final float startZoom;
    private final float targetZoom;

    DoubleTapZoom(float paramFloat1, float paramFloat2, float arg4)
    {
      ZoomableImageView.access$502(ZoomableImageView.this, 4);
      this.startTime = System.currentTimeMillis();
      this.startZoom = ZoomableImageView.this.mSavedScale;
      this.targetZoom = paramFloat1;
      Object localObject;
      PointF localPointF = ZoomableImageView.this.transformCoordTouchToBitmap(paramFloat2, localObject, false);
      this.bitmapX = localPointF.x;
      this.bitmapY = localPointF.y;
      this.startTouch = ZoomableImageView.this.transformCoordBitmapToTouch(this.bitmapX, this.bitmapY);
      this.endTouch = new PointF(ZoomableImageView.this.mCurrViewWidth / 2, ZoomableImageView.this.mCurrViewHeight / 2);
    }

    private double calculateDeltaScale(float paramFloat)
    {
      return (this.startZoom + paramFloat * (this.targetZoom - this.startZoom)) / ZoomableImageView.this.mSavedScale;
    }

    private float interpolate()
    {
      float f = Math.min(1.0F, (float)(System.currentTimeMillis() - this.startTime) / 500.0F);
      return this.interpolator.getInterpolation(f);
    }

    private void translateImageToCenterTouchPosition(float paramFloat)
    {
      float f1 = this.startTouch.x + paramFloat * (this.endTouch.x - this.startTouch.x);
      float f2 = this.startTouch.y + paramFloat * (this.endTouch.y - this.startTouch.y);
      PointF localPointF = ZoomableImageView.this.transformCoordBitmapToTouch(this.bitmapX, this.bitmapY);
      ZoomableImageView.this.mCurrMatrix.postTranslate(f1 - localPointF.x, f2 - localPointF.y);
    }

    public void run()
    {
      float f = interpolate();
      double d = calculateDeltaScale(f);
      ZoomableImageView.this.zoomImage(d, this.bitmapX, this.bitmapY);
      translateImageToCenterTouchPosition(f);
      ZoomableImageView.this.fixScaleTrans();
      ZoomableImageView.this.setImageMatrix(ZoomableImageView.this.mCurrMatrix);
      ZoomableImageView.this.mBus.post(new ImageScaledEvent(ZoomableImageView.this.mPositionInAdapter, ZoomableImageView.this.mSavedScale));
      if (f < 1.0F)
      {
        ZoomableImageView.this.compatPostOnAnimation(this);
        return;
      }
      ZoomableImageView.access$502(ZoomableImageView.this, 0);
    }
  }

  private class Fling
    implements Runnable
  {
    int currX;
    int currY;
    Scroller scroller;

    Fling(int paramInt1, int arg3)
    {
      ZoomableImageView.access$502(ZoomableImageView.this, 3);
      this.scroller = new Scroller(ZoomableImageView.this.mContext);
      ZoomableImageView.this.mCurrMatrix.getValues(ZoomableImageView.this.mMatrixArray);
      int j = (int)ZoomableImageView.this.mMatrixArray[2];
      int k = (int)ZoomableImageView.this.mMatrixArray[5];
      int n;
      int m;
      int i2;
      int i1;
      if (ZoomableImageView.this.mCurrImageWidth * ZoomableImageView.this.mSavedScale > ZoomableImageView.this.mCurrViewWidth)
      {
        n = ZoomableImageView.this.mCurrViewWidth - (int)(ZoomableImageView.this.mCurrImageWidth * ZoomableImageView.this.mSavedScale);
        m = 0;
        if (ZoomableImageView.this.mCurrImageHeight * ZoomableImageView.this.mSavedScale <= ZoomableImageView.this.mCurrViewHeight)
          break label180;
        i2 = ZoomableImageView.this.mCurrViewHeight - (int)(ZoomableImageView.this.mCurrImageHeight * ZoomableImageView.this.mSavedScale);
        i1 = 0;
      }
      while (true)
      {
        int i;
        this.scroller.fling(j, k, paramInt1, i, n, m, i2, i1);
        this.currX = j;
        this.currY = k;
        return;
        m = j;
        n = j;
        break;
        label180: i1 = k;
        i2 = k;
      }
    }

    public void cancelFling()
    {
      if (this.scroller != null)
      {
        ZoomableImageView.access$502(ZoomableImageView.this, 0);
        this.scroller.forceFinished(true);
      }
    }

    public void run()
    {
      if (this.scroller.isFinished())
        this.scroller = null;
      while (!this.scroller.computeScrollOffset())
        return;
      int i = this.scroller.getCurrX();
      int j = this.scroller.getCurrY();
      int k = i - this.currX;
      int m = j - this.currY;
      this.currX = i;
      this.currY = j;
      ZoomableImageView.this.mCurrMatrix.postTranslate(k, m);
      ZoomableImageView.this.fixTrans();
      ZoomableImageView.this.setImageMatrix(ZoomableImageView.this.mCurrMatrix);
      ZoomableImageView.this.compatPostOnAnimation(this);
    }
  }

  private class GestureListener extends GestureDetector.SimpleOnGestureListener
  {
    private GestureListener()
    {
    }

    public boolean onDoubleTap(MotionEvent paramMotionEvent)
    {
      float f = 1.0F;
      int i = ZoomableImageView.this.mode;
      boolean bool = false;
      if (i == 0)
      {
        if (ZoomableImageView.this.mSavedScale == f)
          f = 3.0F;
        ZoomableImageView.DoubleTapZoom localDoubleTapZoom = new ZoomableImageView.DoubleTapZoom(ZoomableImageView.this, f, paramMotionEvent.getX(), paramMotionEvent.getY());
        ZoomableImageView.this.compatPostOnAnimation(localDoubleTapZoom);
        bool = true;
      }
      return bool;
    }

    public boolean onDoubleTapEvent(MotionEvent paramMotionEvent)
    {
      return false;
    }

    public boolean onFling(MotionEvent paramMotionEvent1, MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2)
    {
      if (ZoomableImageView.this.mFling != null)
        ZoomableImageView.this.mFling.cancelFling();
      ZoomableImageView.access$402(ZoomableImageView.this, new ZoomableImageView.Fling(ZoomableImageView.this, (int)paramFloat1, (int)paramFloat2));
      ZoomableImageView.this.compatPostOnAnimation(ZoomableImageView.this.mFling);
      return super.onFling(paramMotionEvent1, paramMotionEvent2, paramFloat1, paramFloat2);
    }

    public boolean onSingleTapConfirmed(MotionEvent paramMotionEvent)
    {
      return ZoomableImageView.this.performClick();
    }
  }

  private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
  {
    private ScaleListener()
    {
    }

    public boolean onScale(ScaleGestureDetector paramScaleGestureDetector)
    {
      float f1 = paramScaleGestureDetector.getScaleFactor();
      float f2 = ZoomableImageView.this.mSavedScale;
      ZoomableImageView localZoomableImageView = ZoomableImageView.this;
      localZoomableImageView.mSavedScale = (f1 * localZoomableImageView.mSavedScale);
      if (ZoomableImageView.this.mSavedScale > 3.0F)
      {
        ZoomableImageView.this.mSavedScale = 3.0F;
        f1 = 3.0F / f2;
        if ((ZoomableImageView.this.mCurrImageWidth * ZoomableImageView.this.mSavedScale > ZoomableImageView.this.mCurrViewWidth) && (ZoomableImageView.this.mCurrImageHeight * ZoomableImageView.this.mSavedScale > ZoomableImageView.this.mCurrViewHeight))
          break label181;
        ZoomableImageView.this.mCurrMatrix.postScale(f1, f1, ZoomableImageView.this.mCurrViewWidth / 2, ZoomableImageView.this.mCurrViewHeight / 2);
      }
      while (true)
      {
        ZoomableImageView.this.fixTrans();
        return true;
        if (ZoomableImageView.this.mSavedScale >= 1.0F)
          break;
        ZoomableImageView.this.mSavedScale = 1.0F;
        f1 = 1.0F / f2;
        break;
        label181: ZoomableImageView.this.mCurrMatrix.postScale(f1, f1, paramScaleGestureDetector.getFocusX(), paramScaleGestureDetector.getFocusY());
      }
    }

    public boolean onScaleBegin(ScaleGestureDetector paramScaleGestureDetector)
    {
      ZoomableImageView.access$502(ZoomableImageView.this, 2);
      return true;
    }

    public void onScaleEnd(ScaleGestureDetector paramScaleGestureDetector)
    {
      super.onScaleEnd(paramScaleGestureDetector);
      ZoomableImageView.this.mBus.post(new ImageScaledEvent(ZoomableImageView.this.mPositionInAdapter, ZoomableImageView.this.mSavedScale));
      ZoomableImageView.access$502(ZoomableImageView.this, 0);
    }
  }

  private class TopTouchListener
    implements View.OnTouchListener
  {
    PointF lastPoint = new PointF();

    private TopTouchListener()
    {
    }

    public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
    {
      ZoomableImageView.this.mScaleDetector.onTouchEvent(paramMotionEvent);
      ZoomableImageView.this.mGestureDetector.onTouchEvent(paramMotionEvent);
      PointF localPointF = new PointF(paramMotionEvent.getX(), paramMotionEvent.getY());
      switch (paramMotionEvent.getAction())
      {
      case 3:
      case 4:
      case 5:
      default:
      case 0:
      case 2:
      case 1:
      case 6:
      }
      while (true)
      {
        ZoomableImageView.this.setImageMatrix(ZoomableImageView.this.mCurrMatrix);
        ZoomableImageView.this.invalidate();
        ZoomableImageView.this.mBus.post(new ImageScaledEvent(ZoomableImageView.this.mPositionInAdapter, ZoomableImageView.this.mSavedScale));
        return true;
        this.lastPoint.set(localPointF);
        ZoomableImageView.this.mStartPoint.set(this.lastPoint);
        if (ZoomableImageView.this.mFling != null)
          ZoomableImageView.this.mFling.cancelFling();
        ZoomableImageView.access$502(ZoomableImageView.this, 1);
        ZoomableImageView.this.assignControl(true);
        continue;
        if (ZoomableImageView.this.mode == 1)
        {
          float f1 = localPointF.x - this.lastPoint.x;
          float f2 = localPointF.y - this.lastPoint.y;
          float f3;
          if (ZoomableImageView.this.mCurrViewWidth >= ZoomableImageView.this.mCurrImageWidth * ZoomableImageView.this.mSavedScale)
          {
            f3 = 0.0F;
            label276: if (ZoomableImageView.this.mCurrViewHeight < ZoomableImageView.this.mCurrViewHeight * ZoomableImageView.this.mSavedScale)
              break label445;
          }
          label445: for (float f4 = 0.0F; ; f4 = f2)
          {
            ZoomableImageView.this.mCurrMatrix.postTranslate(f3, f4);
            ZoomableImageView.this.fixTrans();
            this.lastPoint.set(localPointF.x, localPointF.y);
            float f5 = ZoomableImageView.this.mMatrixArray[2];
            if ((int)(f3 + ZoomableImageView.this.getFixTrans(f5, ZoomableImageView.this.mCurrViewWidth, ZoomableImageView.this.mCurrImageWidth * ZoomableImageView.this.mSavedScale)) != 0)
              break label452;
            ZoomableImageView.this.mBus.post(new ImageScaledEvent(ZoomableImageView.this.mPositionInAdapter, ZoomableImageView.this.mSavedScale));
            ZoomableImageView.this.assignControl(false);
            break;
            f3 = f1;
            break label276;
          }
          label452: ZoomableImageView.this.assignControl(true);
          continue;
          ZoomableImageView.access$502(ZoomableImageView.this, 0);
          ZoomableImageView.this.assignControl(false);
          continue;
          ZoomableImageView.access$502(ZoomableImageView.this, 0);
        }
      }
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.ikea.kompis.view.ZoomableImageView
 * JD-Core Version:    0.6.2
 */