1 /*
2  * Copyright (C) 2023 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 
17 package com.android.permissioncontroller.safetycenter.ui
18 
19 import android.content.Context
20 import android.util.AttributeSet
21 import android.view.View
22 import android.view.ViewGroup
23 import android.view.ViewTreeObserver
24 import androidx.preference.Preference
25 import androidx.preference.PreferenceScreen
26 import androidx.preference.PreferenceViewHolder
27 import com.android.permissioncontroller.R
28 import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity
29 import com.android.settingslib.widget.FooterPreference
30 import kotlin.math.max
31 
32 /**
33  * A preference that adds an empty space to the bottom of a Safety Center subpage.
34  *
35  * Due to the logic of [CollapsingToolbarBaseActivity], its content won't be scrollable if it fits
36  * the single page. This logic conflicts with the UX of collapsible and expandable items of Safety
37  * Center, and with some other use cases (i.e. opening the page from Search might scroll to bottom
38  * while the scroll is disabled). In such cases user won't be able to expand the collapsed toolbar
39  * by scrolling the screen content.
40  *
41  * [SpacerPreference] makes the page to be slightly bigger than the screen size to unlock the scroll
42  * regardless of the content length and to mitigate this UX problem.
43  *
44  * If a [FooterPreference] is added to the same [PreferenceScreen], its order should be decreased to
45  * keep it with the last visible content above the [SpacerPreference].
46  */
47 internal class SpacerPreference(context: Context, attrs: AttributeSet) :
48     Preference(context, attrs) {
49 
50     init {
51         setLayoutResource(R.layout.preference_spacer)
52         isVisible = SafetyCenterUiFlags.getShowSubpages()
53         // spacer should be the last item on screen
54         setOrder(Int.MAX_VALUE - 1)
55     }
56 
57     private var maxKnownToolbarHeight = 0
onBindViewHoldernull58     override fun onBindViewHolder(holder: PreferenceViewHolder) {
59         super.onBindViewHolder(holder)
60         val spacer = holder.itemView
61 
62         // we should ensure we won't add multiple listeners to the same view,
63         // and Preferences API does not allow to do cleanups when onViewRecycled,
64         // so we are keeping a track of the added listener attaching it as a tag to the View
65         val listener: View.OnLayoutChangeListener =
66             spacer.tag as? View.OnLayoutChangeListener
67                 ?: object : View.OnLayoutChangeListener {
68                         override fun onLayoutChange(
69                             v: View?,
70                             left: Int,
71                             top: Int,
72                             right: Int,
73                             bottom: Int,
74                             oldLeft: Int,
75                             oldTop: Int,
76                             oldRight: Int,
77                             oldBottom: Int
78                         ) {
79                             adjustHeight(spacer)
80                         }
81                     }
82                     .also { spacer.tag = it }
83 
84         spacer.removeOnLayoutChangeListener(listener)
85         spacer.addOnLayoutChangeListener(listener)
86     }
87 
adjustHeightnull88     private fun adjustHeight(spacer: View) {
89         val root = spacer.rootView as? ViewGroup
90         if (root == null) {
91             return
92         }
93 
94         val contentParent =
95             root.findViewById<ViewGroup>(
96                 com.android.settingslib.collapsingtoolbar.R.id.content_parent
97             )
98         if (contentParent == null) {
99             return
100         }
101         // when opening the Subpage from Search the layout pass may be triggered
102         // differently due to the auto-scroll to highlight a specific item,
103         // and in this case we need to wait the content parent to be measured
104         if (contentParent.height == 0) {
105             val globalLayoutObserver =
106                 object : ViewTreeObserver.OnGlobalLayoutListener {
107                     override fun onGlobalLayout() {
108                         contentParent.viewTreeObserver.removeOnGlobalLayoutListener(this)
109                         adjustHeight(spacer)
110                     }
111                 }
112             contentParent.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutObserver)
113             return
114         }
115 
116         val collapsingToolbar =
117             root.findViewById<View>(
118                 com.android.settingslib.collapsingtoolbar.R.id.collapsing_toolbar
119             )
120         maxKnownToolbarHeight = max(maxKnownToolbarHeight, collapsingToolbar!!.height)
121 
122         val contentHeight = spacer.top + maxKnownToolbarHeight
123         val desiredSpacerHeight =
124             if (contentHeight > contentParent.height) {
125                 // making it 0 height will remove if from recyclerview
126                 1
127             } else {
128                 // to unlock the scrolling we need spacer to go slightly beyond the screen
129                 contentParent.height - contentHeight + 1
130             }
131 
132         val layoutParams = spacer.layoutParams
133         if (layoutParams.height != desiredSpacerHeight) {
134             layoutParams.height = desiredSpacerHeight
135             spacer.layoutParams = layoutParams
136             // need to let RecyclerView to update scroll position
137             spacer.post(::notifyChanged)
138         }
139     }
140 }
141