blob: 13b3898ed9ff159e4da10a7f24fd51dd0c61ddc4 [file] [log] [blame]
Jorim Jaggi4538027d2014-07-30 15:43:13 +02001/*
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;
Jorim Jaggie210cc82014-08-12 23:44:59 +020020import android.content.res.Configuration;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020021import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
Jorim Jaggie210cc82014-08-12 23:44:59 +020023import android.util.TypedValue;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020024import android.view.View;
Jorim Jaggi98f85302014-08-07 17:45:04 +020025import android.view.ViewTreeObserver;
26import android.view.animation.AnimationUtils;
27import android.view.animation.Interpolator;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020028import android.widget.ImageView;
29import android.widget.RelativeLayout;
30import android.widget.TextView;
31
Jorim Jaggi708f7722014-08-20 17:30:38 +020032import com.android.systemui.BatteryMeterView;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020033import com.android.systemui.R;
34import com.android.systemui.statusbar.policy.BatteryController;
35import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
36import com.android.systemui.statusbar.policy.UserInfoController;
37
Elliott Hughes88d25512014-10-03 12:06:17 -070038import java.text.NumberFormat;
39
Jorim Jaggi4538027d2014-07-30 15:43:13 +020040/**
41 * The header group on Keyguard.
42 */
43public class KeyguardStatusBarView extends RelativeLayout
44 implements BatteryController.BatteryStateChangeCallback {
45
46 private boolean mBatteryCharging;
47 private boolean mKeyguardUserSwitcherShowing;
48 private boolean mBatteryListening;
49
Jorim Jaggie210cc82014-08-12 23:44:59 +020050 private TextView mCarrierLabel;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020051 private View mSystemIconsSuperContainer;
52 private MultiUserSwitch mMultiUserSwitch;
53 private ImageView mMultiUserAvatar;
54 private TextView mBatteryLevel;
55
56 private BatteryController mBatteryController;
57 private KeyguardUserSwitcher mKeyguardUserSwitcher;
58
59 private int mSystemIconsSwitcherHiddenExpandedMargin;
Jorim Jaggi98f85302014-08-07 17:45:04 +020060 private Interpolator mFastOutSlowInInterpolator;
Jorim Jaggi4538027d2014-07-30 15:43:13 +020061
62 public KeyguardStatusBarView(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 }
65
66 @Override
67 protected void onFinishInflate() {
68 super.onFinishInflate();
69 mSystemIconsSuperContainer = findViewById(R.id.system_icons_super_container);
70 mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
71 mMultiUserAvatar = (ImageView) findViewById(R.id.multi_user_avatar);
72 mBatteryLevel = (TextView) findViewById(R.id.battery_level);
Jorim Jaggie210cc82014-08-12 23:44:59 +020073 mCarrierLabel = (TextView) findViewById(R.id.keyguard_carrier_text);
Jorim Jaggi4538027d2014-07-30 15:43:13 +020074 loadDimens();
Jorim Jaggi98f85302014-08-07 17:45:04 +020075 mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(),
76 android.R.interpolator.fast_out_slow_in);
Jorim Jaggi4538027d2014-07-30 15:43:13 +020077 updateUserSwitcher();
78 }
79
Jorim Jaggie210cc82014-08-12 23:44:59 +020080 @Override
81 protected void onConfigurationChanged(Configuration newConfig) {
82 super.onConfigurationChanged(newConfig);
83
84 // Respect font size setting.
85 mCarrierLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX,
86 getResources().getDimensionPixelSize(
87 com.android.internal.R.dimen.text_size_small_material));
88 mBatteryLevel.setTextSize(TypedValue.COMPLEX_UNIT_PX,
89 getResources().getDimensionPixelSize(R.dimen.battery_level_text_size));
90 }
91
Jorim Jaggi4538027d2014-07-30 15:43:13 +020092 private void loadDimens() {
93 mSystemIconsSwitcherHiddenExpandedMargin = getResources().getDimensionPixelSize(
94 R.dimen.system_icons_switcher_hidden_expanded_margin);
95 }
96
97 private void updateVisibilities() {
Jorim Jaggi98f85302014-08-07 17:45:04 +020098 if (mMultiUserSwitch.getParent() != this && !mKeyguardUserSwitcherShowing) {
99 if (mMultiUserSwitch.getParent() != null) {
100 getOverlay().remove(mMultiUserSwitch);
101 }
102 addView(mMultiUserSwitch, 0);
103 } else if (mMultiUserSwitch.getParent() == this && mKeyguardUserSwitcherShowing) {
104 removeView(mMultiUserSwitch);
105 }
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200106 mBatteryLevel.setVisibility(mBatteryCharging ? View.VISIBLE : View.GONE);
107 }
108
109 private void updateSystemIconsLayoutParams() {
110 RelativeLayout.LayoutParams lp =
111 (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
112 int marginEnd = mKeyguardUserSwitcherShowing ? mSystemIconsSwitcherHiddenExpandedMargin : 0;
113 if (marginEnd != lp.getMarginEnd()) {
114 lp.setMarginEnd(marginEnd);
115 mSystemIconsSuperContainer.setLayoutParams(lp);
116 }
117 }
118
119 public void setListening(boolean listening) {
120 if (listening == mBatteryListening) {
121 return;
122 }
123 mBatteryListening = listening;
124 if (mBatteryListening) {
125 mBatteryController.addStateChangedCallback(this);
126 } else {
127 mBatteryController.removeStateChangedCallback(this);
128 }
129 }
130
131 private void updateUserSwitcher() {
132 boolean keyguardSwitcherAvailable = mKeyguardUserSwitcher != null;
133 mMultiUserSwitch.setClickable(keyguardSwitcherAvailable);
134 mMultiUserSwitch.setFocusable(keyguardSwitcherAvailable);
135 mMultiUserSwitch.setKeyguardMode(keyguardSwitcherAvailable);
136 }
137
138 public void setBatteryController(BatteryController batteryController) {
139 mBatteryController = batteryController;
Jorim Jaggi708f7722014-08-20 17:30:38 +0200140 ((BatteryMeterView) findViewById(R.id.battery)).setBatteryController(batteryController);
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200141 }
142
143 public void setUserInfoController(UserInfoController userInfoController) {
144 userInfoController.addListener(new UserInfoController.OnUserInfoChangedListener() {
145 @Override
146 public void onUserInfoChanged(String name, Drawable picture) {
147 mMultiUserAvatar.setImageDrawable(picture);
148 }
149 });
150 }
151
152 @Override
153 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Elliott Hughes88d25512014-10-03 12:06:17 -0700154 String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0);
155 mBatteryLevel.setText(percentage);
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200156 boolean changed = mBatteryCharging != charging;
157 mBatteryCharging = charging;
158 if (changed) {
159 updateVisibilities();
160 }
161 }
162
163 @Override
164 public void onPowerSaveChanged() {
165 // could not care less
166 }
167
168 public void setKeyguardUserSwitcher(KeyguardUserSwitcher keyguardUserSwitcher) {
169 mKeyguardUserSwitcher = keyguardUserSwitcher;
170 mMultiUserSwitch.setKeyguardUserSwitcher(keyguardUserSwitcher);
171 updateUserSwitcher();
172 }
173
Jorim Jaggi98f85302014-08-07 17:45:04 +0200174 public void setKeyguardUserSwitcherShowing(boolean showing, boolean animate) {
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200175 mKeyguardUserSwitcherShowing = showing;
Jorim Jaggi98f85302014-08-07 17:45:04 +0200176 if (animate) {
177 animateNextLayoutChange();
178 }
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200179 updateVisibilities();
180 updateSystemIconsLayoutParams();
181 }
182
Jorim Jaggi98f85302014-08-07 17:45:04 +0200183 private void animateNextLayoutChange() {
184 final int systemIconsCurrentX = mSystemIconsSuperContainer.getLeft();
185 final boolean userSwitcherVisible = mMultiUserSwitch.getParent() == this;
186 getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
187 @Override
188 public boolean onPreDraw() {
189 getViewTreeObserver().removeOnPreDrawListener(this);
190 boolean userSwitcherHiding = userSwitcherVisible
191 && mMultiUserSwitch.getParent() != KeyguardStatusBarView.this;
192 mSystemIconsSuperContainer.setX(systemIconsCurrentX);
193 mSystemIconsSuperContainer.animate()
194 .translationX(0)
195 .setDuration(400)
196 .setStartDelay(userSwitcherHiding ? 300 : 0)
197 .setInterpolator(mFastOutSlowInInterpolator)
198 .start();
199 if (userSwitcherHiding) {
200 getOverlay().add(mMultiUserSwitch);
201 mMultiUserSwitch.animate()
202 .alpha(0f)
203 .setDuration(300)
204 .setStartDelay(0)
205 .setInterpolator(PhoneStatusBar.ALPHA_OUT)
206 .withEndAction(new Runnable() {
207 @Override
208 public void run() {
209 mMultiUserSwitch.setAlpha(1f);
210 getOverlay().remove(mMultiUserSwitch);
211 }
212 })
213 .start();
214
215 } else {
216 mMultiUserSwitch.setAlpha(0f);
217 mMultiUserSwitch.animate()
218 .alpha(1f)
219 .setDuration(300)
220 .setStartDelay(200)
221 .setInterpolator(PhoneStatusBar.ALPHA_IN);
222 }
223 return true;
224 }
225 });
226
227 }
228
229 @Override
230 public void setVisibility(int visibility) {
231 super.setVisibility(visibility);
232 if (visibility != View.VISIBLE) {
233 mSystemIconsSuperContainer.animate().cancel();
234 mMultiUserSwitch.animate().cancel();
235 mMultiUserSwitch.setAlpha(1f);
236 }
237 }
238
Jorim Jaggi4538027d2014-07-30 15:43:13 +0200239 @Override
240 public boolean hasOverlappingRendering() {
241 return false;
242 }
243}