PhoneStatusBarView.java revision fa505a7f26bdf685c701e92b481602eb59d7ff56
1/*
2 * Copyright (C) 2008 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.phone;
18
19import android.app.ActivityManager;
20import android.content.Context;
21import android.content.res.Resources;
22import android.content.res.Resources.NotFoundException;
23import android.util.AttributeSet;
24import android.util.EventLog;
25import android.util.Log;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.accessibility.AccessibilityEvent;
29
30import com.android.systemui.EventLogTags;
31import com.android.systemui.R;
32
33public class PhoneStatusBarView extends PanelBar {
34    private static final String TAG = "PhoneStatusBarView";
35    private static final boolean DEBUG = PhoneStatusBar.DEBUG;
36    private static final boolean DEBUG_GESTURES = true;
37
38    PhoneStatusBar mBar;
39    int mScrimColor;
40    int mScrimColorKeyguard;
41
42    PanelView mFadingPanel = null;
43    PanelView mLastFullyOpenedPanel = null;
44    PanelView mNotificationPanel;
45    private boolean mShouldFade;
46    private final PhoneStatusBarTransitions mBarTransitions;
47
48    public PhoneStatusBarView(Context context, AttributeSet attrs) {
49        super(context, attrs);
50
51        Resources res = getContext().getResources();
52        mScrimColor = res.getColor(R.color.notification_panel_scrim_color);
53        mScrimColorKeyguard = res.getColor(R.color.notification_panel_scrim_color_keyguard);
54        mBarTransitions = new PhoneStatusBarTransitions(this);
55    }
56
57    public BarTransitions getBarTransitions() {
58        return mBarTransitions;
59    }
60
61    public void setBar(PhoneStatusBar bar) {
62        mBar = bar;
63    }
64
65    @Override
66    public void onAttachedToWindow() {
67        mBarTransitions.init();
68    }
69
70    @Override
71    public void addPanel(PanelView pv) {
72        super.addPanel(pv);
73        if (pv.getId() == R.id.notification_panel) {
74            mNotificationPanel = pv;
75        }
76    }
77
78    @Override
79    public boolean panelsEnabled() {
80        return mBar.panelsEnabled();
81    }
82
83    @Override
84    public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
85        if (super.onRequestSendAccessibilityEvent(child, event)) {
86            // The status bar is very small so augment the view that the user is touching
87            // with the content of the status bar a whole. This way an accessibility service
88            // may announce the current item as well as the entire content if appropriate.
89            AccessibilityEvent record = AccessibilityEvent.obtain();
90            onInitializeAccessibilityEvent(record);
91            dispatchPopulateAccessibilityEvent(record);
92            event.appendRecord(record);
93            return true;
94        }
95        return false;
96    }
97
98    @Override
99    public PanelView selectPanelForTouch(MotionEvent touch) {
100        // No double swiping. If either panel is open, nothing else can be pulled down.
101        return mNotificationPanel.getExpandedHeight() > 0
102                ? null
103                : mNotificationPanel;
104    }
105
106    @Override
107    public void onPanelPeeked() {
108        super.onPanelPeeked();
109        mBar.makeExpandedVisible(false);
110    }
111
112    @Override
113    public void startOpeningPanel(PanelView panel) {
114        super.startOpeningPanel(panel);
115        // we only want to start fading if this is the "first" or "last" panel,
116        // which is kind of tricky to determine
117        mShouldFade = (mFadingPanel == null || mFadingPanel.isFullyExpanded());
118        if (DEBUG) {
119            Log.v(TAG, "start opening: " + panel + " shouldfade=" + mShouldFade);
120        }
121        mFadingPanel = panel;
122    }
123
124    @Override
125    public void onAllPanelsCollapsed() {
126        super.onAllPanelsCollapsed();
127        // give animations time to settle
128        mBar.makeExpandedInvisibleSoon();
129        mFadingPanel = null;
130        mLastFullyOpenedPanel = null;
131        if (mScrimColor != 0 && ActivityManager.isHighEndGfx() && mBar.mStatusBarWindow != null) {
132            mBar.mStatusBarWindow.setBackgroundColor(0);
133        }
134    }
135
136    @Override
137    public void onPanelFullyOpened(PanelView openPanel) {
138        super.onPanelFullyOpened(openPanel);
139        if (openPanel != mLastFullyOpenedPanel) {
140            openPanel.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
141        }
142        mFadingPanel = openPanel;
143        mLastFullyOpenedPanel = openPanel;
144        mShouldFade = true; // now you own the fade, mister
145    }
146
147    @Override
148    public boolean onTouchEvent(MotionEvent event) {
149        boolean barConsumedEvent = mBar.interceptTouchEvent(event);
150
151        if (DEBUG_GESTURES) {
152            if (event.getActionMasked() != MotionEvent.ACTION_MOVE) {
153                EventLog.writeEvent(EventLogTags.SYSUI_PANELBAR_TOUCH,
154                        event.getActionMasked(), (int) event.getX(), (int) event.getY(),
155                        barConsumedEvent ? 1 : 0);
156            }
157        }
158
159        return barConsumedEvent || super.onTouchEvent(event);
160    }
161
162    @Override
163    public void onTrackingStarted(PanelView panel) {
164        super.onTrackingStarted(panel);
165        mBar.onTrackingStarted();
166    }
167
168    @Override
169    public void onTrackingStopped(PanelView panel) {
170        super.onTrackingStopped(panel);
171        mBar.onTrackingStopped();
172    }
173
174    @Override
175    public boolean onInterceptTouchEvent(MotionEvent event) {
176        return mBar.interceptTouchEvent(event) || super.onInterceptTouchEvent(event);
177    }
178
179    @Override
180    public void panelExpansionChanged(PanelView panel, float frac) {
181        super.panelExpansionChanged(panel, frac);
182
183        if (DEBUG) {
184            Log.v(TAG, "panelExpansionChanged: f=" + frac);
185        }
186
187        if (panel == mFadingPanel && mScrimColor != 0 && ActivityManager.isHighEndGfx()
188                && mBar.mStatusBarWindow != null) {
189            if (mShouldFade) {
190                int scrimColor = mBar.isOnKeyguard() ? mScrimColorKeyguard : mScrimColor;
191                frac = mPanelExpandedFractionSum; // don't judge me
192                // let's start this 20% of the way down the screen
193                frac = frac * 1.2f - 0.2f;
194                if (frac <= 0) {
195                    mBar.mStatusBarWindow.setBackgroundColor(0);
196                } else {
197                    // woo, special effects
198                    final float k = (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2f))));
199                    // attenuate background color alpha by k
200                    final int color = (int) ((scrimColor >>> 24) * k) << 24 | (scrimColor & 0xFFFFFF);
201                    mBar.mStatusBarWindow.setBackgroundColor(color);
202                }
203            }
204        }
205
206        // fade out the panel as it gets buried into the status bar to avoid overdrawing the
207        // status bar on the last frame of a close animation
208        final int H = mBar.getStatusBarHeight();
209        final float ph = panel.getExpandedHeight() + panel.getPaddingBottom();
210        float alpha = 1f;
211        if (ph < 2*H) {
212            if (ph < H) alpha = 0f;
213            else alpha = (ph - H) / H;
214            alpha = alpha * alpha; // get there faster
215        }
216        if (panel.getAlpha() != alpha) {
217            panel.setAlpha(alpha);
218        }
219
220        mBar.animateHeadsUp(mNotificationPanel == panel, mPanelExpandedFractionSum);
221
222        mBar.updateCarrierLabelVisibility(false);
223        mBar.userActivity();
224    }
225}
226