Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7191124e authored by Darrell Shi's avatar Darrell Shi
Browse files

Remove CommunalLayoutLib

Bug: 312046330
Fix: 312046330
Test: NA
Flag: NA
Change-Id: I6a1c1c13941d4c41c8a230e5be00edcf99dfdc7a
parent 630a48f9
Loading
Loading
Loading
Loading
+0 −35
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package {
    default_applicable_licenses: ["frameworks_base_packages_SystemUI_license"],
}

android_library {
    name: "CommunalLayoutLib",
    srcs: [
        "src/**/*.kt",
    ],
    static_libs: [
        "androidx.arch.core_core-runtime",
        "androidx.compose.animation_animation-graphics",
        "androidx.compose.runtime_runtime",
        "androidx.compose.material3_material3",
        "jsr330",
        "kotlinx-coroutines-android",
        "kotlinx-coroutines-core",
    ],
    manifest: "AndroidManifest.xml",
    kotlincflags: ["-Xjvm-default=all"],
}
+0 −18
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2023 The Android Open Source Project

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<manifest package="com.android.systemui.communal.layout" />
+0 −69
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.layout

import com.android.systemui.communal.layout.ui.compose.config.CommunalGridLayoutCard

/** Computes the arrangement of cards. */
class CommunalLayoutEngine {
    companion object {
        /**
         * Determines the size that each card should be rendered in, and distributes the cards into
         * columns.
         *
         * Returns a nested list where the outer list contains columns, and the inner list contains
         * cards in each column.
         *
         * Currently treats the first supported size as the size to be rendered in, ignoring other
         * supported sizes.
         *
         * Cards are ordered by priority, from highest to lowest.
         */
        fun distributeCardsIntoColumns(
            cards: List<CommunalGridLayoutCard>,
        ): List<List<CommunalGridLayoutCardInfo>> {
            val result = ArrayList<ArrayList<CommunalGridLayoutCardInfo>>()

            var capacityOfLastColumn = 0
            val sorted = cards.sortedByDescending { it.priority }
            for (card in sorted) {
                val cardSize = card.supportedSizes.first()
                if (capacityOfLastColumn >= cardSize.value) {
                    // Card fits in last column
                    capacityOfLastColumn -= cardSize.value
                } else {
                    // Create a new column
                    result.add(arrayListOf())
                    capacityOfLastColumn = CommunalGridLayoutCard.Size.FULL.value - cardSize.value
                }

                result.last().add(CommunalGridLayoutCardInfo(card, cardSize))
            }

            return result
        }
    }

    /**
     * A data class that wraps around a [CommunalGridLayoutCard] and also contains the size that the
     * card should be rendered in.
     */
    data class CommunalGridLayoutCardInfo(
        val card: CommunalGridLayoutCard,
        val size: CommunalGridLayoutCard.Size,
    )
}
+0 −72
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.layout.ui.compose

import android.util.SizeF
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.android.systemui.communal.layout.CommunalLayoutEngine
import com.android.systemui.communal.layout.ui.compose.config.CommunalGridLayoutCard
import com.android.systemui.communal.layout.ui.compose.config.CommunalGridLayoutConfig

/**
 * An arrangement of cards with a horizontal scroll, where each card is displayed in the right size
 * and follows a specific order based on its priority, ensuring a seamless layout without any gaps.
 */
@Composable
fun CommunalGridLayout(
    modifier: Modifier,
    layoutConfig: CommunalGridLayoutConfig,
    communalCards: List<CommunalGridLayoutCard>,
) {
    val columns = CommunalLayoutEngine.distributeCardsIntoColumns(communalCards)
    LazyRow(
        modifier = modifier.height(layoutConfig.gridHeight),
        horizontalArrangement = Arrangement.spacedBy(layoutConfig.gridGutter),
    ) {
        for (column in columns) {
            item {
                Column(
                    modifier = Modifier.width(layoutConfig.cardWidth),
                    verticalArrangement = Arrangement.spacedBy(layoutConfig.gridGutter),
                ) {
                    for (cardInfo in column) {
                        Row(
                            modifier = Modifier.height(layoutConfig.cardHeight(cardInfo.size)),
                        ) {
                            cardInfo.card.Content(
                                modifier = Modifier.fillMaxSize(),
                                size =
                                    SizeF(
                                        layoutConfig.cardWidth.value,
                                        layoutConfig.cardHeight(cardInfo.size).value,
                                    ),
                            )
                        }
                    }
                }
            }
        }
    }
}
+0 −68
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.layout.ui.compose.config

import android.util.SizeF
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier

/** A card that hosts content to be rendered in the communal grid layout. */
abstract class CommunalGridLayoutCard {
    /**
     * Content to be hosted by the card.
     *
     * To host non-Compose views, see
     * https://developer.android.com/jetpack/compose/migrate/interoperability-apis/views-in-compose.
     *
     * @param size The size given to the card. Content of the card should fill all this space, given
     *   that margins and paddings have been taken care of by the layout.
     */
    @Composable abstract fun Content(modifier: Modifier, size: SizeF)

    /**
     * Sizes supported by the card.
     *
     * If multiple sizes are available, they should be ranked in order of preference, from most to
     * least preferred.
     */
    abstract val supportedSizes: List<Size>

    /**
     * Priority of the content hosted by the card.
     *
     * The value of priority is relative to other cards. Cards with a higher priority are generally
     * ordered first.
     */
    open val priority: Int = 0

    /**
     * Size of the card.
     *
     * @param value A numeric value that represents the size. Must be less than or equal to
     *   [Size.FULL].
     */
    enum class Size(val value: Int) {
        /** The card takes up full height of the grid layout. */
        FULL(value = 6),

        /** The card takes up half of the vertical space of the grid layout. */
        HALF(value = 3),

        /** The card takes up a third of the vertical space of the grid layout. */
        THIRD(value = 2),
    }
}
Loading