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 package com.android.server.notification; 17 18 import static android.app.Notification.FLAG_BUBBLE; 19 import static android.app.Notification.FLAG_FOREGROUND_SERVICE; 20 import static android.app.NotificationChannel.ALLOW_BUBBLE_OFF; 21 import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL; 22 import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE; 23 import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED; 24 25 import static com.android.internal.util.FrameworkStatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING; 26 import static com.android.internal.util.FrameworkStatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE; 27 28 import android.app.ActivityManager; 29 import android.app.Notification; 30 import android.app.NotificationChannel; 31 import android.app.PendingIntent; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.content.pm.ActivityInfo; 35 import android.util.Slog; 36 37 import com.android.internal.annotations.VisibleForTesting; 38 import com.android.internal.util.FrameworkStatsLog; 39 40 /** 41 * Determines whether a bubble can be shown for this notification. 42 */ 43 public class BubbleExtractor implements NotificationSignalExtractor { 44 private static final String TAG = "BubbleExtractor"; 45 private static final boolean DBG = false; 46 47 private ShortcutHelper mShortcutHelper; 48 private RankingConfig mConfig; 49 private ActivityManager mActivityManager; 50 private Context mContext; 51 initialize(Context context, NotificationUsageStats usageStats)52 public void initialize(Context context, NotificationUsageStats usageStats) { 53 if (DBG) Slog.d(TAG, "Initializing " + getClass().getSimpleName() + "."); 54 mContext = context; 55 mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); 56 } 57 process(NotificationRecord record)58 public RankingReconsideration process(NotificationRecord record) { 59 if (record == null || record.getNotification() == null) { 60 if (DBG) Slog.d(TAG, "skipping empty notification"); 61 return null; 62 } 63 64 if (mConfig == null) { 65 if (DBG) Slog.d(TAG, "missing config"); 66 return null; 67 } 68 69 if (mShortcutHelper == null) { 70 if (DBG) Slog.d(TAG, "missing shortcut helper"); 71 return null; 72 } 73 74 boolean notifCanPresentAsBubble = canPresentAsBubble(record) 75 && !mActivityManager.isLowRamDevice() 76 && record.isConversation() 77 && record.getShortcutInfo() != null 78 && (record.getNotification().flags & FLAG_FOREGROUND_SERVICE) == 0; 79 80 boolean userEnabledBubbles = mConfig.bubblesEnabled(record.getUser()); 81 int appPreference = 82 mConfig.getBubblePreference( 83 record.getSbn().getPackageName(), record.getSbn().getUid()); 84 NotificationChannel recordChannel = record.getChannel(); 85 if (!userEnabledBubbles 86 || appPreference == BUBBLE_PREFERENCE_NONE 87 || !notifCanPresentAsBubble) { 88 record.setAllowBubble(false); 89 if (!notifCanPresentAsBubble) { 90 // clear out bubble metadata since it can't be used 91 record.getNotification().setBubbleMetadata(null); 92 } 93 } else if (recordChannel == null) { 94 // the app is allowed but there's no channel to check 95 record.setAllowBubble(true); 96 } else if (appPreference == BUBBLE_PREFERENCE_ALL) { 97 record.setAllowBubble(recordChannel.getAllowBubbles() != ALLOW_BUBBLE_OFF); 98 } else if (appPreference == BUBBLE_PREFERENCE_SELECTED) { 99 record.setAllowBubble(recordChannel.canBubble()); 100 } 101 if (DBG) { 102 Slog.d(TAG, "record: " + record.getKey() 103 + " appPref: " + appPreference 104 + " canBubble: " + record.canBubble() 105 + " canPresentAsBubble: " + notifCanPresentAsBubble 106 + " flagRemoved: " + record.isFlagBubbleRemoved()); 107 } 108 109 final boolean applyFlag = record.canBubble() && !record.isFlagBubbleRemoved(); 110 if (applyFlag) { 111 record.getNotification().flags |= FLAG_BUBBLE; 112 } else { 113 record.getNotification().flags &= ~FLAG_BUBBLE; 114 } 115 return null; 116 } 117 118 @Override setConfig(RankingConfig config)119 public void setConfig(RankingConfig config) { 120 mConfig = config; 121 } 122 123 @Override setZenHelper(ZenModeHelper helper)124 public void setZenHelper(ZenModeHelper helper) { 125 } 126 setShortcutHelper(ShortcutHelper helper)127 public void setShortcutHelper(ShortcutHelper helper) { 128 mShortcutHelper = helper; 129 } 130 131 @VisibleForTesting setActivityManager(ActivityManager manager)132 public void setActivityManager(ActivityManager manager) { 133 mActivityManager = manager; 134 } 135 136 /** 137 * @return whether there is valid information for the notification to bubble. 138 */ 139 @VisibleForTesting canPresentAsBubble(NotificationRecord r)140 boolean canPresentAsBubble(NotificationRecord r) { 141 Notification notification = r.getNotification(); 142 Notification.BubbleMetadata metadata = notification.getBubbleMetadata(); 143 String pkg = r.getSbn().getPackageName(); 144 if (metadata == null) { 145 return false; 146 } 147 148 String shortcutId = metadata.getShortcutId(); 149 String notificationShortcutId = r.getShortcutInfo() != null 150 ? r.getShortcutInfo().getId() 151 : null; 152 boolean shortcutValid = false; 153 if (notificationShortcutId != null && shortcutId != null) { 154 // NoMan already checks validity of shortcut, just check if they match. 155 shortcutValid = shortcutId.equals(notificationShortcutId); 156 } else if (shortcutId != null) { 157 shortcutValid = 158 mShortcutHelper.getValidShortcutInfo(shortcutId, pkg, r.getUser()) != null; 159 } 160 if (metadata.getIntent() == null && !shortcutValid) { 161 // Should have a shortcut if intent is null 162 logBubbleError(r.getKey(), 163 "couldn't find valid shortcut for bubble with shortcutId: " + shortcutId); 164 return false; 165 } 166 if (shortcutValid) { 167 // TODO: check the shortcut intent / ensure it can show in activity view 168 return true; 169 } 170 return canLaunchInTaskView(mContext, metadata.getIntent(), pkg); 171 } 172 173 /** 174 * Whether an intent is properly configured to display in an {@link 175 * com.android.wm.shell.TaskView} for bubbling. 176 * 177 * @param context the context to use. 178 * @param pendingIntent the pending intent of the bubble. 179 * @param packageName the notification package name for this bubble. 180 */ 181 // Keep checks in sync with BubbleController#canLaunchInTaskView. 182 @VisibleForTesting canLaunchInTaskView(Context context, PendingIntent pendingIntent, String packageName)183 protected boolean canLaunchInTaskView(Context context, PendingIntent pendingIntent, 184 String packageName) { 185 if (pendingIntent == null) { 186 Slog.w(TAG, "Unable to create bubble -- no intent"); 187 return false; 188 } 189 190 Intent intent = pendingIntent.getIntent(); 191 ActivityInfo info = intent != null 192 ? intent.resolveActivityInfo(context.getPackageManager(), 0) 193 : null; 194 if (info == null) { 195 FrameworkStatsLog.write(FrameworkStatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, 196 packageName, 197 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_MISSING); 198 Slog.w(TAG, "Unable to send as bubble -- couldn't find activity info for intent: " 199 + intent); 200 return false; 201 } 202 if (!ActivityInfo.isResizeableMode(info.resizeMode)) { 203 FrameworkStatsLog.write(FrameworkStatsLog.BUBBLE_DEVELOPER_ERROR_REPORTED, 204 packageName, 205 BUBBLE_DEVELOPER_ERROR_REPORTED__ERROR__ACTIVITY_INFO_NOT_RESIZABLE); 206 Slog.w(TAG, "Unable to send as bubble -- activity is not resizable for intent: " 207 + intent); 208 return false; 209 } 210 return true; 211 } 212 logBubbleError(String key, String failureMessage)213 private void logBubbleError(String key, String failureMessage) { 214 if (DBG) { 215 Slog.w(TAG, "Bubble notification: " + key + " failed: " + failureMessage); 216 } 217 } 218 } 219