1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.privacy 16 17 import android.content.Context 18 import android.util.AttributeSet 19 import android.view.ViewGroup 20 import android.widget.FrameLayout 21 import android.widget.ImageView 22 import android.widget.LinearLayout 23 import com.android.settingslib.Utils 24 import com.android.systemui.R 25 26 class OngoingPrivacyChip @JvmOverloads constructor( 27 context: Context, 28 attrs: AttributeSet? = null, 29 defStyleAttrs: Int = 0, 30 defStyleRes: Int = 0 31 ) : FrameLayout(context, attrs, defStyleAttrs, defStyleRes) { 32 33 private var iconMargin = 0 34 private var iconSize = 0 35 private var iconColor = 0 36 37 private lateinit var iconsContainer: LinearLayout 38 39 var privacyList = emptyList<PrivacyItem>() 40 set(value) { 41 field = value 42 updateView(PrivacyChipBuilder(context, field)) 43 } 44 45 override fun onFinishInflate() { 46 super.onFinishInflate() 47 48 iconsContainer = requireViewById(R.id.icons_container) 49 50 updateResources() 51 } 52 53 // Should only be called if the builder icons or app changed 54 private fun updateView(builder: PrivacyChipBuilder) { 55 fun setIcons(chipBuilder: PrivacyChipBuilder, iconsContainer: ViewGroup) { 56 iconsContainer.removeAllViews() 57 chipBuilder.generateIcons().forEachIndexed { i, it -> 58 it.mutate() 59 it.setTint(iconColor) 60 val image = ImageView(context).apply { 61 setImageDrawable(it) 62 scaleType = ImageView.ScaleType.CENTER_INSIDE 63 } 64 iconsContainer.addView(image, iconSize, iconSize) 65 if (i != 0) { 66 val lp = image.layoutParams as MarginLayoutParams 67 lp.marginStart = iconMargin 68 image.layoutParams = lp 69 } 70 } 71 } 72 73 if (!privacyList.isEmpty()) { 74 generateContentDescription(builder) 75 setIcons(builder, iconsContainer) 76 } else { 77 iconsContainer.removeAllViews() 78 } 79 requestLayout() 80 } 81 82 private fun generateContentDescription(builder: PrivacyChipBuilder) { 83 val typesText = builder.joinTypes() 84 contentDescription = context.getString( 85 R.string.ongoing_privacy_chip_content_multiple_apps, typesText) 86 } 87 88 private fun updateResources() { 89 iconMargin = context.resources 90 .getDimensionPixelSize(R.dimen.ongoing_appops_chip_icon_margin) 91 iconSize = context.resources 92 .getDimensionPixelSize(R.dimen.ongoing_appops_chip_icon_size) 93 iconColor = 94 Utils.getColorAttrDefaultColor(context, com.android.internal.R.attr.colorPrimary) 95 96 val padding = context.resources 97 .getDimensionPixelSize(R.dimen.ongoing_appops_chip_side_padding) 98 iconsContainer.setPaddingRelative(padding, 0, padding, 0) 99 iconsContainer.background = context.getDrawable(R.drawable.privacy_chip_bg) 100 } 101 }