1 /* 2 * Copyright (C) 2020 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.systemui.util 18 19 import android.content.Context 20 import android.util.AttributeSet 21 import android.widget.LinearLayout 22 23 /** 24 * Basically a normal linear layout but doesn't grow its children with weight 1 even when its 25 * measured with exactly. 26 */ 27 class NeverExactlyLinearLayout @JvmOverloads constructor( 28 context: Context, 29 attrs: AttributeSet? = null, 30 defStyleAttr: Int = 0 31 ) : LinearLayout(context, attrs, defStyleAttr) { onMeasurenull32 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 33 34 val (widthExactly, usedWidthSpec, width) = getNonExactlyMeasureSpec(widthMeasureSpec) 35 val (heightExactly, usedHeightSpec, height) = getNonExactlyMeasureSpec(heightMeasureSpec) 36 37 super.onMeasure(usedWidthSpec, usedHeightSpec) 38 if (widthExactly || heightExactly) { 39 val newWidth = if (widthExactly) width else measuredWidth 40 val newHeight = if (heightExactly) height else measuredHeight 41 setMeasuredDimension(newWidth, newHeight) 42 } 43 } 44 45 /** 46 * Obtain a measurespec that's not exactly 47 * 48 * @return a triple, where we return 1. if this was exactly, 2. the new measurespec, 3. the size 49 * of the measurespec 50 */ getNonExactlyMeasureSpecnull51 private fun getNonExactlyMeasureSpec(measureSpec: Int): Triple<Boolean, Int, Int> { 52 var newSpec = measureSpec 53 val isExactly = MeasureSpec.getMode(measureSpec) == MeasureSpec.EXACTLY 54 val size = MeasureSpec.getSize(measureSpec) 55 if (isExactly) { 56 newSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST) 57 } 58 return Triple(isExactly, newSpec, size) 59 } 60 }