111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu/*
211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * Copyright (C) 2015 The Android Open Source Project
311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu *
411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * in compliance with the License. You may obtain a copy of the License at
611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu *
711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * http://www.apache.org/licenses/LICENSE-2.0
811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu *
911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * Unless required by applicable law or agreed to in writing, software distributed under the License
1011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
1111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * or implied. See the License for the specific language governing permissions and limitations under
1211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * the License.
1311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu */
1411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gupackage android.support.v17.leanback.widget;
1511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
1611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Guimport android.content.Context;
1711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Guimport android.util.AttributeSet;
1811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Guimport android.widget.Checkable;
1911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Guimport android.widget.ImageView;
2011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
2111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu/**
2211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu * ImageView that supports Checkable states.
2311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu */
2411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Guclass CheckableImageView extends ImageView implements Checkable {
2511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
2611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    private boolean mChecked;
2711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
2811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
2911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
3011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public CheckableImageView(Context context) {
3111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        this(context, null);
3211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
3311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
3411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public CheckableImageView(Context context, AttributeSet attrs) {
3511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        this(context, attrs, 0);
3611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
3711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
3811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public CheckableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
3911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        super(context, attrs, defStyleAttr);
4011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
4111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
4211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    @Override
4311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public int[] onCreateDrawableState(final int extraSpace) {
4411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
4511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        if (isChecked()) {
4611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
4711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        }
4811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        return drawableState;
4911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
5011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
5111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    @Override
5211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public void toggle() {
5311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        setChecked(!mChecked);
5411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
5511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
5611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    @Override
5711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public boolean isChecked() {
5811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        return mChecked;
5911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
6011cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
6111cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    @Override
6211cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    public void setChecked(final boolean checked) {
6311cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        if (mChecked != checked) {
6411cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu            mChecked = checked;
6511cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu            refreshDrawableState();
6611cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu        }
6711cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu    }
6811cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu
6911cb62de8bfc6b5b6d22811ad12a1e60451b82beDake Gu}
70