<commit_before>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    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.h6ah4i.android.widget.advrecyclerview.draggable;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.NinePatchDrawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Interpolator;
import com.h6ah4i.android.widget.advrecyclerview.utils.CustomRecyclerViewUtils;

class DraggingItemDecorator extends BaseDraggableItemDecorator {
    @SuppressWarnings("unused")
    private static final String TAG = "DraggingItemDecorator";

    private int mTranslationX;
    private int mTranslationY;
    private Bitmap mDraggingItemImage;
    private int mTranslationLeftLimit;
    private int mTranslationRightLimit;
    private int mTranslationTopLimit;
    private int mTranslationBottomLimit;
    private int mTouchPositionX;
    private int mTouchPositionY;
    private NinePatchDrawable mShadowDrawable;
    private final Rect mShadowPadding = new Rect();
    private boolean mStarted;
    private boolean mIsScrolling;
    private ItemDraggableRange mRange;
    private int mLayoutOrientation;
    private int mLayoutType;
    private DraggingItemInfo mDraggingItemInfo;
    private Paint mPaint;
    private long mStartMillis;

    private long mStartAnimationDurationMillis = 0;
    private float mTargetDraggingItemScale = 1.0f;
    private float mTargetDraggingItemRotation = 0.0f;
    private float mTargetDraggingItemAlpha = 1.0f;
    private float mInitialDraggingItemScaleX;
    private float mInitialDraggingItemScaleY;
    private Interpolator mScaleInterpolator = null;
    private Interpolator mRotationInterpolator = null;
    private Interpolator mAlphaInterpolator = null;
    private float mLastDraggingItemScaleX;
    private float mLastDraggingItemScaleY;
    private float mLastDraggingItemRotation;
    private float mLastDraggingItemAlpha;


    public DraggingItemDecorator(RecyclerView recyclerView, RecyclerView.ViewHolder draggingItem, ItemDraggableRange range) {
        super(recyclerView, draggingItem);
        mRange = range;
        mPaint = new Paint();
    }

    private static int clip(int value, int min, int max) {
        return Math.min(Math.max(value, min), max);
    }

    private static View findRangeFirstItem(RecyclerView rv, ItemDraggableRange range, int firstVisiblePosition, int lastVisiblePosition) {
        if (firstVisiblePosition == RecyclerView.NO_POSITION || lastVisiblePosition == RecyclerView.NO_POSITION) {
            return null;
        }

        View v = null;

        final int childCount = rv.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v2 = rv.getChildAt(i);
            final RecyclerView.ViewHolder vh = rv.getChildViewHolder(v2);

            if (vh != null) {
                final int position = vh.getLayoutPosition();

                if ((position >= firstVisiblePosition) &&
                        (position <= lastVisiblePosition) &&
                        range.checkInRange(position)) {
                    v = v2;
                    break;
                }

            }
        }

