From c1903b0b1e7581df01667a432a251626ce8f4254 Mon Sep 17 00:00:00 2001 From: Aaron Liu Date: Fri, 4 Mar 2022 22:48:18 +0000 Subject: [PATCH] Home Controls: Add max columns for edit activity. Moved the findMaxColumns function from ControlsUiControllerImpl to ControlAdapter to be used as a shared functin. It is used across 3 files. I removed orientation from configChanges in the manifest so that the recyclerview can recalculate max columns again after rotation. I moved the spanCount calculation one level higher so that we can set the spancount for ZONE type view holders to be the max number of columns. This makes it so that the view holder will take up the whole row. Fixes: 222729341 Test: manual test on device Change-Id: Id0819e417c8d5995474abb0a8060ee9e694fe4a7 --- packages/SystemUI/AndroidManifest.xml | 4 +-- .../controls/management/ControlAdapter.kt | 33 ++++++++++++++++--- .../management/ControlsEditingActivity.kt | 10 ++++-- .../controls/management/StructureAdapter.kt | 10 ++++-- .../controls/ui/ControlsUiControllerImpl.kt | 31 ++--------------- 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml index 7f8b2f51754c1..e24b2d6c6aa90 100644 --- a/packages/SystemUI/AndroidManifest.xml +++ b/packages/SystemUI/AndroidManifest.xml @@ -794,7 +794,7 @@ android:noHistory="true" android:showForAllUsers="true" android:finishOnTaskLaunch="true" - android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboard|keyboardHidden" + android:configChanges="screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden" android:visibleToInstantApps="true"> @@ -805,7 +805,7 @@ android:showForAllUsers="true" android:finishOnTaskLaunch="true" android:launchMode="singleInstance" - android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboard|keyboardHidden" + android:configChanges="screenSize|smallestScreenSize|screenLayout|keyboard|keyboardHidden" android:visibleToInstantApps="true"> diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt b/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt index 40662536e57ee..f9115b20ca061 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/management/ControlAdapter.kt @@ -17,10 +17,13 @@ package com.android.systemui.controls.management import android.content.ComponentName +import android.content.res.Configuration +import android.content.res.Resources import android.graphics.Rect import android.os.Bundle import android.service.controls.Control import android.service.controls.DeviceTypes +import android.util.TypedValue import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -32,7 +35,6 @@ import android.widget.TextView import androidx.core.view.AccessibilityDelegateCompat import androidx.core.view.ViewCompat import androidx.core.view.accessibility.AccessibilityNodeInfoCompat -import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import com.android.systemui.R import com.android.systemui.controls.ControlInterface @@ -56,11 +58,32 @@ class ControlAdapter( const val TYPE_ZONE = 0 const val TYPE_CONTROL = 1 const val TYPE_DIVIDER = 2 - } - val spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { - override fun getSpanSize(position: Int): Int { - return if (getItemViewType(position) != TYPE_CONTROL) 2 else 1 + /** + * For low-dp width screens that also employ an increased font scale, adjust the + * number of columns. This helps prevent text truncation on these devices. + * + */ + @JvmStatic + fun findMaxColumns(res: Resources): Int { + var maxColumns = res.getInteger(R.integer.controls_max_columns) + val maxColumnsAdjustWidth = + res.getInteger(R.integer.controls_max_columns_adjust_below_width_dp) + + val outValue = TypedValue() + res.getValue(R.dimen.controls_max_columns_adjust_above_font_scale, outValue, true) + val maxColumnsAdjustFontScale = outValue.getFloat() + + val config = res.configuration + val isPortrait = config.orientation == Configuration.ORIENTATION_PORTRAIT + if (isPortrait && + config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED && + config.screenWidthDp <= maxColumnsAdjustWidth && + config.fontScale >= maxColumnsAdjustFontScale) { + maxColumns-- + } + + return maxColumns } } diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt index 6f94943472b1f..f611c3ef966da 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/management/ControlsEditingActivity.kt @@ -195,10 +195,11 @@ class ControlsEditingActivity @Inject constructor( val margin = resources .getDimensionPixelSize(R.dimen.controls_card_margin) val itemDecorator = MarginItemDecorator(margin, margin) + val spanCount = ControlAdapter.findMaxColumns(resources) recyclerView.apply { this.adapter = adapter - layoutManager = object : GridLayoutManager(recyclerView.context, 2) { + layoutManager = object : GridLayoutManager(recyclerView.context, spanCount) { // This will remove from the announcement the row corresponding to the divider, // as it's not something that should be announced. @@ -210,7 +211,12 @@ class ControlsEditingActivity @Inject constructor( return if (initial > 0) initial - 1 else initial } }.apply { - spanSizeLookup = adapter.spanSizeLookup + spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { + override fun getSpanSize(position: Int): Int { + return if (adapter?.getItemViewType(position) + != ControlAdapter.TYPE_CONTROL) spanCount else 1 + } + } } addItemDecoration(itemDecorator) } diff --git a/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt b/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt index cb67454195eca..747bcbe1c2297 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/management/StructureAdapter.kt @@ -60,11 +60,17 @@ class StructureAdapter( val margin = itemView.context.resources .getDimensionPixelSize(R.dimen.controls_card_margin) val itemDecorator = MarginItemDecorator(margin, margin) + val spanCount = ControlAdapter.findMaxColumns(itemView.resources) recyclerView.apply { this.adapter = controlAdapter - layoutManager = GridLayoutManager(recyclerView.context, 2).apply { - spanSizeLookup = controlAdapter.spanSizeLookup + layoutManager = GridLayoutManager(recyclerView.context, spanCount).apply { + spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() { + override fun getSpanSize(position: Int): Int { + return if (adapter?.getItemViewType(position) + != ControlAdapter.TYPE_CONTROL) spanCount else 1 + } + } } addItemDecoration(itemDecorator) } diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt index 59c291cf9e5cc..1268250137b80 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt @@ -25,12 +25,10 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import android.content.SharedPreferences -import android.content.res.Configuration import android.graphics.drawable.Drawable import android.graphics.drawable.LayerDrawable import android.service.controls.Control import android.util.Log -import android.util.TypedValue import android.view.ContextThemeWrapper import android.view.LayoutInflater import android.view.View @@ -51,6 +49,7 @@ import com.android.systemui.controls.CustomIconCache import com.android.systemui.controls.controller.ControlInfo import com.android.systemui.controls.controller.ControlsController import com.android.systemui.controls.controller.StructureInfo +import com.android.systemui.controls.management.ControlAdapter import com.android.systemui.controls.management.ControlsEditingActivity import com.android.systemui.controls.management.ControlsFavoritingActivity import com.android.systemui.controls.management.ControlsListingController @@ -386,7 +385,7 @@ class ControlsUiControllerImpl @Inject constructor ( visibility = View.VISIBLE } - val maxColumns = findMaxColumns() + val maxColumns = ControlAdapter.findMaxColumns(activityContext.resources) val listView = parent.requireViewById(R.id.global_actions_controls_list) as ViewGroup var lastRow: ViewGroup = createRow(inflater, listView) @@ -432,32 +431,6 @@ class ControlsUiControllerImpl @Inject constructor ( } } - /** - * For low-dp width screens that also employ an increased font scale, adjust the - * number of columns. This helps prevent text truncation on these devices. - */ - private fun findMaxColumns(): Int { - val res = activityContext.resources - var maxColumns = res.getInteger(R.integer.controls_max_columns) - val maxColumnsAdjustWidth = - res.getInteger(R.integer.controls_max_columns_adjust_below_width_dp) - - val outValue = TypedValue() - res.getValue(R.dimen.controls_max_columns_adjust_above_font_scale, outValue, true) - val maxColumnsAdjustFontScale = outValue.getFloat() - - val config = res.configuration - val isPortrait = config.orientation == Configuration.ORIENTATION_PORTRAIT - if (isPortrait && - config.screenWidthDp != Configuration.SCREEN_WIDTH_DP_UNDEFINED && - config.screenWidthDp <= maxColumnsAdjustWidth && - config.fontScale >= maxColumnsAdjustFontScale) { - maxColumns-- - } - - return maxColumns - } - override fun getPreferredStructure(structures: List): StructureInfo { if (structures.isEmpty()) return EMPTY_STRUCTURE