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