blob: c96f9a470ca456b4005d3b0281020c19ad5c34d1 [file] [log] [blame]
Mady Mellor3df7ab02019-12-09 15:07:10 -08001/*
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
17package com.android.systemui.bubbles;
18
19import static com.android.systemui.bubbles.BadgedImageView.DEFAULT_PATH_SIZE;
20import static com.android.systemui.bubbles.BadgedImageView.WHITE_SCRIM_ALPHA;
21import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_BUBBLES;
22import static com.android.systemui.bubbles.BubbleDebugConfig.TAG_WITH_CLASS_NAME;
23
Mady Mellordf898fd2020-01-09 09:26:36 -080024import android.annotation.NonNull;
25import android.app.Notification;
26import android.app.Person;
Mady Mellor3df7ab02019-12-09 15:07:10 -080027import android.content.Context;
Lyn Hane566b362020-03-13 17:32:19 -070028import android.content.Intent;
Mady Mellor3df7ab02019-12-09 15:07:10 -080029import android.content.pm.ApplicationInfo;
30import android.content.pm.PackageManager;
31import android.content.pm.ShortcutInfo;
32import android.graphics.Bitmap;
33import android.graphics.Color;
34import android.graphics.Matrix;
35import android.graphics.Path;
36import android.graphics.drawable.Drawable;
Lyn Hane566b362020-03-13 17:32:19 -070037import android.graphics.drawable.Icon;
Mady Mellor3df7ab02019-12-09 15:07:10 -080038import android.os.AsyncTask;
Mady Mellordf898fd2020-01-09 09:26:36 -080039import android.os.Parcelable;
Mady Mellor3df7ab02019-12-09 15:07:10 -080040import android.service.notification.StatusBarNotification;
Mady Mellordf898fd2020-01-09 09:26:36 -080041import android.text.TextUtils;
Mady Mellor3df7ab02019-12-09 15:07:10 -080042import android.util.Log;
43import android.util.PathParser;
44import android.view.LayoutInflater;
45
46import androidx.annotation.Nullable;
47
48import com.android.internal.graphics.ColorUtils;
49import com.android.launcher3.icons.BitmapInfo;
50import com.android.systemui.R;
Mady Mellordf898fd2020-01-09 09:26:36 -080051import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Mady Mellor3df7ab02019-12-09 15:07:10 -080052
53import java.lang.ref.WeakReference;
Mady Mellordf898fd2020-01-09 09:26:36 -080054import java.util.List;
Mady Mellor3df7ab02019-12-09 15:07:10 -080055
56/**
57 * Simple task to inflate views & load necessary info to display a bubble.
58 */
59public class BubbleViewInfoTask extends AsyncTask<Void, Void, BubbleViewInfoTask.BubbleViewInfo> {
60 private static final String TAG = TAG_WITH_CLASS_NAME ? "BubbleViewInfoTask" : TAG_BUBBLES;
61
62
63 /**
64 * Callback to find out when the bubble has been inflated & necessary data loaded.
65 */
66 public interface Callback {
67 /**
68 * Called when data has been loaded for the bubble.
69 */
70 void onBubbleViewsReady(Bubble bubble);
71 }
72
73 private Bubble mBubble;
74 private WeakReference<Context> mContext;
75 private WeakReference<BubbleStackView> mStackView;
76 private BubbleIconFactory mIconFactory;
77 private Callback mCallback;
78
79 /**
80 * Creates a task to load information for the provided {@link Bubble}. Once all info
81 * is loaded, {@link Callback} is notified.
82 */
83 BubbleViewInfoTask(Bubble b,
84 Context context,
85 BubbleStackView stackView,
86 BubbleIconFactory factory,
87 Callback c) {
88 mBubble = b;
89 mContext = new WeakReference<>(context);
90 mStackView = new WeakReference<>(stackView);
91 mIconFactory = factory;
92 mCallback = c;
93 }
94
95 @Override
96 protected BubbleViewInfo doInBackground(Void... voids) {
97 return BubbleViewInfo.populate(mContext.get(), mStackView.get(), mIconFactory, mBubble);
98 }
99
100 @Override
101 protected void onPostExecute(BubbleViewInfo viewInfo) {
102 if (viewInfo != null) {
103 mBubble.setViewInfo(viewInfo);
104 if (mCallback != null && !isCancelled()) {
105 mCallback.onBubbleViewsReady(mBubble);
106 }
107 }
108 }
109
Mady Mellordf898fd2020-01-09 09:26:36 -0800110 /**
111 * Info necessary to render a bubble.
112 */
Mady Mellor3df7ab02019-12-09 15:07:10 -0800113 static class BubbleViewInfo {
114 BadgedImageView imageView;
115 BubbleExpandedView expandedView;
116 ShortcutInfo shortcutInfo;
117 String appName;
118 Bitmap badgedBubbleImage;
119 int dotColor;
120 Path dotPath;
Mady Mellordf898fd2020-01-09 09:26:36 -0800121 Bubble.FlyoutMessage flyoutMessage;
Mady Mellor3df7ab02019-12-09 15:07:10 -0800122
123 @Nullable
124 static BubbleViewInfo populate(Context c, BubbleStackView stackView,
125 BubbleIconFactory iconFactory, Bubble b) {
126 BubbleViewInfo info = new BubbleViewInfo();
127
128 // View inflation: only should do this once per bubble
129 if (!b.isInflated()) {
130 LayoutInflater inflater = LayoutInflater.from(c);
131 info.imageView = (BadgedImageView) inflater.inflate(
132 R.layout.bubble_view, stackView, false /* attachToRoot */);
133
134 info.expandedView = (BubbleExpandedView) inflater.inflate(
135 R.layout.bubble_expanded_view, stackView, false /* attachToRoot */);
136 info.expandedView.setStackView(stackView);
137 }
138
139 StatusBarNotification sbn = b.getEntry().getSbn();
140 String packageName = sbn.getPackageName();
141
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800142 String bubbleShortcutId = b.getEntry().getBubbleMetadata().getShortcutId();
143 if (bubbleShortcutId != null) {
Mady Mellordd6fe612020-04-15 11:47:55 -0700144 info.shortcutInfo = b.getEntry().getRanking().getShortcutInfo();
Mady Mellor3df7ab02019-12-09 15:07:10 -0800145 }
146
147 // App name & app icon
148 PackageManager pm = c.getPackageManager();
149 ApplicationInfo appInfo;
150 Drawable badgedIcon;
Mady Mellor2dae34b2020-01-07 13:52:58 -0800151 Drawable appIcon;
Mady Mellor3df7ab02019-12-09 15:07:10 -0800152 try {
153 appInfo = pm.getApplicationInfo(
154 packageName,
155 PackageManager.MATCH_UNINSTALLED_PACKAGES
156 | PackageManager.MATCH_DISABLED_COMPONENTS
157 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
158 | PackageManager.MATCH_DIRECT_BOOT_AWARE);
159 if (appInfo != null) {
160 info.appName = String.valueOf(pm.getApplicationLabel(appInfo));
161 }
Mady Mellor2dae34b2020-01-07 13:52:58 -0800162 appIcon = pm.getApplicationIcon(packageName);
Mady Mellor3df7ab02019-12-09 15:07:10 -0800163 badgedIcon = pm.getUserBadgedIcon(appIcon, sbn.getUser());
164 } catch (PackageManager.NameNotFoundException exception) {
165 // If we can't find package... don't think we should show the bubble.
166 Log.w(TAG, "Unable to find package: " + packageName);
167 return null;
168 }
169
170 // Badged bubble image
Mady Mellor2ac2d3a2020-01-08 17:18:54 -0800171 Drawable bubbleDrawable = iconFactory.getBubbleDrawable(c, info.shortcutInfo,
172 b.getEntry().getBubbleMetadata());
Mady Mellor2dae34b2020-01-07 13:52:58 -0800173 if (bubbleDrawable == null) {
174 // Default to app icon
175 bubbleDrawable = appIcon;
176 }
177
Mady Mellor3df7ab02019-12-09 15:07:10 -0800178 BitmapInfo badgeBitmapInfo = iconFactory.getBadgeBitmap(badgedIcon);
179 info.badgedBubbleImage = iconFactory.getBubbleBitmap(bubbleDrawable,
180 badgeBitmapInfo).icon;
181
182 // Dot color & placement
183 Path iconPath = PathParser.createPathFromPathData(
184 c.getResources().getString(com.android.internal.R.string.config_icon_mask));
185 Matrix matrix = new Matrix();
186 float scale = iconFactory.getNormalizer().getScale(bubbleDrawable,
187 null /* outBounds */, null /* path */, null /* outMaskShape */);
188 float radius = DEFAULT_PATH_SIZE / 2f;
189 matrix.setScale(scale /* x scale */, scale /* y scale */, radius /* pivot x */,
190 radius /* pivot y */);
191 iconPath.transform(matrix);
192 info.dotPath = iconPath;
193 info.dotColor = ColorUtils.blendARGB(badgeBitmapInfo.color,
194 Color.WHITE, WHITE_SCRIM_ALPHA);
Mady Mellordf898fd2020-01-09 09:26:36 -0800195
196 // Flyout
197 info.flyoutMessage = extractFlyoutMessage(c, b.getEntry());
Mady Mellor3df7ab02019-12-09 15:07:10 -0800198 return info;
199 }
200 }
Mady Mellordf898fd2020-01-09 09:26:36 -0800201
202
203 /**
204 * Returns our best guess for the most relevant text summary of the latest update to this
205 * notification, based on its type. Returns null if there should not be an update message.
206 */
207 @NonNull
208 static Bubble.FlyoutMessage extractFlyoutMessage(Context context,
209 NotificationEntry entry) {
210 final Notification underlyingNotif = entry.getSbn().getNotification();
211 final Class<? extends Notification.Style> style = underlyingNotif.getNotificationStyle();
212
213 Bubble.FlyoutMessage bubbleMessage = new Bubble.FlyoutMessage();
214 bubbleMessage.isGroupChat = underlyingNotif.extras.getBoolean(
215 Notification.EXTRA_IS_GROUP_CONVERSATION);
216 try {
217 if (Notification.BigTextStyle.class.equals(style)) {
218 // Return the big text, it is big so probably important. If it's not there use the
219 // normal text.
220 CharSequence bigText =
221 underlyingNotif.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
222 bubbleMessage.message = !TextUtils.isEmpty(bigText)
223 ? bigText
224 : underlyingNotif.extras.getCharSequence(Notification.EXTRA_TEXT);
225 return bubbleMessage;
226 } else if (Notification.MessagingStyle.class.equals(style)) {
227 final List<Notification.MessagingStyle.Message> messages =
228 Notification.MessagingStyle.Message.getMessagesFromBundleArray(
229 (Parcelable[]) underlyingNotif.extras.get(
230 Notification.EXTRA_MESSAGES));
231
232 final Notification.MessagingStyle.Message latestMessage =
233 Notification.MessagingStyle.findLatestIncomingMessage(messages);
234 if (latestMessage != null) {
235 bubbleMessage.message = latestMessage.getText();
236 Person sender = latestMessage.getSenderPerson();
237 bubbleMessage.senderName = sender != null
238 ? sender.getName()
239 : null;
Lyn Hane566b362020-03-13 17:32:19 -0700240
241 bubbleMessage.senderAvatar = null;
242 if (sender != null && sender.getIcon() != null) {
243 if (sender.getIcon().getType() == Icon.TYPE_URI
244 || sender.getIcon().getType() == Icon.TYPE_URI_ADAPTIVE_BITMAP) {
245 context.grantUriPermission(context.getPackageName(),
246 sender.getIcon().getUri(),
247 Intent.FLAG_GRANT_READ_URI_PERMISSION);
248 }
249 bubbleMessage.senderAvatar = sender.getIcon().loadDrawable(context);
250 }
Mady Mellordf898fd2020-01-09 09:26:36 -0800251 return bubbleMessage;
252 }
253 } else if (Notification.InboxStyle.class.equals(style)) {
254 CharSequence[] lines =
255 underlyingNotif.extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
256
257 // Return the last line since it should be the most recent.
258 if (lines != null && lines.length > 0) {
259 bubbleMessage.message = lines[lines.length - 1];
260 return bubbleMessage;
261 }
262 } else if (Notification.MediaStyle.class.equals(style)) {
263 // Return nothing, media updates aren't typically useful as a text update.
264 return bubbleMessage;
265 } else {
266 // Default to text extra.
267 bubbleMessage.message =
268 underlyingNotif.extras.getCharSequence(Notification.EXTRA_TEXT);
269 return bubbleMessage;
270 }
271 } catch (ClassCastException | NullPointerException | ArrayIndexOutOfBoundsException e) {
272 // No use crashing, we'll just return null and the caller will assume there's no update
273 // message.
274 e.printStackTrace();
275 }
276
277 return bubbleMessage;
278 }
Mady Mellor3df7ab02019-12-09 15:07:10 -0800279}