]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/views/CheckedLinearLayout.kt
Update the URL in the copyright header. https://redmine.stoutner.com/issues/796
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / views / CheckedLinearLayout.kt
1 /*
2  * Copyright © 2019,2021-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android 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 Android 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 Android.  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.widget.Checkable
45 import android.widget.LinearLayout
46
47 // Define the class constants.
48
49 class CheckedLinearLayout : LinearLayout, Checkable {
50     // Define the class variables.
51     private var isCurrentlyChecked = false
52     private val checkedStateSet = intArrayOf(android.R.attr.state_checked)
53
54     // The constructors.
55     constructor(context: Context) : super(context)
56
57     constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)
58
59     constructor(context: Context, attributeSet: AttributeSet?, defaultStyleAttribute: Int) : super(context, attributeSet, defaultStyleAttribute)
60
61     // This constructor can only be added once the minimum API >= 21.
62     // constructor(context: Context, attributeSet: AttributeSet?, defaultStyleAttribute: Int, defaultStyleResource: Int) : super(context, attributeSet, defaultStyleAttribute, defaultStyleResource)
63
64     override fun isChecked(): Boolean {
65         // Return the checked status.
66         return isCurrentlyChecked
67     }
68
69     override fun setChecked(checked: Boolean) {
70         // Only process the command if a change is requested.
71         if (isCurrentlyChecked != checked) {
72             // Update the status tracker.
73             isCurrentlyChecked = checked
74
75             // Refresh the drawable state.
76             refreshDrawableState()
77
78             // Propagate the checked status to the child views.
79             for (i in 0 until childCount) {
80                 // Get a handle for the child view.
81                 val childView = getChildAt(i)
82
83                 // Propagate the checked status if the child view is checkable.
84                 if (childView is Checkable) {
85                     // Cast the child view to `Checkable`.
86                     val checkableChildView = childView as Checkable
87
88                     // Set the checked status.
89                     checkableChildView.isChecked = checked
90                 }
91             }
92         }
93     }
94
95     override fun toggle() {
96         // Toggle the state.
97         isChecked = !isCurrentlyChecked
98     }
99
100     public override fun onCreateDrawableState(extraSpace: Int): IntArray {
101         // Run the default commands.
102         val drawableState = super.onCreateDrawableState(extraSpace + 1)
103
104         if (isCurrentlyChecked) {
105             // Merge the drawable states.
106             mergeDrawableStates(drawableState, checkedStateSet)
107         }
108
109         // Return the drawable state.
110         return drawableState
111     }
112 }