        return v;
    }

    private static View findRangeLastItem(RecyclerView rv, ItemDraggableRange range, int firstVisiblePosition, int lastVisiblePosition) {
        if (firstVisiblePosition == RecyclerView.NO_POSITION || lastVisiblePosition == RecyclerView.NO_POSITION) {
            return null;
        }

        View v = null;

        final int childCount = rv.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View v2 = rv.getChildAt(i);
            final RecyclerView.ViewHolder vh = rv.getChildViewHolder(v2);

            if (vh != null) {
                final int position = vh.getLayoutPosition();

                if ((position >= firstVisiblePosition) &&
                        (position <= lastVisiblePosition) &&
                        range.checkInRange(position)) {
                    v = v2;
                    break;
                }
            }
        }

        return v;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mDraggingItemImage == null) {
            return;
        }

        final int elapsedMillis = (int) Math.min(System.currentTimeMillis() - mStartMillis, mStartAnimationDurationMillis);
        final float t = (mStartAnimationDurationMillis > 0) ? ((float) elapsedMillis / mStartAnimationDurationMillis) : 1.0f;
        final float tScale = getInterpolation(mScaleInterpolator, t);
        final float scaleX = tScale * (mTargetDraggingItemScale - mInitialDraggingItemScaleX) + mInitialDraggingItemScaleX;
        final float scaleY = tScale * (mTargetDraggingItemScale - mInitialDraggingItemScaleY) + mInitialDraggingItemScaleY;
        final float alpha = getInterpolation(mAlphaInterpolator, t) * (mTargetDraggingItemAlpha - 1.0f) + 1.0f;
        final float rotation = getInterpolation(mRotationInterpolator, t) * mTargetDraggingItemRotation;

        if (scaleX > 0.0f && scaleY > 0.0f && alpha > 0.0f) {
            mPaint.setAlpha((int) (alpha * 255));

            int savedCount = c.save();

            c.translate(mTranslationX + mDraggingItemInfo.grabbedPositionX, mTranslationY + mDraggingItemInfo.grabbedPositionY);
            c.scale(scaleX, scaleY);
            c.rotate(rotation);
            c.translate(-(mShadowPadding.left + mDraggingItemInfo.grabbedPositionX), -(mShadowPadding.top + mDraggingItemInfo.grabbedPositionY));

            c.drawBitmap(mDraggingItemImage, 0, 0, mPaint);
            c.restoreToCount(savedCount);
        }

        if (t < 1.0f) {
            ViewCompat.postInvalidateOnAnimation(mRecyclerView);
        }

        mLastDraggingItemScaleX = scaleX;
        mLastDraggingItemScaleY = scaleY;
        mLastDraggingItemRotation = rotation;
        mLastDraggingItemAlpha = alpha;
    }

    public void setupDraggingItemEffects(DraggingItemEffectsInfo info) {
        mStartAnimationDurationMillis = info.durationMillis;
        mTargetDraggingItemScale = info.scale;
        mScaleInterpolator = info.scaleInterpolator;
        mTargetDraggingItemRotation = info.rotation;
        mRotationInterpolator = info.rotationInterpolator;
        mTargetDraggingItemAlpha = info.alpha;
        mAlphaInterpolator = info.alphaInterpolator;
    }

    public void start(DraggingItemInfo draggingItemInfo, int touchX, int touchY) {
        if (mStarted) {
            return;
        }

        final View itemView = mDraggingItemViewHolder.itemView;

        mDraggingItemInfo = draggingItemInfo;
        mDraggingItemImage = createDraggingItemImage(itemView, mShadowDrawable);

        mTranslationLeftLimit = mRecyclerView.getPaddingLeft();
        mTranslationTopLimit = mRecyclerView.getPaddingTop();
        mLayoutOrientation = CustomRecyclerViewUtils.getOrientation(mRecyclerView);
        mLayoutType = CustomRecyclerViewUtils.getLayoutType(mRecyclerView);

        mInitialDraggingItemScaleX = itemView.getScaleX();
        mInitialDraggingItemScaleY = itemView.getScaleX();

        mLastDraggingItemScaleX = 1.0f;
        mLastDraggingItemScaleY = 1.0f;
        mLastDraggingItemRotation = 0.0f;
        mLastDraggingItemAlpha = 1.0f;

        // hide
        itemView.setVisibility(View.INVISIBLE);
<commit_msg>Fix misusage of getScaleX()<commit_after>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    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.h6ah4i.android.widget.advrecyclerview.draggable;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.NinePatchDrawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Interpolator;
import com.h6ah4i.android.widget.advrecyclerview.utils.CustomRecyclerViewUtils;

class DraggingItemDecorator extends BaseDraggableItemDecorator {
    @SuppressWarnings("unused")
    private static final String TAG = "DraggingItemDecorator";

    private int mTranslationX;
    private int mTranslationY;
    private Bitmap mDraggingItemImage;
    private int mTranslationLeftLimit;
    private int mTranslationRightLimit;
    private int mTranslationTopLimit;
    private int mTranslationBottomLimit;
    private int mTouchPositionX;
    private int mTouchPositionY;
    private NinePatchDrawable mShadowDrawable;
    private final Rect mShadowPadding = new Rect();
    private boolean mStarted;
    private boolean mIsScrolling;
    private ItemDraggableRange mRange;
    private int mLayoutOrientation;
    private int mLayoutType;
    private DraggingItemInfo mDraggingItemInfo;
    private Paint mPaint;
    private long mStartMillis;

    private long mStartAnimationDurationMillis = 0;
    private float mTargetDraggingItemScale = 1.0f;
    private float mTargetDraggingItemRotation = 0.0f;
    private float mTargetDraggingItemAlpha = 1.0f;
    private float mInitialDraggingItemScaleX;
    private float mInitialDraggingItemScaleY;
    private Interpolator mScaleInterpolator = null;
    private Interpolator mRotationInterpolator = null;
    private Interpolator mAlphaInterpolator = null;
    private float mLastDraggingItemScaleX;
    private float mLastDraggingItemScaleY;
    private float mLastDraggingItemRotation;
    private float mLastDraggingItemAlpha;


    public DraggingItemDecorator(RecyclerView recyclerView, RecyclerView.ViewHolder draggingItem, ItemDraggableRange range) {
        super(recyclerView, draggingItem);
        mRange = range;
        mPaint = new Paint();
    }

    private static int clip(int value, int min, int max) {
        return Math.min(Math.max(value, min), max);
    }

    private static View findRangeFirstItem(RecyclerView rv, ItemDraggableRange range, int firstVisiblePosition, int lastVisiblePosition) {
        if (firstVisiblePosition == RecyclerView.NO_POSITION || lastVisiblePosition == RecyclerView.NO_POSITION) {
            return null;
        }

        View v = null;

        final int childCount = rv.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v2 = rv.getChildAt(i);
            final RecyclerView.ViewHolder vh = rv.getChildViewHolder(v2);

            if (vh != null) {
                final int position = vh.getLayoutPosition();

                if ((position >= firstVisiblePosition) &&
                        (position <= lastVisiblePosition) &&
                        range.checkInRange(position)) {
                    v = v2;
                    break;
                }

            }
        }

        return v;
    }

    private static View findRangeLastItem(RecyclerView rv, ItemDraggableRange range, int firstVisiblePosition, int lastVisiblePosition) {
        if (firstVisiblePosition == RecyclerView.NO_POSITION || lastVisiblePosition == RecyclerView.NO_POSITION) {
            return null;
        }

        View v = null;

        final int childCount = rv.getChildCount();
        for (int i = childCount - 1; i >= 0; i--) {
            final View v2 = rv.getChildAt(i);
            final RecyclerView.ViewHolder vh = rv.getChildViewHolder(v2);

            if (vh != null) {
                final int position = vh.getLayoutPosition();

                if ((position >= firstVisiblePosition) &&
                        (position <= lastVisiblePosition) &&
                        range.checkInRange(position)) {
                    v = v2;
                    break;
                }
            }
        }

        return v;
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (mDraggingItemImage == null) {
            return;
        }

        final int elapsedMillis = (int) Math.min(System.currentTimeMillis() - mStartMillis, mStartAnimationDurationMillis);
        final float t = (mStartAnimationDurationMillis > 0) ? ((float) elapsedMillis / mStartAnimationDurationMillis) : 1.0f;
        final float tScale = getInterpolation(mScaleInterpolator, t);
        final float scaleX = tScale * (mTargetDraggingItemScale - mInitialDraggingItemScaleX) + mInitialDraggingItemScaleX;
        final float scaleY = tScale * (mTargetDraggingItemScale - mInitialDraggingItemScaleY) + mInitialDraggingItemScaleY;
        final float alpha = getInterpolation(mAlphaInterpolator, t) * (mTargetDraggingItemAlpha - 1.0f) + 1.0f;
        final float rotation = getInterpolation(mRotationInterpolator, t) * mTargetDraggingItemRotation;

        if (scaleX > 0.0f && scaleY > 0.0f && alpha > 0.0f) {
            mPaint.setAlpha((int) (alpha * 255));

            int savedCount = c.save();

            c.translate(mTranslationX + mDraggingItemInfo.grabbedPositionX, mTranslationY + mDraggingItemInfo.grabbedPositionY);
            c.scale(scaleX, scaleY);
            c.rotate(rotation);
            c.translate(-(mShadowPadding.left + mDraggingItemInfo.grabbedPositionX), -(mShadowPadding.top + mDraggingItemInfo.grabbedPositionY));

            c.drawBitmap(mDraggingItemImage, 0, 0, mPaint);
            c.restoreToCount(savedCount);
        }

        if (t < 1.0f) {
            ViewCompat.postInvalidateOnAnimation(mRecyclerView);
        }

        mLastDraggingItemScaleX = scaleX;
        mLastDraggingItemScaleY = scaleY;
        mLastDraggingItemRotation = rotation;
        mLastDraggingItemAlpha = alpha;
    }

    public void setupDraggingItemEffects(DraggingItemEffectsInfo info) {
        mStartAnimationDurationMillis = info.durationMillis;
        mTargetDraggingItemScale = info.scale;
        mScaleInterpolator = info.scaleInterpolator;
        mTargetDraggingItemRotation = info.rotation;
        mRotationInterpolator = info.rotationInterpolator;
        mTargetDraggingItemAlpha = info.alpha;
        mAlphaInterpolator = info.alphaInterpolator;
    }

    public void start(DraggingItemInfo draggingItemInfo, int touchX, int touchY) {
        if (mStarted) {
            return;
        }

        final View itemView = mDraggingItemViewHolder.itemView;

        mDraggingItemInfo = draggingItemInfo;
        mDraggingItemImage = createDraggingItemImage(itemView, mShadowDrawable);

        mTranslationLeftLimit = mRecyclerView.getPaddingLeft();
        mTranslationTopLimit = mRecyclerView.getPaddingTop();
        mLayoutOrientation = CustomRecyclerViewUtils.getOrientation(mRecyclerView);
        mLayoutType = CustomRecyclerViewUtils.getLayoutType(mRecyclerView);

        mInitialDraggingItemScaleX = itemView.getScaleX();
        mInitialDraggingItemScaleY = itemView.getScaleY();

        mLastDraggingItemScaleX = 1.0f;
        mLastDraggingItemScaleY = 1.0f;
        mLastDraggingItemRotation = 0.0f;
        mLastDraggingItemAlpha = 1.0f;

        // hide
        itemView.setVisibility(View.INVISIBLE);

{'fraction_non_alphanumeric': 0.07548801581418335, 'fraction_numerical': 0.00716580182851495, 'mean_word_length': 2.876676245210728, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2684269', 'n_tokens_mistral': 4612, 'n_tokens_neox': 4427, 'n_words': 1061}