/*
 * Copyright (c) 2016. Louis Cognault Ayeva Derman
 *
 * 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 xyz.louiscad.selectableviewgroups.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;

import xyz.louiscad.selectableviewgroups.R;

import static splitties.selectableviews.SelectableViewsUtils.getDefaultForegroundSelector;

/**
 * ConstraintLayout with ripple effect / select foreground when touched
 */
@SuppressWarnings("unused")
public class SelectableConstraintLayout extends ConstraintLayout {

    private Drawable mForegroundSelector;

    public SelectableConstraintLayout(@NonNull Context context) {
        super(context);
        init(context, null, 0, 0);
    }

    public SelectableConstraintLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);
    }

    public SelectableConstraintLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    @CallSuper
    @SuppressWarnings("WeakerAccess")
    protected void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        final TypedArray ta = context.obtainStyledAttributes(attrs,
                R.styleable.SelectableRelativeLayout,
                defStyleAttr,
                defStyleRes
        );
        mForegroundSelector = ta.getDrawable(R.styleable.SelectableRelativeLayout_foreground);
        ta.recycle();
        if (mForegroundSelector == null) {
            mForegroundSelector = getDefaultForegroundSelector(context);
        }
        mForegroundSelector.setCallback(this);
        setWillNotDraw(false);
    }

    @Override
    protected void drawableStateChanged() {
        super.drawableStateChanged();
        mForegroundSelector.setState(getDrawableState());
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mForegroundSelector.setBounds(0, 0, w, h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mForegroundSelector.draw(canvas);
    }

    @Override
    public void jumpDrawablesToCurrentState() {
        super.jumpDrawablesToCurrentState();
        mForegroundSelector.jumpToCurrentState();
    }

    @Override
    protected boolean verifyDrawable(@NonNull Drawable who) {
        return super.verifyDrawable(who) || (who == mForegroundSelector);
    }

    @Override
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public void dispatchDrawableHotspotChanged(float x, float y) {
        super.dispatchDrawableHotspotChanged(x, y);
        mForegroundSelector.setHotspot(x, y);
    }
}

@@ -2227,32 +2227,34 @@
 e.Selectable
-Relative
+Constraint
 Layout,%0A    
@@ -2389,16 +2389,18 @@
 able
-Relative
+Constraint
 Layo

Fix init method of SelectableConstraintLayout