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, CARD_START_END, CARD_START_LIST_END -> 55 context.resources.getDimensionPixelSize(R.dimen.sc_card_margin) 56 LIST_START, LIST_START_CARD_END, LIST_START_END -> 57 context.resources.getDimensionPixelSize(R.dimen.sc_list_margin_top) 58 else -> 0 59 } 60 61 companion object { 62 @JvmStatic 63 @JvmOverloads calculatenull64 fun calculate( 65 isListStart: Boolean, 66 isListEnd: Boolean, 67 isCardStart: Boolean = !isListStart, 68 isCardEnd: Boolean = !isListEnd 69 ): PositionInCardList = 70 if (isListStart && isListEnd) { 71 LIST_START_END 72 } else if (isListStart && isCardEnd) { 73 LIST_START_CARD_END 74 } else if (isListEnd && isCardStart) { 75 CARD_START_LIST_END 76 } else if (isCardStart && isCardEnd) { 77 CARD_START_END 78 } else if (isListStart) { 79 LIST_START 80 } else if (isListEnd) { 81 LIST_END 82 } else if (isCardStart) { 83 CARD_START 84 } else if (isCardEnd) { 85 CARD_END 86 } else { 87 CARD_ELEMENT 88 } 89 } 90 } 91