blob: f6ac4a8bc434b9ffe967e9641a14473671be51a2 [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 Wren157026f2013-06-28 16:54:01 -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;
23import android.graphics.drawable.Drawable;
24import android.util.AttributeSet;
25import android.util.Log;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040026import android.view.MotionEvent;
27import android.view.View;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040028import android.view.ViewConfiguration;
29import android.view.ViewGroup;
30import android.widget.LinearLayout;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040031
32import com.android.systemui.R;
33import com.android.systemui.SwipeHelper;
34import com.android.systemui.statusbar.BaseStatusBar;
35
Chris Wren157026f2013-06-28 16:54:01 -040036public class HeadsUpNotificationView extends LinearLayout implements SwipeHelper.Callback {
37 private static final String TAG = "HeadsUpNotificationView";
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040038 private static final boolean DEBUG = false;
39
40 Rect mTmpRect = new Rect();
41
42 private SwipeHelper mSwipeHelper;
John Spurlock209bede2013-07-17 12:23:27 -040043
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040044 BaseStatusBar mBar;
45 private ViewGroup mContentHolder;
John Spurlock209bede2013-07-17 12:23:27 -040046
Chris Wren157026f2013-06-28 16:54:01 -040047 private Notification mHeadsUp;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040048 private OnClickListener mOnClickListener;
49
Chris Wren157026f2013-06-28 16:54:01 -040050 public HeadsUpNotificationView(Context context, AttributeSet attrs) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040051 this(context, attrs, 0);
52 }
53
Chris Wren157026f2013-06-28 16:54:01 -040054 public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040055 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 Spurlock209bede2013-07-17 12:23:27 -040065
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040066 mContentHolder = (ViewGroup) findViewById(R.id.contentHolder);
Chris Wren157026f2013-06-28 16:54:01 -040067 if (mHeadsUp != null) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040068 // whoops, we're on already!
Chris Wren157026f2013-06-28 16:54:01 -040069 applyContent(mHeadsUp, mOnClickListener);
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040070 }
71 }
John Spurlock209bede2013-07-17 12:23:27 -040072
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040073 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 Wren157026f2013-06-28 16:54:01 -040095 Log.v(TAG, "User swiped heads up to dismiss");
96 mBar.dismissHeadsUp();
Daniel Sandlerfa7887b2012-03-26 09:43:31 -040097 }
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 Spurlockcd686b52013-06-05 10:13:46 -0400127 //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: "
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400128 // + 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 Wren157026f2013-06-28 16:54:01 -0400137 public boolean applyContent(Notification headsUp, OnClickListener listener) {
138 mHeadsUp = headsUp;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400139 mOnClickListener = listener;
Chris Wren157026f2013-06-28 16:54:01 -0400140 if (mContentHolder == null) {
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400141 // too soon!
Chris Wren157026f2013-06-28 16:54:01 -0400142 return false;
143 }
144 if (headsUp.contentView == null) {
145 // bad data
146 return false;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400147 }
148 mContentHolder.setX(0);
149 mContentHolder.setVisibility(View.VISIBLE);
150 mContentHolder.setAlpha(1f);
151 mContentHolder.removeAllViews();
Chris Wren157026f2013-06-28 16:54:01 -0400152 final View content = headsUp.contentView.apply(getContext(), mContentHolder);
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400153 if (listener != null) {
154 content.setOnClickListener(listener);
Chris Wren157026f2013-06-28 16:54:01 -0400155
156 Drawable bg = getResources().getDrawable(R.drawable.heads_up_notification_row_bg);
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400157 if (bg == null) {
Chris Wren157026f2013-06-28 16:54:01 -0400158 Log.e(TAG, String.format("Can't find background drawable id=0x%08x",
159 R.drawable.heads_up_notification_row_bg));
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400160 } else {
161 content.setBackgroundDrawable(bg);
162 }
163 }
164 mContentHolder.addView(content);
Chris Wren157026f2013-06-28 16:54:01 -0400165 return true;
Daniel Sandlerfa7887b2012-03-26 09:43:31 -0400166 }
167}