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

Commit a57cf55f authored by Willie Koomson's avatar Willie Koomson
Browse files

Reland "Update Glanceable Hub's AppWidgetHostListener impl for widget events"

This change updates GlanceableHub's custom AppWidgetHostListener
implementation to add support for the new collectWidgetEvents method.
This enables event reporting for the hub's widgets.

This reland also adds calls to
AppWidgetHostView.start/stopVisibilityTracking to record visibility
durations of individual widgets.

Bug: 429228078
Test: Manual, use widgets on mobile hub and confirm that they are
 reported
Test: atest SystemUITests --test-filter="com.android.systemui.communal.widgets.*"
Flag: android.appwidget.flags.engagement_metrics
Change-Id: I8204c04582cb5e0e59a5375cd75f8e928cbe157e
parent 27743398
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.focusable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.awaitFirstDown
import androidx.compose.foundation.gestures.detectHorizontalDragGestures
import androidx.compose.foundation.gestures.snapping.SnapPosition
@@ -72,6 +73,7 @@ import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyGridLayoutInfo
import androidx.compose.foundation.lazy.grid.LazyGridScope
import androidx.compose.foundation.lazy.grid.LazyGridState
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
@@ -1020,6 +1022,9 @@ private fun BoxScope.CommunalHubLazyGrid(
                } else {
                    null
                }
            val isVisible by remember {
                derivedStateOf { gridState.layoutInfo.isIndexVisible(index) }
            }
            if (viewModel.isEditMode && dragDropState != null) {
                val isItemDragging = dragDropState.draggingItemKey == item.key
                val outlineAlpha by
@@ -1087,6 +1092,7 @@ private fun BoxScope.CommunalHubLazyGrid(
                            interactionHandler = interactionHandler,
                            widgetSection = widgetSection,
                            resizeableItemFrameViewModel = resizeableItemFrameViewModel,
                            isVisible = isVisible,
                        )
                    }
                }
@@ -1111,6 +1117,7 @@ private fun BoxScope.CommunalHubLazyGrid(
                    widgetSection = widgetSection,
                    resizeableItemFrameViewModel = resizeableItemFrameViewModel,
                    contentScope = contentScope,
                    isVisible = isVisible,
                )
            }
        }
@@ -1370,6 +1377,7 @@ private fun CommunalContent(
    widgetSection: CommunalAppWidgetSection,
    resizeableItemFrameViewModel: ResizeableItemFrameViewModel,
    contentScope: ContentScope? = null,
    isVisible: Boolean,
) {
    when (model) {
        is CommunalContentModel.WidgetContent.Widget ->
@@ -1384,6 +1392,7 @@ private fun CommunalContent(
                contentListState,
                widgetSection,
                resizeableItemFrameViewModel,
                isVisible,
            )
        is CommunalContentModel.WidgetPlaceholder -> HighlightedItem(modifier)
        is CommunalContentModel.WidgetContent.DisabledWidget ->
@@ -1519,6 +1528,7 @@ private fun WidgetContent(
    contentListState: ContentListState,
    widgetSection: CommunalAppWidgetSection,
    resizeableItemFrameViewModel: ResizeableItemFrameViewModel,
    isVisible: Boolean,
) {
    val coroutineScope = rememberCoroutineScope()
    val context = LocalContext.current
@@ -1656,6 +1666,7 @@ private fun WidgetContent(
                model = model,
                size = size,
                modifier = Modifier.fillMaxSize().allowGestures(allowed = !viewModel.isEditMode),
                isVisible = isVisible,
            )
        }
        if (
@@ -2279,6 +2290,18 @@ private fun calculatePercentVisible(state: LazyGridState, index: Int): Float {
    }
}

private fun LazyGridLayoutInfo.isIndexVisible(index: Int) =
    visibleItemsInfo
        .firstOrNull { it.index == index }
        ?.let { item ->
            val (start, end) =
                when (orientation) {
                    Orientation.Vertical -> item.offset.y to item.offset.y + item.size.height
                    Orientation.Horizontal -> item.offset.x to item.offset.x + item.size.width
                }
            start >= viewportStartOffset && end <= viewportEndOffset
        } ?: false

private object Colors {
    val DisabledColorFilter by lazy { disabledColorMatrix() }

+3 −0
Original line number Diff line number Diff line
@@ -166,6 +166,9 @@ class GlanceableHubWidgetManagerServiceTest : SysuiTestCase() {

            appWidgetHostListener.onViewDataChanged(1)
            verify(listener).onViewDataChanged(1)

            appWidgetHostListener.collectWidgetEvent()
            verify(listener).collectWidgetEvent()
        }

    @Test
+2 −0
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ constructor(
        openWidgetEditor: () -> Unit,
        model: CommunalContentModel.WidgetContent.Widget,
        size: SizeF,
        isVisible: Boolean,
        modifier: Modifier = Modifier,
    ) {
        val viewModel = rememberViewModel("$TAG#viewModel") { viewModelFactory.create() }
@@ -112,6 +113,7 @@ constructor(
                    view.setTag(LISTENER_TAG, model.appWidgetId)
                }
                viewModel.updateSize(size, view)
                if (isVisible) view.startVisibilityTracking() else view.stopVisibilityTracking()
            },
            modifier = modifier,
            // For reusing composition in lazy lists.
+9 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.communal.widgets

import android.appwidget.AppWidgetEvent
import android.appwidget.AppWidgetHost.AppWidgetHostListener
import android.appwidget.AppWidgetProviderInfo
import android.widget.RemoteViews
@@ -55,4 +56,12 @@ constructor(
    override fun onViewDataChanged(viewId: Int) {
        mainScope.launchTraced("$tag#onViewDataChanged") { listener.onViewDataChanged(viewId) }
    }

    override fun collectWidgetEvent(): AppWidgetEvent? {
        if (!android.appwidget.flags.Flags.engagementMetrics()) {
            return null
        }
        // collectWidgetEvent does not need to run on the main thread.
        return listener.collectWidgetEvent()
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -98,8 +98,8 @@ class CommunalAppWidgetHost(
    }

    override fun stopListening() {
        super.stopListening()
        backgroundScope.launch {
            super.stopListening()
            synchronized(observers) {
                observers.forEach { observer -> observer.onHostStopListening() }
            }
Loading