1 /*
2  * Copyright (C) 2021 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.bedstead.nene.notifications
17 
18 import android.service.notification.NotificationListenerService
19 import android.service.notification.StatusBarNotification
20 import android.util.Log
21 import java.util.concurrent.CountDownLatch
22 import java.util.concurrent.atomic.AtomicReference
23 
24 /**
25  * [NotificationListenerService] for use with Nene.
26  *
27  * To access this use [TestApis.notifications].
28  */
29 class NeneNotificationListenerService : NotificationListenerService() {
onListenerConnectednull30     override fun onListenerConnected() {
31         Log.d(TAG, "Listener connected")
32         instance.set(this)
33         connectedLatch.countDown()
34     }
35 
onListenerDisconnectednull36     override fun onListenerDisconnected() {
37         Log.d(TAG, "Listener disconnected")
38         instance.set(null)
39         connectedLatch = CountDownLatch(1)
40     }
41 
onNotificationPostednull42     override fun onNotificationPosted(sbn: StatusBarNotification) {
43         Notifications.onNotificationPosted(Notification(sbn))
44     }
45 
onNotificationRankingUpdatenull46     override fun onNotificationRankingUpdate(rankingMap: RankingMap?) {
47         super.onNotificationRankingUpdate(rankingMap)
48     }
49 
50     companion object {
51         private const val TAG = "NeneNotificationListenerService"
52         val instance = AtomicReference<NeneNotificationListenerService?>()
53         var connectedLatch = CountDownLatch(1)
54             private set
55     }
56 }
57