StatusBarHeaderView.java revision 9054d94cbac888c3a52427f54420cde2460c0c3b
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.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.LinearLayout;
24import android.widget.RelativeLayout;
25
26import com.android.systemui.R;
27
28/**
29 * The view to manage the header area in the expanded status bar.
30 */
31public class StatusBarHeaderView extends RelativeLayout {
32
33    private boolean mExpanded;
34    private View mBackground;
35    private ViewGroup mSystemIconsContainer;
36    private View mDateTime;
37    private View mKeyguardCarrierText;
38
39    private int mCollapsedHeight;
40    private int mExpandedHeight;
41    private int mKeyguardHeight;
42
43    private boolean mKeyguardShowing;
44
45    public StatusBarHeaderView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47    }
48
49    @Override
50    protected void onFinishInflate() {
51        super.onFinishInflate();
52        mBackground = findViewById(R.id.background);
53        mSystemIconsContainer = (ViewGroup) findViewById(R.id.system_icons_container);
54        mDateTime = findViewById(R.id.datetime);
55        mKeyguardCarrierText = findViewById(R.id.keyguard_carrier_text);
56        loadDimens();
57    }
58
59    private void loadDimens() {
60        mCollapsedHeight = getResources().getDimensionPixelSize(R.dimen.status_bar_header_height);
61        mExpandedHeight = getResources().getDimensionPixelSize(
62                R.dimen.status_bar_header_height_expanded);
63        mKeyguardHeight = getResources().getDimensionPixelSize(
64                R.dimen.status_bar_header_height_keyguard);
65    }
66
67    public int getCollapsedHeight() {
68        return mKeyguardShowing ? mKeyguardHeight : mCollapsedHeight;
69    }
70
71    public int getExpandedHeight() {
72        return mExpandedHeight;
73    }
74
75    public void setExpanded(boolean expanded) {
76        mExpanded = expanded;
77        updateHeights();
78    }
79
80    private void updateHeights() {
81        int height;
82        if (mExpanded) {
83            height = mExpandedHeight;
84        } else if (mKeyguardShowing) {
85            height = mKeyguardHeight;
86        } else {
87            height = mCollapsedHeight;
88        }
89        ViewGroup.LayoutParams lp = getLayoutParams();
90        if (lp.height != height) {
91            lp.height = height;
92            setLayoutParams(lp);
93        }
94        int systemIconsContainerHeight = mKeyguardShowing ? mKeyguardHeight : mCollapsedHeight;
95        lp = mSystemIconsContainer.getLayoutParams();
96        if (lp.height != systemIconsContainerHeight) {
97            lp.height = systemIconsContainerHeight;
98            mSystemIconsContainer.setLayoutParams(lp);
99        }
100    }
101
102    public void setExpansion(float height) {
103        if (height < mCollapsedHeight) {
104            height = mCollapsedHeight;
105        }
106        if (height > mExpandedHeight) {
107            height = mExpandedHeight;
108        }
109        if (mExpanded) {
110            mBackground.setTranslationY(-(mExpandedHeight - height));
111        } else {
112            mBackground.setTranslationY(0);
113        }
114    }
115
116    public View getBackgroundView() {
117        return mBackground;
118    }
119
120    public void attachSystemIcons(LinearLayout systemIcons) {
121        mSystemIconsContainer.addView(systemIcons);
122    }
123
124    public void setKeyguardShowing(boolean keyguardShowing) {
125        mKeyguardShowing = keyguardShowing;
126        mBackground.setVisibility(keyguardShowing ? View.INVISIBLE : View.VISIBLE);
127        mDateTime.setVisibility(keyguardShowing ? View.INVISIBLE : View.VISIBLE);
128        mKeyguardCarrierText.setVisibility(keyguardShowing ? View.VISIBLE : View.GONE);
129        if (keyguardShowing) {
130            setZ(0);
131        } else {
132            setTranslationZ(0);
133        }
134        updateHeights();
135    }
136}
137