KeyguardStatusBarView.java revision 98f8530af3bb8636b7b173443c90686c485205d6
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.animation.LayoutTransition;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.View;
24import android.view.ViewTreeObserver;
25import android.view.animation.AnimationUtils;
26import android.view.animation.Interpolator;
27import android.widget.ImageView;
28import android.widget.RelativeLayout;
29import android.widget.TextView;
30
31import com.android.systemui.R;
32import com.android.systemui.statusbar.policy.BatteryController;
33import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
34import com.android.systemui.statusbar.policy.UserInfoController;
35
36/**
37 * The header group on Keyguard.
38 */
39public class KeyguardStatusBarView extends RelativeLayout
40        implements BatteryController.BatteryStateChangeCallback {
41
42    private boolean mBatteryCharging;
43    private boolean mKeyguardUserSwitcherShowing;
44    private boolean mBatteryListening;
45
46    private View mSystemIconsSuperContainer;
47    private MultiUserSwitch mMultiUserSwitch;
48    private ImageView mMultiUserAvatar;
49    private TextView mBatteryLevel;
50
51    private BatteryController mBatteryController;
52    private KeyguardUserSwitcher mKeyguardUserSwitcher;
53
54    private int mSystemIconsSwitcherHiddenExpandedMargin;
55    private Interpolator mFastOutSlowInInterpolator;
56
57    public KeyguardStatusBarView(Context context, AttributeSet attrs) {
58        super(context, attrs);
59    }
60
61    @Override
62    protected void onFinishInflate() {
63        super.onFinishInflate();
64        mSystemIconsSuperContainer = findViewById(R.id.system_icons_super_container);
65        mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
66        mMultiUserAvatar = (ImageView) findViewById(R.id.multi_user_avatar);
67        mBatteryLevel = (TextView) findViewById(R.id.battery_level);
68        loadDimens();
69        mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(),
70                android.R.interpolator.fast_out_slow_in);
71        updateUserSwitcher();
72    }
73
74    private void loadDimens() {
75        mSystemIconsSwitcherHiddenExpandedMargin = getResources().getDimensionPixelSize(
76                R.dimen.system_icons_switcher_hidden_expanded_margin);
77    }
78
79    private void updateVisibilities() {
80        if (mMultiUserSwitch.getParent() != this && !mKeyguardUserSwitcherShowing) {
81            if (mMultiUserSwitch.getParent() != null) {
82                getOverlay().remove(mMultiUserSwitch);
83            }
84            addView(mMultiUserSwitch, 0);
85        } else if (mMultiUserSwitch.getParent() == this && mKeyguardUserSwitcherShowing) {
86            removeView(mMultiUserSwitch);
87        }
88        mBatteryLevel.setVisibility(mBatteryCharging ? View.VISIBLE : View.GONE);
89    }
90
91    private void updateSystemIconsLayoutParams() {
92        RelativeLayout.LayoutParams lp =
93                (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
94        int marginEnd = mKeyguardUserSwitcherShowing ? mSystemIconsSwitcherHiddenExpandedMargin : 0;
95        if (marginEnd != lp.getMarginEnd()) {
96            lp.setMarginEnd(marginEnd);
97            mSystemIconsSuperContainer.setLayoutParams(lp);
98        }
99    }
100
101    public void setListening(boolean listening) {
102        if (listening == mBatteryListening) {
103            return;
104        }
105        mBatteryListening = listening;
106        if (mBatteryListening) {
107            mBatteryController.addStateChangedCallback(this);
108        } else {
109            mBatteryController.removeStateChangedCallback(this);
110        }
111    }
112
113    private void updateUserSwitcher() {
114        boolean keyguardSwitcherAvailable = mKeyguardUserSwitcher != null;
115        mMultiUserSwitch.setClickable(keyguardSwitcherAvailable);
116        mMultiUserSwitch.setFocusable(keyguardSwitcherAvailable);
117        mMultiUserSwitch.setKeyguardMode(keyguardSwitcherAvailable);
118    }
119
120    public void setBatteryController(BatteryController batteryController) {
121        mBatteryController = batteryController;
122    }
123
124    public void setUserInfoController(UserInfoController userInfoController) {
125        userInfoController.addListener(new UserInfoController.OnUserInfoChangedListener() {
126            @Override
127            public void onUserInfoChanged(String name, Drawable picture) {
128                mMultiUserAvatar.setImageDrawable(picture);
129            }
130        });
131    }
132
133    @Override
134    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
135        mBatteryLevel.setText(getResources().getString(R.string.battery_level_template, level));
136        boolean changed = mBatteryCharging != charging;
137        mBatteryCharging = charging;
138        if (changed) {
139            updateVisibilities();
140        }
141    }
142
143    @Override
144    public void onPowerSaveChanged() {
145        // could not care less
146    }
147
148    public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
149        mKeyguardUserSwitcher = keyguardUserSwitcher;
150        mMultiUserSwitch.setKeyguardUserSwitcher(keyguardUserSwitcher);
151        updateUserSwitcher();
152    }
153
154    public void setKeyguardUserSwitcherShowing(boolean showing, boolean animate) {
155        mKeyguardUserSwitcherShowing = showing;
156        if (animate) {
157            animateNextLayoutChange();
158        }
159        updateVisibilities();
160        updateSystemIconsLayoutParams();
161    }
162
163    private void animateNextLayoutChange() {
164        final int systemIconsCurrentX = mSystemIconsSuperContainer.getLeft();
165        final boolean userSwitcherVisible = mMultiUserSwitch.getParent() == this;
166        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
167            @Override
168            public boolean onPreDraw() {
169                getViewTreeObserver().removeOnPreDrawListener(this);
170                boolean userSwitcherHiding = userSwitcherVisible
171                        && mMultiUserSwitch.getParent() != KeyguardStatusBarView.this;
172                mSystemIconsSuperContainer.setX(systemIconsCurrentX);
173                mSystemIconsSuperContainer.animate()
174                        .translationX(0)
175                        .setDuration(400)
176                        .setStartDelay(userSwitcherHiding ? 300 : 0)
177                        .setInterpolator(mFastOutSlowInInterpolator)
178                        .start();
179                if (userSwitcherHiding) {
180                    getOverlay().add(mMultiUserSwitch);
181                    mMultiUserSwitch.animate()
182                            .alpha(0f)
183                            .setDuration(300)
184                            .setStartDelay(0)
185                            .setInterpolator(PhoneStatusBar.ALPHA_OUT)
186                            .withEndAction(new Runnable() {
187                                @Override
188                                public void run() {
189                                    mMultiUserSwitch.setAlpha(1f);
190                                    getOverlay().remove(mMultiUserSwitch);
191                                }
192                            })
193                            .start();
194
195                } else {
196                    mMultiUserSwitch.setAlpha(0f);
197                    mMultiUserSwitch.animate()
198                            .alpha(1f)
199                            .setDuration(300)
200                            .setStartDelay(200)
201                            .setInterpolator(PhoneStatusBar.ALPHA_IN);
202                }
203                return true;
204            }
205        });
206
207    }
208
209    @Override
210    public void setVisibility(int visibility) {
211        super.setVisibility(visibility);
212        if (visibility != View.VISIBLE) {
213            mSystemIconsSuperContainer.animate().cancel();
214            mMultiUserSwitch.animate().cancel();
215            mMultiUserSwitch.setAlpha(1f);
216        }
217    }
218
219    @Override
220    public boolean hasOverlappingRendering() {
221        return false;
222    }
223}
224