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 AllAppsSpecs(widthSpecs: List<AllAppsSpec>, heightSpecs: List<AllAppsSpec>) : 25 ResponsiveSpecs<AllAppsSpec>(widthSpecs, heightSpecs) { 26 getCalculatedWidthSpecnull27 fun getCalculatedWidthSpec( 28 columns: Int, 29 availableWidth: Int, 30 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 31 ): CalculatedAllAppsSpec { 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 CalculatedAllAppsSpec(availableWidth, columns, spec, calculatedWorkspaceSpec) 40 } 41 getCalculatedHeightSpecnull42 fun getCalculatedHeightSpec( 43 rows: Int, 44 availableHeight: Int, 45 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 46 ): CalculatedAllAppsSpec { 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 CalculatedAllAppsSpec(availableHeight, rows, spec, calculatedWorkspaceSpec) 55 } 56 57 companion object { 58 private const val XML_ALL_APPS_SPEC = "allAppsSpec" 59 60 @JvmStatic createnull61 fun create(resourceHelper: ResourceHelper): AllAppsSpecs { 62 val parser = ResponsiveSpecsParser(resourceHelper) 63 val specs = parser.parseXML(XML_ALL_APPS_SPEC, ::AllAppsSpec) 64 val (widthSpecs, heightSpecs) = specs.partition { it.specType == SpecType.WIDTH } 65 return AllAppsSpecs(widthSpecs, heightSpecs) 66 } 67 } 68 } 69 70 data class AllAppsSpec( 71 override val maxAvailableSize: Int, 72 override val specType: SpecType, 73 override val startPadding: SizeSpec, 74 override val endPadding: SizeSpec, 75 override val gutter: SizeSpec, 76 override val cellSize: SizeSpec 77 ) : ResponsiveSpec(maxAvailableSize, specType, startPadding, endPadding, gutter, cellSize) { 78 79 init { <lambda>null80 check(isValid()) { "Invalid AllAppsSpec found." } 81 } 82 83 constructor( 84 attrs: TypedArray, 85 specs: Map<String, SizeSpec> 86 ) : this( 87 maxAvailableSize = 88 attrs.getDimensionPixelSize(R.styleable.ResponsiveSpec_maxAvailableSize, 0), 89 specType = 90 SpecType.values()[ 91 attrs.getInt(R.styleable.ResponsiveSpec_specType, SpecType.HEIGHT.ordinal)], 92 startPadding = specs.getOrError(SizeSpec.XmlTags.START_PADDING), 93 endPadding = specs.getOrError(SizeSpec.XmlTags.END_PADDING), 94 gutter = specs.getOrError(SizeSpec.XmlTags.GUTTER), 95 cellSize = specs.getOrError(SizeSpec.XmlTags.CELL_SIZE) 96 ) 97 } 98 99 class CalculatedAllAppsSpec( 100 availableSpace: Int, 101 cells: Int, 102 spec: AllAppsSpec, 103 calculatedWorkspaceSpec: CalculatedWorkspaceSpec 104 ) : CalculatedResponsiveSpec(availableSpace, cells, spec, calculatedWorkspaceSpec) 105