package com.wasu.widgets.focuswidget;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;

import com.wasu.widget.FocusGridView;

import cn.com.wasu.main.BuildConfig;
import cn.com.wasu.main.ChannelFlavor;

/**
 * Created by frewen on 2017/1/4.
 * 继承FocusGridView. 1、解决FocusGridView中在 SDK=19的Android系统会抢焦点的问题
 */

public class CustomFocusGridView extends FocusGridView {
    private static final String TAG = "CustomFocusGridView";
    private GridViewColumnBeginListener mGridViewColumnBeginListener;

    public CustomFocusGridView(Context context) {
        super(context);
    }

    public CustomFocusGridView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        //针对乐视的渠道进行焦点的特殊处理
        if (BuildConfig.FLAVOR.equalsIgnoreCase(ChannelFlavor.LETV_MARKET)) {
            if (1 == ((getSelectedItemPosition() + 1) % getNumColumns()) && event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
                if (null != mGridViewColumnBeginListener) {
                    mGridViewColumnBeginListener.onColumnBegin();
                }
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);
    }
    @Override
    public boolean isInTouchMode() {
        if (19 <= Build.VERSION.SDK_INT) {
            return !(hasFocus() && !super.isInTouchMode());
        } else {
            return super.isInTouchMode();
        }
    }
    public interface GridViewColumnBeginListener {
        void onColumnBegin();
    }

    public void setOnGridViewColumnBeginListener(GridViewColumnBeginListener listener){
        this.mGridViewColumnBeginListener = listener;
    }
}
