1 /* 2 * Copyright (C) 2023 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.launcher3.responsive 18 19 import android.content.res.TypedArray 20 import com.android.launcher3.R 21 import com.android.launcher3.responsive.ResponsiveSpec.SpecType 22 import com.android.launcher3.util.ResourceHelper 23 24 class FolderSpecs(widthSpecs: List<FolderSpec>, heightSpecs: List<FolderSpec>) : 25 ResponsiveSpecs<FolderSpec>(widthSpecs, heightSpecs) { 26 getCalculatedWidthSpecnull27 fun getCalculatedWidthSpec( 28 columns: Int, 29 availableWidth: Int, 30 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 31 ): CalculatedFolderSpec { 32 check(calculatedWorkspaceSpec.spec.specType == SpecType.WIDTH) { 33 "Invalid specType for CalculatedWorkspaceSpec. " + 34 "Expected: ${SpecType.WIDTH} - " + 35 "Found: ${calculatedWorkspaceSpec.spec.specType}}" 36 } 37 38 val spec = getWidthSpec(availableWidth) 39 return CalculatedFolderSpec(availableWidth, columns, spec, calculatedWorkspaceSpec) 40 } 41 getCalculatedHeightSpecnull42 fun getCalculatedHeightSpec( 43 rows: Int, 44 availableHeight: Int, 45 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 46 ): CalculatedFolderSpec { 47 check(calculatedWorkspaceSpec.spec.specType == SpecType.HEIGHT) { 48 "Invalid specType for CalculatedWorkspaceSpec. " + 49 "Expected: ${SpecType.HEIGHT} - " + 50 "Found: ${calculatedWorkspaceSpec.spec.specType}}" 51 } 52 53 val spec = getHeightSpec(availableHeight) 54 return CalculatedFolderSpec(availableHeight, rows, spec, calculatedWorkspaceSpec) 55 } 56 57 companion object { 58 59 private const val XML_FOLDER_SPEC = "folderSpec" 60 61 @JvmStatic createnull62 fun create(resourceHelper: ResourceHelper): FolderSpecs { 63 val parser = ResponsiveSpecsParser(resourceHelper) 64 val specs = parser.parseXML(XML_FOLDER_SPEC, ::FolderSpec) 65 val (widthSpecs, heightSpecs) = specs.partition { it.specType == SpecType.WIDTH } 66 return FolderSpecs(widthSpecs, heightSpecs) 67 } 68 } 69 } 70 71 data class FolderSpec( 72 override val maxAvailableSize: Int, 73 override val specType: SpecType, 74 override val startPadding: SizeSpec, 75 override val endPadding: SizeSpec, 76 override val gutter: SizeSpec, 77 override val cellSize: SizeSpec 78 ) : ResponsiveSpec(maxAvailableSize, specType, startPadding, endPadding, gutter, cellSize) { 79 80 init { <lambda>null81 check(isValid()) { "Invalid FolderSpec found." } 82 } 83 84 constructor( 85 attrs: TypedArray, 86 specs: Map<String, SizeSpec> 87 ) : this( 88 maxAvailableSize = 89 attrs.getDimensionPixelSize(R.styleable.ResponsiveSpec_maxAvailableSize, 0), 90 specType = 91 SpecType.values()[ 92 attrs.getInt(R.styleable.ResponsiveSpec_specType, SpecType.HEIGHT.ordinal)], 93 startPadding = specs.getOrError(SizeSpec.XmlTags.START_PADDING), 94 endPadding = specs.getOrError(SizeSpec.XmlTags.END_PADDING), 95 gutter = specs.getOrError(SizeSpec.XmlTags.GUTTER), 96 cellSize = specs.getOrError(SizeSpec.XmlTags.CELL_SIZE) 97 ) 98 } 99 100 class CalculatedFolderSpec( 101 availableSpace: Int, 102 cells: Int, 103 spec: FolderSpec, 104 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 105 ) : CalculatedResponsiveSpec(availableSpace, cells, spec, calculatedWorkspaceSpec) 106