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

Commit 9df183f0 authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

Merge "Make resource functions somewhat more resuable" into main

parents cd5ea677 68053c4d
Loading
Loading
Loading
Loading
+15 −44
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@
 */
package com.android.systemui.plugins.clocks

import android.content.Context
import android.util.DisplayMetrics
import android.view.View
import androidx.constraintlayout.widget.ConstraintSet
@@ -27,6 +26,8 @@ import com.android.internal.policy.SystemBarUtils
import com.android.systemui.plugins.annotations.GeneratedImport
import com.android.systemui.plugins.annotations.ProtectedInterface
import com.android.systemui.plugins.annotations.ProtectedReturn
import com.android.systemui.plugins.clocks.ContextExt.getDimen
import com.android.systemui.plugins.clocks.ContextExt.getId

/** Specifies layout information for the clock face */
@ProtectedInterface
@@ -94,18 +95,18 @@ class DefaultClockFaceLayout(val view: View) : ClockFaceLayout {
            constraints: ConstraintSet,
        ): ConstraintSet {
            constraints.apply {
                val context = clockPreviewConfig.previewContext
                val lockscreenClockViewLargeId = getId(context, "lockscreen_clock_view_large")
                val context = clockPreviewConfig.context
                val lockscreenClockViewLargeId = context.getId("lockscreen_clock_view_large")
                constrainWidth(lockscreenClockViewLargeId, WRAP_CONTENT)
                constrainHeight(lockscreenClockViewLargeId, WRAP_CONTENT)
                constrainMaxHeight(lockscreenClockViewLargeId, 0)

                val largeClockTopMargin =
                    SystemBarUtils.getStatusBarHeight(context) +
                        getDimen(context, "small_clock_padding_top") +
                        getDimen(context, "keyguard_smartspace_top_offset") +
                        getDimen(context, "date_weather_view_height") +
                        getDimen(context, "enhanced_smartspace_height")
                        context.getDimen("small_clock_padding_top") +
                        context.getDimen("keyguard_smartspace_top_offset") +
                        context.getDimen("date_weather_view_height") +
                        context.getDimen("enhanced_smartspace_height")
                connect(lockscreenClockViewLargeId, TOP, PARENT_ID, TOP, largeClockTopMargin)
                connect(lockscreenClockViewLargeId, START, PARENT_ID, START)
                connect(lockscreenClockViewLargeId, END, PARENT_ID, END)
@@ -119,7 +120,7 @@ class DefaultClockFaceLayout(val view: View) : ClockFaceLayout {
                    connect(lockscreenClockViewLargeId, BOTTOM, lockId, TOP)
                }
                    ?: run {
                        val bottomPaddingPx = getDimen(context, "lock_icon_margin_bottom")
                        val bottomPaddingPx = context.getDimen("lock_icon_margin_bottom")
                        val defaultDensity =
                            DisplayMetrics.DENSITY_DEVICE_STABLE.toFloat() /
                                DisplayMetrics.DENSITY_DEFAULT.toFloat()
@@ -134,52 +135,22 @@ class DefaultClockFaceLayout(val view: View) : ClockFaceLayout {
                        )
                    }

                val smallClockViewId = getId(context, "lockscreen_clock_view")
                val smallClockViewId = context.getId("lockscreen_clock_view")
                constrainWidth(smallClockViewId, WRAP_CONTENT)
                constrainHeight(smallClockViewId, getDimen(context, "small_clock_height"))
                constrainHeight(smallClockViewId, context.getDimen("small_clock_height"))
                connect(
                    smallClockViewId,
                    START,
                    PARENT_ID,
                    START,
                    getDimen(context, "clock_padding_start") +
                        getDimen(context, "status_view_margin_horizontal"),
                )
                val smallClockTopMargin =
                    getSmallClockTopPadding(
                        clockPreviewConfig = clockPreviewConfig,
                        SystemBarUtils.getStatusBarHeight(context),
                    context.getDimen("clock_padding_start") +
                        context.getDimen("status_view_margin_horizontal"),
                )

                val smallClockTopMargin = clockPreviewConfig.getSmallClockTopPadding()
                connect(smallClockViewId, TOP, PARENT_ID, TOP, smallClockTopMargin)
            }
            return constraints
        }

        fun getId(context: Context, name: String): Int {
            val packageName = context.packageName
            val res = context.packageManager.getResourcesForApplication(packageName)
            val id = res.getIdentifier(name, "id", packageName)
            return id
        }

        fun getDimen(context: Context, name: String): Int {
            val packageName = context.packageName
            val res = context.resources
            val id = res.getIdentifier(name, "dimen", packageName)
            return if (id == 0) 0 else res.getDimensionPixelSize(id)
        }

        fun getSmallClockTopPadding(
            clockPreviewConfig: ClockPreviewConfig,
            statusBarHeight: Int,
        ): Int {
            return if (clockPreviewConfig.isShadeLayoutWide) {
                getDimen(clockPreviewConfig.previewContext, "keyguard_split_shade_top_margin") -
                    if (clockPreviewConfig.isSceneContainerFlagEnabled) statusBarHeight else 0
            } else {
                getDimen(clockPreviewConfig.previewContext, "keyguard_clock_top_margin") +
                    if (!clockPreviewConfig.isSceneContainerFlagEnabled) statusBarHeight else 0
            }
        }
    }
}
+16 −2
Original line number Diff line number Diff line
@@ -17,10 +17,24 @@
package com.android.systemui.plugins.clocks

