Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 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.systemui.bubbles; |
| 17 | |
| 18 | |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 19 | import static android.view.Display.INVALID_DISPLAY; |
| 20 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 21 | import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
| 22 | |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.PackageManager; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 26 | import android.os.UserHandle; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 29 | import com.android.internal.annotations.VisibleForTesting; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 30 | import com.android.systemui.R; |
| 31 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
| 32 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 33 | import java.util.Objects; |
| 34 | |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Encapsulates the data and UI elements of a bubble. |
| 37 | */ |
| 38 | class Bubble { |
| 39 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 40 | private static final boolean DEBUG = false; |
| 41 | private static final String TAG = "Bubble"; |
| 42 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 43 | private final String mKey; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 44 | private final String mGroupId; |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 45 | private String mAppName; |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 46 | private final BubbleExpandedView.OnBubbleBlockedListener mListener; |
| 47 | |
| 48 | private boolean mInflated; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 49 | public NotificationEntry entry; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 50 | BubbleView iconView; |
| 51 | BubbleExpandedView expandedView; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 52 | private long mLastUpdated; |
| 53 | private long mLastAccessed; |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 54 | private PackageManager mPm; |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 55 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 56 | public static String groupId(NotificationEntry entry) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 57 | UserHandle user = entry.notification.getUser(); |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 58 | return user.getIdentifier() + "|" + entry.notification.getPackageName(); |
| 59 | } |
| 60 | |
| 61 | /** Used in tests when no UI is required. */ |
| 62 | @VisibleForTesting(visibility = PRIVATE) |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 63 | Bubble(Context context, NotificationEntry e) { |
| 64 | this (context, e, null); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 65 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 66 | |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 67 | Bubble(Context context, NotificationEntry e, |
| 68 | BubbleExpandedView.OnBubbleBlockedListener listener) { |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 69 | entry = e; |
| 70 | mKey = e.key; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 71 | mLastUpdated = e.notification.getPostTime(); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 72 | mGroupId = groupId(e); |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 73 | mListener = listener; |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 74 | |
| 75 | mPm = context.getPackageManager(); |
| 76 | ApplicationInfo info; |
| 77 | try { |
| 78 | info = mPm.getApplicationInfo( |
| 79 | entry.notification.getPackageName(), |
| 80 | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 81 | | PackageManager.MATCH_DISABLED_COMPONENTS |
| 82 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 83 | | PackageManager.MATCH_DIRECT_BOOT_AWARE); |
| 84 | if (info != null) { |
| 85 | mAppName = String.valueOf(mPm.getApplicationLabel(info)); |
| 86 | } |
| 87 | } catch (PackageManager.NameNotFoundException unused) { |
| 88 | mAppName = entry.notification.getPackageName(); |
| 89 | } |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 92 | public String getKey() { |
| 93 | return mKey; |
| 94 | } |
| 95 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 96 | public String getGroupId() { |
| 97 | return mGroupId; |
| 98 | } |
| 99 | |
| 100 | public String getPackageName() { |
| 101 | return entry.notification.getPackageName(); |
| 102 | } |
| 103 | |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 104 | public String getAppName() { |
| 105 | return mAppName; |
| 106 | } |
| 107 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 108 | boolean isInflated() { |
| 109 | return mInflated; |
| 110 | } |
| 111 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 112 | public void updateDotVisibility() { |
| 113 | if (iconView != null) { |
Joshua Tsuji | 6549e70 | 2019-05-02 13:13:16 -0400 | [diff] [blame] | 114 | iconView.updateDotVisibility(true /* animate */); |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 118 | void inflate(LayoutInflater inflater, BubbleStackView stackView) { |
| 119 | if (mInflated) { |
| 120 | return; |
| 121 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 122 | iconView = (BubbleView) inflater.inflate( |
| 123 | R.layout.bubble_view, stackView, false /* attachToRoot */); |
| 124 | iconView.setNotif(entry); |
| 125 | |
| 126 | expandedView = (BubbleExpandedView) inflater.inflate( |
| 127 | R.layout.bubble_expanded_view, stackView, false /* attachToRoot */); |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 128 | expandedView.setEntry(entry, stackView, mAppName); |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 129 | expandedView.setOnBlockedListener(mListener); |
Lyn Han | 6c40fe7 | 2019-05-08 14:06:33 -0700 | [diff] [blame] | 130 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 131 | mInflated = true; |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 134 | /** |
| 135 | * Set visibility of bubble in the expanded state. |
| 136 | * |
| 137 | * @param visibility {@code true} if the expanded bubble should be visible on the screen. |
| 138 | * |
| 139 | * Note that this contents visibility doesn't affect visibility at {@link android.view.View}, |
| 140 | * and setting {@code false} actually means rendering the expanded view in transparent. |
| 141 | */ |
| 142 | void setContentVisibility(boolean visibility) { |
| 143 | if (expandedView != null) { |
| 144 | expandedView.setContentVisibility(visibility); |
| 145 | } |
| 146 | } |
| 147 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 148 | void setDismissed() { |
| 149 | entry.setBubbleDismissed(true); |
| 150 | // TODO: move this somewhere where it can be guaranteed not to run until safe from flicker |
| 151 | if (expandedView != null) { |
| 152 | expandedView.cleanUpExpandedState(); |
| 153 | } |
| 154 | } |
| 155 | |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 156 | void setEntry(NotificationEntry entry) { |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 157 | this.entry = entry; |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 158 | mLastUpdated = entry.notification.getPostTime(); |
Mark Renouf | 85e0a90 | 2019-04-05 15:51:51 -0400 | [diff] [blame] | 159 | if (mInflated) { |
| 160 | iconView.update(entry); |
| 161 | expandedView.update(entry); |
| 162 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 163 | } |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 164 | |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 165 | /** |
| 166 | * @return the newer of {@link #getLastUpdateTime()} and {@link #getLastAccessTime()} |
| 167 | */ |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 168 | public long getLastActivity() { |
| 169 | return Math.max(mLastUpdated, mLastAccessed); |
| 170 | } |
| 171 | |
| 172 | /** |
Mark Renouf | ba5ab51 | 2019-05-02 15:21:01 -0400 | [diff] [blame] | 173 | * @return the timestamp in milliseconds of the most recent notification entry for this bubble |
| 174 | */ |
| 175 | public long getLastUpdateTime() { |
| 176 | return mLastUpdated; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @return the timestamp in milliseconds when this bubble was last displayed in expanded state |
| 181 | */ |
| 182 | public long getLastAccessTime() { |
| 183 | return mLastAccessed; |
| 184 | } |
| 185 | |
| 186 | /** |
Issei Suzuki | cac2a50 | 2019-04-16 16:52:50 +0200 | [diff] [blame] | 187 | * @return the display id of the virtual display on which bubble contents is drawn. |
| 188 | */ |
| 189 | int getDisplayId() { |
| 190 | return expandedView != null ? expandedView.getVirtualDisplayId() : INVALID_DISPLAY; |
| 191 | } |
| 192 | |
| 193 | /** |
Mark Renouf | 9ba6cea | 2019-04-17 11:53:50 -0400 | [diff] [blame] | 194 | * Should be invoked whenever a Bubble is accessed (selected while expanded). |
| 195 | */ |
| 196 | void markAsAccessedAt(long lastAccessedMillis) { |
| 197 | mLastAccessed = lastAccessedMillis; |
| 198 | entry.setShowInShadeWhenBubble(false); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @return whether bubble is from a notification associated with a foreground service. |
| 203 | */ |
| 204 | public boolean isOngoing() { |
| 205 | return entry.isForegroundService(); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public String toString() { |
| 210 | return "Bubble{" + mKey + '}'; |
| 211 | } |
| 212 | |
Mark Renouf | 71a3af6 | 2019-04-08 15:02:54 -0400 | [diff] [blame] | 213 | @Override |
| 214 | public boolean equals(Object o) { |
| 215 | if (this == o) return true; |
| 216 | if (!(o instanceof Bubble)) return false; |
| 217 | Bubble bubble = (Bubble) o; |
| 218 | return Objects.equals(mKey, bubble.mKey); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public int hashCode() { |
| 223 | return Objects.hash(mKey); |
| 224 | } |
Mady Mellor | 3dff9e6 | 2019-02-05 18:12:53 -0800 | [diff] [blame] | 225 | } |