1 /* <lambda>null2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.wallpaper.picker.di.modules 18 19 import android.os.Handler 20 import android.os.HandlerThread 21 import android.os.Looper 22 import android.os.Process 23 import dagger.Module 24 import dagger.Provides 25 import dagger.hilt.InstallIn 26 import dagger.hilt.components.SingletonComponent 27 import java.util.concurrent.Executor 28 import javax.inject.Qualifier 29 import javax.inject.Singleton 30 31 @Module 32 @InstallIn(SingletonComponent::class) 33 class ConcurrencyModule { 34 35 private val BROADCAST_SLOW_DISPATCH_THRESHOLD = 1000L 36 private val BROADCAST_SLOW_DELIVERY_THRESHOLD = 1000L 37 38 @Qualifier 39 @MustBeDocumented 40 @Retention(AnnotationRetention.RUNTIME) 41 annotation class BroadcastRunning 42 43 @Provides 44 @Singleton 45 @BroadcastRunning 46 fun provideBroadcastRunningLooper(): Looper { 47 return HandlerThread( 48 "BroadcastRunning", 49 Process.THREAD_PRIORITY_BACKGROUND, 50 ) 51 .apply { 52 start() 53 looper.setSlowLogThresholdMs( 54 BROADCAST_SLOW_DISPATCH_THRESHOLD, 55 BROADCAST_SLOW_DELIVERY_THRESHOLD, 56 ) 57 } 58 .looper 59 } 60 61 /** Provide a BroadcastRunning Executor (for sending and receiving broadcasts). */ 62 @Provides 63 @Singleton 64 @BroadcastRunning 65 fun provideBroadcastRunningExecutor(@BroadcastRunning looper: Looper?): Executor { 66 val handler = Handler(looper ?: Looper.getMainLooper()) 67 return Executor { command -> handler.post(command) } 68 } 69 } 70