import android.content.Context
import com.android.internal.policy.SystemBarUtils
import com.android.systemui.plugins.clocks.ContextExt.getDimen

data class ClockPreviewConfig(
    val previewContext: Context,
    val context: Context,
    val isShadeLayoutWide: Boolean,
    val isSceneContainerFlagEnabled: Boolean = false,
    val lockId: Int? = null,
)
) {
    fun getSmallClockTopPadding(
        statusBarHeight: Int = SystemBarUtils.getStatusBarHeight(context)
    ): Int {
        return if (isShadeLayoutWide) {
            context.getDimen("keyguard_split_shade_top_margin") -
                if (isSceneContainerFlagEnabled) statusBarHeight else 0
        } else {
            context.getDimen("keyguard_clock_top_margin") +
                if (!isSceneContainerFlagEnabled) statusBarHeight else 0
        }
    }
}
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.plugins.clocks

import android.content.Context

object ContextExt {
    fun Context.getId(name: String): Int {
        val res = packageManager.getResourcesForApplication(packageName)
        return res.getIdentifier(name, "id", packageName)
    }

    fun Context.getDimen(name: String): Int {
        val id = resources.getIdentifier(name, "dimen", packageName)
        return if (id == 0) 0 else resources.getDimensionPixelSize(id)
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ object KeyguardPreviewClockViewBinder {
                            lastClock = currentClock
                            updateClockAppearance(
                                currentClock,
                                clockPreviewConfig.previewContext.resources,
                                clockPreviewConfig.context.resources,
                            )

                            if (viewModel.shouldHighlightSelectedAffordance) {
+5 −7
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import com.android.systemui.keyguard.domain.interactor.KeyguardClockInteractor
import com.android.systemui.keyguard.shared.model.ClockSize
import com.android.systemui.keyguard.shared.model.ClockSizeSetting
import com.android.systemui.plugins.clocks.ClockPreviewConfig
import com.android.systemui.plugins.clocks.DefaultClockFaceLayout.Companion.getSmallClockTopPadding
import com.android.systemui.scene.shared.flag.SceneContainerFlag
import com.android.systemui.shade.ShadeDisplayAware
import com.android.systemui.shade.domain.interactor.ShadeInteractor
@@ -161,15 +160,14 @@ constructor(
            )

    /** Calculates the top margin for the small clock. */
    fun getSmallClockTopMargin(): Int =
        getSmallClockTopPadding(
            ClockPreviewConfig(
    fun getSmallClockTopMargin(): Int {
        return ClockPreviewConfig(
                context,
                shadeInteractor.isShadeLayoutWide.value,
                SceneContainerFlag.isEnabled,
            ),
            systemBarUtils.getStatusBarHeaderHeightKeyguard(),
            )
            .getSmallClockTopPadding(systemBarUtils.getStatusBarHeaderHeightKeyguard())
    }

    val smallClockTopMargin =
        combine(
Loading