• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ObjectAnimator;
20 import android.content.Context;
21 import android.content.res.ColorStateList;
22 import android.graphics.drawable.ColorDrawable;
23 import android.graphics.drawable.RippleDrawable;
24 import android.text.TextUtils;
25 import android.util.AttributeSet;
26 import android.view.MotionEvent;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.FrameLayout;
30 import android.widget.TextView;
31 
32 import com.android.launcher3.ItemInfo;
33 import com.android.launcher3.Launcher;
34 import com.android.launcher3.R;
35 import com.android.launcher3.userevent.nano.LauncherLogProto;
36 import com.android.launcher3.util.Themes;
37 
38 /**
39  * A {@link android.widget.FrameLayout} that contains a single notification,
40  * e.g. icon + title + text.
41  */
42 public class NotificationMainView extends FrameLayout implements SwipeHelper.Callback {
43 
44     private NotificationInfo mNotificationInfo;
45     private ViewGroup mTextAndBackground;
46     private int mBackgroundColor;
47     private TextView mTitleView;
48     private TextView mTextView;
49 
NotificationMainView(Context context)50     public NotificationMainView(Context context) {
51         this(context, null, 0);
52     }
53 
NotificationMainView(Context context, AttributeSet attrs)54     public NotificationMainView(Context context, AttributeSet attrs) {
55         this(context, attrs, 0);
56     }
57 
NotificationMainView(Context context, AttributeSet attrs, int defStyle)58     public NotificationMainView(Context context, AttributeSet attrs, int defStyle) {
59         super(context, attrs, defStyle);
60     }
61 
62     @Override
onFinishInflate()63     protected void onFinishInflate() {
64         super.onFinishInflate();
65 
66         mTextAndBackground = (ViewGroup) findViewById(R.id.text_and_background);
67         ColorDrawable colorBackground = (ColorDrawable) mTextAndBackground.getBackground();
68         mBackgroundColor = colorBackground.getColor();
69         RippleDrawable rippleBackground = new RippleDrawable(ColorStateList.valueOf(
70                 Themes.getAttrColor(getContext(), android.R.attr.colorControlHighlight)),
71                 colorBackground, null);
72         mTextAndBackground.setBackground(rippleBackground);
73         mTitleView = (TextView) mTextAndBackground.findViewById(R.id.title);
74         mTextView = (TextView) mTextAndBackground.findViewById(R.id.text);
75     }
76 
applyNotificationInfo(NotificationInfo mainNotification, View iconView)77     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView) {
78         applyNotificationInfo(mainNotification, iconView, false);
79     }
80 
81     /**
82      * Sets the content of this view, animating it after a new icon shifts up if necessary.
83      */
applyNotificationInfo(NotificationInfo mainNotification, View iconView, boolean animate)84     public void applyNotificationInfo(NotificationInfo mainNotification, View iconView,
85            boolean animate) {
86         mNotificationInfo = mainNotification;
87         CharSequence title = mNotificationInfo.title;
88         CharSequence text = mNotificationInfo.text;
89         if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(text)) {
90             mTitleView.setText(title);
91             mTextView.setText(text);
92         } else {
93             mTitleView.setMaxLines(2);
94             mTitleView.setText(TextUtils.isEmpty(title) ? text : title);
95             mTextView.setVisibility(GONE);
96         }
97         iconView.setBackground(mNotificationInfo.getIconForBackground(getContext(),
98                 mBackgroundColor));
99         if (mNotificationInfo.intent != null) {
100             setOnClickListener(mNotificationInfo);
101         }
102         setTranslationX(0);
103         // Add a dummy ItemInfo so that logging populates the correct container and item types
104         // instead of DEFAULT_CONTAINERTYPE and DEFAULT_ITEMTYPE, respectively.
105         setTag(new ItemInfo());
106         if (animate) {
107             ObjectAnimator.ofFloat(mTextAndBackground, ALPHA, 0, 1).setDuration(150).start();
108         }
109     }
110 
getNotificationInfo()111     public NotificationInfo getNotificationInfo() {
112         return mNotificationInfo;
113     }
114 
115 
116     // SwipeHelper.Callback's
117 
118     @Override
getChildAtPosition(MotionEvent ev)119     public View getChildAtPosition(MotionEvent ev) {
120         return this;
121     }
122 
123     @Override
canChildBeDismissed(View v)124     public boolean canChildBeDismissed(View v) {
125         return mNotificationInfo != null && mNotificationInfo.dismissable;
126     }
127 
128     @Override
isAntiFalsingNeeded()129     public boolean isAntiFalsingNeeded() {
130         return false;
131     }
132 
133     @Override
onBeginDrag(View v)134     public void onBeginDrag(View v) {
135     }
136 
137     @Override
onChildDismissed(View v)138     public void onChildDismissed(View v) {
139         Launcher launcher = Launcher.getLauncher(getContext());
140         launcher.getPopupDataProvider().cancelNotification(
141                 mNotificationInfo.notificationKey);
142         launcher.getUserEventDispatcher().logActionOnItem(
143                 LauncherLogProto.Action.Touch.SWIPE,
144                 LauncherLogProto.Action.Direction.RIGHT, // Assume all swipes are right for logging.
145                 LauncherLogProto.ItemType.NOTIFICATION);
146     }
147 
148     @Override
onDragCancelled(View v)149     public void onDragCancelled(View v) {
150     }
151 
152     @Override
onChildSnappedBack(View animView, float targetLeft)153     public void onChildSnappedBack(View animView, float targetLeft) {
154     }
155 
156     @Override
updateSwipeProgress(View animView, boolean dismissable, float swipeProgress)157     public boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress) {
158         // Don't fade out.
159         return true;
160     }
161 
162     @Override
getFalsingThresholdFactor()163     public float getFalsingThresholdFactor() {
164         return 1;
165     }
166 }
167