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 static com.android.launcher3.anim.Interpolators.scrollInterpolatorForVelocity;
20 
21 import android.animation.Animator;
22 import android.animation.ObjectAnimator;
23 import android.annotation.TargetApi;
24 import android.content.Context;
25 import android.content.res.ColorStateList;
26 import android.graphics.drawable.ColorDrawable;
27 import android.graphics.drawable.RippleDrawable;
28 import android.os.Build;
29 import android.text.TextUtils;
30 import android.util.AttributeSet;
31 import android.util.FloatProperty;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.widget.FrameLayout;
35 import android.widget.TextView;
36 
37 import com.android.launcher3.Launcher;
38 import com.android.launcher3.R;
39 import com.android.launcher3.anim.AnimationSuccessListener;
40 import com.android.launcher3.model.data.ItemInfo;
41 import com.android.launcher3.touch.BaseSwipeDetector;
42 import com.android.launcher3.touch.OverScroll;
43 import com.android.launcher3.touch.SingleAxisSwipeDetector;
44 import com.android.launcher3.userevent.nano.LauncherLogProto;
45 import com.android.launcher3.util.Themes;
46 
47 /**
48  * A {@link android.widget.FrameLayout} that contains a single notification,
49  * e.g. icon + title + text.
50  */
51 @TargetApi(Build.VERSION_CODES.N)
52 public class NotificationMainView extends FrameLayout implements SingleAxisSwipeDetector.Listener {
53 
54     private static final FloatProperty<NotificationMainView> CONTENT_TRANSLATION =
55             new FloatProperty<NotificationMainView>("contentTranslation") {
56         @Override
57         public void setValue(NotificationMainView view, float v) {
58             view.setContentTranslation(v);
59         }
60 
61         @Override
62         public Float get(NotificationMainView view) {
63             return view.mTextAndBackground.getTranslationX();
64         }
65     };
66 
67     // This is used only to track the notification view, so that it can be properly logged.
68     public static final ItemInfo NOTIFICATION_ITEM_INFO = new ItemInfo();
69 
70     private final ObjectAnimator mContentTranslateAnimator;
71 
72     private NotificationInfo mNotificationInfo;
73     private ViewGroup mTextAndBackground;
74     private int mBackgroundColor;
75     private TextView mTitleView;
76     private TextView mTextView;
77     private View mIconView;
78 
79     private SingleAxisSwipeDetector mSwipeDetector;
80 
NotificationMainView(Context context)81     public NotificationMainView(Context context) {
82         this(context, null, 0);
83     }
84 
NotificationMainView(Context context, AttributeSet attrs)85     public NotificationMainView(Context context, AttributeSet attrs) {
86         this(context, attrs, 0);
87     }
88 
NotificationMainView(Context context, AttributeSet attrs, int defStyle)89     public NotificationMainView(Context context, AttributeSet attrs, int defStyle) {
90         super(context, attrs, defStyle);
91 
92         mContentTranslateAnimator = ObjectAnimator.ofFloat(this, CONTENT_TRANSLATION, 0);
93     }
94 
95     @Override
onFinishInflate()96     protected void onFinishInflate() {
97         super.onFinishInflate();
98 
99         mTextAndBackground = findViewById(R.id.text_and_background);
100         ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
101         mBackgroundColor = colorBackground.getColor();
102         RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
103                 Themes.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
104                 colorBackground, null);
105         mTextAndBackground.setBackground(rippleBackground);
106         mTitleView = mTextAndBackground.findViewById(R.id.title);
107         mTextView = mTextAndBackground.findViewById(R.id.text);
108         mIconView = findViewById(R.id.popup_item_icon);
109     }
110 
setSwipeDetector(SingleAxisSwipeDetector swipeDetector)111     public void setSwipeDetector(SingleAxisSwipeDetector swipeDetector) {
112         mSwipeDetector = swipeDetector;
113     }
114 
115     /**
116      * Sets the content of this view, animating it after a new icon shifts up if necessary.
117      */
applyNotificationInfo(NotificationInfo mainNotification, boolean animate)118     public void applyNotificationInfo(NotificationInfo mainNotification, boolean animate) {
119         mNotificationInfo = mainNotification;
120         NotificationListener listener = NotificationListener.getInstanceIfConnected();
121         if (listener != null) {
122             listener.setNotificationsShown(new String[] {mNotificationInfo.notificationKey});
123         }
124         CharSequence title = mNotificationInfo.title;
125         CharSequence text = mNotificationInfo.text;
126         if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(text)) {
127             mTitleView.setText(title.toString());
128             mTextView.setText(text.toString());
129         } else {
130             mTitleView.setMaxLines(2);
131             mTitleView.setText(TextUtils.isEmpty(title) ? text.toString() : title.toString());
132             mTextView.setVisibility(GONE);
133         }
134         mIconView.setBackground(mNotificationInfo.getIconForBackground(getContext(),
135                 mBackgroundColor));
136         if (mNotificationInfo.intent != null) {
137             setOnClickListener(mNotificationInfo);
138         }
139         setContentTranslation(0);
140         // Add a dummy ItemInfo so that logging populates the correct container and item types
141         // instead of DEFAULT_CONTAINERTYPE and DEFAULT_ITEMTYPE, respectively.
142         setTag(NOTIFICATION_ITEM_INFO);
143         if (animate) {
144             ObjectAnimator.ofFloat(mTextAndBackground, ALPHA, 0, 1).setDuration(150).start();
145         }
146     }
147 
setContentTranslation(float translation)148     public void setContentTranslation(float translation) {
149         mTextAndBackground.setTranslationX(translation);
150         mIconView.setTranslationX(translation);
151     }
152 
setContentVisibility(int visibility)153     public void setContentVisibility(int visibility) {
154         mTextAndBackground.setVisibility(visibility);
155         mIconView.setVisibility(visibility);
156     }
157 
getNotificationInfo()158     public NotificationInfo getNotificationInfo() {
159         return mNotificationInfo;
160     }
161 
162 
canChildBeDismissed()163     public boolean canChildBeDismissed() {
164         return mNotificationInfo != null && mNotificationInfo.dismissable;
165     }
166 
onChildDismissed()167     public void onChildDismissed() {
168         Launcher launcher = Launcher.getLauncher(getContext());
169         launcher.getPopupDataProvider().cancelNotification(
170                 mNotificationInfo.notificationKey);
171         launcher.getUserEventDispatcher().logActionOnItem(
172                 LauncherLogProto.Action.Touch.SWIPE,
173                 LauncherLogProto.Action.Direction.RIGHT, // Assume all swipes are right for logging.
174                 LauncherLogProto.ItemType.NOTIFICATION);
175     }
176 
177     // SingleAxisSwipeDetector.Listener's
178     @Override
onDragStart(boolean start, float startDisplacement)179     public void onDragStart(boolean start, float startDisplacement) { }
180 
181 
182     @Override
onDrag(float displacement)183     public boolean onDrag(float displacement) {
184         setContentTranslation(canChildBeDismissed()
185                 ? displacement : OverScroll.dampedScroll(displacement, getWidth()));
186         mContentTranslateAnimator.cancel();
187         return true;
188     }
189 
190     @Override
onDragEnd(float velocity)191     public void onDragEnd(float velocity) {
192         final boolean willExit;
193         final float endTranslation;
194         final float startTranslation = mTextAndBackground.getTranslationX();
195 
196         if (!canChildBeDismissed()) {
197             willExit = false;
198             endTranslation = 0;
199         } else if (mSwipeDetector.isFling(velocity)) {
200             willExit = true;
201             endTranslation = velocity < 0 ? - getWidth() : getWidth();
202         } else if (Math.abs(startTranslation) > getWidth() / 2) {
203             willExit = true;
204             endTranslation = (startTranslation < 0 ? -getWidth() : getWidth());
205         } else {
206             willExit = false;
207             endTranslation = 0;
208         }
209 
210         long duration = BaseSwipeDetector.calculateDuration(velocity,
211                 (endTranslation - startTranslation) / getWidth());
212 
213         mContentTranslateAnimator.removeAllListeners();
214         mContentTranslateAnimator.setDuration(duration)
215                 .setInterpolator(scrollInterpolatorForVelocity(velocity));
216         mContentTranslateAnimator.setFloatValues(startTranslation, endTranslation);
217         mContentTranslateAnimator.addListener(new AnimationSuccessListener() {
218             @Override
219             public void onAnimationSuccess(Animator animator) {
220                 mSwipeDetector.finishedScrolling();
221                 if (willExit) {
222                     onChildDismissed();
223                 }
224             }
225         });
226         mContentTranslateAnimator.start();
227     }
228 }
229