• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.systemui.statusbar.notification.row.icon
18 
19 import android.app.Flags.notificationsRedesignThemedAppIcons
20 import android.content.Context
21 import android.graphics.drawable.Drawable
22 import android.util.AttributeSet
23 import android.view.View
24 import com.android.internal.widget.NotificationRowIconView
25 import com.android.internal.widget.NotificationRowIconView.NotificationIconProvider
26 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow
27 import com.android.systemui.statusbar.notification.row.NotifRemoteViewsFactory
28 import com.android.systemui.statusbar.notification.row.NotificationRowContentBinder
29 import com.android.systemui.statusbar.notification.shared.NotificationBundleUi
30 import javax.inject.Inject
31 
32 /**
33  * A factory which owns the construction of any NotificationRowIconView inside of Notifications in
34  * SystemUI. This allows overriding the small icon with the app icon in notifications.
35  */
36 class NotificationRowIconViewInflaterFactory
37 @Inject
38 constructor(
39     private val appIconProvider: AppIconProvider,
40     private val iconStyleProvider: NotificationIconStyleProvider,
41 ) : NotifRemoteViewsFactory {
42     override fun instantiate(
43         row: ExpandableNotificationRow,
44         @NotificationRowContentBinder.InflationFlag layoutType: Int,
45         parent: View?,
46         name: String,
47         context: Context,
48         attrs: AttributeSet,
49     ): View? {
50         return when (name) {
51             NotificationRowIconView::class.java.name ->
52                 NotificationRowIconView(context, attrs).also { view ->
53                     view.setIconProvider(createIconProvider(row, context))
54                 }
55 
56             else -> null
57         }
58     }
59 
60     private fun createIconProvider(
61         row: ExpandableNotificationRow,
62         context: Context,
63     ): NotificationIconProvider {
64         val sbn = if (NotificationBundleUi.isEnabled) row.entryAdapter?.sbn else row.entryLegacy.sbn
65         if (sbn == null) {
66             return object : NotificationIconProvider {
67                 override fun shouldShowAppIcon(): Boolean {
68                     return false
69                 }
70 
71                 override fun getAppIcon(): Drawable? {
72                     return null
73                 }
74             }
75         }
76         return object : NotificationIconProvider {
77             override fun shouldShowAppIcon(): Boolean {
78                 val shouldShowAppIcon = iconStyleProvider.shouldShowAppIcon(sbn, context)
79                 row.setIsShowingAppIcon(shouldShowAppIcon)
80                 return shouldShowAppIcon
81             }
82 
83             override fun getAppIcon(): Drawable? {
84                 val withWorkProfileBadge =
85                     iconStyleProvider.shouldShowWorkProfileBadge(sbn, context)
86                 return appIconProvider.getOrFetchAppIcon(
87                     sbn.packageName,
88                     context,
89                     withWorkProfileBadge,
90                     themed = notificationsRedesignThemedAppIcons(),
91                 )
92             }
93         }
94     }
95 }
96