OSDN Git Service

a05fef352025fb4b705f9af32a850d4a90a6692a
[android-x86/packages-apps-Launcher3.git] / src / com / android / launcher3 / notification / NotificationMainView.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.animation.AnimatorSet;
21 import android.animation.ArgbEvaluator;
22 import android.animation.ObjectAnimator;
23 import android.animation.ValueAnimator;
24 import android.content.Context;
25 import android.graphics.drawable.ColorDrawable;
26 import android.util.AttributeSet;
27 import android.view.MotionEvent;
28 import android.view.View;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31
32 import com.android.launcher3.ItemInfo;
33 import com.android.launcher3.Launcher;
34 import com.android.launcher3.LauncherAnimUtils;
35 import com.android.launcher3.LauncherViewPropertyAnimator;
36 import com.android.launcher3.R;
37 import com.android.launcher3.graphics.IconPalette;
38 import com.android.launcher3.userevent.nano.LauncherLogProto;
39
40 /**
41  * A {@link LinearLayout} that contains a single notification, e.g. icon + title + text.
42  */
43 public class NotificationMainView extends LinearLayout implements SwipeHelper.Callback {
44
45     private final ArgbEvaluator mArgbEvaluator = new ArgbEvaluator();
46
47     private NotificationInfo mNotificationInfo;
48     private TextView mTitleView;
49     private TextView mTextView;
50     private IconPalette mIconPalette;
51     private ColorDrawable mColorBackground;
52
53     public NotificationMainView(Context context) {
54         this(context, null, 0);
55     }
56
57     public NotificationMainView(Context context, AttributeSet attrs) {
58         this(context, attrs, 0);
59     }
60
61     public NotificationMainView(Context context, AttributeSet attrs, int defStyle) {
62         super(context, attrs, defStyle);
63     }
64
65     @Override
66     protected void onFinishInflate() {
67         super.onFinishInflate();
68
69         mTitleView = (TextView) findViewById(R.id.title);
70         mTextView = (TextView) findViewById(R.id.text);
71     }
72
73     public void applyColors(IconPalette iconPalette) {
74         mColorBackground = new ColorDrawable(iconPalette.backgroundColor);
75         setBackground(mColorBackground);
76         mIconPalette = iconPalette;
77     }
78
79     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView) {
80         applyNotificationInfo(mainNotification, iconView, false);
81     }
82
83     /**
84      * Sets the content of this view, animating it after a new icon shifts up if necessary.
85      */
86     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView,
87            boolean animate) {
88         if (animate) {
89             mTitleView.setAlpha(0);
90             mTextView.setAlpha(0);
91             mColorBackground.setColor(mIconPalette.secondaryColor);
92         }
93         mNotificationInfo = mainNotification;
94         mTitleView.setText(mNotificationInfo.title);
95         mTextView.setText(mNotificationInfo.text);
96         iconView.setBackground(mNotificationInfo.getIconForBackground(
97                 getContext(), mIconPalette.backgroundColor));
98         setOnClickListener(mNotificationInfo);
99         setTranslationX(0);
100         // Add a dummy ItemInfo so that logging populates the correct container and item types
101         // instead of DEFAULT_CONTAINERTYPE and DEFAULT_ITEMTYPE, respectively.
102         setTag(new ItemInfo());
103         if (animate) {
104             AnimatorSet animation = LauncherAnimUtils.createAnimatorSet();
105             Animator textFade = new LauncherViewPropertyAnimator(mTextView).alpha(1);
106             Animator titleFade = new LauncherViewPropertyAnimator(mTitleView).alpha(1);
107             ValueAnimator colorChange = ObjectAnimator.ofObject(mColorBackground, "color",
108                     mArgbEvaluator, mIconPalette.secondaryColor, mIconPalette.backgroundColor);
109             animation.playTogether(textFade, titleFade, colorChange);
110             animation.setDuration(150);
111             animation.start();
112         }
113     }
114
115     public NotificationInfo getNotificationInfo() {
116         return mNotificationInfo;
117     }
118
119
120     // SwipeHelper.Callback's
121
122     @Override
123     public View getChildAtPosition(MotionEvent ev) {
124         return this;
125     }
126
127     @Override
128     public boolean canChildBeDismissed(View v) {
129         return mNotificationInfo.dismissable;
130     }
131
132     @Override
133     public boolean isAntiFalsingNeeded() {
134         return false;
135     }
136
137     @Override
138     public void onBeginDrag(View v) {
139     }
140
141     @Override
142     public void onChildDismissed(View v) {
143         Launcher launcher = Launcher.getLauncher(getContext());
144         launcher.getPopupDataProvider().cancelNotification(
145                 mNotificationInfo.notificationKey);
146         launcher.getUserEventDispatcher().logActionOnItem(
147                 LauncherLogProto.Action.Touch.SWIPE,
148                 LauncherLogProto.Action.Direction.RIGHT, // Assume all swipes are right for logging.
149                 LauncherLogProto.ItemType.NOTIFICATION);
150     }
151
152     @Override
153     public void onDragCancelled(View v) {
154     }
155
156     @Override
157     public void onChildSnappedBack(View animView, float targetLeft) {
158     }
159
160     @Override
161     public boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress) {
162         // Don't fade out.
163         return true;
164     }
165
166     @Override
167     public float getFalsingThresholdFactor() {
168         return 1;
169     }
170 }