StatusBarHeaderView.java revision 3e04cc8c9bcb6daa8f5336934a88dc6c4fdd5221
1/*
2 * Copyright (C) 2014 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.content.Context;
20import android.content.Intent;
21import android.graphics.Outline;
22import android.graphics.Rect;
23import android.util.AttributeSet;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.ImageView;
27import android.widget.LinearLayout;
28import android.widget.RelativeLayout;
29
30import com.android.systemui.R;
31import com.android.systemui.qs.QSPanel;
32import com.android.systemui.settings.BrightnessController;
33import com.android.systemui.settings.ToggleSlider;
34import com.android.systemui.statusbar.policy.UserInfoController;
35
36/**
37 * The view to manage the header area in the expanded status bar.
38 */
39public class StatusBarHeaderView extends RelativeLayout implements View.OnClickListener {
40
41    /**
42     * How much the header expansion gets rubberbanded while expanding the panel.
43     */
44    private static final float EXPANSION_RUBBERBAND_FACTOR = 0.35f;
45
46    private boolean mExpanded;
47    private boolean mKeyguardShowing;
48
49    private View mBackground;
50    private ViewGroup mSystemIconsContainer;
51    private View mDateTime;
52    private View mKeyguardCarrierText;
53    private MultiUserSwitch mMultiUserSwitch;
54    private View mDate;
55    private View mStatusIcons;
56    private View mSignalCluster;
57    private View mSettingsButton;
58    private View mBrightnessContainer;
59
60    private int mCollapsedHeight;
61    private int mExpandedHeight;
62    private int mKeyguardHeight;
63
64    private int mKeyguardWidth = ViewGroup.LayoutParams.MATCH_PARENT;
65    private int mNormalWidth;
66
67    private ActivityStarter mActivityStarter;
68    private BrightnessController mBrightnessController;
69    private QSPanel mQSPanel;
70
71    private final Rect mClipBounds = new Rect();
72    private final Outline mOutline = new Outline();
73
74    public StatusBarHeaderView(Context context, AttributeSet attrs) {
75        super(context, attrs);
76    }
77
78    @Override
79    protected void onFinishInflate() {
80        super.onFinishInflate();
81        mBackground = findViewById(R.id.background);
82        mSystemIconsContainer = (ViewGroup) findViewById(R.id.system_icons_container);
83        mDateTime = findViewById(R.id.datetime);
84        mKeyguardCarrierText = findViewById(R.id.keyguard_carrier_text);
85        mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
86        mDate = findViewById(R.id.date);
87        mSettingsButton = findViewById(R.id.settings_button);
88        mSettingsButton.setOnClickListener(this);
89        mBrightnessContainer = findViewById(R.id.brightness_container);
90        mBrightnessController = new BrightnessController(getContext(),
91                (ImageView) findViewById(R.id.brightness_icon),
92                (ToggleSlider) findViewById(R.id.brightness_slider));
93        loadDimens();
94        updateVisibilities();
95        addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
96            @Override
97            public void onLayoutChange(View v, int left, int top, int right,
98                    int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
99                if ((right - left) != (oldRight - oldLeft)) {
100                    // width changed, update clipping
101                    setClipping(getHeight());
102                }
103            }
104        });
105    }
106
107    private void loadDimens() {
108        mCollapsedHeight = getResources().getDimensionPixelSize(R.dimen.status_bar_header_height);
109        mExpandedHeight = getResources().getDimensionPixelSize(
110                R.dimen.status_bar_header_height_expanded);
111        mKeyguardHeight = getResources().getDimensionPixelSize(
112                R.dimen.status_bar_header_height_keyguard);
113        mNormalWidth = getLayoutParams().width;
114    }
115
116    public void setActivityStarter(ActivityStarter activityStarter) {
117        mActivityStarter = activityStarter;
118    }
119
120    public int getCollapsedHeight() {
121        return mKeyguardShowing ? mKeyguardHeight : mCollapsedHeight;
122    }
123
124    public int getExpandedHeight() {
125        return mExpandedHeight;
126    }
127
128    public void setExpanded(boolean expanded) {
129        boolean changed = expanded != mExpanded;
130        mExpanded = expanded;
131        if (changed) {
132            updateHeights();
133            updateVisibilities();
134            updateSystemIconsLayoutParams();
135            updateBrightnessControllerState();
136            updateZTranslation();
137            updateClickTargets();
138            if (mQSPanel != null) {
139                mQSPanel.setExpanded(expanded);
140            }
141        }
142    }
143
144    private void updateHeights() {
145        boolean onKeyguardAndCollapsed = mKeyguardShowing && !mExpanded;
146        int height;
147        if (mExpanded) {
148            height = mExpandedHeight;
149        } else if (onKeyguardAndCollapsed) {
150            height = mKeyguardHeight;
151        } else {
152            height = mCollapsedHeight;
153        }
154        ViewGroup.LayoutParams lp = getLayoutParams();
155        if (lp.height != height) {
156            lp.height = height;
157            setLayoutParams(lp);
158        }
159        int systemIconsContainerHeight = onKeyguardAndCollapsed ? mKeyguardHeight : mCollapsedHeight;
160        lp = mSystemIconsContainer.getLayoutParams();
161        if (lp.height != systemIconsContainerHeight) {
162            lp.height = systemIconsContainerHeight;
163            mSystemIconsContainer.setLayoutParams(lp);
164        }
165        lp = mMultiUserSwitch.getLayoutParams();
166        if (lp.height != systemIconsContainerHeight) {
167            lp.height = systemIconsContainerHeight;
168            mMultiUserSwitch.setLayoutParams(lp);
169        }
170    }
171
172    private void updateWidth() {
173        int width = mKeyguardShowing ? mKeyguardWidth : mNormalWidth;
174        ViewGroup.LayoutParams lp = getLayoutParams();
175        if (width != lp.width) {
176            lp.width = width;
177            setLayoutParams(lp);
178        }
179    }
180
181    private void updateVisibilities() {
182        boolean onKeyguardAndCollapsed = mKeyguardShowing && !mExpanded;
183        mBackground.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
184        mDateTime.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
185        mKeyguardCarrierText.setVisibility(onKeyguardAndCollapsed ? View.VISIBLE : View.GONE);
186        mDate.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
187        mSettingsButton.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
188        mBrightnessContainer.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
189        if (mStatusIcons != null) {
190            mStatusIcons.setVisibility(!mExpanded ? View.VISIBLE : View.GONE);
191        }
192        if (mSignalCluster != null) {
193            mSignalCluster.setVisibility(!mExpanded ? View.VISIBLE : View.GONE);
194        }
195    }
196
197    private void updateSystemIconsLayoutParams() {
198        RelativeLayout.LayoutParams lp = (LayoutParams) mSystemIconsContainer.getLayoutParams();
199        lp.addRule(RelativeLayout.START_OF, mExpanded
200                ? mSettingsButton.getId()
201                : mMultiUserSwitch.getId());
202    }
203
204    private void updateBrightnessControllerState() {
205        if (mExpanded) {
206            mBrightnessController.registerCallbacks();
207        } else {
208            mBrightnessController.unregisterCallbacks();
209        }
210    }
211
212    private void updateClickTargets() {
213        mDateTime.setClickable(mExpanded);
214        mMultiUserSwitch.setClickable(mExpanded);
215    }
216
217    private void updateZTranslation() {
218
219        // If we are on the Keyguard, we need to set our z position to zero, so we don't get
220        // shadows.
221        if (mKeyguardShowing && !mExpanded) {
222            setZ(0);
223        } else {
224            setTranslationZ(0);
225        }
226    }
227
228    public void setExpansion(float height) {
229        height = (height - mCollapsedHeight) * EXPANSION_RUBBERBAND_FACTOR + mCollapsedHeight;
230        if (height < mCollapsedHeight) {
231            height = mCollapsedHeight;
232        }
233        if (height > mExpandedHeight) {
234            height = mExpandedHeight;
235        }
236        setClipping(height);
237    }
238
239    private void setClipping(float height) {
240        mClipBounds.set(getPaddingLeft(), 0, getWidth() - getPaddingRight(), (int) height);
241        setClipBounds(mClipBounds);
242        mOutline.setRect(mClipBounds);
243        setOutline(mOutline);
244    }
245
246    public View getBackgroundView() {
247        return mBackground;
248    }
249
250    public void attachSystemIcons(LinearLayout systemIcons) {
251        mSystemIconsContainer.addView(systemIcons);
252        mStatusIcons = systemIcons.findViewById(R.id.statusIcons);
253        mSignalCluster = systemIcons.findViewById(R.id.signal_cluster);
254    }
255
256    public void onSystemIconsDetached() {
257        if (mStatusIcons != null) {
258            mStatusIcons.setVisibility(View.VISIBLE);
259        }
260        if (mSignalCluster != null) {
261            mSignalCluster.setVisibility(View.VISIBLE);
262        }
263        mStatusIcons = null;
264        mSignalCluster = null;
265    }
266
267    public void setKeyguardShowing(boolean keyguardShowing) {
268        mKeyguardShowing = keyguardShowing;
269        updateHeights();
270        updateWidth();
271        updateVisibilities();
272        updateZTranslation();
273    }
274
275    public void setUserInfoController(UserInfoController userInfoController) {
276        mMultiUserSwitch.setUserInfoController(userInfoController);
277    }
278
279    public void setOverlayParent(ViewGroup parent) {
280        mMultiUserSwitch.setOverlayParent(parent);
281    }
282
283    @Override
284    public void onClick(View v) {
285        if (v == mSettingsButton) {
286            startSettingsActivity();
287        }
288    }
289
290    private void startSettingsActivity() {
291        mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
292    }
293
294    public void setQSPanel(QSPanel qsp) {
295        mQSPanel = qsp;
296        if (mQSPanel != null) {
297            mQSPanel.setCallback(mQsPanelCallback);
298        }
299    }
300
301    private final QSPanel.Callback mQsPanelCallback = new QSPanel.Callback() {
302        @Override
303        public void onShowingDetail(boolean showingDetail) {
304            mBrightnessContainer.animate().alpha(showingDetail ? 0 : 1).withLayer().start();
305        }
306    };
307}
308