/*
 * Copyright 2015 Cesar Diez Sanchez
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cesards.cropimageview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;
import com.cesards.cropimageview.model.CropImage;
import com.cesards.cropimageview.model.CropImageFactory;
import com.cesards.cropimageview.model.CropType;

public class CropImageView extends ImageView {

  private CropImage cropImage;
  private int cropType = CropType.NONE;

  public CropImageView(Context context) {
    super(context);

    initImageView();
  }

  public CropImageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CropImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    parseAttributes(attrs);
    initImageView();
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public CropImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    parseAttributes(attrs);
    initImageView();
  }

  /**
   * Set crop type for the {@link ImageView}
   *
   * @param cropType A {@link CropType} desired scaling mode.
   */
  public void setCropType(@CropType int cropType) {
    this.cropType = cropType;

    setWillNotCacheDrawing(false);

    requestLayout();
    invalidate();
  }

  /**
   * Return the current crop type in use by this ImageView.
   *
   * @return a {@link CropType} in use by this ImageView
   */
  public
  @CropType
  int getCropType() {
    return cropType;
  }

  @Override
  protected boolean setFrame(int l, int t, int r, int b) {
    final boolean changed = super.setFrame(l, t, r, b);
    if (!isInEditMode()) {
      cropImage.computeImageTransformation();
    }
    return changed;
  }

  private void parseAttributes(AttributeSet attrs) {
    final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.civ_CropImageView);

    cropType = a.getInt(R.styleable.civ_CropImageView_civ_crop, CropType.NONE);

    a.recycle();
  }

  private void initImageView() {
    setScaleType(ScaleType.MATRIX);

    cropImage = new CropImageFactory().getCropImage(this);
  }
}

@@ -2350,16 +2350,62 @@
 itMode()
+ && cropImage != null && getDrawable() != null
 ) %7B%0A    
@@ -2473,24 +2473,134 @@
 anged;%0A  %7D%0A%0A
+  @Override%0A  public void setImageBitmap(Bitmap bm) %7B%0A    super.setImageBitmap(bm);%0A    initImageView();%0A  %7D%0A%0A
   private vo
@@ -2836,32 +2836,277 @@
 recycle();%0A  %7D%0A%0A
+  @Override%0A  public void setImageDrawable(Drawable drawable) %7B%0A    super.setImageDrawable(drawable);%0A    initImageView();%0A  %7D%0A%0A  @Override%0A  public void setImageResource(int resId) %7B%0A    super.setImageResource(resId);%0A    initImageView();%0A  %7D%0A%0A
   private void i
@@ -3155,24 +3155,59 @@
 e.MATRIX);%0A%0A
+    if (getDrawable() != null) %7B%0A  
     cropImag
@@ -3255,12 +3255,19 @@
 his);%0A  
-%7D
+  %7D%0A  %7D%0A
 %0A%7D%0A

FIX crash if drawable not specified