OSDN Git Service

Ensure notification icons have enough contrast with background.
[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.ValueAnimator;
22 import android.content.Context;
23 import android.support.annotation.Nullable;
24 import android.util.AttributeSet;
25 import android.view.MotionEvent;
26 import android.view.View;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29
30 import com.android.launcher3.Launcher;
31 import com.android.launcher3.LauncherAnimUtils;
32 import com.android.launcher3.LauncherViewPropertyAnimator;
33 import com.android.launcher3.R;
34 import com.android.launcher3.graphics.IconPalette;
35
36 /**
37  * A {@link LinearLayout} that contains a single notification, e.g. icon + title + text.
38  */
39 public class NotificationMainView extends LinearLayout implements SwipeHelper.Callback {
40
41     private NotificationInfo mNotificationInfo;
42     private TextView mTitleView;
43     private TextView mTextView;
44     private IconPalette mIconPalette;
45
46     public NotificationMainView(Context context) {
47         this(context, null, 0);
48     }
49
50     public NotificationMainView(Context context, AttributeSet attrs) {
51         this(context, attrs, 0);
52     }
53
54     public NotificationMainView(Context context, AttributeSet attrs, int defStyle) {
55         super(context, attrs, defStyle);
56     }
57
58     @Override
59     protected void onFinishInflate() {
60         super.onFinishInflate();
61
62         mTitleView = (TextView) findViewById(R.id.title);
63         mTextView = (TextView) findViewById(R.id.text);
64     }
65
66     public void applyColors(IconPalette iconPalette) {
67         setBackgroundColor(iconPalette.backgroundColor);
68         mIconPalette = iconPalette;
69     }
70
71     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView) {
72         applyNotificationInfo(mainNotification, iconView, false);
73     }
74
75     /**
76      * Sets the content of this view, animating it after a new icon shifts up if necessary.
77      */
78     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView,
79            boolean animate) {
80         if (animate) {
81             mTitleView.setAlpha(0);
82             mTextView.setAlpha(0);
83             setBackgroundColor(mIconPalette.secondaryColor);
84         }
85         mNotificationInfo = mainNotification;
86         mTitleView.setText(mNotificationInfo.title);
87         mTextView.setText(mNotificationInfo.text);
88         iconView.setBackground(mNotificationInfo.getIconForBackground(
89                 getContext(), mIconPalette.backgroundColor));
90         setOnClickListener(mNotificationInfo);
91         setTranslationX(0);
92         if (animate) {
93             AnimatorSet animation = LauncherAnimUtils.createAnimatorSet();
94             Animator textFade = new LauncherViewPropertyAnimator(mTextView).alpha(1);
95             Animator titleFade = new LauncherViewPropertyAnimator(mTitleView).alpha(1);
96             ValueAnimator colorChange = ValueAnimator.ofArgb(mIconPalette.secondaryColor,
97                     mIconPalette.backgroundColor);
98             colorChange.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
99                 @Override
100                 public void onAnimationUpdate(ValueAnimator valueAnimator) {
101                     setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
102                 }
103             });
104             animation.playTogether(textFade, titleFade, colorChange);
105             animation.setDuration(150);
106             animation.start();
107         }
108     }
109
110     public NotificationInfo getNotificationInfo() {
111         return mNotificationInfo;
112     }
113
114
115     // SwipeHelper.Callback's
116
117     @Override
118     public View getChildAtPosition(MotionEvent ev) {
119         return this;
120     }
121
122     @Override
123     public boolean canChildBeDismissed(View v) {
124         return mNotificationInfo.dismissable;
125     }
126
127     @Override
128     public boolean isAntiFalsingNeeded() {
129         return false;
130     }
131
132     @Override
133     public void onBeginDrag(View v) {
134     }
135
136     @Override
137     public void onChildDismissed(View v) {
138         Launcher.getLauncher(getContext()).getPopupDataProvider().cancelNotification(
139                 mNotificationInfo.notificationKey);
140     }
141
142     @Override
143     public void onDragCancelled(View v) {
144     }
145
146     @Override
147     public void onChildSnappedBack(View animView, float targetLeft) {
148     }
149
150     @Override
151     public boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress) {
152         // Don't fade out.
153         return true;
154     }
155
156     @Override
157     public float getFalsingThresholdFactor() {
158         return 1;
159     }
160 }