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.ExpandableNotificationRow; 27 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRowController; 28 import com.android.systemui.statusbar.notification.stack.NotificationListContainer; 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); 62 @BindsInstance listContainer(NotificationListContainer listContainer)63 Builder listContainer(NotificationListContainer listContainer); build()64 ExpandableNotificationRowComponent build(); 65 } 66 67 /** 68 * Creates a ExpandableNotificationRowController. 69 */ 70 @NotificationRowScope getExpandableNotificationRowController()71 ExpandableNotificationRowController getExpandableNotificationRowController(); 72 73 /** 74 * Dagger Module that extracts interesting properties from an ExpandableNotificationRow. 75 */ 76 @Module 77 abstract class ExpandableNotificationRowModule { 78 79 /** ExpandableNotificationRow is provided as an instance of ActivatableNotificationView. */ 80 @Binds bindExpandableView(ExpandableNotificationRow view)81 abstract ActivatableNotificationView bindExpandableView(ExpandableNotificationRow view); 82 83 @Provides provideStatusBarNotification( NotificationEntry notificationEntry)84 static StatusBarNotification provideStatusBarNotification( 85 NotificationEntry notificationEntry) { 86 return notificationEntry.getSbn(); 87 } 88 89 @Provides 90 @NotificationKey provideNotificationKey(StatusBarNotification statusBarNotification)91 static String provideNotificationKey(StatusBarNotification statusBarNotification) { 92 return statusBarNotification.getKey(); 93 } 94 95 @Provides 96 @AppName provideAppName(Context context, StatusBarNotification statusBarNotification)97 static String provideAppName(Context context, StatusBarNotification statusBarNotification) { 98 // Get the app name. 99 // Note that Notification.Builder#bindHeaderAppName has similar logic 100 // but since this field is used in the guts, it must be accurate. 101 // Therefore we will only show the application label, or, failing that, the 102 // package name. No substitutions. 103 PackageManager pmUser = CentralSurfaces.getPackageManagerForUser( 104 context, statusBarNotification.getUser().getIdentifier()); 105 final String pkg = statusBarNotification.getPackageName(); 106 try { 107 final ApplicationInfo info = pmUser.getApplicationInfo(pkg, 108 PackageManager.MATCH_UNINSTALLED_PACKAGES 109 | PackageManager.MATCH_DISABLED_COMPONENTS); 110 if (info != null) { 111 return String.valueOf(pmUser.getApplicationLabel(info)); 112 } 113 } catch (PackageManager.NameNotFoundException e) { 114 // Do nothing 115 } 116 117 return pkg; 118 } 119 } 120 } 121