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

Commit 9cea0863 authored by archisha's avatar archisha Committed by Archisha Baranwal
Browse files

Adding a WindowManagerProvider and WindowManagerUtils to provide the required...

Adding a WindowManagerProvider and WindowManagerUtils to provide the required WindowManager instance based on the status of ViewCapture tracing being enabled or disabled in SysUI.

Use WindowManagerProvider in case a WindowManager instance needs to be
created from a custom context.
Use WindowManagerUtils only in classes which are not part of a dagger
graph.

Bug: 397878336
Flag: com.android.systemui.enable_view_capture_tracing
Test: NA
Change-Id: I9c0e38c151a47d6e57ac18b3d275cfacdc0c9bcc
parent 6050e733
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -120,7 +120,7 @@ public class WindowManagerImpl implements WindowManager {
        this(context, null /* parentWindow */, null /* clientToken */);
    }

    private WindowManagerImpl(Context context, Window parentWindow,
    public WindowManagerImpl(Context context, Window parentWindow,
            @Nullable IBinder windowContextToken) {
        mContext = context;
        mParentWindow = parentWindow;
+53 −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.systemui.utils

import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.view.WindowManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.utils.windowmanager.WindowManagerProviderImpl
import com.google.common.truth.Truth.assertThat
import org.junit.runner.RunWith
import org.junit.Test

@RunWith(AndroidJUnit4::class)
@SmallTest
class WindowManagerProviderImplTest : SysuiTestCase() {

    private val windowManagerProvider = WindowManagerProviderImpl()
    private val windowManagerFromSystemService = mContext.getSystemService(WindowManager::class.java)

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_VIEW_CAPTURE_TRACING)
    fun viewCaptureTracingEnabled_verifyWMInstanceDoesNotMatchContextOne() {
        val windowManagerFromProvider = windowManagerProvider.getWindowManager(mContext)
        assertThat(windowManagerFromProvider).isNotEqualTo(windowManagerFromSystemService)
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_VIEW_CAPTURE_TRACING)
    fun viewCaptureTracingDisabled_verifyWMInstanceMatchesContextOne() {
        mContext.addMockSystemService(WindowManager::class.java, windowManagerFromSystemService)

        val windowManagerFromProvider = windowManagerProvider.getWindowManager(mContext)
        assertThat(windowManagerFromProvider).isEqualTo(windowManagerFromSystemService)
    }
}
 No newline at end of file
+28 −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.systemui.utils.windowmanager

import android.content.Context
import android.view.WindowManager

/** Fake implementation of [WindowManagerProvider], to be used in tests only. */
class FakeWindowManagerProvider(private val windowManager: WindowManager) : WindowManagerProvider {

    override fun getWindowManager(context: Context): WindowManager {
        return windowManager
    }
}
 No newline at end of file
+2 −0
Original line number Diff line number Diff line
@@ -26,6 +26,8 @@ java_library {
        "src/**/*.kt",
    ],
    static_libs: [
        "//frameworks/libs/systemui:view_capture",
        "com_android_systemui_flags_lib",
        "kotlin-stdlib",
        "kotlinx_coroutines",
    ],
+56 −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.systemui.utils.windowmanager

import android.content.Context
import android.view.WindowManager

/**
 * Provider for [WindowManager] in SystemUI.
 *
 * Use this class over [WindowManagerUtils] in cases where
 * a [WindowManager] is needed for a context created inside the class. [WindowManagerUtils] should
 * only be used in a class where the [WindowManager] is needed for a custom context inside the
 * class, and the class is not part of the dagger graph. Example usage:
 * ```kotlin
 * class Sample {
 *      private final WindowManager mWindowManager;
 *
 *      @Inject
 *      public Sample(WindowManagerProvider windowManagerProvider) {
 *          Context context = getCustomContext();
 *          mWindowManager = windowManagerProvider.getWindowManager(context);
 *      }
 *      // use mWindowManager
 * }
 *
 * class SampleTest {
 *
 *      @Mock
 *      WindowManager mWindowManager;
 *
 *      FakeWindowManagerProvider fakeProvider = new FakeWindowManagerProvider(mWindowManager);
 *
 *      // define the behaviour of mWindowManager to get required WindowManager instance in tests.
 * }
 * ```
 */
interface WindowManagerProvider {

    /** Method to return the required [WindowManager]. */
    fun getWindowManager(context: Context): WindowManager
}
Loading