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 = spacer.tag as? View.OnLayoutChangeListener 66 ?: object : View.OnLayoutChangeListener { 67 override fun onLayoutChange( 68 v: View?, 69 left: Int, 70 top: Int, 71 right: Int, 72 bottom: Int, 73 oldLeft: Int, 74 oldTop: Int, 75 oldRight: Int, 76 oldBottom: Int 77 ) { 78 adjustHeight(spacer) 79 }}.also { spacer.tag = it } 80 81 spacer.removeOnLayoutChangeListener(listener) 82 spacer.addOnLayoutChangeListener(listener) 83 } 84 adjustHeightnull85 private fun adjustHeight(spacer: View) { 86 val root = spacer.rootView as? ViewGroup 87 if (root == null) { 88 return 89 } 90 91 val contentParent = root.findViewById<ViewGroup>(R.id.content_parent) 92 if (contentParent == null) { 93 return 94 } 95 // when opening the Subpage from Search the layout pass may be triggered 96 // differently due to the auto-scroll to highlight a specific item, 97 // and in this case we need to wait the content parent to be measured 98 if (contentParent.height == 0) { 99 val globalLayoutObserver = object : ViewTreeObserver.OnGlobalLayoutListener { 100 override fun onGlobalLayout() { 101 contentParent.viewTreeObserver.removeOnGlobalLayoutListener(this) 102 adjustHeight(spacer) 103 } 104 } 105 contentParent.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutObserver) 106 return 107 } 108 109 val collapsingToolbar = root.findViewById<View>(R.id.collapsing_toolbar) 110 maxKnownToolbarHeight = max(maxKnownToolbarHeight, collapsingToolbar.height) 111 112 val contentHeight = spacer.top + maxKnownToolbarHeight 113 val desiredSpacerHeight = if (contentHeight > contentParent.height) { 114 // making it 0 height will remove if from recyclerview 115 1 116 } else { 117 // to unlock the scrolling we need spacer to go slightly beyond the screen 118 contentParent.height - contentHeight + 1 119 } 120 121 val layoutParams = spacer.layoutParams 122 if (layoutParams.height != desiredSpacerHeight) { 123 layoutParams.height = desiredSpacerHeight 124 spacer.layoutParams = layoutParams 125 // need to let RecyclerView to update scroll position 126 spacer.post(::notifyChanged) 127 } 128 } 129 } 130