package com.yeamy.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.SoundEffectConstants;
import android.widget.Checkable;
import android.widget.ImageView;

public class ImageCheckButton extends ImageView implements Checkable {
    private boolean checked;
    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    public ImageCheckButton(Context context) {
        this(context, null);
    }

    public ImageCheckButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageCheckButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    @TargetApi(21)
    public ImageCheckButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes){
        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.ImageCheckButton, defStyleAttr, defStyleRes);
        boolean checked = a.getBoolean(R.styleable.ImageCheckButton_checked, false);
        a.recycle();
        setChecked(checked);
        setFocusable(true);
        setClickable(true);
        setScaleType(ScaleType.CENTER);
    }

    @Override
    public boolean performClick() {
        toggle();

        final boolean handled = super.performClick();
        if (!handled) {
            // View only makes a sound effect if the onClickListener was
            // called, so we'll need to make one here instead.
            playSoundEffect(SoundEffectConstants.CLICK);
        }

        return handled;
    }

    @Override
    public void setChecked(boolean checked) {
        if (this.checked != checked) {
            this.checked = checked;
            refreshDrawableState();
        }
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void toggle() {
        setChecked(!checked);
    }

    public interface OnCheckedChangeListener {
        public void onCheckedChanged(ImageCheckButton imageView, boolean isChecked);
    }
}

@@ -367,24 +367,70 @@
 an checked;%0A
+    private OnCheckedChangeListener listener;%0A
     private 
@@ -1211,16 +1211,17 @@
 tyleRes)
+ 
 %7B%0A      
@@ -1545,48 +1545,8 @@
 e);%0A
-        setScaleType(ScaleType.CENTER);%0A
     
@@ -1617,16 +1617,134 @@
 gle();%0A%0A
+        if (listener != null) %7B%0A            listener.onCheckedChanged(ImageCheckButton.this, isChecked());%0A        %7D%0A%0A
         
@@ -2008,33 +2008,32 @@
 ICK);%0A        %7D%0A
-%0A
         return h
@@ -2314,22 +2314,16 @@
         
-final 
 int%5B%5D dr
@@ -2670,32 +2670,152 @@
 hecked);%0A    %7D%0A%0A
+    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) %7B%0A        this.listener = listener;%0A    %7D%0A%0A
     public inter
@@ -2849,31 +2849,24 @@
 r %7B%0A        
-public 
 void onCheck
@@ -2929,10 +2929,10 @@
 ;%0A    %7D%0A
-%7D
 %0A
+%7D

add listener support