<commit_before>package org.evilbinary.tv.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;


/**
 * 作者:evilbinary on 1/31/16.
 * 邮箱:<EMAIL>
 */

public class BorderView extends View implements ViewTreeObserver.OnGlobalFocusChangeListener, ViewTreeObserver.OnScrollChangedListener, ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnTouchModeChangeListener {

    private static String TAG = "BorderView";


    private BorderBaseEffect mEffect;


    private BorderView mBorderView;
    private boolean mEnableBorder = true;
    private ViewGroup mViewGroup;
    private boolean mInTouchMode = false;
    private boolean mFocusLimit = false;

    private AdapterView.OnItemSelectedListener mOnItemSelectedListener;

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

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

    private void init() {
        mBorderView = this;
        if (mEffect == null)
            mEffect = BorderBaseEffect.getDefault();
        setVisibility(INVISIBLE);
    }

    public BorderBaseEffect getEffect() {
        return mEffect;
    }

    public void setEffect(BorderBaseEffect effect) {
        mEffect = effect;
    }

    public void attachTo(ViewGroup viewGroup) {
        if (mViewGroup != viewGroup) {
            mViewGroup = viewGroup;
            ViewTreeObserver viewTreeObserver = mViewGroup.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalFocusChangeListener(this);
                viewTreeObserver.addOnScrollChangedListener(this);
                viewTreeObserver.addOnGlobalLayoutListener(this);
                viewTreeObserver.addOnTouchModeChangeListener(this);
            }
        }

    }

    public void detachFrom(ViewGroup viewGroup) {
        if (viewGroup == mViewGroup) {
            ViewTreeObserver viewTreeObserver = mViewGroup.getViewTreeObserver();
            viewTreeObserver.removeOnGlobalFocusChangeListener(this);
            viewTreeObserver.removeOnScrollChangedListener(this);
            viewTreeObserver.removeOnGlobalLayoutListener(this);
            viewTreeObserver.removeOnTouchModeChangeListener(this);
            if (getParent() == mViewGroup) {
                mViewGroup.removeView(this);
            }
        }
    }

    public boolean isFocusLimit() {
        return mFocusLimit;
    }

    public void setFocusLimit(boolean focusLimit) {
        this.mFocusLimit = focusLimit;
    }

    @Override
    public void onGlobalFocusChanged(View oldFocus, View newFocus) {
//        Log.d(TAG, "onGlobalFocusChanged");
        if (!mEnableBorder) return;
        if (mInTouchMode) return;
        if (mFocusLimit) {
            if (mViewGroup.indexOfChild(newFocus) < 0) {
                mEffect.end();
                return;
            }
            if (mViewGroup.indexOfChild(oldFocus) < 0) {
                oldFocus = null;
            }
        }

        if (mViewGroup instanceof GridView || mViewGroup instanceof ListView) {
            AbsListView gridView = (AbsListView) mViewGroup;
            if (mOnItemSelectedListener == null) {
                mOnItemSelectedListener = gridView.getOnItemSelectedListener();
                if (gridView.getSelectedView() != null) {
                    newFocus = gridView.getSelectedView();
                }
                final View tempFocus = newFocus;
                gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    private View oldFocus = tempFocus;
                    private View newFocus = null;

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        newFocus = view;
                        mEffect.cancle();
                        mEffect.start(mBorderView, oldFocus, newFocus);
                        oldFocus = newFocus;
                        if (mOnItemSelectedListener != null)
                            mOnItemSelectedListener.onItemSelected(parent, view, position, id);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        mOnItemSelectedListener.onNothingSelected(parent);

                    }
                });
            }
        } else {
            mEffect.cancle();
            mEffect.start(this, oldFocus, newFocus);
        }


    }

    @Override
    public void onScrollChanged() {
//        Log.d(TAG, "onScrollChanged");

        if (mViewGroup instanceof GridView || mViewGroup instanceof ListView) {

        }else{
            mEffect.notifyChangeAnimation();
        }

    }

    @Override
    public void onGlobalLayout() {
//        Log.d(TAG, "onGlobalLayout");


    }

    @Override<commit_msg>gridview listview 默认焦点<commit_after>package org.evilbinary.tv.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;


/**
 * 作者:evilbinary on 1/31/16.
 * 邮箱:<EMAIL>
 */

