1/*
2 * Copyright (C) 2015 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 */
16package com.android.settings;
17
18import android.content.Context;
19import android.util.AttributeSet;
20import android.util.TypedValue;
21import android.view.View;
22import android.widget.Checkable;
23import android.widget.LinearLayout;
24
25public class CheckableLinearLayout extends LinearLayout implements Checkable {
26
27    private boolean mChecked;
28    private float mDisabledAlpha;
29
30    public CheckableLinearLayout(Context context, AttributeSet attrs) {
31        super(context, attrs);
32        TypedValue alpha = new TypedValue();
33        context.getTheme().resolveAttribute(android.R.attr.disabledAlpha, alpha, true);
34        mDisabledAlpha = alpha.getFloat();
35    }
36
37    @Override
38    public void setEnabled(boolean enabled) {
39        super.setEnabled(enabled);
40        final int N = getChildCount();
41        for (int i = 0; i < N; i++) {
42            getChildAt(i).setAlpha(enabled ? 1 : mDisabledAlpha);
43        }
44    }
45
46    @Override
47    public void setChecked(boolean checked) {
48        mChecked = checked;
49        updateChecked();
50    }
51
52    @Override
53    public boolean isChecked() {
54        return mChecked;
55    }
56
57    @Override
58    public void toggle() {
59        setChecked(!mChecked);
60    }
61
62    private void updateChecked() {
63        final int N = getChildCount();
64        for (int i = 0; i < N; i++) {
65            View child = getChildAt(i);
66            if (child instanceof Checkable) {
67                ((Checkable) child).setChecked(mChecked);
68            }
69        }
70    }
71
72}
73