]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/views/CheckedLinearLayout.java
06187dbfb28de5f07ed3a0144036fca3919c1c12
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / views / CheckedLinearLayout.java
1 /*
2  * Copyright © 2019 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  *
20  *
21  * This file is a modified version of <https://android.googlesource.com/platform/packages/apps/Camera/+/master/src/com/android/camera/ui/CheckedLinearLayout.java>.
22  *
23  * The original licensing information is below.
24  *
25  * Copyright (C) 2012 The Android Open Source Project
26  *
27  * Licensed under the Apache License, Version 2.0 (the "License");
28  * you may not use this file except in compliance with the License.
29  * You may obtain a copy of the License at
30  *
31  *      http://www.apache.org/licenses/LICENSE-2.0
32  *
33  * Unless required by applicable law or agreed to in writing, software
34  * distributed under the License is distributed on an "AS IS" BASIS,
35  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36  * See the License for the specific language governing permissions and
37  * limitations under the License.
38  */
39
40 package com.stoutner.privacybrowser.views;
41
42 import android.content.Context;
43 import android.util.AttributeSet;
44 import android.view.View;
45 import android.widget.Checkable;
46 import android.widget.LinearLayout;
47
48 import androidx.annotation.Nullable;
49
50 public class CheckedLinearLayout extends LinearLayout implements Checkable {
51     private boolean isCurrentlyChecked;
52     private static final int[] CHECKED_STATE_SET = {
53             android.R.attr.state_checked
54     };
55
56     public CheckedLinearLayout(Context context) {
57         // Run the default commands.
58         super(context);
59     }
60
61     public CheckedLinearLayout(Context context, @Nullable AttributeSet attributeSet) {
62         // Run the default commands.
63         super(context, attributeSet);
64     }
65
66     public CheckedLinearLayout(Context context, @Nullable AttributeSet attributeSet, int defaultStyleAttribute) {
67         // Run the default commands.
68         super(context, attributeSet, defaultStyleAttribute);
69     }
70
71     /*  This constructor can only be added once the minimum API >= 21.
72     public CheckedLinearLayout(Context context, @Nullable AttributeSet attributeSet, int defaultStyleAttribute, int defaultStyleResource) {
73         // Run the default commands.
74         super(context, attributeSet, defaultStyleAttribute, defaultStyleResource);
75     } */
76
77     @Override
78     public boolean isChecked() {
79         // Return the checked status.
80         return isCurrentlyChecked;
81     }
82
83     @Override
84     public void setChecked(boolean checked) {
85         // Only process the command if a change is requested.
86         if (isCurrentlyChecked != checked) {
87             // Update the is currently checked tracker.
88             isCurrentlyChecked = checked;
89
90             // Refresh the drawable state.
91             refreshDrawableState();
92
93             // Propagate the checked status to the child views.
94             for (int i = 0; i < getChildCount(); i++) {
95                 // Get a handle for the child view.
96                 View childView = getChildAt(i);
97
98                 // Propagate the checked status if the child view is checkable.
99                 if (childView instanceof Checkable) {
100                     // Cast the child view to `Checkable`.
101                     Checkable checkableChildView = (Checkable) childView;
102
103                     // Set the checked status.
104                     checkableChildView.setChecked(checked);
105                 }
106             }
107         }
108     }
109
110     @Override
111     public void toggle() {
112         // Toggle the state.
113         setChecked(!isCurrentlyChecked);
114     }
115
116     @Override
117     public int[] onCreateDrawableState(int extraSpace) {
118         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
119
120         if (isCurrentlyChecked) {
121             mergeDrawableStates(drawableState, CHECKED_STATE_SET);
122         }
123
124         return drawableState;
125     }
126 }