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

Commit 878b1320 authored by Orhan Uysal's avatar Orhan Uysal Committed by Android (Google) Code Review
Browse files

Merge "Migrate WallpaperActivity to FragmentActivity" into main

parents 9beea219 8c402433
Loading
Loading
Loading
Loading
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.wm.shell.desktopmode

import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.SetFlagsRule
import android.view.WindowManager
import androidx.activity.BackEventCompat
import androidx.fragment.app.FragmentActivity
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import com.android.window.flags.Flags
import com.android.window.flags.Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
import com.android.window.flags.Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/** Tests for [DesktopWallpaperActivity].
 *
 * Build/Install/Run:
 *  atest WMShellRobolectricTests:DesktopWallpaperActivityTest (on host)
 *  atest WMShellMultivalentTestsOnDevice:DesktopWallpaperActivityTest (on device)
 */
@SmallTest
@RunWith(AndroidJUnit4::class)
class DesktopWallpaperActivityTest() {

    @get:Rule
    val setFlagsRule = SetFlagsRule()
    @Rule
    @JvmField
    val activityScenarioRule =
        ActivityScenarioRule(DesktopWallpaperActivity::class.java)

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
    )
    fun onTopResumedActivityChanged_whenTrue_setsWindowFocusable() {
        activityScenarioRule.scenario.onActivity { activity ->
            activity.onTopResumedActivityChanged(true)

            val windowFlags = activity.window.attributes.flags
            assertThat(windowFlags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
                .isEqualTo(0)
        }
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
    )
    fun onTopResumedActivityChanged_whenFalse_setsWindowNotFocusable() {
        activityScenarioRule.scenario.onActivity { activity ->
            activity.onTopResumedActivityChanged(true)
            assertThat(
                activity.window.attributes.flags and
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            )
                .isEqualTo(0)

            activity.onTopResumedActivityChanged(false)

            val windowFlags = activity.window.attributes.flags
            assertThat(windowFlags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
                .isNotEqualTo(0)
        }
    }

    @Test
    @EnableFlags(
        Flags.FLAG_ENABLE_MULTIPLE_DESKTOPS_BACKEND,
        Flags.FLAG_ENABLE_EMPTY_DESK_ON_MINIMIZE
    )
    fun onBackPressed_movesTaskToBack() {
        var wallpaperActivity: FragmentActivity? = null

        activityScenarioRule.scenario.onActivity { activity ->
            wallpaperActivity = activity
            activity.onBackPressedDispatcher.dispatchOnBackStarted(backEvent())
        }
        InstrumentationRegistry.getInstrumentation().waitForIdleSync()

        assertThat(wallpaperActivity?.isFinishing).isFalse()
    }

    private fun backEvent(progress: Float = 0f): BackEventCompat {
        return BackEventCompat(
            touchX = 0f,
            touchY = 0f,
            progress = progress,
            swipeEdge = BackEventCompat.EDGE_LEFT,
        )
    }
}
 No newline at end of file
+10 −2
Original line number Diff line number Diff line
@@ -16,13 +16,14 @@

package com.android.wm.shell.desktopmode

import android.app.Activity
import android.app.TaskInfo
import android.content.ComponentName
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.window.DesktopExperienceFlags
import androidx.activity.addCallback
import androidx.fragment.app.FragmentActivity

/**
 * A transparent activity used in the desktop mode to show the wallpaper under the freeform windows.
@@ -33,7 +34,7 @@ import android.window.DesktopExperienceFlags
 * Note! This activity should NOT interact directly with any other code in the Shell without calling
 * onto the shell main thread. Activities are always started on the main thread.
 */
class DesktopWallpaperActivity : Activity() {
class DesktopWallpaperActivity : FragmentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
@@ -47,6 +48,13 @@ class DesktopWallpaperActivity : Activity() {
        // (entering through an empty desk may result in a reversed bug: unfocusable when we wanted
        // it to be focusable).
        updateFocusableFlag(focusable = false)

        if (
            DesktopExperienceFlags.ENABLE_MULTIPLE_DESKTOPS_BACKEND.isTrue &&
                DesktopExperienceFlags.ENABLE_EMPTY_DESK_ON_MINIMIZE.isTrue
        ) {
            onBackPressedDispatcher.addCallback(this) { moveTaskToBack(true) }
        }
    }

    override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {