<commit_before>package com.johnliu.swipefinish.core;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.johnliu.swipefinish.R;

/**
 * SwipeFinishLayout
 * <p>
 * Use this Layout,you can make a Activity with swiping to finish.
 * <p>
 * Then thanks for xiaanming's blog.
 * 
 * Created by John on 2014-6-18
 */
public class SwipeFinishLayout extends FrameLayout implements OnTouchListener {

    private static final String TAG = "SwipeFinish";

    /**
     * If the speed of moving Activity is more than MIN_SPEED,then finish the Activity.
     */
    private static final int MIN_SPEED = 500;

    /**
     * The speed of moving at the horizontal direction.
     */
    private float speedX = 0;

    private float downX = 0;

    private float lastX = 0;

    private float lastY;

    private View root;

    private Scroller mScroller;

    private Activity mActivity;

    /**
     * A VelocityTracker.
     */
    private VelocityTracker mVelocityTracker = null;

    private ViewPager mViewPager;

    /**
     * Distance in pixels a touch can wander before we think the user is scrolling.
     * {@link ViewConfiguration#getScaledTouchSlop()}
     */
    private int mScaledTouchSlop;

    /**
     * Whether finish the Activity after scrolling.
     */
    private boolean isFinish = false;

    public SwipeFinishLayout(Context context) {
        super(context);
        init();
    }

    public SwipeFinishLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SwipeFinishLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        this.setOnTouchListener(this);
        mScroller = new Scroller(getContext());
        mScaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    public void init(Activity act) {
        // get the root view of Activity
        mActivity = act;
        root = (View) act.getWindow().getDecorView();
    }

    private Drawable myShadow;

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (root != null) {
            if (myShadow == null) myShadow = getResources().getDrawable(R.drawable.shadow_left);
            int left = -10;
            int right = -20;
            int top = root.getTop();
            int bottom = root.getBottom();
            myShadow.setBounds(left, top, right, bottom);
            myShadow.draw(canvas);
        }
    }

    /**
     * Intercept a ViewPager.
     * <p>
     * To avoid conflict.
     * 
     * @param viewPager
     */
    public void interceptViewPager(ViewPager viewPager) {
        this.mViewPager = viewPager;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mViewPager != null && mViewPager.getCurrentItem() != 0) {
                    // If the ViewPager isn't selected the first item .
                    // Return false.Then all events will passed to the ViewPager
                    // continue.
                    return false;
                }
                downX = event.getRawX();
                lastX = downX;
                lastY = event.getRawY();
                createVelocityTracker(event);<commit_msg>add a shadow for layout.<commit_after>package com.johnliu.swipefinish.core;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Scroller;

import com.johnliu.swipefinish.R;

/**
 * SwipeFinishLayout
 * <p>
 * Use this Layout,you can make a Activity with swiping to finish.
 * <p>
 * Then thanks for xiaanming's blog.
 * 
 * Created by John on 2014-6-18
 */
public class SwipeFinishLayout extends FrameLayout implements OnTouchListener {

    private static final String TAG = "SwipeFinish";

    /**
     * If the speed of moving Activity is more than MIN_SPEED,then finish the Activity.
     */
    private static final int MIN_SPEED = 500;

    /**
     * The speed of moving at the horizontal direction.
     */
    private float speedX = 0;

    private float downX = 0;

    private float lastX = 0;

    private float lastY;

    private Scroller mScroller;

    private Activity mActivity;

    /**
     * A VelocityTracker.
     */
    private VelocityTracker mVelocityTracker = null;

    private ViewPager mViewPager;

    /**
     * Distance in pixels a touch can wander before we think the user is scrolling.
     * {@link ViewConfiguration#getScaledTouchSlop()}
     */
    private int mScaledTouchSlop;

    /**
     * Whether finish the Activity after scrolling.
     */
    private boolean isFinish = false;

    public SwipeFinishLayout(Context context) {
        super(context);
        init();
    }

    public SwipeFinishLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SwipeFinishLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        this.setOnTouchListener(this);
        mScroller = new Scroller(getContext());
        mScaledTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    ViewGroup root;

    public void init(Activity act) {
        // get the root view of Activity
        mActivity = act;
        root = (ViewGroup) act.getWindow().getDecorView();
        Display dis = act.getWindowManager().getDefaultDisplay();
        Point outSize = new Point(0, 0);
        dis.getSize(outSize);
        if (outSize != null) {
            drawShadow(outSize.y);
        }
    }

    /**
     * Draw a shadow for layout.
     * 
     * @param height
     */
    private void drawShadow(int height) {
        ImageView iv = new ImageView(getContext());
        iv.setBackgroundResource(R.drawable.shadow_left);
        FrameLayout.LayoutParams params = new LayoutParams(50, height);
        params.leftMargin = -50;
        root.addView(iv, params);
    }

    /**
     * Intercept a ViewPager.
     * <p>
     * To avoid conflict.
     * 
     * @param viewPager
     */
    public void interceptViewPager(ViewPager viewPager) {
        this.mViewPager = viewPager;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mViewPager != null && mViewPager.getCurrentItem() != 0) {
                    // If the ViewPager isn't selected the first item .
                    // Return false.Then all events will passed to the ViewPager
                    // continue.
                    return false;
                }
                downX = event.getRawX();
                lastX = downX;
                lastY = event.getRawY();
                createVelocityTracker(event);
{'fraction_non_alphanumeric': 0.09223300970873786, 'fraction_numerical': 0.005109862033725089, 'mean_word_length': 2.3687607573149743, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1563839', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2144, 'n_words': 602}