2017-01-20 09:38:25 -08:00
|
|
|
/*
|
|
|
|
|
* 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.Animator;
|
|
|
|
|
import android.animation.AnimatorListenerAdapter;
|
|
|
|
|
import android.animation.AnimatorSet;
|
2017-02-13 12:13:43 -08:00
|
|
|
import android.animation.ObjectAnimator;
|
2017-01-20 09:38:25 -08:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.graphics.Rect;
|
2017-02-21 15:16:12 -08:00
|
|
|
import android.graphics.drawable.ColorDrawable;
|
2017-01-20 09:38:25 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
import android.view.View;
|
2017-02-21 15:16:12 -08:00
|
|
|
import android.view.ViewGroup;
|
2017-01-20 09:38:25 -08:00
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.Launcher;
|
|
|
|
|
import com.android.launcher3.LauncherAnimUtils;
|
|
|
|
|
import com.android.launcher3.R;
|
2017-02-21 15:16:12 -08:00
|
|
|
import com.android.launcher3.Utilities;
|
2017-02-13 12:13:43 -08:00
|
|
|
import com.android.launcher3.anim.PropertyListBuilder;
|
2017-02-21 15:16:12 -08:00
|
|
|
import com.android.launcher3.anim.PropertyResetListener;
|
2017-01-20 09:38:25 -08:00
|
|
|
import com.android.launcher3.graphics.IconPalette;
|
|
|
|
|
import com.android.launcher3.popup.PopupContainerWithArrow;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-26 09:24:41 -08:00
|
|
|
* A {@link LinearLayout} that contains only icons of notifications.
|
2017-01-20 09:38:25 -08:00
|
|
|
* If there are more than {@link #MAX_FOOTER_NOTIFICATIONS} icons, we add a "+x" overflow.
|
|
|
|
|
*/
|
|
|
|
|
public class NotificationFooterLayout extends LinearLayout {
|
|
|
|
|
|
|
|
|
|
public interface IconAnimationEndListener {
|
|
|
|
|
void onIconAnimationEnd(NotificationInfo animatedNotification);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 15:16:12 -08:00
|
|
|
private static final int MAX_FOOTER_NOTIFICATIONS = 4;
|
2017-01-20 09:38:25 -08:00
|
|
|
|
|
|
|
|
private static final Rect sTempRect = new Rect();
|
|
|
|
|
|
|
|
|
|
private final List<NotificationInfo> mNotifications = new ArrayList<>();
|
|
|
|
|
private final List<NotificationInfo> mOverflowNotifications = new ArrayList<>();
|
2017-02-21 15:16:12 -08:00
|
|
|
private final boolean mRtl;
|
2017-01-20 09:38:25 -08:00
|
|
|
|
|
|
|
|
LinearLayout.LayoutParams mIconLayoutParams;
|
|
|
|
|
private LinearLayout mIconRow;
|
2017-02-21 15:16:12 -08:00
|
|
|
private final ColorDrawable mBackgroundColor;
|
2017-01-20 09:38:25 -08:00
|
|
|
private int mTextColor;
|
2017-01-27 13:18:30 -08:00
|
|
|
private TextView mOverflowView;
|
2017-01-20 09:38:25 -08:00
|
|
|
|
|
|
|
|
public NotificationFooterLayout(Context context) {
|
|
|
|
|
this(context, null, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NotificationFooterLayout(Context context, AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NotificationFooterLayout(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
|
|
2017-02-21 15:16:12 -08:00
|
|
|
mRtl = Utilities.isRtl(getResources());
|
|
|
|
|
|
2017-01-20 09:38:25 -08:00
|
|
|
int size = getResources().getDimensionPixelSize(
|
|
|
|
|
R.dimen.notification_footer_icon_size);
|
2017-02-21 15:16:12 -08:00
|
|
|
int padding = getResources().getDimensionPixelSize(R.dimen.notification_padding);
|
2017-01-20 09:38:25 -08:00
|
|
|
mIconLayoutParams = new LayoutParams(size, size);
|
2017-02-21 15:16:12 -08:00
|
|
|
mIconLayoutParams.setMarginEnd(padding);
|
2017-01-20 09:38:25 -08:00
|
|
|
mIconLayoutParams.gravity = Gravity.CENTER_VERTICAL;
|
2017-02-21 15:16:12 -08:00
|
|
|
|
|
|
|
|
mBackgroundColor = new ColorDrawable();
|
|
|
|
|
setBackground(mBackgroundColor);
|
2017-01-20 09:38:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
mIconRow = (LinearLayout) findViewById(R.id.icon_row);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void applyColors(IconPalette iconPalette) {
|
2017-02-21 15:16:12 -08:00
|
|
|
mBackgroundColor.setColor(iconPalette.backgroundColor);
|
2017-01-20 09:38:25 -08:00
|
|
|
findViewById(R.id.divider).setBackgroundColor(iconPalette.secondaryColor);
|
|
|
|
|
mTextColor = iconPalette.textColor;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-21 15:16:12 -08:00
|
|
|
public int getBackgroundColor() {
|
|
|
|
|
return mBackgroundColor.getColor();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-20 09:38:25 -08:00
|
|
|
/**
|
|
|
|
|
* Keep track of the NotificationInfo, and then update the UI when
|
|
|
|
|
* {@link #commitNotificationInfos()} is called.
|
|
|
|
|
*/
|
|
|
|
|
public void addNotificationInfo(final NotificationInfo notificationInfo) {
|
|
|
|
|
if (mNotifications.size() < MAX_FOOTER_NOTIFICATIONS) {
|
|
|
|
|
mNotifications.add(notificationInfo);
|
|
|
|
|
} else {
|
|
|
|
|
mOverflowNotifications.add(notificationInfo);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds icons and potentially overflow text for all of the NotificationInfo's
|
|
|
|
|
* added using {@link #addNotificationInfo(NotificationInfo)}.
|
|
|
|
|
*/
|
|
|
|
|
public void commitNotificationInfos() {
|
|
|
|
|
mIconRow.removeAllViews();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < mNotifications.size(); i++) {
|
|
|
|
|
NotificationInfo info = mNotifications.get(i);
|
|
|
|
|
addNotificationIconForInfo(info, false /* fromOverflow */);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mOverflowNotifications.isEmpty()) {
|
2017-01-27 13:18:30 -08:00
|
|
|
mOverflowView = new TextView(getContext());
|
|
|
|
|
mOverflowView.setTextColor(mTextColor);
|
|
|
|
|
updateOverflowText();
|
2017-02-21 15:16:12 -08:00
|
|
|
mIconRow.addView(mOverflowView, 0, mIconLayoutParams);
|
2017-01-20 09:38:25 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addNotificationIconForInfo(NotificationInfo info, boolean fromOverflow) {
|
|
|
|
|
View icon = new View(getContext());
|
2017-02-21 15:16:12 -08:00
|
|
|
icon.setBackground(info.getIconForBackground(getContext(), getBackgroundColor()));
|
2017-01-20 09:38:25 -08:00
|
|
|
icon.setOnClickListener(info);
|
2017-02-21 15:16:12 -08:00
|
|
|
int addIndex = 0;
|
2017-01-20 09:38:25 -08:00
|
|
|
if (fromOverflow) {
|
|
|
|
|
// Add the notification before the overflow view.
|
2017-02-21 15:16:12 -08:00
|
|
|
addIndex = 1;
|
2017-01-20 09:38:25 -08:00
|
|
|
icon.setAlpha(0);
|
|
|
|
|
icon.animate().alpha(1);
|
|
|
|
|
}
|
2017-01-27 14:28:42 -08:00
|
|
|
icon.setTag(info);
|
2017-01-20 09:38:25 -08:00
|
|
|
mIconRow.addView(icon, addIndex, mIconLayoutParams);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 13:18:30 -08:00
|
|
|
private void updateOverflowText() {
|
|
|
|
|
mOverflowView.setText(getResources().getString(R.string.deep_notifications_overflow,
|
2017-01-20 09:38:25 -08:00
|
|
|
mOverflowNotifications.size()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void animateFirstNotificationTo(Rect toBounds,
|
|
|
|
|
final IconAnimationEndListener callback) {
|
|
|
|
|
AnimatorSet animation = LauncherAnimUtils.createAnimatorSet();
|
2017-02-21 15:16:12 -08:00
|
|
|
final View firstNotification = mIconRow.getChildAt(mIconRow.getChildCount() - 1);
|
2017-01-20 09:38:25 -08:00
|
|
|
|
|
|
|
|
Rect fromBounds = sTempRect;
|
|
|
|
|
firstNotification.getGlobalVisibleRect(fromBounds);
|
|
|
|
|
float scale = (float) toBounds.height() / fromBounds.height();
|
2017-02-13 12:13:43 -08:00
|
|
|
Animator moveAndScaleIcon = LauncherAnimUtils.ofPropertyValuesHolder(firstNotification,
|
|
|
|
|
new PropertyListBuilder().scale(scale).translationY(toBounds.top - fromBounds.top
|
|
|
|
|
+ (fromBounds.height() * scale - fromBounds.height()) / 2).build());
|
2017-01-20 09:38:25 -08:00
|
|
|
moveAndScaleIcon.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
2017-01-27 14:28:42 -08:00
|
|
|
callback.onIconAnimationEnd((NotificationInfo) firstNotification.getTag());
|
2017-01-27 13:18:30 -08:00
|
|
|
removeViewFromIconRow(firstNotification);
|
2017-01-20 09:38:25 -08:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
animation.play(moveAndScaleIcon);
|
|
|
|
|
|
|
|
|
|
// Shift all notifications (not the overflow) over to fill the gap.
|
2017-02-21 15:16:12 -08:00
|
|
|
int gapWidth = mIconLayoutParams.width + mIconLayoutParams.getMarginEnd();
|
|
|
|
|
if (mRtl) {
|
|
|
|
|
gapWidth = -gapWidth;
|
|
|
|
|
}
|
|
|
|
|
int numIcons = mIconRow.getChildCount() - 1;
|
|
|
|
|
// We have to set the translation X to 0 when the new main notification
|
|
|
|
|
// is removed from the footer.
|
|
|
|
|
PropertyResetListener<View, Float> propertyResetListener
|
|
|
|
|
= new PropertyResetListener<>(TRANSLATION_X, 0f);
|
|
|
|
|
for (int i = mOverflowNotifications.isEmpty() ? 0 : 1; i < numIcons; i++) {
|
2017-01-20 09:38:25 -08:00
|
|
|
final View child = mIconRow.getChildAt(i);
|
2017-02-21 15:16:12 -08:00
|
|
|
Animator shiftChild = ObjectAnimator.ofFloat(child, TRANSLATION_X, gapWidth);
|
|
|
|
|
shiftChild.addListener(propertyResetListener);
|
2017-01-20 09:38:25 -08:00
|
|
|
animation.play(shiftChild);
|
|
|
|
|
}
|
|
|
|
|
animation.start();
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 13:18:30 -08:00
|
|
|
private void removeViewFromIconRow(View child) {
|
|
|
|
|
mIconRow.removeView(child);
|
|
|
|
|
mNotifications.remove((NotificationInfo) child.getTag());
|
|
|
|
|
if (!mOverflowNotifications.isEmpty()) {
|
|
|
|
|
NotificationInfo notification = mOverflowNotifications.remove(0);
|
|
|
|
|
mNotifications.add(notification);
|
|
|
|
|
addNotificationIconForInfo(notification, true /* fromOverflow */);
|
|
|
|
|
}
|
|
|
|
|
if (mOverflowView != null) {
|
|
|
|
|
if (mOverflowNotifications.isEmpty()) {
|
|
|
|
|
mIconRow.removeView(mOverflowView);
|
|
|
|
|
mOverflowView = null;
|
|
|
|
|
} else {
|
|
|
|
|
updateOverflowText();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mIconRow.getChildCount() == 0) {
|
2017-02-21 15:16:12 -08:00
|
|
|
// There are no more icons in the footer, so hide it.
|
2017-01-27 13:18:30 -08:00
|
|
|
PopupContainerWithArrow popup = PopupContainerWithArrow.getOpen(
|
|
|
|
|
Launcher.getLauncher(getContext()));
|
2017-02-21 15:16:12 -08:00
|
|
|
Animator collapseFooter = popup.reduceNotificationViewHeight(getHeight(),
|
|
|
|
|
getResources().getInteger(R.integer.config_removeNotificationViewDuration));
|
|
|
|
|
collapseFooter.addListener(new AnimatorListenerAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationEnd(Animator animation) {
|
|
|
|
|
((ViewGroup) getParent()).removeView(NotificationFooterLayout.this);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
collapseFooter.start();
|
2017-01-27 13:18:30 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-26 09:54:48 -08:00
|
|
|
public void trimNotifications(List<String> notifications) {
|
2017-01-20 09:38:25 -08:00
|
|
|
if (!isAttachedToWindow() || mIconRow.getChildCount() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Iterator<NotificationInfo> overflowIterator = mOverflowNotifications.iterator();
|
|
|
|
|
while (overflowIterator.hasNext()) {
|
|
|
|
|
if (!notifications.contains(overflowIterator.next().notificationKey)) {
|
|
|
|
|
overflowIterator.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TextView overflowView = null;
|
|
|
|
|
for (int i = mIconRow.getChildCount() - 1; i >= 0; i--) {
|
|
|
|
|
View child = mIconRow.getChildAt(i);
|
|
|
|
|
if (child instanceof TextView) {
|
|
|
|
|
overflowView = (TextView) child;
|
|
|
|
|
} else {
|
2017-01-27 14:28:42 -08:00
|
|
|
NotificationInfo childInfo = (NotificationInfo) child.getTag();
|
2017-01-20 09:38:25 -08:00
|
|
|
if (!notifications.contains(childInfo.notificationKey)) {
|
2017-01-27 13:18:30 -08:00
|
|
|
removeViewFromIconRow(child);
|
2017-01-20 09:38:25 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|