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 package com.android.systemui.statusbar.notification.row
17 
18 import com.android.systemui.dagger.qualifiers.Main
19 import com.android.systemui.statusbar.notification.collection.NotificationEntry
20 import com.android.systemui.util.concurrency.DelayableExecutor
21 import java.util.concurrent.ConcurrentHashMap
22 import java.util.function.Consumer
23 import javax.inject.Inject
24 
25 class NotificationEntryProcessorFactoryExecutorImpl
26 @Inject
27 constructor(@Main private val mMainExecutor: DelayableExecutor) :
28     NotificationEntryProcessorFactory {
29     override fun create(consumer: Consumer<NotificationEntry>): Processor<NotificationEntry> {
30         return ExecutorProcessor(mMainExecutor, consumer)
31     }
32 
33     private class ExecutorProcessor(
34         private val executor: DelayableExecutor,
35         private val consumer: Consumer<NotificationEntry>,
36     ) : Processor<NotificationEntry> {
37         val cancellationsByEntry = ConcurrentHashMap<NotificationEntry, Runnable>()
38 
39         override fun request(obj: NotificationEntry) {
40             cancellationsByEntry.computeIfAbsent(obj) { entry ->
41                 executor.executeDelayed({ processEntry(entry) }, 0L)
42             }
43         }
44 
45         private fun processEntry(entry: NotificationEntry) {
46             val cancellation = cancellationsByEntry.remove(entry)
47             if (cancellation != null) {
48                 consumer.accept(entry)
49             }
50         }
51 
52         override fun cancel(obj: NotificationEntry) {
53             cancellationsByEntry.remove(obj)?.run()
54         }
55     }
56 }
57