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

Commit b502a816 authored by Lucas Silva's avatar Lucas Silva
Browse files

Add support for using the hub with 3P launchers

We currently read the active launcher from package manager, but a 3P
launcher may not provide the widget picker activity and therefore users
may not be able to edit widgets if they are using a 3P launcher. This
change will always use the system launcher's widget picker, not the
active launcher. This ensures the feature works regardless of the user's
chosen launcher.

Fixes: 361569158
Test: atest CommunalEditModeViewModelTest
Flag: com.android.systemui.communal_hub
Change-Id: Ia97e52b8721110d75ede9f23c7734911c916ceb4
parent ace98e70
Loading
Loading
Loading
Loading
+2 −38
Original line number Diff line number Diff line
@@ -20,9 +20,7 @@ import android.appwidget.AppWidgetProviderInfo
import android.content.ActivityNotFoundException
import android.content.ComponentName
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.pm.UserInfo
import android.provider.Settings
import android.view.accessibility.AccessibilityEvent
@@ -72,7 +70,6 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.Mockito.never
@@ -141,6 +138,7 @@ class CommunalEditModeViewModelTest : SysuiTestCase() {
                context,
                accessibilityManager,
                packageManager,
                WIDGET_PICKER_PACKAGE_NAME,
            )
    }

@@ -259,57 +257,23 @@ class CommunalEditModeViewModelTest : SysuiTestCase() {
    @Test
    fun onOpenWidgetPicker_launchesWidgetPickerActivity() {
        testScope.runTest {
            whenever(packageManager.resolveActivity(any(), anyInt())).then {
                ResolveInfo().apply {
                    activityInfo = ActivityInfo().apply { packageName = WIDGET_PICKER_PACKAGE_NAME }
                }
            }

            val success =
                underTest.onOpenWidgetPicker(
                    testableResources.resources,
                    packageManager,
                    activityResultLauncher
                )
                underTest.onOpenWidgetPicker(testableResources.resources, activityResultLauncher)

            verify(activityResultLauncher).launch(any())
            assertTrue(success)
        }
    }

    @Test
    fun onOpenWidgetPicker_launcherActivityNotResolved_doesNotLaunchWidgetPickerActivity() {
        testScope.runTest {
            whenever(packageManager.resolveActivity(any(), anyInt())).thenReturn(null)

            val success =
                underTest.onOpenWidgetPicker(
                    testableResources.resources,
                    packageManager,
                    activityResultLauncher
                )

            verify(activityResultLauncher, never()).launch(any())
            assertFalse(success)
        }
    }

    @Test
    fun onOpenWidgetPicker_activityLaunchThrowsException_failure() {
        testScope.runTest {
            whenever(packageManager.resolveActivity(any(), anyInt())).then {
                ResolveInfo().apply {
                    activityInfo = ActivityInfo().apply { packageName = WIDGET_PICKER_PACKAGE_NAME }
                }
            }

            whenever(activityResultLauncher.launch(any()))
                .thenThrow(ActivityNotFoundException::class.java)

            val success =
                underTest.onOpenWidgetPicker(
                    testableResources.resources,
                    packageManager,
                    activityResultLauncher,
                )

+11 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.communal.dagger

import android.content.Context
import android.content.res.Resources
import com.android.systemui.CoreStartable
import com.android.systemui.communal.data.backup.CommunalBackupUtils
import com.android.systemui.communal.data.db.CommunalDatabaseModule
@@ -38,6 +39,8 @@ import com.android.systemui.communal.widgets.EditWidgetsActivityStarter
import com.android.systemui.communal.widgets.EditWidgetsActivityStarterImpl
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Application
import com.android.systemui.dagger.qualifiers.Main
import com.android.systemui.res.R
import com.android.systemui.scene.shared.model.SceneContainerConfig
import com.android.systemui.scene.shared.model.SceneDataSource
import com.android.systemui.scene.shared.model.SceneDataSourceDelegator
@@ -90,6 +93,7 @@ interface CommunalModule {

    companion object {
        const val LOGGABLE_PREFIXES = "loggable_prefixes"
        const val LAUNCHER_PACKAGE = "launcher_package"

        @Provides
        @Communal
@@ -126,5 +130,12 @@ interface CommunalModule {
                .getStringArray(com.android.internal.R.array.config_loggable_dream_prefixes)
                .toList()
        }

        /** The package name of the launcher */
        @Provides
        @Named(LAUNCHER_PACKAGE)
        fun provideLauncherPackage(@Main resources: Resources): String {
            return resources.getString(R.string.launcher_overlayable_package)
        }
    }
}
+4 −21
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityManager
import androidx.activity.result.ActivityResultLauncher
import com.android.internal.logging.UiEventLogger
import com.android.systemui.communal.dagger.CommunalModule.Companion.LAUNCHER_PACKAGE
import com.android.systemui.communal.data.model.CommunalWidgetCategories
import com.android.systemui.communal.domain.interactor.CommunalInteractor
import com.android.systemui.communal.domain.interactor.CommunalSceneInteractor
@@ -81,6 +82,7 @@ constructor(
    @Application private val context: Context,
    private val accessibilityManager: AccessibilityManager,
    private val packageManager: PackageManager,
    @Named(LAUNCHER_PACKAGE) private val launcherPackage: String,
) : BaseCommunalViewModel(communalSceneInteractor, communalInteractor, mediaHost) {

    private val logger = Logger(logBuffer, "CommunalEditModeViewModel")
@@ -185,7 +187,6 @@ constructor(
    /** Launch the widget picker activity using the given {@link ActivityResultLauncher}. */
    suspend fun onOpenWidgetPicker(
        resources: Resources,
        packageManager: PackageManager,
        activityLauncher: ActivityResultLauncher<Intent>
    ): Boolean =
        withContext(backgroundDispatcher) {
@@ -196,7 +197,7 @@ constructor(
                ) {
                    it.providerInfo
                }
            getWidgetPickerActivityIntent(resources, packageManager, excludeList)?.let {
            getWidgetPickerActivityIntent(resources, excludeList)?.let {
                try {
                    activityLauncher.launch(it)
                    return@withContext true
@@ -209,18 +210,10 @@ constructor(

    private fun getWidgetPickerActivityIntent(
        resources: Resources,
        packageManager: PackageManager,
        excludeList: ArrayList<AppWidgetProviderInfo>
    ): Intent? {
        val packageName =
            getLauncherPackageName(packageManager)
                ?: run {
                    Log.e(TAG, "Couldn't resolve launcher package name")
                    return@getWidgetPickerActivityIntent null
                }

        return Intent(Intent.ACTION_PICK).apply {
            setPackage(packageName)
            setPackage(launcherPackage)
            putExtra(
                EXTRA_DESIRED_WIDGET_WIDTH,
                resources.getDimensionPixelSize(R.dimen.communal_widget_picker_desired_width)
@@ -247,16 +240,6 @@ constructor(
        }
    }

    private fun getLauncherPackageName(packageManager: PackageManager): String? {
        return packageManager
            .resolveActivity(
                Intent(Intent.ACTION_MAIN).also { it.addCategory(Intent.CATEGORY_HOME) },
                PackageManager.MATCH_DEFAULT_ONLY
            )
            ?.activityInfo
            ?.packageName
    }

    /** Sets whether edit mode is currently open */
    fun setEditModeOpen(isOpen: Boolean) = communalInteractor.setEditModeOpen(isOpen)

+1 −5
Original line number Diff line number Diff line
@@ -270,11 +270,7 @@ constructor(

    private fun onOpenWidgetPicker() {
        lifecycleScope.launch {
            communalViewModel.onOpenWidgetPicker(
                resources,
                packageManager,
                addWidgetActivityLauncher
            )
            communalViewModel.onOpenWidgetPicker(resources, addWidgetActivityLauncher)
        }
    }