public class BorderView extends View implements ViewTreeObserver.OnGlobalFocusChangeListener, ViewTreeObserver.OnScrollChangedListener, ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnTouchModeChangeListener {

    private static String TAG = "BorderView";


    private BorderBaseEffect mEffect;


    private BorderView mBorderView;
    private boolean mEnableBorder = true;
    private ViewGroup mViewGroup;
    private boolean mInTouchMode = false;
    private boolean mFocusLimit = false;

    private AdapterView.OnItemSelectedListener mOnItemSelectedListener;

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

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

    private void init() {
        mBorderView = this;
        if (mEffect == null)
            mEffect = BorderBaseEffect.getDefault();
        setVisibility(INVISIBLE);
    }

    public BorderBaseEffect getEffect() {
        return mEffect;
    }

    public void setEffect(BorderBaseEffect effect) {
        mEffect = effect;
    }

    public void attachTo(ViewGroup viewGroup) {
        if (mViewGroup != viewGroup) {
            mViewGroup = viewGroup;
            ViewTreeObserver viewTreeObserver = mViewGroup.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalFocusChangeListener(this);
                viewTreeObserver.addOnScrollChangedListener(this);
                viewTreeObserver.addOnGlobalLayoutListener(this);
                viewTreeObserver.addOnTouchModeChangeListener(this);
            }
        }

    }

    public void detachFrom(ViewGroup viewGroup) {
        if (viewGroup == mViewGroup) {
            ViewTreeObserver viewTreeObserver = mViewGroup.getViewTreeObserver();
            viewTreeObserver.removeOnGlobalFocusChangeListener(this);
            viewTreeObserver.removeOnScrollChangedListener(this);
            viewTreeObserver.removeOnGlobalLayoutListener(this);
            viewTreeObserver.removeOnTouchModeChangeListener(this);
            if (getParent() == mViewGroup) {
                mViewGroup.removeView(this);
            }
        }
    }

    public boolean isFocusLimit() {
        return mFocusLimit;
    }

    public void setFocusLimit(boolean focusLimit) {
        this.mFocusLimit = focusLimit;
    }

    @Override
    public void onGlobalFocusChanged(View oldFocus, View newFocus) {
//        Log.d(TAG, "onGlobalFocusChanged");
        if (!mEnableBorder) return;
        if (mInTouchMode) return;
        if (mFocusLimit) {
            if (mViewGroup.indexOfChild(newFocus) < 0) {
                mEffect.end();
                return;
            }
            if (mViewGroup.indexOfChild(oldFocus) < 0) {
                oldFocus = null;
            }
        }

        if (mViewGroup instanceof GridView || mViewGroup instanceof ListView) {
            AbsListView gridView = (AbsListView) mViewGroup;
            if (mOnItemSelectedListener == null) {
                mOnItemSelectedListener = gridView.getOnItemSelectedListener();
                if (gridView.getSelectedView() != null) {
                    newFocus = gridView.getSelectedView();
                }
                final View tempFocus = newFocus;
                gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    private View oldFocus = tempFocus;
                    private View newFocus = null;

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        newFocus = view;
                        mEffect.cancle();
                        mEffect.start(mBorderView, oldFocus, newFocus);
                        oldFocus = newFocus;
                        if (mOnItemSelectedListener != null)
                            mOnItemSelectedListener.onItemSelected(parent, view, position, id);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        mOnItemSelectedListener.onNothingSelected(parent);

                    }
                });
            }
        }
            mEffect.cancle();
            mEffect.start(this, oldFocus, newFocus);


    }

    @Override
    public void onScrollChanged() {
//        Log.d(TAG, "onScrollChanged");

        if (mViewGroup instanceof GridView || mViewGroup instanceof ListView) {

        }else{
            mEffect.notifyChangeAnimation();
        }

    }

    @Override
    public void onGlobalLayout() {
//        Log.d(TAG, "onGlobalLayout");


    }

    @Override
{'fraction_non_alphanumeric': 0.08078101071975498, 'fraction_numerical': 0.001339969372128637, 'mean_word_length': 2.120967741935484, 'pattern_counts': {'":': 0, '<': 13, '<?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': '12212', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2590, 'n_words': 540}