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

Commit d1e2bc06 authored by William Leshner's avatar William Leshner Committed by Android (Google) Code Review
Browse files

Merge "Implement communal widget filtering based on category filter setting." into main

parents 945ffd78 9486146e
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.admin.DevicePolicyManager
import android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NONE
import android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_WIDGETS_ALL
import android.app.admin.devicePolicyManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Intent
import android.content.pm.UserInfo
import android.platform.test.annotations.DisableFlags
@@ -136,6 +137,29 @@ class CommunalSettingsRepositoryImplTest : SysuiTestCase() {
                )
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubShowsWidgetCategoriesSetByUser() =
        testScope.runTest {
            kosmos.fakeSettings.putIntForUser(
                CommunalSettingsRepositoryImpl.GLANCEABLE_HUB_CONTENT_SETTING,
                AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN,
                PRIMARY_USER.id
            )
            val setting by collectLastValue(underTest.getWidgetCategories(PRIMARY_USER))
            assertThat(setting?.categories)
                .isEqualTo(AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN)
        }

    @EnableFlags(FLAG_COMMUNAL_HUB)
    @Test
    fun hubShowsKeyguardWidgetsByDefault() =
        testScope.runTest {
            val setting by collectLastValue(underTest.getWidgetCategories(PRIMARY_USER))
            assertThat(setting?.categories)
                .isEqualTo(AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD)
        }

    private fun setKeyguardFeaturesDisabled(user: UserInfo, disabledFlags: Int) {
        whenever(kosmos.devicePolicyManager.getKeyguardDisabledFeatures(nullable(), eq(user.id)))
            .thenReturn(disabledFlags)
+2 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import com.android.systemui.communal.data.repository.fakeCommunalMediaRepository
import com.android.systemui.communal.data.repository.fakeCommunalTutorialRepository
import com.android.systemui.communal.data.repository.fakeCommunalWidgetRepository
import com.android.systemui.communal.domain.interactor.communalInteractor
import com.android.systemui.communal.domain.interactor.communalSettingsInteractor
import com.android.systemui.communal.domain.model.CommunalContentModel
import com.android.systemui.communal.shared.log.CommunalUiEvent
import com.android.systemui.communal.shared.model.CommunalWidgetContentModel
@@ -81,6 +82,7 @@ class CommunalEditModeViewModelTest : SysuiTestCase() {
        underTest =
            CommunalEditModeViewModel(
                kosmos.communalInteractor,
                kosmos.communalSettingsInteractor,
                mediaHost,
                uiEventLogger,
                logcatLogBuffer("CommunalEditModeViewModelTest"),
+31 −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.communal.data.model

import android.appwidget.AppWidgetProviderInfo

/**
 * The widget categories to display on communal hub (where categories is a bitfield with values that
 * match those in {@link AppWidgetProviderInfo}).
 */
@JvmInline
value class CommunalWidgetCategories(
    // The default is keyguard widgets.
    val categories: Int = AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD
) {
    fun contains(category: Int) = (categories and category) == category
}
+26 −0
Original line number Diff line number Diff line
@@ -18,11 +18,13 @@ package com.android.systemui.communal.data.repository

import android.app.admin.DevicePolicyManager
import android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_WIDGETS_ALL
import android.appwidget.AppWidgetProviderInfo
import android.content.IntentFilter
import android.content.pm.UserInfo
import com.android.systemui.Flags.communalHub
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.communal.data.model.CommunalEnabledState
import com.android.systemui.communal.data.model.CommunalWidgetCategories
import com.android.systemui.communal.data.model.DisabledReason
import com.android.systemui.communal.data.model.DisabledReason.DISABLED_REASON_DEVICE_POLICY
import com.android.systemui.communal.data.model.DisabledReason.DISABLED_REASON_FLAG
@@ -48,6 +50,12 @@ import kotlinx.coroutines.flow.onStart
interface CommunalSettingsRepository {
    /** A [CommunalEnabledState] for the specified user. */
    fun getEnabledState(user: UserInfo): Flow<CommunalEnabledState>

    /**
     * A flow that reports the widget categories to show on the hub as selected by the user in
     * Settings.
     */
    fun getWidgetCategories(user: UserInfo): Flow<CommunalWidgetCategories>
}

@SysUISingleton
@@ -89,6 +97,23 @@ constructor(
            .flowOn(bgDispatcher)
    }

    override fun getWidgetCategories(user: UserInfo): Flow<CommunalWidgetCategories> =
        secureSettings
            .observerFlow(userId = user.id, names = arrayOf(GLANCEABLE_HUB_CONTENT_SETTING))
            // Force an update
            .onStart { emit(Unit) }
            .map {
                CommunalWidgetCategories(
                    // The default is to show only keyguard widgets.
                    secureSettings.getIntForUser(
                        GLANCEABLE_HUB_CONTENT_SETTING,
                        AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD,
                        user.id
                    )
                )
            }
            .flowOn(bgDispatcher)

    private fun getEnabledByUser(user: UserInfo): Flow<Boolean> =
        secureSettings
            .observerFlow(userId = user.id, names = arrayOf(GLANCEABLE_HUB_ENABLED))
@@ -114,6 +139,7 @@ constructor(

    companion object {
        const val GLANCEABLE_HUB_ENABLED = "glanceable_hub_enabled"
        const val GLANCEABLE_HUB_CONTENT_SETTING = "glanceable_hub_content_setting"
        private const val ENABLED_SETTING_DEFAULT = 1
    }
}
+13 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.communal.domain.interactor

import com.android.systemui.communal.data.model.CommunalEnabledState
import com.android.systemui.communal.data.model.CommunalWidgetCategories
import com.android.systemui.communal.data.repository.CommunalSettingsRepository
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
@@ -55,4 +56,16 @@ constructor(
            .map { model -> model.enabled }
            // Start this eagerly since the value is accessed synchronously in many places.
            .stateIn(scope = bgScope, started = SharingStarted.Eagerly, initialValue = false)

    /** What widget categories to show on the hub. */
    val communalWidgetCategories: StateFlow<Int> =
        userInteractor.selectedUserInfo
            .flatMapLatest { user -> repository.getWidgetCategories(user) }
            .map { categories -> categories.categories }
            .stateIn(
                scope = bgScope,
                // Start this eagerly since the value can be accessed synchronously.
                started = SharingStarted.Eagerly,
                initialValue = CommunalWidgetCategories().categories
            )
}
Loading