OSDN Git Service

Visual updates for popup
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / notification / NotificationItemView.java
1 /*
2  * Copyright (C) 2017 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
17 package com.android.launcher3.notification;
18
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;
30
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;
38
39 import java.util.List;
40
41 /**
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
46  */
47 public class NotificationItemView extends PopupItemView implements LogContainerProvider {
48
49     private static final Rect sTempRect = new Rect();
50
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;
57
58     public NotificationItemView(Context context) {
59         this(context, null, 0);
60     }
61
62     public NotificationItemView(Context context, AttributeSet attrs) {
63         this(context, attrs, 0);
64     }
65
66     public NotificationItemView(Context context, AttributeSet attrs, int defStyle) {
67         super(context, attrs, defStyle);
68     }
69
70     @Override
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);
78     }
79
80     public int getHeightMinusFooter() {
81         int footerHeight = mFooter.getParent() == null ? 0 : mFooter.getHeight();
82         return getHeight() - footerHeight;
83     }
84
85     public Animator animateHeightRemoval(int heightToRemove) {
86         final int newHeight = getHeight() - heightToRemove;
87         return new PillHeightRevealOutlineProvider(mPillRect,
88                 getBackgroundRadius(), newHeight).createRevealAnimator(this, true /* isReversed */);
89     }
90
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));
98             }
99             mHeaderCount.setTextColor(mNotificationHeaderTextColor);
100         }
101     }
102
103     @Override
104     public boolean onInterceptTouchEvent(MotionEvent ev) {
105         if (mMainView.getNotificationInfo() == null) {
106             // The notification hasn't been populated yet.
107             return false;
108         }
109         getParent().requestDisallowInterceptTouchEvent(true);
110         return mSwipeHelper.onInterceptTouchEvent(ev);
111     }
112
113     @Override
114     public boolean onTouchEvent(MotionEvent ev) {
115         if (mMainView.getNotificationInfo() == null) {
116             // The notification hasn't been populated yet.
117             return false;
118         }
119         return mSwipeHelper.onTouchEvent(ev) || super.onTouchEvent(ev);
120     }
121
122     public void applyNotificationInfos(final List<NotificationInfo> notificationInfos) {
123         if (notificationInfos.isEmpty()) {
124             return;
125         }
126
127         NotificationInfo mainNotification = notificationInfos.get(0);
128         mMainView.applyNotificationInfo(mainNotification, mIconView);
129
130         for (int i = 1; i < notificationInfos.size(); i++) {
131             mFooter.addNotificationInfo(notificationInfos.get(i));
132         }
133         mFooter.commitNotificationInfos();
134     }
135
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() {
147                 @Override
148                 public void onIconAnimationEnd(NotificationInfo newMainNotification) {
149                     if (newMainNotification != null) {
150                         mMainView.applyNotificationInfo(newMainNotification, mIconView, true);
151                         mMainView.setVisibility(VISIBLE);
152                     }
153                     mAnimatingNextIcon = false;
154                 }
155             });
156         } else {
157             mFooter.trimNotifications(notificationKeys);
158         }
159     }
160
161     @Override
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);
166     }
167
168     @Override
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;
173     }
174 }