1 /*
2  * Copyright (C) 2022 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.permissioncontroller.safetycenter.ui
17 
18 import android.content.Context
19 import com.android.permissioncontroller.R
20 
21 /**
22  * Determines the correct background drawable for a given element in the Safety Center card list.
23  *
24  * This class helps transform a flat list of elements (which are typically Preferences in the
25  * PreferenceScreen's recycler view) into a visually grouped list of cards by providing the correct
26  * background drawable for each element based on both its position in the flat list of elements and
27  * its conceptual position in the card list. While Preferences have a nested XML structure, with
28  * Preferences nested inside of PreferenceGroups, they are displayed as a flat list of views, with
29  * the PreferenceGroup's view inserted as a header above its constituent preference's views.
30  *
31  * A list is a group conceptually-related of cards. The top card in the list has large top corners
32  * and the bottom card in the list has large bottom corners. Corners between cards in the list are
33  * small. In the Safety Center, the entry list is a single list.
34  *
35  * A card is a group of one or more elements that appear as a single visual card, without corners
36  * between its constituent elements. In the Safety Center, a single expanded entry list group is a
37  * single card composed of a list of separate preferences (one for the header and one for each entry
38  * in the group).
39  */
40 internal enum class PositionInCardList(val backgroundDrawableResId: Int) {
41     INSIDE_GROUP(R.drawable.safety_group_entry_background),
42     LIST_START_END(R.drawable.safety_entity_top_large_bottom_large_background),
43     LIST_START(R.drawable.safety_entity_top_large_bottom_flat_background),
44     LIST_START_CARD_END(R.drawable.safety_entity_top_large_bottom_small_background),
45     CARD_START(R.drawable.safety_entity_top_small_bottom_flat_background),
46     CARD_START_END(R.drawable.safety_entity_top_small_bottom_small_background),
47     CARD_START_LIST_END(R.drawable.safety_entity_top_small_bottom_large_background),
48     CARD_ELEMENT(R.drawable.safety_entity_top_flat_bottom_flat_background),
49     CARD_END(R.drawable.safety_entity_top_flat_bottom_small_background),
50     LIST_END(R.drawable.safety_entity_top_flat_bottom_large_background);
51 
getTopMarginnull52     fun getTopMargin(context: Context): Int =
53         when (this) {
54             CARD_START,
55             CARD_START_END,
56             CARD_START_LIST_END -> context.resources.getDimensionPixelSize(R.dimen.sc_card_margin)
57             LIST_START,
58             LIST_START_CARD_END,
59             LIST_START_END -> context.resources.getDimensionPixelSize(R.dimen.sc_list_margin_top)
60             else -> 0
61         }
62 
63     companion object {
64         @JvmStatic
65         @JvmOverloads
calculatenull66         fun calculate(
67             isListStart: Boolean,
68             isListEnd: Boolean,
69             isCardStart: Boolean = !isListStart,
70             isCardEnd: Boolean = !isListEnd
71         ): PositionInCardList =
72             if (isListStart && isListEnd) {
73                 LIST_START_END
74             } else if (isListStart && isCardEnd) {
75                 LIST_START_CARD_END
76             } else if (isListEnd && isCardStart) {
77                 CARD_START_LIST_END
78             } else if (isCardStart && isCardEnd) {
79                 CARD_START_END
80             } else if (isListStart) {
81                 LIST_START
82             } else if (isListEnd) {
83                 LIST_END
84             } else if (isCardStart) {
85                 CARD_START
86             } else if (isCardEnd) {
87                 CARD_END
88             } else {
89                 CARD_ELEMENT
90             }
91     }
92 }
93