1 /* 2 * Copyright (C) 2024 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.settings.connecteddevice.display 18 19 import com.android.settings.R 20 21 import android.content.Context 22 import android.graphics.Bitmap 23 import android.graphics.Color 24 import android.graphics.PointF 25 import android.graphics.RectF 26 import android.graphics.drawable.BitmapDrawable 27 import android.graphics.drawable.Drawable 28 import android.graphics.drawable.ColorDrawable 29 import android.graphics.drawable.LayerDrawable 30 import android.widget.Button 31 32 import androidx.annotation.VisibleForTesting 33 34 /** Represents a draggable block in the topology pane. */ 35 class DisplayBlock(context : Context) : Button(context) { 36 @VisibleForTesting var mSelectedImage: Drawable = ColorDrawable(Color.BLACK) 37 @VisibleForTesting var mUnselectedImage: Drawable = ColorDrawable(Color.BLACK) 38 39 private val mSelectedBg = context.getDrawable( 40 R.drawable.display_block_selection_marker_background)!! 41 private val mUnselectedBg = context.getDrawable( 42 R.drawable.display_block_unselected_background)!! 43 private val mInsetPx = context.resources.getDimensionPixelSize(R.dimen.display_block_padding) 44 45 init { 46 isScrollContainer = false 47 isVerticalScrollBarEnabled = false 48 isHorizontalScrollBarEnabled = false 49 50 // Prevents shadow from appearing around edge of button. 51 stateListAnimator = null 52 } 53 54 /** Sets position of the block given unpadded coordinates. */ placenull55 fun place(topLeft: PointF) { 56 x = topLeft.x 57 y = topLeft.y 58 } 59 setWallpapernull60 fun setWallpaper(wallpaper: Bitmap?) { 61 val wallpaperDrawable = BitmapDrawable(context.resources, wallpaper ?: return) 62 63 fun framedBy(bg: Drawable): Drawable = 64 LayerDrawable(arrayOf(wallpaperDrawable, bg)).apply { 65 setLayerInsetRelative(0, mInsetPx, mInsetPx, mInsetPx, mInsetPx) 66 } 67 mSelectedImage = framedBy(mSelectedBg) 68 mUnselectedImage = framedBy(mUnselectedBg) 69 } 70 setHighlightednull71 fun setHighlighted(value: Boolean) { 72 background = if (value) mSelectedImage else mUnselectedImage 73 } 74 75 /** Sets position and size of the block given unpadded bounds. */ placeAndSizenull76 fun placeAndSize(bounds : RectF, scale : TopologyScale) { 77 val topLeft = scale.displayToPaneCoor(bounds.left, bounds.top) 78 val bottomRight = scale.displayToPaneCoor(bounds.right, bounds.bottom) 79 val layout = layoutParams 80 layout.width = (bottomRight.x - topLeft.x).toInt() 81 layout.height = (bottomRight.y - topLeft.y).toInt() 82 layoutParams = layout 83 place(topLeft) 84 } 85 } 86