Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 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; |
Jorim Jaggi | e210cc8 | 2014-08-12 23:44:59 +0200 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.util.AttributeSet; |
Jorim Jaggi | e210cc8 | 2014-08-12 23:44:59 +0200 | [diff] [blame] | 23 | import android.util.TypedValue; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 24 | import android.view.View; |
Jorim Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 25 | import android.view.ViewTreeObserver; |
| 26 | import android.view.animation.AnimationUtils; |
| 27 | import android.view.animation.Interpolator; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 28 | import android.widget.ImageView; |
| 29 | import android.widget.RelativeLayout; |
| 30 | import android.widget.TextView; |
| 31 | |
Jorim Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 32 | import com.android.systemui.BatteryMeterView; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 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 | |
Elliott Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 38 | import java.text.NumberFormat; |
| 39 | |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 40 | /** |
| 41 | * The header group on Keyguard. |
| 42 | */ |
| 43 | public class KeyguardStatusBarView extends RelativeLayout |
| 44 | implements BatteryController.BatteryStateChangeCallback { |
| 45 | |
| 46 | private boolean mBatteryCharging; |
| 47 | private boolean mKeyguardUserSwitcherShowing; |
| 48 | private boolean mBatteryListening; |
| 49 | |
Jorim Jaggi | e210cc8 | 2014-08-12 23:44:59 +0200 | [diff] [blame] | 50 | private TextView mCarrierLabel; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 51 | 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 Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 60 | private Interpolator mFastOutSlowInInterpolator; |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 61 | |
| 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 Jaggi | e210cc8 | 2014-08-12 23:44:59 +0200 | [diff] [blame] | 73 | mCarrierLabel = (TextView) findViewById(R.id.keyguard_carrier_text); |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 74 | loadDimens(); |
Jorim Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 75 | mFastOutSlowInInterpolator = AnimationUtils.loadInterpolator(getContext(), |
| 76 | android.R.interpolator.fast_out_slow_in); |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 77 | updateUserSwitcher(); |
| 78 | } |
| 79 | |
Jorim Jaggi | e210cc8 | 2014-08-12 23:44:59 +0200 | [diff] [blame] | 80 | @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 Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 92 | private void loadDimens() { |
| 93 | mSystemIconsSwitcherHiddenExpandedMargin = getResources().getDimensionPixelSize( |
| 94 | R.dimen.system_icons_switcher_hidden_expanded_margin); |
| 95 | } |
| 96 | |
| 97 | private void updateVisibilities() { |
Jorim Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 98 | 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 Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 106 | 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 Jaggi | 708f772 | 2014-08-20 17:30:38 +0200 | [diff] [blame] | 140 | ((BatteryMeterView) findViewById(R.id.battery)).setBatteryController(batteryController); |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 141 | } |
| 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 Hughes | 88d2551 | 2014-10-03 12:06:17 -0700 | [diff] [blame] | 154 | String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0); |
| 155 | mBatteryLevel.setText(percentage); |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 156 | 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 Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 174 | public void setKeyguardUserSwitcherShowing(boolean showing, boolean animate) { |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 175 | mKeyguardUserSwitcherShowing = showing; |
Jorim Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 176 | if (animate) { |
| 177 | animateNextLayoutChange(); |
| 178 | } |
Jorim Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 179 | updateVisibilities(); |
| 180 | updateSystemIconsLayoutParams(); |
| 181 | } |
| 182 | |
Jorim Jaggi | 98f8530 | 2014-08-07 17:45:04 +0200 | [diff] [blame] | 183 | 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 Jaggi | 4538027d | 2014-07-30 15:43:13 +0200 | [diff] [blame] | 239 | @Override |
| 240 | public boolean hasOverlappingRendering() { |
| 241 | return false; |
| 242 | } |
| 243 | } |