StatusBarHeaderView.java revision db3e6ed4270061538908ecc577df6f6a0d17ada3
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.settings.BrightnessController;
32import com.android.systemui.settings.ToggleSlider;
33import com.android.systemui.statusbar.policy.UserInfoController;
34
35/**
36 * The view to manage the header area in the expanded status bar.
37 */
38public class StatusBarHeaderView extends RelativeLayout implements View.OnClickListener {
39
40    private boolean mExpanded;
41    private boolean mKeyguardShowing;
42
43    private View mBackground;
44    private ViewGroup mSystemIconsContainer;
45    private View mDateTime;
46    private View mKeyguardCarrierText;
47    private MultiUserSwitch mMultiUserSwitch;
48    private View mDate;
49    private View mStatusIcons;
50    private View mSignalCluster;
51    private View mSettingsButton;
52    private View mBrightnessContainer;
53
54    private int mCollapsedHeight;
55    private int mExpandedHeight;
56    private int mKeyguardHeight;
57
58    private int mKeyguardWidth = ViewGroup.LayoutParams.MATCH_PARENT;
59    private int mNormalWidth;
60
61    private ActivityStarter mActivityStarter;
62    private BrightnessController mBrightnessController;
63
64    private final Rect mClipBounds = new Rect();
65    private final Outline mOutline = new Outline();
66
67    public StatusBarHeaderView(Context context, AttributeSet attrs) {
68        super(context, attrs);
69    }
70
71    @Override
72    protected void onFinishInflate() {
73        super.onFinishInflate();
74        mBackground = findViewById(R.id.background);
75        mSystemIconsContainer = (ViewGroup) findViewById(R.id.system_icons_container);
76        mDateTime = findViewById(R.id.datetime);
77        mKeyguardCarrierText = findViewById(R.id.keyguard_carrier_text);
78        mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
79        mDate = findViewById(R.id.date);
80        mSettingsButton = findViewById(R.id.settings_button);
81        mSettingsButton.setOnClickListener(this);
82        mBrightnessContainer = findViewById(R.id.brightness_container);
83        mBrightnessController = new BrightnessController(getContext(),
84                (ImageView) findViewById(R.id.brightness_icon),
85                (ToggleSlider) findViewById(R.id.brightness_slider));
86        loadDimens();
87    }
88
89    private void loadDimens() {
90        mCollapsedHeight = getResources().getDimensionPixelSize(R.dimen.status_bar_header_height);
91        mExpandedHeight = getResources().getDimensionPixelSize(
92                R.dimen.status_bar_header_height_expanded);
93        mKeyguardHeight = getResources().getDimensionPixelSize(
94                R.dimen.status_bar_header_height_keyguard);
95        mNormalWidth = getLayoutParams().width;
96    }
97
98    public void setActivityStarter(ActivityStarter activityStarter) {
99        mActivityStarter = activityStarter;
100    }
101
102    public int getCollapsedHeight() {
103        return mKeyguardShowing ? mKeyguardHeight : mCollapsedHeight;
104    }
105
106    public int getExpandedHeight() {
107        return mExpandedHeight;
108    }
109
110    public void setExpanded(boolean expanded) {
111        boolean changed = expanded != mExpanded;
112        mExpanded = expanded;
113        if (changed) {
114            updateHeights();
115            updateVisibilities();
116            updateSystemIconsLayoutParams();
117            updateBrightnessControllerState();
118        }
119    }
120
121    private void updateHeights() {
122        boolean onKeyguardAndCollapsed = mKeyguardShowing && !mExpanded;
123        int height;
124        if (mExpanded) {
125            height = mExpandedHeight;
126        } else if (onKeyguardAndCollapsed) {
127            height = mKeyguardHeight;
128        } else {
129            height = mCollapsedHeight;
130        }
131        ViewGroup.LayoutParams lp = getLayoutParams();
132        if (lp.height != height) {
133            lp.height = height;
134            setLayoutParams(lp);
135        }
136        int systemIconsContainerHeight = onKeyguardAndCollapsed ? mKeyguardHeight : mCollapsedHeight;
137        lp = mSystemIconsContainer.getLayoutParams();
138        if (lp.height != systemIconsContainerHeight) {
139            lp.height = systemIconsContainerHeight;
140            mSystemIconsContainer.setLayoutParams(lp);
141        }
142        lp = mMultiUserSwitch.getLayoutParams();
143        if (lp.height != systemIconsContainerHeight) {
144            lp.height = systemIconsContainerHeight;
145            mMultiUserSwitch.setLayoutParams(lp);
146        }
147    }
148
149    private void updateWidth() {
150        int width = mKeyguardShowing ? mKeyguardWidth : mNormalWidth;
151        ViewGroup.LayoutParams lp = getLayoutParams();
152        if (width != lp.width) {
153            lp.width = width;
154            setLayoutParams(lp);
155        }
156    }
157
158    private void updateVisibilities() {
159        boolean onKeyguardAndCollapsed = mKeyguardShowing && !mExpanded;
160        mBackground.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
161        mDateTime.setVisibility(onKeyguardAndCollapsed ? View.INVISIBLE : View.VISIBLE);
162        mKeyguardCarrierText.setVisibility(onKeyguardAndCollapsed ? View.VISIBLE : View.GONE);
163        mDate.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
164        mSettingsButton.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
165        mBrightnessContainer.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
166        if (mStatusIcons != null) {
167            mStatusIcons.setVisibility(!mExpanded ? View.VISIBLE : View.GONE);
168        }
169        if (mSignalCluster != null) {
170            mSignalCluster.setVisibility(!mExpanded ? View.VISIBLE : View.GONE);
171        }
172    }
173
174    private void updateSystemIconsLayoutParams() {
175        RelativeLayout.LayoutParams lp = (LayoutParams) mSystemIconsContainer.getLayoutParams();
176        lp.addRule(RelativeLayout.START_OF, mExpanded
177                ? mSettingsButton.getId()
178                : mMultiUserSwitch.getId());
179    }
180
181    private void updateBrightnessControllerState() {
182        if (mExpanded) {
183            mBrightnessController.registerCallbacks();
184        } else {
185            mBrightnessController.unregisterCallbacks();
186        }
187    }
188
189    public void setExpansion(float height) {
190        if (height < mCollapsedHeight) {
191            height = mCollapsedHeight;
192        }
193        if (height > mExpandedHeight) {
194            height = mExpandedHeight;
195        }
196        if (mExpanded) {
197            mBackground.setTranslationY(-(mExpandedHeight - height));
198        } else {
199            mBackground.setTranslationY(0);
200        }
201        setClipping(height);
202    }
203
204    private void setClipping(float height) {
205        mClipBounds.set(getPaddingLeft(), 0, getWidth() - getPaddingRight(), (int) height);
206        setClipBounds(mClipBounds);
207        mOutline.setRect(mClipBounds);
208        setOutline(mOutline);
209    }
210
211    public View getBackgroundView() {
212        return mBackground;
213    }
214
215    public void attachSystemIcons(LinearLayout systemIcons) {
216        mSystemIconsContainer.addView(systemIcons);
217        mStatusIcons = systemIcons.findViewById(R.id.statusIcons);
218        mSignalCluster = systemIcons.findViewById(R.id.signal_cluster);
219    }
220
221    public void onSystemIconsDetached() {
222        if (mStatusIcons != null) {
223            mStatusIcons.setVisibility(View.VISIBLE);
224        }
225        if (mSignalCluster != null) {
226            mSignalCluster.setVisibility(View.VISIBLE);
227        }
228        mStatusIcons = null;
229        mSignalCluster = null;
230    }
231
232    public void setKeyguardShowing(boolean keyguardShowing) {
233        mKeyguardShowing = keyguardShowing;
234        if (keyguardShowing) {
235            setZ(0);
236        } else {
237            setTranslationZ(0);
238        }
239        updateHeights();
240        updateWidth();
241        updateVisibilities();
242    }
243
244    public void setUserInfoController(UserInfoController userInfoController) {
245        mMultiUserSwitch.setUserInfoController(userInfoController);
246    }
247
248    public void setOverlayParent(ViewGroup parent) {
249        mMultiUserSwitch.setOverlayParent(parent);
250    }
251
252    @Override
253    public void onClick(View v) {
254        if (v == mSettingsButton) {
255            startSettingsActivity();
256        }
257    }
258
259    private void startSettingsActivity() {
260        mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
261    }
262}
263