1 /*
2  * Copyright (C) 2019 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.systemui.statusbar.notification
18 
19 import android.content.Context
20 import android.provider.DeviceConfig
21 import com.android.internal.annotations.VisibleForTesting
22 import com.android.internal.config.sysui.SystemUiDeviceConfigFlags.NOTIFICATIONS_USE_PEOPLE_FILTERING
23 import com.android.systemui.dagger.SysUISingleton
24 import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype
25 import com.android.systemui.statusbar.notification.shared.PriorityPeopleSection
26 import com.android.systemui.statusbar.notification.stack.BUCKET_ALERTING
27 import com.android.systemui.statusbar.notification.stack.BUCKET_FOREGROUND_SERVICE
28 import com.android.systemui.statusbar.notification.stack.BUCKET_HEADS_UP
29 import com.android.systemui.statusbar.notification.stack.BUCKET_MEDIA_CONTROLS
30 import com.android.systemui.statusbar.notification.stack.BUCKET_PEOPLE
31 import com.android.systemui.statusbar.notification.stack.BUCKET_SILENT
32 import com.android.systemui.statusbar.notification.stack.PriorityBucket
33 import com.android.systemui.util.DeviceConfigProxy
34 import com.android.systemui.util.Utils
35 import javax.inject.Inject
36 
37 private var sUsePeopleFiltering: Boolean? = null
38 
39 /** Feature controller for the NOTIFICATIONS_USE_PEOPLE_FILTERING config. */
40 @SysUISingleton
41 class NotificationSectionsFeatureManager
42 @Inject
43 constructor(val proxy: DeviceConfigProxy, val context: Context) {
44 
isFilteringEnablednull45     fun isFilteringEnabled(): Boolean {
46         return usePeopleFiltering(proxy)
47     }
48 
isMediaControlsEnablednull49     fun isMediaControlsEnabled(): Boolean {
50         return Utils.useQsMediaPlayer(context)
51     }
52 
getNotificationBucketsnull53     fun getNotificationBuckets(): IntArray {
54         if (PriorityPeopleSection.isEnabled || NotificationMinimalismPrototype.V2.isEnabled) {
55             // We don't need this list to be adaptive, it can be the superset of all features.
56             return PriorityBucket.getAllInOrder()
57         }
58         return when {
59             isFilteringEnabled() && isMediaControlsEnabled() ->
60                 intArrayOf(
61                     BUCKET_HEADS_UP,
62                     BUCKET_FOREGROUND_SERVICE,
63                     BUCKET_MEDIA_CONTROLS,
64                     BUCKET_PEOPLE,
65                     BUCKET_ALERTING,
66                     BUCKET_SILENT
67                 )
68             !isFilteringEnabled() && isMediaControlsEnabled() ->
69                 intArrayOf(
70                     BUCKET_HEADS_UP,
71                     BUCKET_FOREGROUND_SERVICE,
72                     BUCKET_MEDIA_CONTROLS,
73                     BUCKET_ALERTING,
74                     BUCKET_SILENT
75                 )
76             isFilteringEnabled() && !isMediaControlsEnabled() ->
77                 intArrayOf(
78                     BUCKET_HEADS_UP,
79                     BUCKET_FOREGROUND_SERVICE,
80                     BUCKET_PEOPLE,
81                     BUCKET_ALERTING,
82                     BUCKET_SILENT
83                 )
84             else -> intArrayOf(BUCKET_ALERTING, BUCKET_SILENT)
85         }
86     }
87 
getNumberOfBucketsnull88     fun getNumberOfBuckets(): Int {
89         return getNotificationBuckets().size
90     }
91 
92     @VisibleForTesting
clearCachenull93     fun clearCache() {
94         sUsePeopleFiltering = null
95     }
96 }
97 
usePeopleFilteringnull98 private fun usePeopleFiltering(proxy: DeviceConfigProxy): Boolean {
99     if (sUsePeopleFiltering == null) {
100         sUsePeopleFiltering =
101             proxy.getBoolean(
102                 DeviceConfig.NAMESPACE_SYSTEMUI,
103                 NOTIFICATIONS_USE_PEOPLE_FILTERING,
104                 true
105             )
106     }
107 
108     return sUsePeopleFiltering!!
109 }
110