blob: 467e19a92a0f6b113bf01297e3dc4c1cc47fcd4d [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
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040019import android.content.Context;
20import android.content.res.Configuration;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040021import android.graphics.Rect;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040022import android.util.AttributeSet;
23import android.util.Log;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040024import android.view.MotionEvent;
25import android.view.View;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040026import android.view.ViewConfiguration;
27import android.view.ViewGroup;
Chris Wren0f2aa682013-08-02 12:03:02 -040028import android.widget.FrameLayout;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040029
Chris Wren51c75102013-07-16 20:49:17 -040030import com.android.systemui.ExpandHelper;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040031import com.android.systemui.R;
32import com.android.systemui.SwipeHelper;
33import com.android.systemui.statusbar.BaseStatusBar;
Chris Wren51c75102013-07-16 20:49:17 -040034import com.android.systemui.statusbar.NotificationData;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040035
Chris Wren0f2aa682013-08-02 12:03:02 -040036public class HeadsUpNotificationView extends FrameLayout implements SwipeHelper.Callback, ExpandHelper.Callback {
Chris Wren157026f2013-06-28 16:54:01 -040037 private static final String TAG = "HeadsUpNotificationView";
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040038 private static final boolean DEBUG = false;
Chris Wren6d15a362013-08-20 18:46:29 -040039 private static final boolean SPEW = DEBUG;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040040
41 Rect mTmpRect = new Rect();
42
Chris Wren51c75102013-07-16 20:49:17 -040043 private final int mTouchSensitivityDelay;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040044 private SwipeHelper mSwipeHelper;
John Spurlock209bede2013-07-17 12:23:27 -040045
Chris Wren51c75102013-07-16 20:49:17 -040046 private BaseStatusBar mBar;
47 private ExpandHelper mExpandHelper;
48 private long mStartTouchTime;
49
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040050 private ViewGroup mContentHolder;
Chris Wren0f2aa682013-08-02 12:03:02 -040051 private ViewGroup mContentSlider;
John Spurlock209bede2013-07-17 12:23:27 -040052
Chris Wren51c75102013-07-16 20:49:17 -040053 private NotificationData.Entry mHeadsUp;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040054
Chris Wren157026f2013-06-28 16:54:01 -040055 public HeadsUpNotificationView(Context context, AttributeSet attrs) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040056 this(context, attrs, 0);
57 }
58
Chris Wren157026f2013-06-28 16:54:01 -040059 public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040060 super(context, attrs, defStyle);
Chris Wren51c75102013-07-16 20:49:17 -040061 mTouchSensitivityDelay = getResources().getInteger(R.integer.heads_up_sensitivity_delay);
62 if (DEBUG) Log.v(TAG, "create() " + mTouchSensitivityDelay);
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040063 }
64
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040065 public void setBar(BaseStatusBar bar) {
66 mBar = bar;
67 }
68
Chris Wren0f2aa682013-08-02 12:03:02 -040069 public ViewGroup getHolder() {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040070 return mContentHolder;
71 }
72
Chris Wren51c75102013-07-16 20:49:17 -040073 public boolean setNotification(NotificationData.Entry headsUp) {
Chris Wren157026f2013-06-28 16:54:01 -040074 mHeadsUp = headsUp;
Chris Wren51c75102013-07-16 20:49:17 -040075 mHeadsUp.row.setExpanded(false);
Chris Wren157026f2013-06-28 16:54:01 -040076 if (mContentHolder == null) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040077 // too soon!
Chris Wren157026f2013-06-28 16:54:01 -040078 return false;
79 }
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040080 mContentHolder.setX(0);
81 mContentHolder.setVisibility(View.VISIBLE);
82 mContentHolder.setAlpha(1f);
83 mContentHolder.removeAllViews();
Chris Wren51c75102013-07-16 20:49:17 -040084 mContentHolder.addView(mHeadsUp.row);
Chris Wren4c913122013-08-05 18:08:16 -040085 mSwipeHelper.snapChild(mContentSlider, 1f);
Chris Wren51c75102013-07-16 20:49:17 -040086 mStartTouchTime = System.currentTimeMillis() + mTouchSensitivityDelay;
Chris Wren157026f2013-06-28 16:54:01 -040087 return true;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040088 }
Chris Wren51c75102013-07-16 20:49:17 -040089
Chris Wren6d15a362013-08-20 18:46:29 -040090 public boolean isClearable() {
91 return mHeadsUp == null || mHeadsUp.notification.isClearable();
Chris Wren0f2aa682013-08-02 12:03:02 -040092 }
93
94 public void setMargin(int notificationPanelMarginPx) {
Chris Wren6d15a362013-08-20 18:46:29 -040095 if (SPEW) Log.v(TAG, "setMargin() " + notificationPanelMarginPx);
Chris Wren0f2aa682013-08-02 12:03:02 -040096 if (mContentSlider != null) {
97 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mContentSlider.getLayoutParams();
98 lp.setMarginStart(notificationPanelMarginPx);
99 mContentSlider.setLayoutParams(lp);
100 }
101 }
102
103 // LinearLayout methods
104
105 @Override
106 public void onDraw(android.graphics.Canvas c) {
107 super.onDraw(c);
108 if (DEBUG) {
109 //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: "
110 // + getMeasuredHeight() + "px");
111 c.save();
112 c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6,
113 android.graphics.Region.Op.DIFFERENCE);
114 c.drawColor(0xFFcc00cc);
115 c.restore();
116 }
117 }
118
119 // ViewGroup methods
120
121 @Override
122 public void onAttachedToWindow() {
123 float densityScale = getResources().getDisplayMetrics().density;
124 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
125 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
126
127 int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
128 int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_max_height);
John Spurlock01534782014-01-13 11:59:22 -0500129 mExpandHelper = new ExpandHelper(getContext(), this, minHeight, maxHeight);
Chris Wren0f2aa682013-08-02 12:03:02 -0400130
131 mContentHolder = (ViewGroup) findViewById(R.id.content_holder);
132 mContentSlider = (ViewGroup) findViewById(R.id.content_slider);
133
134 if (mHeadsUp != null) {
135 // whoops, we're on already!
136 setNotification(mHeadsUp);
137 }
138 }
139
140 @Override
141 public boolean onInterceptTouchEvent(MotionEvent ev) {
142 if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
143 if (System.currentTimeMillis() < mStartTouchTime) {
144 return true;
145 }
146 return mSwipeHelper.onInterceptTouchEvent(ev)
147 || mExpandHelper.onInterceptTouchEvent(ev)
148 || super.onInterceptTouchEvent(ev);
149 }
150
151 // View methods
152
153 @Override
154 public boolean onTouchEvent(MotionEvent ev) {
155 if (System.currentTimeMillis() < mStartTouchTime) {
156 return false;
157 }
158 mBar.resetHeadsUpDecayTimer();
159 return mSwipeHelper.onTouchEvent(ev)
160 || mExpandHelper.onTouchEvent(ev)
161 || super.onTouchEvent(ev);
162 }
163
164 @Override
165 protected void onConfigurationChanged(Configuration newConfig) {
166 super.onConfigurationChanged(newConfig);
167 float densityScale = getResources().getDisplayMetrics().density;
168 mSwipeHelper.setDensityScale(densityScale);
169 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
170 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
171 }
172
173 // ExpandHelper.Callback methods
174
Chris Wren51c75102013-07-16 20:49:17 -0400175 @Override
176 public View getChildAtRawPosition(float x, float y) {
177 return getChildAtPosition(x, y);
178 }
179
180 @Override
181 public View getChildAtPosition(float x, float y) {
182 return mHeadsUp == null ? null : mHeadsUp.row;
183 }
184
185 @Override
186 public boolean canChildBeExpanded(View v) {
187 return mHeadsUp != null && mHeadsUp.row == v && mHeadsUp.row.isExpandable();
188 }
189
190 @Override
191 public void setUserExpandedChild(View v, boolean userExpanded) {
192 if (mHeadsUp != null && mHeadsUp.row == v) {
193 mHeadsUp.row.setUserExpanded(userExpanded);
194 }
195 }
196
197 @Override
198 public void setUserLockedChild(View v, boolean userLocked) {
199 if (mHeadsUp != null && mHeadsUp.row == v) {
200 mHeadsUp.row.setUserLocked(userLocked);
201 }
202 }
Chris Wreneda110f2013-07-25 15:31:59 -0400203
Chris Wren0f2aa682013-08-02 12:03:02 -0400204 // SwipeHelper.Callback methods
205
206 @Override
207 public boolean canChildBeDismissed(View v) {
208 return true;
209 }
210
211 @Override
212 public void onChildDismissed(View v) {
213 Log.v(TAG, "User swiped heads up to dismiss");
214 mBar.onHeadsUpDismissed();
215 }
216
217 @Override
218 public void onBeginDrag(View v) {
219 }
220
221 @Override
222 public void onDragCancelled(View v) {
223 mContentHolder.setAlpha(1f); // sometimes this isn't quite reset
224 }
225
226 @Override
227 public View getChildAtPosition(MotionEvent ev) {
228 return mContentSlider;
229 }
230
231 @Override
232 public View getChildContentView(View v) {
Chris Wren4c913122013-08-05 18:08:16 -0400233 return mContentSlider;
Chris Wreneda110f2013-07-25 15:31:59 -0400234 }
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400235}