blob: f1fda784ad85f32b34395900871cdd5389e59d9d [file] [log] [blame]
Daniel Sandlerfa7887b2012-03-26 09:43:31 -04001/*
2 * Copyright (C) 2011 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
17package com.android.systemui.statusbar.policy;
18
Chris Wreneda110f2013-07-25 15:31:59 -040019import android.app.Notification;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040020import android.content.Context;
21import android.content.res.Configuration;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040022import android.graphics.Rect;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040023import android.util.AttributeSet;
24import android.util.Log;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040025import android.view.MotionEvent;
26import android.view.View;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040027import android.view.ViewConfiguration;
28import android.view.ViewGroup;
Chris Wren0f2aa682013-08-02 12:03:02 -040029import android.widget.FrameLayout;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040030import android.widget.LinearLayout;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040031
Chris Wren51c75102013-07-16 20:49:17 -040032import com.android.systemui.ExpandHelper;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040033import com.android.systemui.R;
34import com.android.systemui.SwipeHelper;
35import com.android.systemui.statusbar.BaseStatusBar;
Chris Wren51c75102013-07-16 20:49:17 -040036import com.android.systemui.statusbar.NotificationData;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040037
Chris Wren0f2aa682013-08-02 12:03:02 -040038public class HeadsUpNotificationView extends FrameLayout implements SwipeHelper.Callback, ExpandHelper.Callback {
Chris Wren157026f2013-06-28 16:54:01 -040039 private static final String TAG = "HeadsUpNotificationView";
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040040 private static final boolean DEBUG = false;
Chris Wren6d15a362013-08-20 18:46:29 -040041 private static final boolean SPEW = DEBUG;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040042
43 Rect mTmpRect = new Rect();
44
Chris Wren51c75102013-07-16 20:49:17 -040045 private final int mTouchSensitivityDelay;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040046 private SwipeHelper mSwipeHelper;
John Spurlock209bede2013-07-17 12:23:27 -040047
Chris Wren51c75102013-07-16 20:49:17 -040048 private BaseStatusBar mBar;
49 private ExpandHelper mExpandHelper;
50 private long mStartTouchTime;
51
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040052 private ViewGroup mContentHolder;
Chris Wren0f2aa682013-08-02 12:03:02 -040053 private ViewGroup mContentSlider;
John Spurlock209bede2013-07-17 12:23:27 -040054
Chris Wren51c75102013-07-16 20:49:17 -040055 private NotificationData.Entry mHeadsUp;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040056
Chris Wren157026f2013-06-28 16:54:01 -040057 public HeadsUpNotificationView(Context context, AttributeSet attrs) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040058 this(context, attrs, 0);
59 }
60
Chris Wren157026f2013-06-28 16:54:01 -040061 public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040062 super(context, attrs, defStyle);
Chris Wren51c75102013-07-16 20:49:17 -040063 mTouchSensitivityDelay = getResources().getInteger(R.integer.heads_up_sensitivity_delay);
64 if (DEBUG) Log.v(TAG, "create() " + mTouchSensitivityDelay);
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040065 }
66
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040067 public void setBar(BaseStatusBar bar) {
68 mBar = bar;
69 }
70
Chris Wren0f2aa682013-08-02 12:03:02 -040071 public ViewGroup getHolder() {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040072 return mContentHolder;
73 }
74
Chris Wren51c75102013-07-16 20:49:17 -040075 public boolean setNotification(NotificationData.Entry headsUp) {
Chris Wren157026f2013-06-28 16:54:01 -040076 mHeadsUp = headsUp;
Chris Wren51c75102013-07-16 20:49:17 -040077 mHeadsUp.row.setExpanded(false);
Chris Wren157026f2013-06-28 16:54:01 -040078 if (mContentHolder == null) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040079 // too soon!
Chris Wren157026f2013-06-28 16:54:01 -040080 return false;
81 }
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040082 mContentHolder.setX(0);
83 mContentHolder.setVisibility(View.VISIBLE);
84 mContentHolder.setAlpha(1f);
85 mContentHolder.removeAllViews();
Chris Wren51c75102013-07-16 20:49:17 -040086 mContentHolder.addView(mHeadsUp.row);
Chris Wren4c913122013-08-05 18:08:16 -040087 mSwipeHelper.snapChild(mContentSlider, 1f);
Chris Wren51c75102013-07-16 20:49:17 -040088 mStartTouchTime = System.currentTimeMillis() + mTouchSensitivityDelay;
Chris Wren157026f2013-06-28 16:54:01 -040089 return true;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040090 }
Chris Wren51c75102013-07-16 20:49:17 -040091
Chris Wren6d15a362013-08-20 18:46:29 -040092 public boolean isClearable() {
93 return mHeadsUp == null || mHeadsUp.notification.isClearable();
Chris Wren0f2aa682013-08-02 12:03:02 -040094 }
95
96 public void setMargin(int notificationPanelMarginPx) {
Chris Wren6d15a362013-08-20 18:46:29 -040097 if (SPEW) Log.v(TAG, "setMargin() " + notificationPanelMarginPx);
Chris Wren0f2aa682013-08-02 12:03:02 -040098 if (mContentSlider != null) {
99 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentSlider.getLayoutParams();
100 lp.setMarginStart(notificationPanelMarginPx);
101 mContentSlider.setLayoutParams(lp);
102 }
103 }
104
105 // LinearLayout methods
106
107 @Override
108 public void onDraw(android.graphics.Canvas c) {
109 super.onDraw(c);
110 if (DEBUG) {
111 //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: "
112 // + getMeasuredHeight() + "px");
113 c.save();
114 c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6,
115 android.graphics.Region.Op.DIFFERENCE);
116 c.drawColor(0xFFcc00cc);
117 c.restore();
118 }
119 }
120
121 // ViewGroup methods
122
123 @Override
124 public void onAttachedToWindow() {
125 float densityScale = getResources().getDisplayMetrics().density;
126 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
127 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
128
129 int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
130 int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_max_height);
131 mExpandHelper = new ExpandHelper(mContext, this, minHeight, maxHeight);
132
133 mContentHolder = (ViewGroup) findViewById(R.id.content_holder);
134 mContentSlider = (ViewGroup) findViewById(R.id.content_slider);
135
136 if (mHeadsUp != null) {
137 // whoops, we're on already!
138 setNotification(mHeadsUp);
139 }
140 }
141
142 @Override
143 public boolean onInterceptTouchEvent(MotionEvent ev) {
144 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
145 if (System.currentTimeMillis() < mStartTouchTime) {
146 return true;
147 }
148 return mSwipeHelper.onInterceptTouchEvent(ev)
149 || mExpandHelper.onInterceptTouchEvent(ev)
150 || super.onInterceptTouchEvent(ev);
151 }
152
153 // View methods
154
155 @Override
156 public boolean onTouchEvent(MotionEvent ev) {
157 if (System.currentTimeMillis() < mStartTouchTime) {
158 return false;
159 }
160 mBar.resetHeadsUpDecayTimer();
161 return mSwipeHelper.onTouchEvent(ev)
162 || mExpandHelper.onTouchEvent(ev)
163 || super.onTouchEvent(ev);
164 }
165
166 @Override
167 protected void onConfigurationChanged(Configuration newConfig) {
168 super.onConfigurationChanged(newConfig);
169 float densityScale = getResources().getDisplayMetrics().density;
170 mSwipeHelper.setDensityScale(densityScale);
171 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
172 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
173 }
174
175 // ExpandHelper.Callback methods
176
Chris Wren51c75102013-07-16 20:49:17 -0400177 @Override
178 public View getChildAtRawPosition(float x, float y) {
179 return getChildAtPosition(x, y);
180 }
181
182 @Override
183 public View getChildAtPosition(float x, float y) {
184 return mHeadsUp == null ? null : mHeadsUp.row;
185 }
186
187 @Override
188 public boolean canChildBeExpanded(View v) {
189 return mHeadsUp != null && mHeadsUp.row == v && mHeadsUp.row.isExpandable();
190 }
191
192 @Override
193 public void setUserExpandedChild(View v, boolean userExpanded) {
194 if (mHeadsUp != null && mHeadsUp.row == v) {
195 mHeadsUp.row.setUserExpanded(userExpanded);
196 }
197 }
198
199 @Override
200 public void setUserLockedChild(View v, boolean userLocked) {
201 if (mHeadsUp != null && mHeadsUp.row == v) {
202 mHeadsUp.row.setUserLocked(userLocked);
203 }
204 }
Chris Wreneda110f2013-07-25 15:31:59 -0400205
Chris Wren0f2aa682013-08-02 12:03:02 -0400206 // SwipeHelper.Callback methods
207
208 @Override
209 public boolean canChildBeDismissed(View v) {
210 return true;
211 }
212
213 @Override
214 public void onChildDismissed(View v) {
215 Log.v(TAG, "User swiped heads up to dismiss");
216 mBar.onHeadsUpDismissed();
217 }
218
219 @Override
220 public void onBeginDrag(View v) {
221 }
222
223 @Override
224 public void onDragCancelled(View v) {
225 mContentHolder.setAlpha(1f); // sometimes this isn't quite reset
226 }
227
228 @Override
229 public View getChildAtPosition(MotionEvent ev) {
230 return mContentSlider;
231 }
232
233 @Override
234 public View getChildContentView(View v) {
Chris Wren4c913122013-08-05 18:08:16 -0400235 return mContentSlider;
Chris Wreneda110f2013-07-25 15:31:59 -0400236 }
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400237}