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.systemui.user 17 18 import android.content.Context 19 import android.graphics.Canvas 20 import android.graphics.drawable.ShapeDrawable 21 import android.view.View 22 import android.view.View.MeasureSpec 23 import android.widget.ListAdapter 24 import android.widget.ListPopupWindow 25 import android.widget.ListView 26 import com.android.systemui.res.R 27 28 /** 29 * Popup menu for displaying items on the fullscreen user switcher. 30 */ 31 class UserSwitcherPopupMenu( 32 private val context: Context 33 ) : ListPopupWindow(context) { 34 35 private val res = context.resources 36 private var adapter: ListAdapter? = null 37 38 init { 39 setBackgroundDrawable(null) 40 setModal(false) 41 setOverlapAnchor(true) 42 } 43 setAdapternull44 override fun setAdapter(adapter: ListAdapter?) { 45 super.setAdapter(adapter) 46 this.adapter = adapter 47 } 48 49 /** 50 * Show the dialog. 51 */ shownull52 override fun show() { 53 // need to call show() first in order to construct the listView 54 super.show() 55 listView?.apply { 56 isVerticalScrollBarEnabled = false 57 isHorizontalScrollBarEnabled = false 58 59 // Creates a transparent spacer between items 60 val shape = ShapeDrawable() 61 shape.alpha = 0 62 divider = shape 63 dividerHeight = res.getDimensionPixelSize( 64 R.dimen.bouncer_user_switcher_popup_divider_height) 65 66 val height = res.getDimensionPixelSize(R.dimen.bouncer_user_switcher_popup_header_height) 67 addHeaderView(createSpacer(height), null, false) 68 addFooterView(createSpacer(height), null, false) 69 setWidth(findMaxWidth(this)) 70 } 71 72 super.show() 73 } 74 findMaxWidthnull75 private fun findMaxWidth(listView: ListView): Int { 76 var maxWidth = 0 77 adapter?.let { 78 val parentWidth = res.getDisplayMetrics().widthPixels 79 val spec = MeasureSpec.makeMeasureSpec( 80 (parentWidth * 0.25).toInt(), 81 MeasureSpec.AT_MOST 82 ) 83 84 for (i in 0 until it.getCount()) { 85 val child = it.getView(i, null, listView) 86 child.measure(spec, MeasureSpec.UNSPECIFIED) 87 maxWidth = Math.max(child.getMeasuredWidth(), maxWidth) 88 } 89 } 90 return maxWidth 91 } 92 createSpacernull93 private fun createSpacer(height: Int): View { 94 return object : View(context) { 95 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 96 setMeasuredDimension(1, height) 97 } 98 99 override fun draw(canvas: Canvas) { 100 } 101 } 102 } 103 } 104