diff options
author | 2025-02-26 03:30:26 +0000 | |
---|---|---|
committer | 2025-03-16 22:58:08 -0700 | |
commit | 9cea08635b27b040f795d56683a878a64a757ea6 (patch) | |
tree | 00629d019e4e831f90fb9b230941a7131e14500d | |
parent | 6050e733ca738f6ebc7914062635cb00ba4c4c58 (diff) |
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
7 files changed, 217 insertions, 1 deletions
diff --git a/core/java/android/view/WindowManagerImpl.java b/core/java/android/view/WindowManagerImpl.java index 97cf8fc748e8..8944c3fa0b19 100644 --- a/core/java/android/view/WindowManagerImpl.java +++ b/core/java/android/view/WindowManagerImpl.java @@ -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; diff --git a/packages/SystemUI/tests/src/com/android/systemui/utils/WindowManagerProviderImplTest.kt b/packages/SystemUI/tests/src/com/android/systemui/utils/WindowManagerProviderImplTest.kt new file mode 100644 index 000000000000..7b52237f0a01 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/utils/WindowManagerProviderImplTest.kt @@ -0,0 +1,53 @@ +/* + * 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 diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/utils/windowmanager/FakeWindowManagerProvider.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/utils/windowmanager/FakeWindowManagerProvider.kt new file mode 100644 index 000000000000..5c8eae3183c7 --- /dev/null +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/utils/windowmanager/FakeWindowManagerProvider.kt @@ -0,0 +1,28 @@ +/* + * 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 diff --git a/packages/SystemUI/utils/Android.bp b/packages/SystemUI/utils/Android.bp index 1efb11b436ff..8b63c07b270f 100644 --- a/packages/SystemUI/utils/Android.bp +++ b/packages/SystemUI/utils/Android.bp @@ -26,6 +26,8 @@ java_library { "src/**/*.kt", ], static_libs: [ + "//frameworks/libs/systemui:view_capture", + "com_android_systemui_flags_lib", "kotlin-stdlib", "kotlinx_coroutines", ], diff --git a/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProvider.kt b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProvider.kt new file mode 100644 index 000000000000..4e6eacbc8808 --- /dev/null +++ b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProvider.kt @@ -0,0 +1,56 @@ +/* + * 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 +} diff --git a/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProviderImpl.kt b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProviderImpl.kt new file mode 100644 index 000000000000..5e965ed47403 --- /dev/null +++ b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerProviderImpl.kt @@ -0,0 +1,28 @@ +/* + * 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 + +/** Implementation of [WindowManagerProvider]. */ +class WindowManagerProviderImpl : WindowManagerProvider { + + override fun getWindowManager(context: Context): WindowManager { + return WindowManagerUtils.getWindowManager(context) + } +} diff --git a/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerUtils.kt b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerUtils.kt new file mode 100644 index 000000000000..643e93422294 --- /dev/null +++ b/packages/SystemUI/utils/src/com/android/systemui/utils/windowmanager/WindowManagerUtils.kt @@ -0,0 +1,49 @@ +/* + * 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 +import com.android.app.viewcapture.ViewCaptureAwareWindowManagerFactory +import com.android.systemui.Flags.enableViewCaptureTracing + +/** + * Provides [WindowManager] in SystemUI. Use [WindowManagerProvider] unless [WindowManager] instance + * needs to be created in a class that is not part of the dagger dependency graph. + */ +object WindowManagerUtils { + + /** Method to return the required [WindowManager]. */ + @JvmStatic + fun getWindowManager(context: Context): WindowManager { + return if (!enableViewCaptureTracing()) { + context.getSystemService(WindowManager::class.java) + } else { + /** + * We use this token to supply windowContextToken to [WindowManager] for + * [WindowContext]. + */ + val windowContextToken = context.windowContextToken + + ViewCaptureAwareWindowManagerFactory.getInstance( + context, + parent = null, + windowContextToken, + ) + } + } +} |