<commit_before>package com.mobilesolutionworks.android.cropkit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.mobilesolutionworks.android.cropimage.R;
import com.mobilesolutionworks.android.cropimage.camera.CropImage;

import java.util.ArrayList;

import bolts.TaskCompletionSource;

/**
 * Created by yunarta on 7/7/14.
 */
public class CropImageView extends ImageViewTouchBase
{
    ArrayList<HighlightView> mHighlightViews      = new ArrayList<HighlightView>();
    HighlightView            mMotionHighlightView = null;
    float mLastX, mLastY;
    int mMotionEdge;

    final LayerDrawable mHighlight;

    Bitmap mImageBitmapResetBase;

    private boolean mSaving;

    @Override
    protected void onLayout(boolean changed, int left, int top,
                            int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        if (mBitmapDisplayed.getBitmap() != null)
        {
            for (HighlightView hv : mHighlightViews)
            {
                hv.mMatrix.set(getImageMatrix());
                hv.invalidate();
                if (hv.mIsFocused)
                {
                    centerBasedOnHighlightView(hv);
                }
            }
        }
    }

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

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, R.attr.cropKitStyle, R.style.CropKit);

        Drawable dHighlight = a.getDrawable(R.styleable.CropImageView_cropKitHighlight);
        if (!(dHighlight instanceof LayerDrawable)) throw new IllegalStateException("cropKitHightlight must be a layer-list");

        mHighlight = (LayerDrawable) dHighlight;
        int[] idCheck = {R.id.cropkit_highlight_diagonal, R.id.cropkit_highlight_horizontal, R.id.cropkit_highlight_vertical};

        for (int id : idCheck)
        {
            if (mHighlight.findDrawableByLayerId(id) == null)
            {
                throw new IllegalStateException("@id/" + getResources().getResourceEntryName(id) + " is not included in cropKitHightlight layer-list");
            }
        }

        a.recycle();
    }

    private TaskCompletionSource<Void> mFaceDetectionTask;

    @Override
    protected void onAttachedToWindow()
    {
        super.onAttachedToWindow();

        if (mFaceDetectionTask == null) {
            mFaceDetectionTask = new TaskCompletionSource<>();
            if (getScale() == 1F) {
                center(true, true);
                mRunFaceDetection.run();
            }
        }

    }

    @Override
    protected void zoomTo(float scale, float centerX, float centerY)
    {
        super.zoomTo(scale, centerX, centerY);
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void zoomIn()
    {
        super.zoomIn();
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void zoomOut()
    {
        super.zoomOut();
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void postTranslate(float deltaX, float deltaY)
    {
        super.postTranslate(deltaX, deltaY);
        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv = mHighlightViews.get(i);
            hv.mMatrix.postTranslate(deltaX, deltaY);
            hv.invalidate();
        }
    }

    // According to the event's position, change the focus to the first
    // hitting cropping rectangle.
    private void recomputeFocus(MotionEvent event)
    {
        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv = mHighlightViews.get(i);
            hv.setFocus(false);
            hv.invalidate();
        }

        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv   = mHighlightViews.get(i);
            int           edge = hv.getHit(event.getX(), event.getY());
            if (edge != HighlightView.GROW_NONE)
            {
                if (!hv.hasFocus())
                {
                    hv.setFocus(true);
                    hv.invalidate();
                }
                break;
            }
        }
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        CropImage cropImage = (CropImage) this.getContext();
        if (mSaving)
        {
            return false;
        }

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                if (mWaitingToPick)
                {
                    recomputeFocus(event);
                }
                else
                {
                    for (int i = 0; i < mHighlightViews.size(); i++)
                    {
                        HighlightView hv   = mHighlightViews.get(i);
                        int           edge = hv.getHit(event.getX(), event.getY());
                        if (edge != HighlightView.GROW_NONE)
                        {
                            mMotionEdge = edge;
                            mMotionHighlightView = hv;
                            mLastX = event.getX();
                            mLastY = event.getY();
                            mMotionHighlightView.setMode(
                                    (edge == HighlightView.MOVE)
                                            ? HighlightView.ModifyMode.Move
                                            : HighlightView.ModifyMode.Grow);
                            break;
                        }<commit_msg>Removed the last dependency from CropImage<commit_after>package com.mobilesolutionworks.android.cropkit;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.mobilesolutionworks.android.cropimage.R;

import java.util.ArrayList;

import bolts.TaskCompletionSource;

/**
 * Created by yunarta on 7/7/14.
 */
public class CropImageView extends ImageViewTouchBase
{
    ArrayList<HighlightView> mHighlightViews      = new ArrayList<HighlightView>();
    HighlightView            mMotionHighlightView = null;
    float mLastX, mLastY;
    int mMotionEdge;

    final LayerDrawable mHighlight;

    Bitmap mImageBitmapResetBase;

    private boolean mSaving;

    @Override
    protected void onLayout(boolean changed, int left, int top,
                            int right, int bottom)
    {
        super.onLayout(changed, left, top, right, bottom);
        if (mBitmapDisplayed.getBitmap() != null)
        {
            for (HighlightView hv : mHighlightViews)
            {
                hv.mMatrix.set(getImageMatrix());
                hv.invalidate();
                if (hv.mIsFocused)
                {
                    centerBasedOnHighlightView(hv);
                }
            }
        }
    }

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

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CropImageView, R.attr.cropKitStyle, R.style.CropKit);

        Drawable dHighlight = a.getDrawable(R.styleable.CropImageView_cropKitHighlight);
        if (!(dHighlight instanceof LayerDrawable)) throw new IllegalStateException("cropKitHightlight must be a layer-list");

        mHighlight = (LayerDrawable) dHighlight;
        int[] idCheck = {R.id.cropkit_highlight_diagonal, R.id.cropkit_highlight_horizontal, R.id.cropkit_highlight_vertical};

        for (int id : idCheck)
        {
            if (mHighlight.findDrawableByLayerId(id) == null)
            {
                throw new IllegalStateException("@id/" + getResources().getResourceEntryName(id) + " is not included in cropKitHightlight layer-list");
            }
        }

        a.recycle();
    }

    private TaskCompletionSource<Void> mFaceDetectionTask;

    @Override
    protected void onAttachedToWindow()
    {
        super.onAttachedToWindow();

        if (mFaceDetectionTask == null) {
            mFaceDetectionTask = new TaskCompletionSource<>();
            if (getScale() == 1F) {
                center(true, true);
                mRunFaceDetection.run();
            }
        }

    }

    @Override
    protected void zoomTo(float scale, float centerX, float centerY)
    {
        super.zoomTo(scale, centerX, centerY);
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void zoomIn()
    {
        super.zoomIn();
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void zoomOut()
    {
        super.zoomOut();
        for (HighlightView hv : mHighlightViews)
        {
            hv.mMatrix.set(getImageMatrix());
            hv.invalidate();
        }
    }

    @Override
    protected void postTranslate(float deltaX, float deltaY)
    {
        super.postTranslate(deltaX, deltaY);
        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv = mHighlightViews.get(i);
            hv.mMatrix.postTranslate(deltaX, deltaY);
            hv.invalidate();
        }
    }

    // According to the event's position, change the focus to the first
    // hitting cropping rectangle.
    private void recomputeFocus(MotionEvent event)
    {
        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv = mHighlightViews.get(i);
            hv.setFocus(false);
            hv.invalidate();
        }

        for (int i = 0; i < mHighlightViews.size(); i++)
        {
            HighlightView hv   = mHighlightViews.get(i);
            int           edge = hv.getHit(event.getX(), event.getY());
            if (edge != HighlightView.GROW_NONE)
            {
                if (!hv.hasFocus())
                {
                    hv.setFocus(true);
                    hv.invalidate();
                }
                break;
            }
        }
        invalidate();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        if (mSaving)
        {
            return false;
        }

        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                if (mWaitingToPick)
                {
                    recomputeFocus(event);
                }
                else
                {
                    for (int i = 0; i < mHighlightViews.size(); i++)
                    {
                        HighlightView hv   = mHighlightViews.get(i);
                        int           edge = hv.getHit(event.getX(), event.getY());
                        if (edge != HighlightView.GROW_NONE)
                        {
                            mMotionEdge = edge;
                            mMotionHighlightView = hv;
                            mLastX = event.getX();
                            mLastY = event.getY();
                            mMotionHighlightView.setMode(
                                    (edge == HighlightView.MOVE)
                                            ? HighlightView.ModifyMode.Move
                                            : HighlightView.ModifyMode.Grow);
                            break;
                        }
{'fraction_non_alphanumeric': 0.09326884320838372, 'fraction_numerical': 0.0014510278113663846, 'mean_word_length': 1.6491565235959855, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683296', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3272, 'n_words': 671}