[go: nahoru, domu]

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.res.Configuration;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.util.TypedValue;
24import android.view.View;
25import android.view.ViewTreeObserver;
26import android.widget.ImageView;
27import android.widget.RelativeLayout;
28import android.widget.TextView;
29
30import com.android.systemui.BatteryMeterView;
31import com.android.systemui.Interpolators;
32import com.android.systemui.R;
33import com.android.systemui.qs.QSPanel;
34import com.android.systemui.statusbar.policy.BatteryController;
35import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
36import com.android.systemui.statusbar.policy.UserInfoController;
37import com.android.systemui.statusbar.policy.UserSwitcherController;
38
39import java.text.NumberFormat;
40
41/**
42 * The header group on Keyguard.
43 */
44public class KeyguardStatusBarView extends RelativeLayout
45        implements BatteryController.BatteryStateChangeCallback {
46
47    private boolean mBatteryCharging;
48    private boolean mKeyguardUserSwitcherShowing;
49    private boolean mBatteryListening;
50
51    private TextView mCarrierLabel;
52    private View mSystemIconsSuperContainer;
53    private MultiUserSwitch mMultiUserSwitch;
54    private ImageView mMultiUserAvatar;
55    private TextView mBatteryLevel;
56
57    private BatteryController mBatteryController;
58    private KeyguardUserSwitcher mKeyguardUserSwitcher;
59
60    private int mSystemIconsSwitcherHiddenExpandedMargin;
61    private View mSystemIconsContainer;
62
63    public KeyguardStatusBarView(Context context, AttributeSet attrs) {
64        super(context, attrs);
65    }
66
67    @Override
68    protected void onFinishInflate() {
69        super.onFinishInflate();
70        mSystemIconsSuperContainer = findViewById(R.id.system_icons_super_container);
71        mSystemIconsContainer = findViewById(R.id.system_icons_container);
72        mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
73        mMultiUserAvatar = (ImageView) findViewById(R.id.multi_user_avatar);
74        mBatteryLevel = (TextView) findViewById(R.id.battery_level);
75        mCarrierLabel = (TextView) findViewById(R.id.keyguard_carrier_text);
76        loadDimens();
77        updateUserSwitcher();
78    }
79
80    @Override
81    protected void onConfigurationChanged(Configuration newConfig) {
82        super.onConfigurationChanged(newConfig);
83
84        MarginLayoutParams lp = (MarginLayoutParams) mMultiUserAvatar.getLayoutParams();
85        lp.width = lp.height = getResources().getDimensionPixelSize(
86                R.dimen.multi_user_avatar_keyguard_size);
87        mMultiUserAvatar.setLayoutParams(lp);
88
89        lp = (MarginLayoutParams) mMultiUserSwitch.getLayoutParams();
90        lp.width = getResources().getDimensionPixelSize(
91                R.dimen.multi_user_switch_width_keyguard);
92        lp.setMarginEnd(getResources().getDimensionPixelSize(
93                R.dimen.multi_user_switch_keyguard_margin));
94        mMultiUserSwitch.setLayoutParams(lp);
95
96        lp = (MarginLayoutParams) mSystemIconsSuperContainer.getLayoutParams();
97        lp.height = getResources().getDimensionPixelSize(
98                R.dimen.status_bar_header_height);
99        lp.setMarginStart(getResources().getDimensionPixelSize(
100                R.dimen.system_icons_super_container_margin_start));
101        mSystemIconsSuperContainer.setLayoutParams(lp);
102        mSystemIconsSuperContainer.setPaddingRelative(mSystemIconsSuperContainer.getPaddingStart(),
103                mSystemIconsSuperContainer.getPaddingTop(),
104                getResources().getDimensionPixelSize(R.dimen.system_icons_keyguard_padding_end),
105                mSystemIconsSuperContainer.getPaddingBottom());
106
107        lp = (MarginLayoutParams) mSystemIconsContainer.getLayoutParams();
108        lp.height = getResources().getDimensionPixelSize(
109                R.dimen.status_bar_height);
110        mSystemIconsContainer.setLayoutParams(lp);
111
112        lp = (MarginLayoutParams) mBatteryLevel.getLayoutParams();
113        lp.setMarginStart(
114                getResources().getDimensionPixelSize(R.dimen.header_battery_margin_keyguard));
115        mBatteryLevel.setLayoutParams(lp);
116        mBatteryLevel.setPaddingRelative(mBatteryLevel.getPaddingStart(),
117                mBatteryLevel.getPaddingTop(),
118                getResources().getDimensionPixelSize(R.dimen.battery_level_padding_end),
119                mBatteryLevel.getPaddingBottom());
120        mBatteryLevel.setTextSize(TypedValue.COMPLEX_UNIT_PX,
121                getResources().getDimensionPixelSize(R.dimen.battery_level_text_size));
122
123        // Respect font size setting.
124        mCarrierLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX,
125                getResources().getDimensionPixelSize(
126                        com.android.internal.R.dimen.text_size_small_material));
127        lp = (MarginLayoutParams) mCarrierLabel.getLayoutParams();
128        lp.setMarginStart(
129                getResources().getDimensionPixelSize(R.dimen.keyguard_carrier_text_margin));
130        mCarrierLabel.setLayoutParams(lp);
131
132        lp = (MarginLayoutParams) getLayoutParams();
133        lp.height =  getResources().getDimensionPixelSize(
134                R.dimen.status_bar_header_height_keyguard);
135        setLayoutParams(lp);
136    }
137
138    private void loadDimens() {
139        mSystemIconsSwitcherHiddenExpandedMargin = getResources().getDimensionPixelSize(
140                R.dimen.system_icons_switcher_hidden_expanded_margin);
141    }
142
143    private void updateVisibilities() {
144        if (mMultiUserSwitch.getParent() != this && !mKeyguardUserSwitcherShowing) {
145            if (mMultiUserSwitch.getParent() != null) {
146                getOverlay().remove(mMultiUserSwitch);
147            }
148            addView(mMultiUserSwitch, 0);
149        } else if (mMultiUserSwitch.getParent() == this && mKeyguardUserSwitcherShowing) {
150            removeView(mMultiUserSwitch);
151        }
152        mBatteryLevel.setVisibility(mBatteryCharging ? View.VISIBLE : View.GONE);
153    }
154
155    private void updateSystemIconsLayoutParams() {
156        RelativeLayout.LayoutParams lp =
157                (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
158        int marginEnd = mKeyguardUserSwitcherShowing ? mSystemIconsSwitcherHiddenExpandedMargin : 0;
159        if (marginEnd != lp.getMarginEnd()) {
160            lp.setMarginEnd(marginEnd);
161            mSystemIconsSuperContainer.setLayoutParams(lp);
162        }
163    }
164
165    public void setListening(boolean listening) {
166        if (listening == mBatteryListening) {
167            return;
168        }
169        mBatteryListening = listening;
170        if (mBatteryListening) {
171            mBatteryController.addStateChangedCallback(this);
172        } else {
173            mBatteryController.removeStateChangedCallback(this);
174        }
175    }
176
177    private void updateUserSwitcher() {
178        boolean keyguardSwitcherAvailable = mKeyguardUserSwitcher != null;
179        mMultiUserSwitch.setClickable(keyguardSwitcherAvailable);
180        mMultiUserSwitch.setFocusable(keyguardSwitcherAvailable);
181        mMultiUserSwitch.setKeyguardMode(keyguardSwitcherAvailable);
182    }
183
184    public void setBatteryController(BatteryController batteryController) {
185        mBatteryController = batteryController;
186        ((BatteryMeterView) findViewById(R.id.battery)).setBatteryController(batteryController);
187    }
188
189    public void setUserSwitcherController(UserSwitcherController controller) {
190        mMultiUserSwitch.setUserSwitcherController(controller);
191    }
192
193    public void setUserInfoController(UserInfoController userInfoController) {
194        userInfoController.addListener(new UserInfoController.OnUserInfoChangedListener() {
195            @Override
196            public void onUserInfoChanged(String name, Drawable picture) {
197                mMultiUserAvatar.setImageDrawable(picture);
198            }
199        });
200    }
201
202    public void setQSPanel(QSPanel qsp) {
203        mMultiUserSwitch.setQsPanel(qsp);
204    }
205
206    @Override
207    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
208        String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0);
209        mBatteryLevel.setText(percentage);
210        boolean changed = mBatteryCharging != charging;
211        mBatteryCharging = charging;
212        if (changed) {
213            updateVisibilities();
214        }
215    }
216
217    @Override
218    public void onPowerSaveChanged(boolean isPowerSave) {
219        // could not care less
220    }
221
222    public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
223        mKeyguardUserSwitcher = keyguardUserSwitcher;
224        mMultiUserSwitch.setKeyguardUserSwitcher(keyguardUserSwitcher);
225        updateUserSwitcher();
226    }
227
228    public void setKeyguardUserSwitcherShowing(boolean showing, boolean animate) {
229        mKeyguardUserSwitcherShowing = showing;
230        if (animate) {
231            animateNextLayoutChange();
232        }
233        updateVisibilities();
234        updateSystemIconsLayoutParams();
235    }
236
237    private void animateNextLayoutChange() {
238        final int systemIconsCurrentX = mSystemIconsSuperContainer.getLeft();
239        final boolean userSwitcherVisible = mMultiUserSwitch.getParent() == this;
240        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
241            @Override
242            public boolean onPreDraw() {
243                getViewTreeObserver().removeOnPreDrawListener(this);
244                boolean userSwitcherHiding = userSwitcherVisible
245                        && mMultiUserSwitch.getParent() != KeyguardStatusBarView.this;
246                mSystemIconsSuperContainer.setX(systemIconsCurrentX);
247                mSystemIconsSuperContainer.animate()
248                        .translationX(0)
249                        .setDuration(400)
250                        .setStartDelay(userSwitcherHiding ? 300 : 0)
251                        .setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
252                        .start();
253                if (userSwitcherHiding) {
254                    getOverlay().add(mMultiUserSwitch);
255                    mMultiUserSwitch.animate()
256                            .alpha(0f)
257                            .setDuration(300)
258                            .setStartDelay(0)
259                            .setInterpolator(Interpolators.ALPHA_OUT)
260                            .withEndAction(new Runnable() {
261                                @Override
262                                public void run() {
263                                    mMultiUserSwitch.setAlpha(1f);
264                                    getOverlay().remove(mMultiUserSwitch);
265                                }
266                            })
267                            .start();
268
269                } else {
270                    mMultiUserSwitch.setAlpha(0f);
271                    mMultiUserSwitch.animate()
272                            .alpha(1f)
273                            .setDuration(300)
274                            .setStartDelay(200)
275                            .setInterpolator(Interpolators.ALPHA_IN);
276                }
277                return true;
278            }
279        });
280
281    }
282
283    @Override
284    public void setVisibility(int visibility) {
285        super.setVisibility(visibility);
286        if (visibility != View.VISIBLE) {
287            mSystemIconsSuperContainer.animate().cancel();
288            mMultiUserSwitch.animate().cancel();
289            mMultiUserSwitch.setAlpha(1f);
290        }
291    }
292
293    @Override
294    public boolean hasOverlappingRendering() {
295        return false;
296    }
297}
298