Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.policy; |
| 18 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 19 | import android.app.Notification; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.Configuration; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.util.AttributeSet; |
| 25 | import android.util.Log; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 26 | import android.view.MotionEvent; |
| 27 | import android.view.View; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 28 | import android.view.ViewConfiguration; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.widget.LinearLayout; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 31 | |
| 32 | import com.android.systemui.R; |
| 33 | import com.android.systemui.SwipeHelper; |
| 34 | import com.android.systemui.statusbar.BaseStatusBar; |
| 35 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 36 | public class HeadsUpNotificationView extends LinearLayout implements SwipeHelper.Callback { |
| 37 | private static final String TAG = "HeadsUpNotificationView"; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 38 | private static final boolean DEBUG = false; |
| 39 | |
| 40 | Rect mTmpRect = new Rect(); |
| 41 | |
| 42 | private SwipeHelper mSwipeHelper; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 43 | |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 44 | BaseStatusBar mBar; |
| 45 | private ViewGroup mContentHolder; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 46 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 47 | private Notification mHeadsUp; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 48 | private OnClickListener mOnClickListener; |
| 49 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 50 | public HeadsUpNotificationView(Context context, AttributeSet attrs) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 51 | this(context, attrs, 0); |
| 52 | } |
| 53 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 54 | public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 55 | super(context, attrs, defStyle); |
| 56 | |
| 57 | setOrientation(LinearLayout.VERTICAL); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onAttachedToWindow() { |
| 62 | float densityScale = getResources().getDisplayMetrics().density; |
| 63 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
| 64 | mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 65 | |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 66 | mContentHolder = (ViewGroup) findViewById(R.id.contentHolder); |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 67 | if (mHeadsUp != null) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 68 | // whoops, we're on already! |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 69 | applyContent(mHeadsUp, mOnClickListener); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 72 | |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 73 | public void setBar(BaseStatusBar bar) { |
| 74 | mBar = bar; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 79 | if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()"); |
| 80 | return mSwipeHelper.onInterceptTouchEvent(ev) || |
| 81 | super.onInterceptTouchEvent(ev); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean onTouchEvent(MotionEvent ev) { |
| 86 | return mSwipeHelper.onTouchEvent(ev) || |
| 87 | super.onTouchEvent(ev); |
| 88 | } |
| 89 | |
| 90 | public boolean canChildBeDismissed(View v) { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | public void onChildDismissed(View v) { |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 95 | Log.v(TAG, "User swiped heads up to dismiss"); |
| 96 | mBar.dismissHeadsUp(); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | public void onBeginDrag(View v) { |
| 100 | } |
| 101 | |
| 102 | public void onDragCancelled(View v) { |
| 103 | mContentHolder.setAlpha(1f); // sometimes this isn't quite reset |
| 104 | } |
| 105 | |
| 106 | public View getChildAtPosition(MotionEvent ev) { |
| 107 | return mContentHolder; |
| 108 | } |
| 109 | |
| 110 | public View getChildContentView(View v) { |
| 111 | return v; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | protected void onConfigurationChanged(Configuration newConfig) { |
| 116 | super.onConfigurationChanged(newConfig); |
| 117 | float densityScale = getResources().getDisplayMetrics().density; |
| 118 | mSwipeHelper.setDensityScale(densityScale); |
| 119 | float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); |
| 120 | mSwipeHelper.setPagingTouchSlop(pagingTouchSlop); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void onDraw(android.graphics.Canvas c) { |
| 125 | super.onDraw(c); |
| 126 | if (DEBUG) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 127 | //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: " |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 128 | // + getMeasuredHeight() + "px"); |
| 129 | c.save(); |
| 130 | c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6, |
| 131 | android.graphics.Region.Op.DIFFERENCE); |
| 132 | c.drawColor(0xFFcc00cc); |
| 133 | c.restore(); |
| 134 | } |
| 135 | } |
| 136 | |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 137 | public boolean applyContent(Notification headsUp, OnClickListener listener) { |
| 138 | mHeadsUp = headsUp; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 139 | mOnClickListener = listener; |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 140 | if (mContentHolder == null) { |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 141 | // too soon! |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | if (headsUp.contentView == null) { |
| 145 | // bad data |
| 146 | return false; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 147 | } |
| 148 | mContentHolder.setX(0); |
| 149 | mContentHolder.setVisibility(View.VISIBLE); |
| 150 | mContentHolder.setAlpha(1f); |
| 151 | mContentHolder.removeAllViews(); |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 152 | final View content = headsUp.contentView.apply(getContext(), mContentHolder); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 153 | if (listener != null) { |
| 154 | content.setOnClickListener(listener); |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 155 | |
| 156 | Drawable bg = getResources().getDrawable(R.drawable.heads_up_notification_row_bg); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 157 | if (bg == null) { |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 158 | Log.e(TAG, String.format("Can't find background drawable id=0x%08x", |
| 159 | R.drawable.heads_up_notification_row_bg)); |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 160 | } else { |
| 161 | content.setBackgroundDrawable(bg); |
| 162 | } |
| 163 | } |
| 164 | mContentHolder.addView(content); |
Chris Wren | 157026f | 2013-06-28 16:54:01 -0400 | [diff] [blame] | 165 | return true; |
Daniel Sandler | fa7887b | 2012-03-26 09:43:31 -0400 | [diff] [blame] | 166 | } |
| 167 | } |