2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher3.notification;
19 import android.animation.Animator;
20 import android.app.Notification;
21 import android.content.Context;
22 import android.graphics.Rect;
23 import android.support.annotation.Nullable;
24 import android.support.v4.content.ContextCompat;
25 import android.util.AttributeSet;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.widget.FrameLayout;
29 import android.widget.TextView;
31 import com.android.launcher3.ItemInfo;
32 import com.android.launcher3.R;
33 import com.android.launcher3.anim.PillHeightRevealOutlineProvider;
34 import com.android.launcher3.graphics.IconPalette;
35 import com.android.launcher3.logging.UserEventDispatcher.LogContainerProvider;
36 import com.android.launcher3.popup.PopupItemView;
37 import com.android.launcher3.userevent.nano.LauncherLogProto;
39 import java.util.List;
42 * A {@link FrameLayout} that contains a header, main view and a footer.
43 * The main view contains the icon and text (title + subtext) of the first notification.
44 * The footer contains: A list of just the icons of all the notifications past the first one.
45 * @see NotificationFooterLayout
47 public class NotificationItemView extends PopupItemView implements LogContainerProvider {
49 private static final Rect sTempRect = new Rect();
51 private TextView mHeaderCount;
52 private NotificationMainView mMainView;
53 private NotificationFooterLayout mFooter;
54 private SwipeHelper mSwipeHelper;
55 private boolean mAnimatingNextIcon;
56 private int mNotificationHeaderTextColor = Notification.COLOR_DEFAULT;
58 public NotificationItemView(Context context) {
59 this(context, null, 0);
62 public NotificationItemView(Context context, AttributeSet attrs) {
63 this(context, attrs, 0);
66 public NotificationItemView(Context context, AttributeSet attrs, int defStyle) {
67 super(context, attrs, defStyle);
71 protected void onFinishInflate() {
72 super.onFinishInflate();
73 mHeaderCount = (TextView) findViewById(R.id.notification_count);
74 mMainView = (NotificationMainView) findViewById(R.id.main_view);
75 mFooter = (NotificationFooterLayout) findViewById(R.id.footer);
76 mSwipeHelper = new SwipeHelper(SwipeHelper.X, mMainView, getContext());
77 mSwipeHelper.setDisableHardwareLayers(true);
80 public int getHeightMinusFooter() {
81 int footerHeight = mFooter.getParent() == null ? 0 : mFooter.getHeight();
82 return getHeight() - footerHeight;
85 public Animator animateHeightRemoval(int heightToRemove) {
86 final int newHeight = getHeight() - heightToRemove;
87 return new PillHeightRevealOutlineProvider(mPillRect,
88 getBackgroundRadius(), newHeight).createRevealAnimator(this, true /* isReversed */);
91 public void updateHeader(int notificationCount, @Nullable IconPalette palette) {
92 mHeaderCount.setText(notificationCount <= 1 ? "" : String.valueOf(notificationCount));
93 if (palette != null) {
94 if (mNotificationHeaderTextColor == Notification.COLOR_DEFAULT) {
95 mNotificationHeaderTextColor =
96 IconPalette.resolveContrastColor(getContext(), palette.dominantColor,
97 getResources().getColor(R.color.popup_header_background_color));
99 mHeaderCount.setTextColor(mNotificationHeaderTextColor);
104 public boolean onInterceptTouchEvent(MotionEvent ev) {
105 if (mMainView.getNotificationInfo() == null) {
106 // The notification hasn't been populated yet.
109 getParent().requestDisallowInterceptTouchEvent(true);
110 return mSwipeHelper.onInterceptTouchEvent(ev);
114 public boolean onTouchEvent(MotionEvent ev) {
115 if (mMainView.getNotificationInfo() == null) {
116 // The notification hasn't been populated yet.
119 return mSwipeHelper.onTouchEvent(ev) || super.onTouchEvent(ev);
122 public void applyNotificationInfos(final List<NotificationInfo> notificationInfos) {
123 if (notificationInfos.isEmpty()) {
127 NotificationInfo mainNotification = notificationInfos.get(0);
128 mMainView.applyNotificationInfo(mainNotification, mIconView);
130 for (int i = 1; i < notificationInfos.size(); i++) {
131 mFooter.addNotificationInfo(notificationInfos.get(i));
133 mFooter.commitNotificationInfos();
136 public void trimNotifications(final List<String> notificationKeys) {
137 boolean dismissedMainNotification = !notificationKeys.contains(
138 mMainView.getNotificationInfo().notificationKey);
139 if (dismissedMainNotification && !mAnimatingNextIcon) {
140 // Animate the next icon into place as the new main notification.
141 mAnimatingNextIcon = true;
142 mMainView.setVisibility(INVISIBLE);
143 mMainView.setTranslationX(0);
144 mIconView.getGlobalVisibleRect(sTempRect);
145 mFooter.animateFirstNotificationTo(sTempRect,
146 new NotificationFooterLayout.IconAnimationEndListener() {
148 public void onIconAnimationEnd(NotificationInfo newMainNotification) {
149 if (newMainNotification != null) {
150 mMainView.applyNotificationInfo(newMainNotification, mIconView, true);
151 mMainView.setVisibility(VISIBLE);
153 mAnimatingNextIcon = false;
157 mFooter.trimNotifications(notificationKeys);
162 public int getArrowColor(boolean isArrowAttachedToBottom) {
163 return ContextCompat.getColor(getContext(), isArrowAttachedToBottom
164 ? R.color.popup_background_color
165 : R.color.popup_header_background_color);
169 public void fillInLogContainerData(View v, ItemInfo info, LauncherLogProto.Target target,
170 LauncherLogProto.Target targetParent) {
171 target.itemType = LauncherLogProto.ItemType.NOTIFICATION;
172 targetParent.containerType = LauncherLogProto.ContainerType.DEEPSHORTCUTS;