Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/Bubble.java

raw symbols SCIP
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 package com.android.wm.shell.bubbles;
17
18 import static android.app.ActivityTaskManager.INVALID_TASK_ID;
19 import static android.os.AsyncTask.Status.FINISHED;
20
21 import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
22
23 import android.annotation.DimenRes;
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.app.Notification;
27 import android.app.PendingIntent;
28 import android.app.Person;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.LocusId;
32 import android.content.pm.ApplicationInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ShortcutInfo;
35 import android.content.res.Resources;
36 import android.graphics.Bitmap;
37 import android.graphics.Path;
38 import android.graphics.drawable.Drawable;
39 import android.graphics.drawable.Icon;
40 import android.os.Parcelable;
41 import android.os.UserHandle;
42 import android.provider.Settings;
43 import android.service.notification.StatusBarNotification;
44 import android.text.TextUtils;
45 import android.util.Log;
46
47 import com.android.internal.annotations.VisibleForTesting;
48 import com.android.internal.logging.InstanceId;
49
50 import java.io.PrintWriter;
51 import java.util.List;
52 import java.util.Objects;
53 import java.util.concurrent.Executor;
54
55 /**
56 * Encapsulates the data and UI elements of a bubble.
57 */
58 @VisibleForTesting
59 public class Bubble implements BubbleViewProvider {
60 private static final String TAG = "Bubble";
61
62 public static final String KEY_APP_BUBBLE = "key_app_bubble";
63
64 private final String mKey;
65 @Nullable
66 private final String mGroupKey;
67 @Nullable
68 private final LocusId mLocusId;
69
70 private final Executor mMainExecutor;
71
72 private long mLastUpdated;
73 private long mLastAccessed;
74
75 @Nullable
76 private Bubbles.BubbleMetadataFlagListener mBubbleMetadataFlagListener;
77
78 /** Whether the bubble should show a dot for the notification indicating updated content. */
79 private boolean mShowBubbleUpdateDot = true;
80
81 /** Whether flyout text should be suppressed, regardless of any other flags or state. */
82 private boolean mSuppressFlyout;
83
84 // Items that are typically loaded later
85 private String mAppName;
86 private ShortcutInfo mShortcutInfo;
87 private String mMetadataShortcutId;
88 private BadgedImageView mIconView;
89 private BubbleExpandedView mExpandedView;
90
91 private BubbleViewInfoTask mInflationTask;
92 private boolean mInflateSynchronously;
93 private boolean mPendingIntentCanceled;
94 private boolean mIsImportantConversation;
95
96 /**
97 * Presentational info about the flyout.
98 */
99 public static class FlyoutMessage {
100 @Nullable public Icon senderIcon;
101 @Nullable public Drawable senderAvatar;
102 @Nullable public CharSequence senderName;
103 @Nullable public CharSequence message;
104 @Nullable public boolean isGroupChat;
105 }
106
107 private FlyoutMessage mFlyoutMessage;
108 // The developer provided image for the bubble
109 private Bitmap mBubbleBitmap;
110 // The app badge for the bubble
111 private Bitmap mBadgeBitmap;
112 // App badge without any markings for important conversations
113 private Bitmap mRawBadgeBitmap;
114 private int mDotColor;
115 private Path mDotPath;
116 private int mFlags;
117
118 @NonNull
119 private UserHandle mUser;
120 @NonNull
121 private String mPackageName;
122 @Nullable
123 private String mTitle;
124 @Nullable
125 private Icon mIcon;
126 private boolean mIsBubble;
127 private boolean mIsTextChanged;
128 private boolean mIsClearable;
129 private boolean mShouldSuppressNotificationDot;
130 private boolean mShouldSuppressNotificationList;
131 private boolean mShouldSuppressPeek;
132 private int mDesiredHeight;
133 @DimenRes
134 private int mDesiredHeightResId;
135 private int mTaskId;
136
137 /** for logging **/
138 @Nullable
139 private InstanceId mInstanceId;
140 @Nullable
141 private String mChannelId;
142 private int mNotificationId;
143 private int mAppUid = -1;
144
145 /**
146 * A bubble is created and can be updated. This intent is updated until the user first
147 * expands the bubble. Once the user has expanded the contents, we ignore the intent updates
148 * to prevent restarting the intent & possibly altering UI state in the activity in front of
149 * the user.
150 *
151 * Once the bubble is overflowed, the activity is finished and updates to the
152 * notification are respected. Typically an update to an overflowed bubble would result in
153 * that bubble being added back to the stack anyways.
154 */
155 @Nullable
156 private PendingIntent mIntent;
157 private boolean mIntentActive;
158 @Nullable
159 private PendingIntent.CancelListener mIntentCancelListener;
160
161 /**
162 * Sent when the bubble & notification are no longer visible to the user (i.e. no
163 * notification in the shade, no bubble in the stack or overflow).
164 */
165 @Nullable
166 private PendingIntent mDeleteIntent;
167
168 /**
169 * Used only for a special bubble in the stack that has the key {@link #KEY_APP_BUBBLE}.
170 * There can only be one of these bubbles in the stack and this intent will be populated for
171 * that bubble.
172 */
173 @Nullable
174 private Intent mAppIntent;
175
176 /**
177 * Create a bubble with limited information based on given {@link ShortcutInfo}.
178 * Note: Currently this is only being used when the bubble is persisted to disk.
179 */
180 @VisibleForTesting(visibility = PRIVATE)
181 public Bubble(@NonNull final String key, @NonNull final ShortcutInfo shortcutInfo,
182 final int desiredHeight, final int desiredHeightResId, @Nullable final String title,
183 int taskId, @Nullable final String locus, Executor mainExecutor,
184 final Bubbles.BubbleMetadataFlagListener listener) {
185 Objects.requireNonNull(key);
186 Objects.requireNonNull(shortcutInfo);
187 mMetadataShortcutId = shortcutInfo.getId();
188 mShortcutInfo = shortcutInfo;
189 mKey = key;
190 mGroupKey = null;
191 mLocusId = locus != null ? new LocusId(locus) : null;
192 mFlags = 0;
193 mUser = shortcutInfo.getUserHandle();
194 mPackageName = shortcutInfo.getPackage();
195 mIcon = shortcutInfo.getIcon();
196 mDesiredHeight = desiredHeight;
197 mDesiredHeightResId = desiredHeightResId;
198 mTitle = title;
199 mShowBubbleUpdateDot = false;
200 mMainExecutor = mainExecutor;
201 mTaskId = taskId;
202 mBubbleMetadataFlagListener = listener;
203 }
204
205 public Bubble(Intent intent,
206 UserHandle user,
207 Executor mainExecutor) {
208 mKey = KEY_APP_BUBBLE;
209 mGroupKey = null;
210 mLocusId = null;
211 mFlags = 0;
212 mUser = user;
213 mShowBubbleUpdateDot = false;
214 mMainExecutor = mainExecutor;
215 mTaskId = INVALID_TASK_ID;
216 mAppIntent = intent;
217 mDesiredHeight = Integer.MAX_VALUE;
218 mPackageName = intent.getPackage();
219 }
220
221 @VisibleForTesting(visibility = PRIVATE)
222 public Bubble(@NonNull final BubbleEntry entry,
223 final Bubbles.BubbleMetadataFlagListener listener,
224 final Bubbles.PendingIntentCanceledListener intentCancelListener,
225 Executor mainExecutor) {
226 mKey = entry.getKey();
227 mGroupKey = entry.getGroupKey();
228 mLocusId = entry.getLocusId();
229 mBubbleMetadataFlagListener = listener;
230 mIntentCancelListener = intent -> {
231 if (mIntent != null) {
232 mIntent.unregisterCancelListener(mIntentCancelListener);
233 }
234 mainExecutor.execute(() -> {
235 intentCancelListener.onPendingIntentCanceled(this);
236 });
237 };
238 mMainExecutor = mainExecutor;
239 mTaskId = INVALID_TASK_ID;
240 setEntry(entry);
241 }
242
243 @Override
244 public String getKey() {
245 return mKey;
246 }
247
248 /**
249 * @see StatusBarNotification#getGroupKey()
250 * @return the group key for this bubble, if one exists.
251 */
252 public String getGroupKey() {
253 return mGroupKey;
254 }
255
256 public LocusId getLocusId() {
257 return mLocusId;
258 }
259
260 public UserHandle getUser() {
261 return mUser;
262 }
263
264 @NonNull
265 public String getPackageName() {
266 return mPackageName;
267 }
268
269 @Override
270 public Bitmap getBubbleIcon() {
271 return mBubbleBitmap;
272 }
273
274 @Override
275 public Bitmap getAppBadge() {
276 return mBadgeBitmap;
277 }
278
279 @Override
280 public Bitmap getRawAppBadge() {
281 return mRawBadgeBitmap;
282 }
283
284 @Override
285 public int getDotColor() {
286 return mDotColor;
287 }
288
289 @Override
290 public Path getDotPath() {
291 return mDotPath;
292 }
293
294 @Nullable
295 public String getAppName() {
296 return mAppName;
297 }
298
299 @Nullable
300 public ShortcutInfo getShortcutInfo() {
301 return mShortcutInfo;
302 }
303
304 @Nullable
305 @Override
306 public BadgedImageView getIconView() {
307 return mIconView;
308 }
309
310 @Override
311 @Nullable
312 public BubbleExpandedView getExpandedView() {
313 return mExpandedView;
314 }
315
316 @Nullable
317 public String getTitle() {
318 return mTitle;
319 }
320
321 /**
322 * @return the ShortcutInfo id if it exists, or the metadata shortcut id otherwise.
323 */
324 String getShortcutId() {
325 return getShortcutInfo() != null
326 ? getShortcutInfo().getId()
327 : getMetadataShortcutId();
328 }
329
330 String getMetadataShortcutId() {
331 return mMetadataShortcutId;
332 }
333
334 boolean hasMetadataShortcutId() {
335 return (mMetadataShortcutId != null && !mMetadataShortcutId.isEmpty());
336 }
337
338 /**
339 * Call this to clean up the task for the bubble. Ensure this is always called when done with
340 * the bubble.
341 */
342 void cleanupExpandedView() {
343 if (mExpandedView != null) {
344 mExpandedView.cleanUpExpandedState();
345 mExpandedView = null;
346 }
347 if (mIntent != null) {
348 mIntent.unregisterCancelListener(mIntentCancelListener);
349 }
350 mIntentActive = false;
351 }
352
353 /**
354 * Call when all the views should be removed/cleaned up.
355 */
356 void cleanupViews() {
357 cleanupExpandedView();
358 mIconView = null;
359 }
360
361 void setPendingIntentCanceled() {
362 mPendingIntentCanceled = true;
363 }
364
365 boolean getPendingIntentCanceled() {
366 return mPendingIntentCanceled;
367 }
368
369 /**
370 * Sets whether to perform inflation on the same thread as the caller. This method should only
371 * be used in tests, not in production.
372 */
373 @VisibleForTesting
374 void setInflateSynchronously(boolean inflateSynchronously) {
375 mInflateSynchronously = inflateSynchronously;
376 }
377
378 /**
379 * Sets whether this bubble is considered text changed. This method is purely for
380 * testing.
381 */
382 @VisibleForTesting
383 void setTextChangedForTest(boolean textChanged) {
384 mIsTextChanged = textChanged;
385 }
386
387 /**
388 * Starts a task to inflate & load any necessary information to display a bubble.
389 *
390 * @param callback the callback to notify one the bubble is ready to be displayed.
391 * @param context the context for the bubble.
392 * @param controller the bubble controller.
393 * @param stackView the stackView the bubble is eventually added to.
394 * @param iconFactory the icon factory use to create images for the bubble.
395 * @param badgeIconFactory the icon factory to create app badges for the bubble.
396 */
397 void inflate(BubbleViewInfoTask.Callback callback,
398 Context context,
399 BubbleController controller,
400 BubbleStackView stackView,
401 BubbleIconFactory iconFactory,
402 BubbleBadgeIconFactory badgeIconFactory,
403 boolean skipInflation) {
404 if (isBubbleLoading()) {
405 mInflationTask.cancel(true /* mayInterruptIfRunning */);
406 }
407 mInflationTask = new BubbleViewInfoTask(this,
408 context,
409 controller,
410 stackView,
411 iconFactory,
412 badgeIconFactory,
413 skipInflation,
414 callback,
415 mMainExecutor);
416 if (mInflateSynchronously) {
417 mInflationTask.onPostExecute(mInflationTask.doInBackground());
418 } else {
419 mInflationTask.execute();
420 }
421 }
422
423 private boolean isBubbleLoading() {
424 return mInflationTask != null && mInflationTask.getStatus() != FINISHED;
425 }
426
427 boolean isInflated() {
428 return mIconView != null && mExpandedView != null;
429 }
430
431 void stopInflation() {
432 if (mInflationTask == null) {
433 return;
434 }
435 mInflationTask.cancel(true /* mayInterruptIfRunning */);
436 }
437
438 void setViewInfo(BubbleViewInfoTask.BubbleViewInfo info) {
439 if (!isInflated()) {
440 mIconView = info.imageView;
441 mExpandedView = info.expandedView;
442 }
443
444 mShortcutInfo = info.shortcutInfo;
445 mAppName = info.appName;
446 if (mTitle == null) {
447 mTitle = mAppName;
448 }
449 mFlyoutMessage = info.flyoutMessage;
450
451 mBadgeBitmap = info.badgeBitmap;
452 mRawBadgeBitmap = info.mRawBadgeBitmap;
453 mBubbleBitmap = info.bubbleBitmap;
454
455 mDotColor = info.dotColor;
456 mDotPath = info.dotPath;
457
458 if (mExpandedView != null) {
459 mExpandedView.update(this /* bubble */);
460 }
461 if (mIconView != null) {
462 mIconView.setRenderedBubble(this /* bubble */);
463 }
464 }
465
466 /**
467 * Set visibility of bubble in the expanded state.
468 *
469 * @param visibility {@code true} if the expanded bubble should be visible on the screen.
470 *
471 * Note that this contents visibility doesn't affect visibility at {@link android.view.View},
472 * and setting {@code false} actually means rendering the expanded view in transparent.
473 */
474 @Override
475 public void setTaskViewVisibility(boolean visibility) {
476 if (mExpandedView != null) {
477 mExpandedView.setContentVisibility(visibility);
478 }
479 }
480
481 /**
482 * Sets the entry associated with this bubble.
483 */
484 void setEntry(@NonNull final BubbleEntry entry) {
485 Objects.requireNonNull(entry);
486 boolean showingDotPreviously = showDot();
487 mLastUpdated = entry.getStatusBarNotification().getPostTime();
488 mIsBubble = entry.getStatusBarNotification().getNotification().isBubbleNotification();
489 mPackageName = entry.getStatusBarNotification().getPackageName();
490 mUser = entry.getStatusBarNotification().getUser();
491 mTitle = getTitle(entry);
492 mChannelId = entry.getStatusBarNotification().getNotification().getChannelId();
493 mNotificationId = entry.getStatusBarNotification().getId();
494 mAppUid = entry.getStatusBarNotification().getUid();
495 mInstanceId = entry.getStatusBarNotification().getInstanceId();
496 mFlyoutMessage = extractFlyoutMessage(entry);
497 if (entry.getRanking() != null) {
498 mShortcutInfo = entry.getRanking().getConversationShortcutInfo();
499 mIsTextChanged = entry.getRanking().isTextChanged();
500 if (entry.getRanking().getChannel() != null) {
501 mIsImportantConversation =
502 entry.getRanking().getChannel().isImportantConversation();
503 }
504 }
505 if (entry.getBubbleMetadata() != null) {
506 mMetadataShortcutId = entry.getBubbleMetadata().getShortcutId();
507 mFlags = entry.getBubbleMetadata().getFlags();
508 mDesiredHeight = entry.getBubbleMetadata().getDesiredHeight();
509 mDesiredHeightResId = entry.getBubbleMetadata().getDesiredHeightResId();
510 mIcon = entry.getBubbleMetadata().getIcon();
511
512 if (!mIntentActive || mIntent == null) {
513 if (mIntent != null) {
514 mIntent.unregisterCancelListener(mIntentCancelListener);
515 }
516 mIntent = entry.getBubbleMetadata().getIntent();
517 if (mIntent != null) {
518 mIntent.registerCancelListener(mIntentCancelListener);
519 }
520 } else if (mIntent != null && entry.getBubbleMetadata().getIntent() == null) {
521 // Was an intent bubble now it's a shortcut bubble... still unregister the listener
522 mIntent.unregisterCancelListener(mIntentCancelListener);
523 mIntentActive = false;
524 mIntent = null;
525 }
526 mDeleteIntent = entry.getBubbleMetadata().getDeleteIntent();
527 }
528
529 mIsClearable = entry.isClearable();
530 mShouldSuppressNotificationDot = entry.shouldSuppressNotificationDot();
531 mShouldSuppressNotificationList = entry.shouldSuppressNotificationList();
532 mShouldSuppressPeek = entry.shouldSuppressPeek();
533 if (showingDotPreviously != showDot()) {
534 // This will update the UI if needed
535 setShowDot(showDot());
536 }
537 }
538
539 @Nullable
540 Icon getIcon() {
541 return mIcon;
542 }
543
544 boolean isTextChanged() {
545 return mIsTextChanged;
546 }
547
548 /**
549 * @return the last time this bubble was updated or accessed, whichever is most recent.
550 */
551 long getLastActivity() {
552 return isAppBubble() ? Long.MAX_VALUE : Math.max(mLastUpdated, mLastAccessed);
553 }
554
555 /**
556 * Sets if the intent used for this bubble is currently active (i.e. populating an
557 * expanded view, expanded or not).
558 */
559 void setIntentActive() {
560 mIntentActive = true;
561 }
562
563 boolean isIntentActive() {
564 return mIntentActive;
565 }
566
567 public InstanceId getInstanceId() {
568 return mInstanceId;
569 }
570
571 @Nullable
572 public String getChannelId() {
573 return mChannelId;
574 }
575
576 public int getNotificationId() {
577 return mNotificationId;
578 }
579
580 /**
581 * @return the task id of the task in which bubble contents is drawn.
582 */
583 @Override
584 public int getTaskId() {
585 return mExpandedView != null ? mExpandedView.getTaskId() : mTaskId;
586 }
587
588 /**
589 * Should be invoked whenever a Bubble is accessed (selected while expanded).
590 */
591 void markAsAccessedAt(long lastAccessedMillis) {
592 mLastAccessed = lastAccessedMillis;
593 setSuppressNotification(true);
594 setShowDot(false /* show */);
595 }
596
597 /**
598 * Should be invoked whenever a Bubble is promoted from overflow.
599 */
600 void markUpdatedAt(long lastAccessedMillis) {
601 mLastUpdated = lastAccessedMillis;
602 }
603
604 /**
605 * Whether this notification should be shown in the shade.
606 */
607 boolean showInShade() {
608 return !shouldSuppressNotification() || !mIsClearable;
609 }
610
611 /**
612 * Whether this bubble is currently being hidden from the stack.
613 */
614 boolean isSuppressed() {
615 return (mFlags & Notification.BubbleMetadata.FLAG_SUPPRESS_BUBBLE) != 0;
616 }
617
618 /**
619 * Whether this bubble is able to be suppressed (i.e. has the developer opted into the API to
620 * hide the bubble when in the same content).
621 */
622 boolean isSuppressable() {
623 return (mFlags & Notification.BubbleMetadata.FLAG_SUPPRESSABLE_BUBBLE) != 0;
624 }
625
626 /**
627 * Whether this notification conversation is important.
628 */
629 boolean isImportantConversation() {
630 return mIsImportantConversation;
631 }
632
633 /**
634 * Sets whether this notification should be suppressed in the shade.
635 */
636 @VisibleForTesting
637 public void setSuppressNotification(boolean suppressNotification) {
638 boolean prevShowInShade = showInShade();
639 if (suppressNotification) {
640 mFlags |= Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
641 } else {
642 mFlags &= ~Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION;
643 }
644
645 if (showInShade() != prevShowInShade && mBubbleMetadataFlagListener != null) {
646 mBubbleMetadataFlagListener.onBubbleMetadataFlagChanged(this);
647 }
648 }
649
650 /**
651 * Sets whether this bubble should be suppressed from the stack.
652 */
653 public void setSuppressBubble(boolean suppressBubble) {
654 if (!isSuppressable()) {
655 Log.e(TAG, "calling setSuppressBubble on "
656 + getKey() + " when bubble not suppressable");
657 return;
658 }
659 boolean prevSuppressed = isSuppressed();
660 if (suppressBubble) {
661 mFlags |= Notification.BubbleMetadata.FLAG_SUPPRESS_BUBBLE;
662 } else {
663 mFlags &= ~Notification.BubbleMetadata.FLAG_SUPPRESS_BUBBLE;
664 }
665 if (prevSuppressed != suppressBubble && mBubbleMetadataFlagListener != null) {
666 mBubbleMetadataFlagListener.onBubbleMetadataFlagChanged(this);
667 }
668 }
669
670 /**
671 * Sets whether the bubble for this notification should show a dot indicating updated content.
672 */
673 void setShowDot(boolean showDot) {
674 mShowBubbleUpdateDot = showDot;
675
676 if (mIconView != null) {
677 mIconView.updateDotVisibility(true /* animate */);
678 }
679 }
680
681 /**
682 * Whether the bubble for this notification should show a dot indicating updated content.
683 */
684 @Override
685 public boolean showDot() {
686 return mShowBubbleUpdateDot
687 && !mShouldSuppressNotificationDot
688 && !shouldSuppressNotification();
689 }
690
691 /**
692 * Whether the flyout for the bubble should be shown.
693 */
694 @VisibleForTesting
695 public boolean showFlyout() {
696 return !mSuppressFlyout && !mShouldSuppressPeek
697 && !shouldSuppressNotification()
698 && !mShouldSuppressNotificationList;
699 }
700
701 /**
702 * Set whether the flyout text for the bubble should be shown when an update is received.
703 *
704 * @param suppressFlyout whether the flyout text is shown
705 */
706 void setSuppressFlyout(boolean suppressFlyout) {
707 mSuppressFlyout = suppressFlyout;
708 }
709
710 FlyoutMessage getFlyoutMessage() {
711 return mFlyoutMessage;
712 }
713
714 int getRawDesiredHeight() {
715 return mDesiredHeight;
716 }
717
718 int getRawDesiredHeightResId() {
719 return mDesiredHeightResId;
720 }
721
722 float getDesiredHeight(Context context) {
723 boolean useRes = mDesiredHeightResId != 0;
724 if (useRes) {
725 return getDimenForPackageUser(context, mDesiredHeightResId, mPackageName,
726 mUser.getIdentifier());
727 } else {
728 return mDesiredHeight * context.getResources().getDisplayMetrics().density;
729 }
730 }
731
732 String getDesiredHeightString() {
733 boolean useRes = mDesiredHeightResId != 0;
734 if (useRes) {
735 return String.valueOf(mDesiredHeightResId);
736 } else {
737 return String.valueOf(mDesiredHeight);
738 }
739 }
740
741 @Nullable
742 PendingIntent getBubbleIntent() {
743 return mIntent;
744 }
745
746 @Nullable
747 PendingIntent getDeleteIntent() {
748 return mDeleteIntent;
749 }
750
751 @Nullable
752 Intent getAppBubbleIntent() {
753 return mAppIntent;
754 }
755
756 boolean isAppBubble() {
757 return KEY_APP_BUBBLE.equals(mKey);
758 }
759
760 Intent getSettingsIntent(final Context context) {
761 final Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_BUBBLE_SETTINGS);
762 intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
763 final int uid = getUid(context);
764 if (uid != -1) {
765 intent.putExtra(Settings.EXTRA_APP_UID, uid);
766 }
767 intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
768 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
769 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
770 return intent;
771 }
772
773 public int getAppUid() {
774 return mAppUid;
775 }
776
777 private int getUid(final Context context) {
778 if (mAppUid != -1) return mAppUid;
779 final PackageManager pm = BubbleController.getPackageManagerForUser(context,
780 mUser.getIdentifier());
781 if (pm == null) return -1;
782 try {
783 final ApplicationInfo info = pm.getApplicationInfo(mShortcutInfo.getPackage(), 0);
784 return info.uid;
785 } catch (PackageManager.NameNotFoundException e) {
786 Log.e(TAG, "cannot find uid", e);
787 }
788 return -1;
789 }
790
791 private int getDimenForPackageUser(Context context, int resId, String pkg, int userId) {
792 Resources r;
793 if (pkg != null) {
794 try {
795 if (userId == UserHandle.USER_ALL) {
796 userId = UserHandle.USER_SYSTEM;
797 }
798 r = context.createContextAsUser(UserHandle.of(userId), /* flags */ 0)
799 .getPackageManager().getResourcesForApplication(pkg);
800 return r.getDimensionPixelSize(resId);
801 } catch (PackageManager.NameNotFoundException ex) {
802 // Uninstalled, don't care
803 } catch (Resources.NotFoundException e) {
804 // Invalid res id, return 0 and user our default
805 Log.e(TAG, "Couldn't find desired height res id", e);
806 }
807 }
808 return 0;
809 }
810
811 private boolean shouldSuppressNotification() {
812 return isEnabled(Notification.BubbleMetadata.FLAG_SUPPRESS_NOTIFICATION);
813 }
814
815 public boolean shouldAutoExpand() {
816 return isEnabled(Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE);
817 }
818
819 @VisibleForTesting
820 public void setShouldAutoExpand(boolean shouldAutoExpand) {
821 boolean prevAutoExpand = shouldAutoExpand();
822 if (shouldAutoExpand) {
823 enable(Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE);
824 } else {
825 disable(Notification.BubbleMetadata.FLAG_AUTO_EXPAND_BUBBLE);
826 }
827 if (prevAutoExpand != shouldAutoExpand && mBubbleMetadataFlagListener != null) {
828 mBubbleMetadataFlagListener.onBubbleMetadataFlagChanged(this);
829 }
830 }
831
832 public void setIsBubble(final boolean isBubble) {
833 mIsBubble = isBubble;
834 }
835
836 public boolean isBubble() {
837 return mIsBubble;
838 }
839
840 public void enable(int option) {
841 mFlags |= option;
842 }
843
844 public void disable(int option) {
845 mFlags &= ~option;
846 }
847
848 public boolean isEnabled(int option) {
849 return (mFlags & option) != 0;
850 }
851
852 public int getFlags() {
853 return mFlags;
854 }
855
856 @Override
857 public String toString() {
858 return "Bubble{" + mKey + '}';
859 }
860
861 /**
862 * Description of current bubble state.
863 */
864 public void dump(@NonNull PrintWriter pw) {
865 pw.print("key: "); pw.println(mKey);
866 pw.print(" showInShade: "); pw.println(showInShade());
867 pw.print(" showDot: "); pw.println(showDot());
868 pw.print(" showFlyout: "); pw.println(showFlyout());
869 pw.print(" lastActivity: "); pw.println(getLastActivity());
870 pw.print(" desiredHeight: "); pw.println(getDesiredHeightString());
871 pw.print(" suppressNotif: "); pw.println(shouldSuppressNotification());
872 pw.print(" autoExpand: "); pw.println(shouldAutoExpand());
873 pw.print(" isClearable: "); pw.println(mIsClearable);
874 pw.println(" bubbleMetadataFlagListener null: " + (mBubbleMetadataFlagListener == null));
875 if (mExpandedView != null) {
876 mExpandedView.dump(pw);
877 }
878 }
879
880 @Override
881 public boolean equals(Object o) {
882 if (this == o) return true;
883 if (!(o instanceof Bubble)) return false;
884 Bubble bubble = (Bubble) o;
885 return Objects.equals(mKey, bubble.mKey);
886 }
887
888 @Override
889 public int hashCode() {
890 return Objects.hash(mKey);
891 }
892
893 @Nullable
894 private static String getTitle(@NonNull final BubbleEntry e) {
895 final CharSequence titleCharSeq = e.getStatusBarNotification()
896 .getNotification().extras.getCharSequence(Notification.EXTRA_TITLE);
897 return titleCharSeq == null ? null : titleCharSeq.toString();
898 }
899
900 /**
901 * Returns our best guess for the most relevant text summary of the latest update to this
902 * notification, based on its type. Returns null if there should not be an update message.
903 */
904 @NonNull
905 static Bubble.FlyoutMessage extractFlyoutMessage(BubbleEntry entry) {
906 Objects.requireNonNull(entry);
907 final Notification underlyingNotif = entry.getStatusBarNotification().getNotification();
908 final Class<? extends Notification.Style> style = underlyingNotif.getNotificationStyle();
909
910 Bubble.FlyoutMessage bubbleMessage = new Bubble.FlyoutMessage();
911 bubbleMessage.isGroupChat = underlyingNotif.extras.getBoolean(
912 Notification.EXTRA_IS_GROUP_CONVERSATION);
913 try {
914 if (Notification.BigTextStyle.class.equals(style)) {
915 // Return the big text, it is big so probably important. If it's not there use the
916 // normal text.
917 CharSequence bigText =
918 underlyingNotif.extras.getCharSequence(Notification.EXTRA_BIG_TEXT);
919 bubbleMessage.message = !TextUtils.isEmpty(bigText)
920 ? bigText
921 : underlyingNotif.extras.getCharSequence(Notification.EXTRA_TEXT);
922 return bubbleMessage;
923 } else if (Notification.MessagingStyle.class.equals(style)) {
924 final List<Notification.MessagingStyle.Message> messages =
925 Notification.MessagingStyle.Message.getMessagesFromBundleArray(
926 (Parcelable[]) underlyingNotif.extras.get(
927 Notification.EXTRA_MESSAGES));
928
929 final Notification.MessagingStyle.Message latestMessage =
930 Notification.MessagingStyle.findLatestIncomingMessage(messages);
931 if (latestMessage != null) {
932 bubbleMessage.message = latestMessage.getText();
933 Person sender = latestMessage.getSenderPerson();
934 bubbleMessage.senderName = sender != null ? sender.getName() : null;
935 bubbleMessage.senderAvatar = null;
936 bubbleMessage.senderIcon = sender != null ? sender.getIcon() : null;
937 return bubbleMessage;
938 }
939 } else if (Notification.InboxStyle.class.equals(style)) {
940 CharSequence[] lines =
941 underlyingNotif.extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
942
943 // Return the last line since it should be the most recent.
944 if (lines != null && lines.length > 0) {
945 bubbleMessage.message = lines[lines.length - 1];
946 return bubbleMessage;
947 }
948 } else if (Notification.MediaStyle.class.equals(style)) {
949 // Return nothing, media updates aren't typically useful as a text update.
950 return bubbleMessage;
951 } else {
952 // Default to text extra.
953 bubbleMessage.message =
954 underlyingNotif.extras.getCharSequence(Notification.EXTRA_TEXT);
955 return bubbleMessage;
956 }
957 } catch (ClassCastException | NullPointerException | ArrayIndexOutOfBoundsException e) {
958 // No use crashing, we'll just return null and the caller will assume there's no update
959 // message.
960 e.printStackTrace();
961 }
962
963 return bubbleMessage;
964 }
965 }