1 /*
2  * Copyright (C) 2019 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 package com.android.car.developeroptions;
17 
18 import android.content.Context;
19 import android.util.AttributeSet;
20 import android.util.TypedValue;
21 import android.view.View;
22 import android.widget.Checkable;
23 import android.widget.LinearLayout;
24 
25 public class CheckableLinearLayout extends LinearLayout implements Checkable {
26 
27     private boolean mChecked;
28     private float mDisabledAlpha;
29 
CheckableLinearLayout(Context context, AttributeSet attrs)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
setEnabled(boolean enabled)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
setChecked(boolean checked)47     public void setChecked(boolean checked) {
48         mChecked = checked;
49         updateChecked();
50     }
51 
52     @Override
isChecked()53     public boolean isChecked() {
54         return mChecked;
55     }
56 
57     @Override
toggle()58     public void toggle() {
59         setChecked(!mChecked);
60     }
61 
updateChecked()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