1 /*
2  * 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 android.os.Handler
19 import android.os.Looper
20 import android.os.Message
21 import com.android.systemui.dagger.qualifiers.Main
22 import com.android.systemui.statusbar.notification.collection.NotificationEntry
23 import java.util.function.Consumer
24 import javax.inject.Inject
25 
26 class NotificationEntryProcessorFactoryLooperImpl
27 @Inject
28 constructor(@Main private val mMainLooper: Looper) : NotificationEntryProcessorFactory {
createnull29     override fun create(consumer: Consumer<NotificationEntry>): Processor<NotificationEntry> {
30         return HandlerProcessor(mMainLooper, consumer)
31     }
32 
33     private class HandlerProcessor(
34         looper: Looper,
35         private val consumer: Consumer<NotificationEntry>,
36     ) : Handler(looper), Processor<NotificationEntry> {
handleMessagenull37         override fun handleMessage(msg: Message) {
38             if (msg.what == PROCESS_MSG) {
39                 val entry = msg.obj as NotificationEntry
40                 consumer.accept(entry)
41             } else {
42                 throw IllegalArgumentException("Unknown message type: " + msg.what)
43             }
44         }
45 
requestnull46         override fun request(obj: NotificationEntry) {
47             if (!hasMessages(PROCESS_MSG, obj)) {
48                 val msg = Message.obtain(this, PROCESS_MSG, obj)
49                 sendMessage(msg)
50             }
51         }
52 
cancelnull53         override fun cancel(obj: NotificationEntry) {
54             removeMessages(PROCESS_MSG, obj)
55         }
56         companion object {
57             private const val PROCESS_MSG = 1
58         }
59     }
60 }
61