PhoneStatusBarView.java revision 750bb9bff9fccf94f4bbf5945e10ce3f76534e7f
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.app.StatusBarManager;
21import android.content.Context;
22import android.content.res.Resources;
23import android.content.res.Resources.NotFoundException;
24import android.util.AttributeSet;
25import android.util.Slog;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.accessibility.AccessibilityEvent;
29import com.android.systemui.R;
30
31public class PhoneStatusBarView extends PanelBar {
32    private static final String TAG = "PhoneStatusBarView";
33    private static final boolean DEBUG = PhoneStatusBar.DEBUG;
34
35    PhoneStatusBar mBar;
36    int mScrimColor;
37    float mSettingsPanelDragzoneFrac;
38    float mSettingsPanelDragzoneMin;
39
40    boolean mFullWidthNotifications;
41    PanelView mFadingPanel = null;
42    PanelView mNotificationPanel, mSettingsPanel;
43    private boolean mShouldFade;
44
45    public PhoneStatusBarView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47    }
48
49    public void setBar(PhoneStatusBar bar) {
50        mBar = bar;
51    }
52
53    @Override
54    public void onAttachedToWindow() {
55        Resources res = getContext().getResources();
56        mScrimColor = res.getColor(R.color.notification_panel_scrim_color);
57        mSettingsPanelDragzoneMin = res.getDimension(R.dimen.settings_panel_dragzone_min);
58        try {
59            mSettingsPanelDragzoneFrac = res.getFraction(R.dimen.settings_panel_dragzone_fraction, 1, 1);
60        } catch (NotFoundException ex) {
61            mSettingsPanelDragzoneFrac = 0f;
62        }
63
64        mFullWidthNotifications = mSettingsPanelDragzoneFrac <= 0f;
65    }
66
67    @Override
68    public void addPanel(PanelView pv) {
69        super.addPanel(pv);
70        if (pv.getId() == R.id.notification_panel) {
71            mNotificationPanel = pv;
72        } else if (pv.getId() == R.id.settings_panel){
73            mSettingsPanel = pv;
74        }
75    }
76
77    @Override
78    public boolean panelsEnabled() {
79        return ((mBar.mDisabled & StatusBarManager.DISABLE_EXPAND) == 0);
80    }
81
82    @Override
83    public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
84        if (super.onRequestSendAccessibilityEvent(child, event)) {
85            // The status bar is very small so augment the view that the user is touching
86            // with the content of the status bar a whole. This way an accessibility service
87            // may announce the current item as well as the entire content if appropriate.
88            AccessibilityEvent record = AccessibilityEvent.obtain();
89            onInitializeAccessibilityEvent(record);
90            dispatchPopulateAccessibilityEvent(record);
91            event.appendRecord(record);
92            return true;
93        }
94        return false;
95    }
96
97    @Override
98    public PanelView selectPanelForTouchX(float x) {
99        if (mFullWidthNotifications) {
100            if (DEBUG) {
101                Slog.v(TAG, "notif frac=" + mNotificationPanel.getExpandedFraction());
102            }
103            return (mNotificationPanel.getExpandedFraction() > 0f)
104                ? mSettingsPanel : mNotificationPanel;
105        }
106
107        // We split the status bar into thirds: the left 2/3 are for notifications, and the
108        // right 1/3 for quick settings. If you pull the status bar down a second time you'll
109        // toggle panels no matter where you pull it down.
110
111        final float w = getMeasuredWidth();
112        float region = (w * mSettingsPanelDragzoneFrac);
113
114        if (DEBUG) {
115            Slog.v(TAG, String.format(
116                "w=%.1f frac=%.3f region=%.1f min=%.1f x=%.1f w-x=%.1f",
117                w, mSettingsPanelDragzoneFrac, region, mSettingsPanelDragzoneMin, x, (w-x)));
118        }
119
120        if (region < mSettingsPanelDragzoneMin) region = mSettingsPanelDragzoneMin;
121
122        return (w - x < region) ? mSettingsPanel : mNotificationPanel;
123    }
124
125    @Override
126    public void onPanelPeeked() {
127        super.onPanelPeeked();
128        mBar.makeExpandedVisible(true);
129    }
130
131    @Override
132    public void startOpeningPanel(PanelView panel) {
133        super.startOpeningPanel(panel);
134        // we only want to start fading if this is the "first" or "last" panel,
135        // which is kind of tricky to determine
136        mShouldFade = (mFadingPanel == null || mFadingPanel.isFullyExpanded());
137        if (DEBUG) {
138            Slog.v(TAG, "start opening: " + panel + " shouldfade=" + mShouldFade);
139        }
140        mFadingPanel = panel;
141    }
142
143    @Override
144    public void onAllPanelsCollapsed() {
145        super.onAllPanelsCollapsed();
146        mBar.makeExpandedInvisible();
147        mFadingPanel = null;
148    }
149
150    @Override
151    public void onPanelFullyOpened(PanelView openPanel) {
152        super.onPanelFullyOpened(openPanel);
153        mFadingPanel = openPanel;
154        mShouldFade = true; // now you own the fade, mister
155    }
156
157    @Override
158    public boolean onTouchEvent(MotionEvent event) {
159        return mBar.interceptTouchEvent(event) || super.onTouchEvent(event);
160    }
161
162    @Override
163    public boolean onInterceptTouchEvent(MotionEvent event) {
164        return mBar.interceptTouchEvent(event) || super.onInterceptTouchEvent(event);
165    }
166
167    @Override
168    public void panelExpansionChanged(PanelView panel, float frac) {
169        super.panelExpansionChanged(panel, frac);
170
171        if (DEBUG) {
172            Slog.v(TAG, "panelExpansionChanged: f=" + frac);
173        }
174
175        if (panel == mFadingPanel && mScrimColor != 0 && ActivityManager.isHighEndGfx()) {
176            if (mShouldFade) {
177                frac = mPanelExpandedFractionSum; // don't judge me
178                // woo, special effects
179                final float k = (float)(1f-0.5f*(1f-Math.cos(3.14159f * Math.pow(1f-frac, 2.2f))));
180                // attenuate background color alpha by k
181                final int color = (int) ((mScrimColor >>> 24) * k) << 24 | (mScrimColor & 0xFFFFFF);
182                mBar.mStatusBarWindow.setBackgroundColor(color);
183            }
184        }
185
186        mBar.updateCarrierLabelVisibility(false);
187    }
188}
189