1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.music;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.widget.Checkable;
22import android.widget.RelativeLayout;
23
24/**
25 * A special variation of RelativeLayout that can be used as a checkable object.
26 * This allows it to be used as the top-level view of a list view item, which
27 * also supports checking.  Otherwise, it works identically to a RelativeLayout.
28 */
29public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
30    private boolean mChecked;
31
32    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
33
34    public CheckableRelativeLayout(Context context, AttributeSet attrs) {
35        super(context, attrs);
36    }
37
38    @Override
39    protected int[] onCreateDrawableState(int extraSpace) {
40        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
41        if (isChecked()) {
42            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
43        }
44        return drawableState;
45    }
46
47    public void toggle() {
48        setChecked(!mChecked);
49    }
50
51    public boolean isChecked() {
52        return mChecked;
53    }
54
55    public void setChecked(boolean checked) {
56        if (mChecked != checked) {
57            mChecked = checked;
58            refreshDrawableState();
59        }
60    }
61}
62