From ce396d8abf74a082f62ba1d111138d5dbd84033d Mon Sep 17 00:00:00 2001 From: Jordan Silva Date: Mon, 11 Dec 2023 21:50:00 -0300 Subject: [PATCH] Fix crash when restoring data from phone to tablet with responsive grid This issue happens when restoring the data from a phone to a tablet or vice-versa. When Launcher is restoring the data, it allows the device to load a disabled or invalid grid temporarly for the migration. This leads to a crash when the responsive grid is enabled. The hotseat spec has only WIDTH or HEIGHT specs in a certain posture. When the user restores the data from a phone to a tablet, the Launcher loads a handheld spec that doesn't have HEIGHT specs for the hotseat in landscape (spec_handheld_hotseat_[grid].xml). However, the tablet, in landscape, tries to retrieve the hotseat HEIGHT spec. It also prevent crash when responsive grid is used with multi window mode. isVerticalBarLayout becomes false. Then, the responsive grid tries to load a hotseat spec that doesn't exist (using DimensionType.HEIGHT). Phones have hotseat specs only for DimensionType.WIDTH. Bug: 315548992 Fix: 315069300 Fix: 315377544 Flag: ACONFIG com.android.launcher3.enable_responsive_workspace TEAMFOOD Test: v2/android-crystalball-eng/health/microbench/systemui/main/systemui-notification-3-jank-suite Test: atest CtsWindowManagerDeviceDisplay:android.server.wm.display.MultiDisplaySystemDecorationTests#testSendPrimaryHomeIntentActivityOnDisplayWithDecorations -- --abi x86_64 Change-Id: Ibd6537e0528868da9a1b7672c43b5455fa6a8184 --- .../responsive/HotseatSpecsProvider.kt | 19 ++++++++++++++-- .../responsive/ResponsiveSpecGroup.kt | 22 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/com/android/launcher3/responsive/HotseatSpecsProvider.kt b/src/com/android/launcher3/responsive/HotseatSpecsProvider.kt index ebbff51996..7502a43ff9 100644 --- a/src/com/android/launcher3/responsive/HotseatSpecsProvider.kt +++ b/src/com/android/launcher3/responsive/HotseatSpecsProvider.kt @@ -40,13 +40,28 @@ class HotseatSpecsProvider(groupOfSpecs: List>) return specsGroup } + private fun getSpecIgnoringDimensionType( + availableSize: Int, + specsGroup: ResponsiveSpecGroup + ): HotseatSpec? { + val specWidth = specsGroup.widthSpecs.firstOrNull { availableSize <= it.maxAvailableSize } + val specHeight = specsGroup.heightSpecs.firstOrNull { availableSize <= it.maxAvailableSize } + return specWidth ?: specHeight + } + fun getCalculatedSpec( aspectRatio: Float, dimensionType: DimensionType, - availableSpace: Int + availableSpace: Int, ): CalculatedHotseatSpec { val specsGroup = getSpecsByAspectRatio(aspectRatio) - val spec = specsGroup.getSpec(dimensionType, availableSpace) + + // TODO(b/315548992): Ignore the dimension type to prevent crash before launcher + // data migration is finished. The restore process allows the initialization of + // an invalid or disabled grid until the data is restored and migrated. + val spec = getSpecIgnoringDimensionType(availableSpace, specsGroup) + check(spec != null) { "No available spec found within $availableSpace. $specsGroup" } + // val spec = specsGroup.getSpec(dimensionType, availableSpace) return CalculatedHotseatSpec(availableSpace, spec) } diff --git a/src/com/android/launcher3/responsive/ResponsiveSpecGroup.kt b/src/com/android/launcher3/responsive/ResponsiveSpecGroup.kt index b233d7c3d5..a758be8e1c 100644 --- a/src/com/android/launcher3/responsive/ResponsiveSpecGroup.kt +++ b/src/com/android/launcher3/responsive/ResponsiveSpecGroup.kt @@ -18,6 +18,7 @@ package com.android.launcher3.responsive import android.content.res.TypedArray import com.android.launcher3.R +import com.android.launcher3.responsive.ResponsiveSpec.Companion.ResponsiveSpecType import com.android.launcher3.responsive.ResponsiveSpec.DimensionType /** @@ -54,10 +55,29 @@ class ResponsiveSpecGroup( } else { heightSpecs.firstOrNull { availableSize <= it.maxAvailableSize } } - check(spec != null) { "No available $type spec found within $availableSize." } + check(spec != null) { "No available $type spec found within $availableSize. $this" } return spec } + override fun toString(): String { + fun printSpec(spec: IResponsiveSpec) = + when (spec.specType) { + ResponsiveSpecType.AllApps, + ResponsiveSpecType.Folder, + ResponsiveSpecType.Workspace -> (spec as ResponsiveSpec).toString() + ResponsiveSpecType.Hotseat -> (spec as HotseatSpec).toString() + ResponsiveSpecType.Cell -> (spec as CellSpec).toString() + } + + val widthSpecsString = widthSpecs.joinToString(", ") { printSpec(it) } + val heightSpecsString = heightSpecs.joinToString(", ") { printSpec(it) } + return "ResponsiveSpecGroup(" + + "aspectRatio=${aspectRatio}, " + + "widthSpecs=[${widthSpecsString}], " + + "heightSpecs=[${heightSpecsString}]" + + ")" + } + companion object { const val XML_GROUP_NAME = "specs"