diff --git a/concurrent/Android.bp b/concurrent/Android.bp index b3ed449990..81ec210d9f 100644 --- a/concurrent/Android.bp +++ b/concurrent/Android.bp @@ -24,6 +24,10 @@ java_library { "src/com/android/launcher3/concurrent/annotations/LightweightBackground.kt", "src/com/android/launcher3/concurrent/annotations/ThreadPool.kt", "src/com/android/launcher3/concurrent/annotations/Ui.kt", + "src/com/android/launcher3/concurrent/annotations/UiContext.kt", + "src/com/android/launcher3/concurrent/annotations/BackgroundContext.kt", + "src/com/android/launcher3/concurrent/annotations/LightweightBackgroundContext.kt", + "src/com/android/launcher3/concurrent/annotations/ThreadPoolContext.kt", ], sdk_version: "current", min_sdk_version: min_launcher3_sdk_version, @@ -49,5 +53,6 @@ java_library { "guava", "jsr330", "launcher-executor-qualifiers", + "kotlinx-coroutines-android", ], } diff --git a/concurrent/src/com/android/launcher3/concurrent/ExecutorsModule.kt b/concurrent/src/com/android/launcher3/concurrent/ExecutorsModule.kt index 0a761cb46b..c4aefc7732 100644 --- a/concurrent/src/com/android/launcher3/concurrent/ExecutorsModule.kt +++ b/concurrent/src/com/android/launcher3/concurrent/ExecutorsModule.kt @@ -22,16 +22,24 @@ import com.android.launcher3.concurrent.annotations.LightweightBackgroundPriorit import com.android.launcher3.concurrent.annotations.ThreadPool import com.android.launcher3.concurrent.annotations.Ui +import com.android.launcher3.concurrent.annotations.UiContext +import com.android.launcher3.concurrent.annotations.LightweightBackgroundContext +import com.android.launcher3.concurrent.annotations.BackgroundContext +import com.android.launcher3.concurrent.annotations.ThreadPoolContext + import com.google.common.util.concurrent.ListeningExecutorService import dagger.Binds import dagger.Module import dagger.Provides +import kotlin.coroutines.CoroutineContext import java.util.concurrent.Executor import java.util.concurrent.ExecutorService import javax.inject.Singleton +import kotlinx.coroutines.asCoroutineDispatcher + /** * Module that stipulates the executors that are usable by common launcher * modules. @@ -127,4 +135,50 @@ interface ExecutorsModule { @Ui listeningExecutorService: ListeningExecutorService ): ExecutorService // end UI executors + + // The following methods provide the CoroutineContext for the executors. + companion object { + + @Provides + @UiContext + fun provideUiContext( + @Ui executor: Executor + ): CoroutineContext { + return executor.asCoroutineDispatcher() + } + + @Provides + @LightweightBackgroundContext(LightweightBackgroundPriority.DATA) + fun provideDataLightweightContext( + @LightweightBackground(LightweightBackgroundPriority.DATA) + executor: Executor + ): CoroutineContext { + return executor.asCoroutineDispatcher() + } + + @Provides + @LightweightBackgroundContext(LightweightBackgroundPriority.UI) + fun provideUiLightweightContext( + @LightweightBackground(LightweightBackgroundPriority.UI) + executor: Executor + ): CoroutineContext { + return executor.asCoroutineDispatcher() + } + + @Provides + @BackgroundContext + fun provideBackgroundContext( + @Background executor: Executor + ): CoroutineContext { + return executor.asCoroutineDispatcher() + } + + @Provides + @ThreadPoolContext + fun provideThreadPoolContext( + @ThreadPool executor: Executor + ): CoroutineContext { + return executor.asCoroutineDispatcher() + } + } } \ No newline at end of file diff --git a/concurrent/src/com/android/launcher3/concurrent/annotations/BackgroundContext.kt b/concurrent/src/com/android/launcher3/concurrent/annotations/BackgroundContext.kt new file mode 100644 index 0000000000..799890a32d --- /dev/null +++ b/concurrent/src/com/android/launcher3/concurrent/annotations/BackgroundContext.kt @@ -0,0 +1,27 @@ +/* + * 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.launcher3.concurrent.annotations + +import javax.inject.Qualifier + +/** + * Qualifier for the [kotlinx.coroutines.CoroutineContext] of the [Background] + * qualifier. + */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class BackgroundContext \ No newline at end of file diff --git a/concurrent/src/com/android/launcher3/concurrent/annotations/LightweightBackgroundContext.kt b/concurrent/src/com/android/launcher3/concurrent/annotations/LightweightBackgroundContext.kt new file mode 100644 index 0000000000..63f972e7d2 --- /dev/null +++ b/concurrent/src/com/android/launcher3/concurrent/annotations/LightweightBackgroundContext.kt @@ -0,0 +1,29 @@ +/* + * 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.launcher3.concurrent.annotations + +import javax.inject.Qualifier + +/** + * Qualifier for the [kotlinx.coroutines.CoroutineContext] of the + * [LightweightBackground] qualifier. + */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class LightweightBackgroundContext( + val priority: LightweightBackgroundPriority = LightweightBackgroundPriority.DATA +) \ No newline at end of file diff --git a/concurrent/src/com/android/launcher3/concurrent/annotations/ThreadPoolContext.kt b/concurrent/src/com/android/launcher3/concurrent/annotations/ThreadPoolContext.kt new file mode 100644 index 0000000000..75d0620a92 --- /dev/null +++ b/concurrent/src/com/android/launcher3/concurrent/annotations/ThreadPoolContext.kt @@ -0,0 +1,27 @@ +/* + * 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.launcher3.concurrent.annotations + +import javax.inject.Qualifier + +/** + * Qualifier for the [kotlinx.coroutines.CoroutineContext] of the [ThreadPool] + * qualifier. + */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class ThreadPoolContext \ No newline at end of file diff --git a/concurrent/src/com/android/launcher3/concurrent/annotations/UiContext.kt b/concurrent/src/com/android/launcher3/concurrent/annotations/UiContext.kt new file mode 100644 index 0000000000..0e1bcb49e1 --- /dev/null +++ b/concurrent/src/com/android/launcher3/concurrent/annotations/UiContext.kt @@ -0,0 +1,27 @@ +/* + * 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.launcher3.concurrent.annotations + +import javax.inject.Qualifier + +/** + * Qualifier for the [kotlinx.coroutines.CoroutineContext] of the + * [Ui] qualifier. + */ +@Qualifier +@Retention(AnnotationRetention.RUNTIME) +annotation class UiContext \ No newline at end of file