1 /*
2  * Copyright (C) 2020 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.dagger;
18 
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.content.pm.PackageManager;
22 import android.service.notification.StatusBarNotification;
23 
24 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
25 import com.android.systemui.statusbar.notification.row.ActivatableNotificationView;
26 import com.android.systemui.statusbar.notification.row.BigPictureIconManager;
27 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
28 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRowController;
29 import com.android.systemui.statusbar.phone.CentralSurfaces;
30 
31 import dagger.Binds;
32 import dagger.BindsInstance;
33 import dagger.Module;
34 import dagger.Provides;
35 import dagger.Subcomponent;
36 
37 /**
38  * Dagger Component for a {@link ExpandableNotificationRow}.
39  */
40 @Subcomponent(modules = {
41         ActivatableNotificationViewModule.class,
42         ExpandableNotificationRowComponent.ExpandableNotificationRowModule.class,
43         RemoteInputViewModule.class
44 })
45 @NotificationRowScope
46 public interface ExpandableNotificationRowComponent {
47 
48     /**
49      * Builder for {@link NotificationRowComponent}.
50      */
51     @Subcomponent.Builder
52     interface Builder {
53         // TODO: NotificationEntry contains a reference to ExpandableNotificationRow, so it
54         // should be possible to pull one from the other, but they aren't connected at the time
55         // this component is constructed.
56         @BindsInstance
expandableNotificationRow(ExpandableNotificationRow view)57         Builder expandableNotificationRow(ExpandableNotificationRow view);
58         @BindsInstance
notificationEntry(NotificationEntry entry)59         Builder notificationEntry(NotificationEntry entry);
60         @BindsInstance
onExpandClickListener(ExpandableNotificationRow.OnExpandClickListener presenter)61         Builder onExpandClickListener(ExpandableNotificationRow.OnExpandClickListener presenter);
build()62         ExpandableNotificationRowComponent build();
63     }
64 
65     /**
66      * Creates a ExpandableNotificationRowController.
67      */
68     @NotificationRowScope
getExpandableNotificationRowController()69     ExpandableNotificationRowController getExpandableNotificationRowController();
70 
71     /**
72      * Creates a BigPictureIconManager.
73      */
74     @NotificationRowScope
getBigPictureIconManager()75     BigPictureIconManager getBigPictureIconManager();
76 
77     /**
78      * Dagger Module that extracts interesting properties from an ExpandableNotificationRow.
79      */
80     @Module
81     abstract class ExpandableNotificationRowModule {
82 
83         /** ExpandableNotificationRow is provided as an instance of ActivatableNotificationView. */
84         @Binds
bindExpandableView(ExpandableNotificationRow view)85         abstract ActivatableNotificationView bindExpandableView(ExpandableNotificationRow view);
86 
87         @Provides
provideStatusBarNotification( NotificationEntry notificationEntry)88         static StatusBarNotification provideStatusBarNotification(
89                 NotificationEntry notificationEntry) {
90             return notificationEntry.getSbn();
91         }
92 
93         @Provides
94         @NotificationKey
provideNotificationKey(StatusBarNotification statusBarNotification)95         static String provideNotificationKey(StatusBarNotification statusBarNotification) {
96             return statusBarNotification.getKey();
97         }
98 
99         @Provides
100         @AppName
provideAppName(Context context, StatusBarNotification statusBarNotification)101         static String provideAppName(Context context, StatusBarNotification statusBarNotification) {
102             // Get the app name.
103             // Note that Notification.Builder#bindHeaderAppName has similar logic
104             // but since this field is used in the guts, it must be accurate.
105             // Therefore we will only show the application label, or, failing that, the
106             // package name. No substitutions.
107             PackageManager pmUser = CentralSurfaces.getPackageManagerForUser(
108                     context, statusBarNotification.getUser().getIdentifier());
109             final String pkg = statusBarNotification.getPackageName();
110             try {
111                 final ApplicationInfo info = pmUser.getApplicationInfo(pkg,
112                         PackageManager.MATCH_UNINSTALLED_PACKAGES
113                                 | PackageManager.MATCH_DISABLED_COMPONENTS);
114                 if (info != null) {
115                     return String.valueOf(pmUser.getApplicationLabel(info));
116                 }
117             } catch (PackageManager.NameNotFoundException e) {
118                 // Do nothing
119             }
120 
121             return pkg;
122         }
123     }
124 }
125