summaryrefslogtreecommitdiff
path: root/src/com/android/launcher3/notification/NotificationItemView.java
blob: af943a693337fe34e930d8024bc643e84f947b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher3.notification;

import android.animation.AnimatorSet;
import android.content.Context;
import android.graphics.Outline;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.ViewOutlineProvider;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.popup.PopupContainerWithArrow;
import com.android.launcher3.util.Themes;

import java.util.ArrayList;
import java.util.List;

/**
 * Utility class to manage notification UI
 */
public class NotificationItemView {

    private static final Rect sTempRect = new Rect();

    private final Context mContext;
    private final PopupContainerWithArrow mPopupContainer;
    private final ViewGroup mRootView;

    private final TextView mHeaderCount;
    private final NotificationMainView mMainView;

    private final View mHeader;

    private View mGutter;

    private boolean mIgnoreTouch = false;
    private List<NotificationInfo> mNotificationInfos = new ArrayList<>();

    public NotificationItemView(PopupContainerWithArrow container, ViewGroup rootView) {
        mPopupContainer = container;
        mRootView = rootView;
        mContext = container.getContext();

        mHeaderCount = container.findViewById(R.id.notification_count);
        mMainView = container.findViewById(R.id.main_view);

        mHeader = container.findViewById(R.id.header);

        float radius = Themes.getDialogCornerRadius(mContext);
        rootView.setClipToOutline(true);
        rootView.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
            }
        });
    }

    /**
     * Animates the background color to a new color.
     * @param color The color to change to.
     * @param animatorSetOut The AnimatorSet where we add the color animator to.
     */
    public void updateBackgroundColor(int color, AnimatorSet animatorSetOut) {
        mMainView.updateBackgroundColor(color, animatorSetOut);
    }

    public void addGutter() {
        if (mGutter == null) {
            mGutter = mPopupContainer.inflateAndAdd(R.layout.notification_gutter, mRootView);
        }
    }

    public void inverseGutterMargin() {
        MarginLayoutParams lp = (MarginLayoutParams) mGutter.getLayoutParams();
        int top = lp.topMargin;
        lp.topMargin = lp.bottomMargin;
        lp.bottomMargin = top;
    }

    public void removeAllViews() {
        mRootView.removeView(mMainView);
        mRootView.removeView(mHeader);
        if (mGutter != null) {
            mRootView.removeView(mGutter);
        }
    }

    /**
     * Updates the header text.
     * @param notificationCount The number of notifications.
     */
    public void updateHeader(int notificationCount) {
        final String text;
        final int visibility;
        if (notificationCount <= 1) {
            text = "";
            visibility = View.INVISIBLE;
        } else {
            text = String.valueOf(notificationCount);
            visibility = View.VISIBLE;

        }
        mHeaderCount.setText(text);
        mHeaderCount.setVisibility(visibility);
    }

    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            sTempRect.set(mRootView.getLeft(), mRootView.getTop(),
                    mRootView.getRight(), mRootView.getBottom());
            mIgnoreTouch = !sTempRect.contains((int) ev.getX(), (int) ev.getY());
            if (!mIgnoreTouch) {
                mPopupContainer.getParent().requestDisallowInterceptTouchEvent(true);
            }
        }
        if (mIgnoreTouch) {
            return false;
        }
        if (mMainView.getNotificationInfo() == null) {
            // The notification hasn't been populated yet.
            return false;
        }

        return false;
    }

    public void applyNotificationInfos(final List<NotificationInfo> notificationInfos) {
        mNotificationInfos.clear();
        if (notificationInfos.isEmpty()) {
            return;
        }
        mNotificationInfos.addAll(notificationInfos);

        NotificationInfo mainNotification = notificationInfos.get(0);
        mMainView.applyNotificationInfo(mainNotification, false);
    }

    public void trimNotifications(final List<String> notificationKeys) {
        NotificationInfo currentMainNotificationInfo = mMainView.getNotificationInfo();
        boolean shouldUpdateMainNotification = !notificationKeys.contains(
                currentMainNotificationInfo.notificationKey);

        if (shouldUpdateMainNotification) {
            int size = notificationKeys.size();
            NotificationInfo nextNotification = null;
            // We get the latest notification by finding the notification after the one that was
            // just dismissed.
            for (int i = 0; i < size; ++i) {
                if (currentMainNotificationInfo == mNotificationInfos.get(i) && i + 1 < size) {
                    nextNotification = mNotificationInfos.get(i + 1);
                    break;
                }
            }
            if (nextNotification != null) {
                mMainView.applyNotificationInfo(nextNotification, true);
            }
        }
    }
}