HeadsUpNotificationView.java revision eda110fdf13021704111c6939abfafdca3d16f9c
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
17package com.android.systemui.statusbar.policy;
18
19import android.app.Notification;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.graphics.Rect;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.MotionEvent;
26import android.view.View;
27import android.view.ViewConfiguration;
28import android.view.ViewGroup;
29import android.widget.LinearLayout;
30
31import com.android.systemui.ExpandHelper;
32import com.android.systemui.R;
33import com.android.systemui.SwipeHelper;
34import com.android.systemui.statusbar.BaseStatusBar;
35import com.android.systemui.statusbar.NotificationData;
36
37public class HeadsUpNotificationView extends LinearLayout implements SwipeHelper.Callback, ExpandHelper.Callback {
38    private static final String TAG = "HeadsUpNotificationView";
39    private static final boolean DEBUG = false;
40
41    Rect mTmpRect = new Rect();
42
43    private final int mTouchSensitivityDelay;
44    private SwipeHelper mSwipeHelper;
45
46    private BaseStatusBar mBar;
47    private ExpandHelper mExpandHelper;
48    private long mStartTouchTime;
49
50    public ViewGroup getHolder() {
51        return mContentHolder;
52    }
53
54    private ViewGroup mContentHolder;
55
56    private NotificationData.Entry mHeadsUp;
57
58    public HeadsUpNotificationView(Context context, AttributeSet attrs) {
59        this(context, attrs, 0);
60    }
61
62    public HeadsUpNotificationView(Context context, AttributeSet attrs, int defStyle) {
63        super(context, attrs, defStyle);
64        setOrientation(LinearLayout.VERTICAL);
65        mTouchSensitivityDelay = getResources().getInteger(R.integer.heads_up_sensitivity_delay);
66        if (DEBUG) Log.v(TAG, "create() " + mTouchSensitivityDelay);
67    }
68
69    @Override
70    public void onAttachedToWindow() {
71        float densityScale = getResources().getDisplayMetrics().density;
72        float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
73        mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop);
74
75        int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_min_height);
76        int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_row_max_height);
77        mExpandHelper = new ExpandHelper(mContext, this, minHeight, maxHeight);
78
79        mContentHolder = (ViewGroup) findViewById(R.id.contentHolder);
80        if (mHeadsUp != null) {
81            // whoops, we're on already!
82            setNotification(mHeadsUp);
83        }
84    }
85
86    public void setBar(BaseStatusBar bar) {
87        mBar = bar;
88    }
89
90    @Override
91    public boolean onInterceptTouchEvent(MotionEvent ev) {
92        if (DEBUG) Log.v(TAG, "onInterceptTouchEvent()");
93        if (System.currentTimeMillis() < mStartTouchTime) {
94            return true;
95        }
96        return mSwipeHelper.onInterceptTouchEvent(ev)
97                || mExpandHelper.onInterceptTouchEvent(ev)
98                || super.onInterceptTouchEvent(ev);
99    }
100
101    @Override
102    public boolean onTouchEvent(MotionEvent ev) {
103        if (System.currentTimeMillis() < mStartTouchTime) {
104            return false;
105        }
106        mBar.resetHeadsUpDecayTimer();
107        return mSwipeHelper.onTouchEvent(ev)
108                || mExpandHelper.onTouchEvent(ev)
109                || super.onTouchEvent(ev);
110    }
111
112    public boolean canChildBeDismissed(View v) {
113        return true;
114    }
115
116    public void onChildDismissed(View v) {
117        Log.v(TAG, "User swiped heads up to dismiss");
118        mBar.onHeadsUpDismissed();
119    }
120
121    public void onBeginDrag(View v) {
122    }
123
124    public void onDragCancelled(View v) {
125        mContentHolder.setAlpha(1f); // sometimes this isn't quite reset
126    }
127
128    public View getChildAtPosition(MotionEvent ev) {
129        return mContentHolder;
130    }
131
132    public View getChildContentView(View v) {
133        return v;
134    }
135
136    @Override
137    protected void onConfigurationChanged(Configuration newConfig) {
138        super.onConfigurationChanged(newConfig);
139        float densityScale = getResources().getDisplayMetrics().density;
140        mSwipeHelper.setDensityScale(densityScale);
141        float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop();
142        mSwipeHelper.setPagingTouchSlop(pagingTouchSlop);
143    }
144
145    @Override
146    public void onDraw(android.graphics.Canvas c) {
147        super.onDraw(c);
148        if (DEBUG) {
149            //Log.d(TAG, "onDraw: canvas height: " + c.getHeight() + "px; measured height: "
150            //        + getMeasuredHeight() + "px");
151            c.save();
152            c.clipRect(6, 6, c.getWidth() - 6, getMeasuredHeight() - 6,
153                    android.graphics.Region.Op.DIFFERENCE);
154            c.drawColor(0xFFcc00cc);
155            c.restore();
156        }
157    }
158
159    public boolean setNotification(NotificationData.Entry headsUp) {
160        mHeadsUp = headsUp;
161        mHeadsUp.row.setExpanded(false);
162        if (mContentHolder == null) {
163            // too soon!
164            return false;
165        }
166        mContentHolder.setX(0);
167        mContentHolder.setVisibility(View.VISIBLE);
168        mContentHolder.setAlpha(1f);
169        mContentHolder.removeAllViews();
170        mContentHolder.addView(mHeadsUp.row);
171        mStartTouchTime = System.currentTimeMillis() + mTouchSensitivityDelay;
172        return true;
173    }
174
175    @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    }
203
204    public boolean isInsistent() {
205        return mHeadsUp != null
206            && (mHeadsUp.notification.getNotification().flags & Notification.FLAG_INSISTENT) != 0;
207    }
208}