Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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; |
| 18 | |
| 19 | import android.annotation.Nullable; |
| 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 22 | import android.os.UserHandle; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.widget.ImageView; |
| 25 | |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 26 | import com.android.internal.hardware.AmbientDisplayConfiguration; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 27 | import com.android.systemui.statusbar.policy.BatteryController; |
| 28 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 29 | |
| 30 | /** |
| 31 | * A view that only shows its drawable while the phone is charging. |
| 32 | * |
| 33 | * Also reloads its drawable upon density changes. |
| 34 | */ |
| 35 | public class ChargingView extends ImageView implements |
| 36 | BatteryController.BatteryStateChangeCallback, |
| 37 | ConfigurationController.ConfigurationListener { |
| 38 | |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 39 | private static final long CHARGING_INDICATION_DELAY_MS = 1000; |
| 40 | |
| 41 | private final AmbientDisplayConfiguration mConfig; |
| 42 | private final Runnable mClearSuppressCharging = this::clearSuppressCharging; |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 43 | private BatteryController mBatteryController; |
| 44 | private int mImageResource; |
| 45 | private boolean mCharging; |
| 46 | private boolean mDark; |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 47 | private boolean mSuppressCharging; |
| 48 | |
| 49 | |
| 50 | private void clearSuppressCharging() { |
| 51 | mSuppressCharging = false; |
| 52 | removeCallbacks(mClearSuppressCharging); |
| 53 | updateVisibility(); |
| 54 | } |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 55 | |
| 56 | public ChargingView(Context context, @Nullable AttributeSet attrs) { |
| 57 | super(context, attrs); |
| 58 | |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 59 | mConfig = new AmbientDisplayConfiguration(context); |
| 60 | |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 61 | TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.src}); |
| 62 | int srcResId = a.getResourceId(0, 0); |
| 63 | |
| 64 | if (srcResId != 0) { |
| 65 | mImageResource = srcResId; |
| 66 | } |
| 67 | |
| 68 | a.recycle(); |
| 69 | |
| 70 | updateVisibility(); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onAttachedToWindow() { |
| 75 | super.onAttachedToWindow(); |
| 76 | mBatteryController = Dependency.get(BatteryController.class); |
| 77 | mBatteryController.addCallback(this); |
| 78 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void onDetachedFromWindow() { |
| 83 | super.onDetachedFromWindow(); |
| 84 | mBatteryController.removeCallback(this); |
| 85 | Dependency.get(ConfigurationController.class).removeCallback(this); |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 86 | removeCallbacks(mClearSuppressCharging); |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 91 | boolean startCharging = charging && !mCharging; |
| 92 | if (startCharging && deviceWillWakeUpWhenPluggedIn() && mDark) { |
| 93 | // We're about to wake up, and thus don't want to show the indicator just for it to be |
| 94 | // hidden again. |
| 95 | clearSuppressCharging(); |
| 96 | mSuppressCharging = true; |
| 97 | postDelayed(mClearSuppressCharging, CHARGING_INDICATION_DELAY_MS); |
| 98 | } |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 99 | mCharging = charging; |
| 100 | updateVisibility(); |
| 101 | } |
| 102 | |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 103 | private boolean deviceWillWakeUpWhenPluggedIn() { |
| 104 | boolean plugTurnsOnScreen = getResources().getBoolean( |
| 105 | com.android.internal.R.bool.config_unplugTurnsOnScreen); |
| 106 | boolean aod = mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT); |
| 107 | return !aod && plugTurnsOnScreen; |
| 108 | } |
| 109 | |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 110 | @Override |
| 111 | public void onDensityOrFontScaleChanged() { |
| 112 | setImageResource(mImageResource); |
| 113 | } |
| 114 | |
| 115 | public void setDark(boolean dark) { |
| 116 | mDark = dark; |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 117 | if (!dark) { |
| 118 | clearSuppressCharging(); |
| 119 | } |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 120 | updateVisibility(); |
| 121 | } |
| 122 | |
| 123 | private void updateVisibility() { |
Adrian Roos | af1700a | 2017-08-17 18:35:43 +0200 | [diff] [blame] | 124 | setVisibility(mCharging && !mSuppressCharging && mDark ? VISIBLE : INVISIBLE); |
Adrian Roos | 5602189 | 2017-02-27 20:25:09 +0100 | [diff] [blame] | 125 | } |
| 126 | } |